From 49887a544132d27003ad92bc0354e33eaf4474f8 Mon Sep 17 00:00:00 2001 From: Aniket Dalvi Date: Thu, 14 May 2026 11:49:58 -0700 Subject: [PATCH] fix(interpreter): Skip rvalue evaluation for MCM-dependent assignments in non-simulating contexts When a classical assignment has an MCM-dependent rvalue and the context has no active simulation paths (e.g., Qiskit circuit-building contexts), skip evaluating the rvalue and just propagate MCM-dependency. This prevents NameError when measurement results have no stored runtime value. --- .../default_simulator/openqasm/interpreter.py | 7 ++++++ .../braket/default_simulator/test_mcm.py | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/braket/default_simulator/openqasm/interpreter.py b/src/braket/default_simulator/openqasm/interpreter.py index c40d16c9..6e539da5 100644 --- a/src/braket/default_simulator/openqasm/interpreter.py +++ b/src/braket/default_simulator/openqasm/interpreter.py @@ -597,6 +597,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 71429c81..2eb813aa 100644 --- a/test/unit_tests/braket/default_simulator/test_mcm.py +++ b/test/unit_tests/braket/default_simulator/test_mcm.py @@ -4976,3 +4976,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