Skip to content

Commit

Permalink
classical decl w measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
arulandu committed Jan 9, 2025
1 parent e1e53ec commit 7444d8a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/pyqasm/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,11 @@ def _visit_classical_declaration(
init_value = self._evaluate_array_initialization(
statement.init_expression, final_dimensions, base_type
)
elif isinstance(statement.init_expression, qasm3_ast.QuantumMeasurement):
measurement, statement.init_expression = statement.init_expression, None
return self._visit_classical_declaration(statement) + self._visit_measurement(
qasm3_ast.QuantumMeasurementStatement(measurement, statement.identifier)
) # type: ignore
else:
init_value, stmts = Qasm3ExprEvaluator.evaluate_expression(
statement.init_expression
Expand Down
25 changes: 25 additions & 0 deletions tests/qasm3/test_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,31 @@ def test_remove_measurement():
check_unrolled_qasm(dumps(module), expected_qasm)


def test_init_measure():
qasm3_string = """
OPENQASM 3.0;
qubit a;
qubit[2] b;
bit c = measure a;
bit[2] d = measure b;
"""

expected_qasm = """
OPENQASM 3.0;
qubit[1] a;
qubit[2] b;
bit[1] c;
c[0] = measure a[0];
bit[2] d;
d[0] = measure b[0];
d[1] = measure b[1];
"""

module = loads(qasm3_string)
module.unroll()
check_unrolled_qasm(dumps(module), expected_qasm)


def test_incorrect_measure():
def run_test(qasm3_code, error_message):
with pytest.raises(ValidationError, match=error_message):
Expand Down

0 comments on commit 7444d8a

Please sign in to comment.