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
7 changes: 7 additions & 0 deletions src/braket/default_simulator/openqasm/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,13 @@ def _execute_classical_assignment(self, node: ClassicalAssignment) -> None:
lvalue_name = get_identifier_name(node.lvalue)
if self.context.get_const(lvalue_name):
raise TypeError(f"Cannot update const value {lvalue_name}")
if (
self.context.supports_midcircuit_measurement
and not self.context.active_paths
and self.context.is_mcm_dependent(node.rvalue)
):
self.context.track_mcm_dependency(lvalue_name, node.rvalue)
return
rvalue = self.visit(
node.rvalue
if node.op == getattr(AssignmentOperator, "=")
Expand Down
23 changes: 23 additions & 0 deletions test/unit_tests/braket/default_simulator/test_mcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5030,3 +5030,26 @@ def test_flat_context_preserves_mcm_while_loop(self):
"}"
)
assert Interpreter(context=FlatProgramContext()).run(qasm).circuit == qasm

def test_flat_context_mcm_propagation_through_assignment(self):
"""MCM dependency propagates through classical assignment without crashing.

When a measurement result is assigned to an intermediate variable and then
used in a condition, the interpreter should skip rvalue evaluation (since
non-simulating contexts have no runtime values) and just propagate the
MCM-dependency so the condition is correctly routed to evaluate_condition.
"""
qasm = (
"OPENQASM 3.0;\n"
"qubit[2] q;\n"
"bit mcm;\n"
"bit tmp;\n"
"tmp = measure q[0];\n"
"mcm = tmp;\n"
"if (mcm == 1) {\n"
" x q[1];\n"
"}"
)
result = Interpreter(context=FlatProgramContext()).run(qasm).circuit
assert "if (mcm == 1)" in result
assert "x q[1]" in result
Loading