Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Types of changes:
### Removed

### Fixed
- Fixed Complex value initialization error. ([#253](https://github.com/qBraid/pyqasm/pull/253))

### Dependencies
- Bumps `@actions/checkout` from 4 to 5 ([#250](https://github.com/qBraid/pyqasm/pull/250))
Expand Down
10 changes: 6 additions & 4 deletions src/pyqasm/maps/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ def qasm_variable_type_cast(openqasm_type, var_name, base_size, rhs_value):

if type_of_rhs not in VARIABLE_TYPE_CAST_MAP[openqasm_type]:
raise ValidationError(
f"Cannot cast {type_of_rhs} to {openqasm_type}. "
f"Invalid assignment of type {type_of_rhs} to variable {var_name} "
f"of type {openqasm_type}"
f"Cannot cast '{type_of_rhs.__name__}' to '{openqasm_type.__name__}'. "
f"Invalid assignment of type '{type_of_rhs.__name__}' to variable '{var_name}' "
f"of type '{openqasm_type.__name__}'"
)

if openqasm_type == BoolType:
Expand All @@ -130,6 +130,8 @@ def qasm_variable_type_cast(openqasm_type, var_name, base_size, rhs_value):
return ((2 * CONSTANTS_MAP["pi"]) * (1 / 2)) if rhs_value else 0.0
return rhs_value # not sure
if openqasm_type == ComplexType:
if isinstance(rhs_value, float):
return complex(rhs_value)
return rhs_value


Expand Down Expand Up @@ -166,7 +168,7 @@ def qasm_variable_type_cast(openqasm_type, var_name, base_size, rhs_value):
UintType: (bool, int, float, np.int64, np.uint64, np.float64, np.bool_),
FloatType: (bool, int, float, np.int64, np.float64, np.bool_),
AngleType: (float, np.float64, bool, np.bool_),
ComplexType: (complex, np.complex128),
ComplexType: (complex, np.complex128, float, np.float64),
}

ARRAY_TYPE_MAP = {
Expand Down
8 changes: 8 additions & 0 deletions tests/qasm3/declarations/test_classical.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,14 @@ def test_complex_type_variables():
complex[float[64]] g = a ** b;
complex[float] h = a + b;
complex i = sqrt(1.0 + 2.0im);
complex j = 1.0;
complex k;
k = 1.0;
const complex l = 20.0;
float m = 1.0;
complex n = m;
complex o = l * (2 - 1im);
complex p = o + (1.0 + 1.0im);
"""

loads(qasm3_string).validate()
Expand Down
4 changes: 2 additions & 2 deletions tests/qasm3/resources/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@
const float[64] f1 = 2.5;
const bit[2] b1 = bit[2](f1);
""",
"Cannot cast <class 'float'> to <class 'openqasm3.ast.BitType'>. Invalid assignment "
"of type <class 'float'> to variable f1 of type <class 'openqasm3.ast.BitType'>",
"Cannot cast 'float' to 'BitType'. Invalid assignment of type "
"'float' to variable 'f1' of type 'BitType'",
5,
8,
"const bit[2] b1 = bit[2](f1);",
Expand Down
Loading