37 if (type->as_real()) {
38 if (
auto const_int = value->as_const_int()) {
39 retval = tree::make<values::ConstReal>(const_int->value);
44 if (type->as_complex()) {
45 if (
auto const_int = value->as_const_int()) {
46 retval = tree::make<values::ConstComplex>(const_int->value);
47 }
else if (
auto const_real = value->as_const_real()) {
48 retval = tree::make<values::ConstComplex>(const_real->value);
53 if (
auto mat_type = type->as_complex_matrix()) {
54 if (
auto const_real_matrix = value->as_const_real_matrix()) {
56 if ((
tree::signed_size_t) const_real_matrix->value.size_rows() == mat_type->num_rows || mat_type->num_rows < 0) {
57 if ((
tree::signed_size_t) const_real_matrix->value.size_cols() == mat_type->num_cols || mat_type->num_cols < 0) {
59 const size_t rows = const_real_matrix->value.size_rows();
60 const size_t cols = const_real_matrix->value.size_cols();
62 for (
size_t row = 1; row <= rows; row++) {
63 for (
size_t col = 1; col <= cols; col++) {
64 complex_mat_value.
at(row, col) = const_real_matrix->value.at(row, col);
67 retval = tree::make<values::ConstComplexMatrix>(complex_mat_value);
77 if (retval.empty() && mat_type->num_rows == mat_type->num_cols && mat_type->num_rows > 0) {
78 const size_t size = mat_type->num_rows;
79 const size_t num_elements = 2 * size * size;
80 if (const_real_matrix->value.size_rows() == 1 && const_real_matrix->value.size_cols() == num_elements) {
83 for (
size_t row = 1; row <= size; row++) {
84 for (
size_t col = 1; col <= size; col++) {
85 double re = const_real_matrix->value.at(1, index++);
86 double im = const_real_matrix->value.at(1, index++);
87 complex_mat_value.
at(row, col) = std::complex<double>(re, im);
90 retval = tree::make<values::ConstComplexMatrix>(complex_mat_value);
98 if (!retval.empty()) {
109 if (value->as_const_bool()) {
110 return tree::make<types::Bool>(
false);
111 }
else if (value->as_const_axis()) {
112 return tree::make<types::Axis>(
false);
113 }
else if (value->as_const_int()) {
114 return tree::make<types::Int>(
false);
115 }
else if (value->as_const_real()) {
116 return tree::make<types::Real>(
false);
117 }
else if (value->as_const_complex()) {
118 return tree::make<types::Complex>(
false);
119 }
else if (
auto const_real_matrix = value->as_const_real_matrix()) {
120 return tree::make<types::RealMatrix>(
121 const_real_matrix->value.size_rows(),
122 const_real_matrix->value.size_cols(),
124 }
else if (
auto const_complex_matrix = value->as_const_complex_matrix()) {
125 return tree::make<types::ComplexMatrix>(
126 const_complex_matrix->value.size_rows(),
127 const_complex_matrix->value.size_cols(),
129 }
else if (value->as_const_string()) {
130 return tree::make<types::String>(
false);
131 }
else if (value->as_const_json()) {
132 return tree::make<types::Json>(
false);
133 }
else if (value->as_qubit_refs()) {
134 return tree::make<types::Qubit>(
true);
135 }
else if (value->as_bit_refs()) {
136 return tree::make<types::Bool>(
true);
137 }
else if (
auto fn = value->as_function()) {
138 return fn->return_type;
139 }
else if (
auto var = value->as_variable_ref()) {
140 return var->variable->typ;
142 throw std::runtime_error(
"unknown type!");
151 for (
auto value : values) {
162 if (!value->as_constant()) {
172 for (
auto value : values) {
195 for (
const auto &value : values) {
Two-dimensional matrix of some kind of type.
types::Types types_of(const Values &values)
Returns the types of the given values.
std::ostream & operator<<(std::ostream &os, const Value &value)
Stream << overload for a single value.
types::Type type_of(const Value &value)
Returns the type of the given value.
Toplevel namespace with entry points for the new API.
Defines classes representing the values (collective name for constants, references, and dynamically evaluated expressions) available within cQASM's type system, as well as some utility functions.
Source location annotation object, containing source file line numbers etc.
tree::Any< Node > Values
Zero or more cQASM values.
Defines the types for the cQASM semantic tree, based on the classes from cqasm::tree.
Contains helper classes and objects for the lexer and parser generated by flex/bison, as well as the entry points for invoking the parser directly, in case you don't need semantic analysis.
Value promote(const Value &value, const types::Type &type)
Type-checks and (if necessary) promotes the given value to the given type.
Namespace for the "new" cQASM 1.x API.
NodeType
Enumeration of all node types.
Contains custom exception objects used by libqasm.
tree::One< Node > Value
A cQASM value, either known at compile-time or an expression for something only known at runtime...
tree::One< TypeBase > Type
A cQASM type.
bool type_check(const Type &expected, const Type &actual)
Returns whether the actual type matches the constraints of the expected type.
void check_const(const Value &value)
Throws an AnalysisError if the given value is not a constant, i.e.
tree::Any< TypeBase > Types
Zero or more cQASM types.
::tree::signed_size_t signed_size_t
Defines the types of values available within cQASM's type system, as well as some utility functions...
NodeType
Enumeration of all node types.
Exception used for analysis errors.
T at(size_t row, size_t col) const
Returns the value at the given position.