diff --git a/src/braket/default_simulator/openqasm/interpreter.py b/src/braket/default_simulator/openqasm/interpreter.py index f7d3630e..06dd6dcd 100644 --- a/src/braket/default_simulator/openqasm/interpreter.py +++ b/src/braket/default_simulator/openqasm/interpreter.py @@ -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, "=") diff --git a/test/unit_tests/braket/default_simulator/test_mcm.py b/test/unit_tests/braket/default_simulator/test_mcm.py index 4435cac7..c0969a82 100644 --- a/test/unit_tests/braket/default_simulator/test_mcm.py +++ b/test/unit_tests/braket/default_simulator/test_mcm.py @@ -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