From e077116032dbad0aa300185f8e3dd6f2d3a3096c Mon Sep 17 00:00:00 2001 From: rturrado Date: Tue, 8 Sep 2026 16:56:10 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Clear=20classical=20bits=20in=20?= =?UTF-8?q?`resetSimulationState`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `resetSimulationState` rebuilt the quantum state on `reset` but left `ddsim->variables` (the map of classical bit values) untouched. After a first `run` followed by `reset`, reading `c[k]` returned the value from the previous run rather than the initial `false`, and the next `measure` on the same bit looked like a no-op because the bit already held the value it would receive. Iterate `ddsim->variables` with `std::ranges::for_each` over `std::views::values` after the quantum state rebuild and set each `boolValue` back to `false`, matching how the quantum side is reset. Closes #464. Assisted-by: Claude Opus 4.7 via Claude Code --- src/backend/dd/DDSimDebug.cpp | 5 +++++ test/test_custom_code.cpp | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/backend/dd/DDSimDebug.cpp b/src/backend/dd/DDSimDebug.cpp index 4e2af62..5cc3b31 100644 --- a/src/backend/dd/DDSimDebug.cpp +++ b/src/backend/dd/DDSimDebug.cpp @@ -190,6 +190,11 @@ void resetSimulationState(DDSimulationState* ddsim) { dd::makeZeroState(ddsim->qc->getNqubits(), *(ddsim->dd)); ddsim->dd->incRef(ddsim->simulationState); ddsim->paused = false; + // Return the classical side to its initial state (all bits false), + // matching the quantum state rebuilt above. + std::ranges::for_each( + ddsim->variables | std::views::values, + [](auto& variable) { variable.value.boolValue = false; }); } /** diff --git a/test/test_custom_code.cpp b/test/test_custom_code.cpp index e55337a..0d47091 100644 --- a/test/test_custom_code.cpp +++ b/test/test_custom_code.cpp @@ -279,6 +279,27 @@ TEST_F(CustomCodeTest, ResetGate) { ASSERT_TRUE(complexEquality(result, -1.0, 0.0)); } +/** + * @test Test that `resetSimulation` returns the classical side to its initial + * state, not just the quantum state. + * After running a program that measures `q[0] = |1>` into `c[0]`, `c[0]` holds + * `true`; a subsequent `resetSimulation` must set it back to `false`. + */ +TEST_F(CustomCodeTest, ResetSimulationClearsClassicalBits) { + loadCode(1, 1, + "x q[0];" + "measure q[0] -> c[0];"); + ASSERT_EQ(state->runSimulation(state), OK); + + Variable v; + ASSERT_EQ(state->getClassicalVariable(state, "c[0]", &v), OK); + ASSERT_TRUE(classicalEquals(v, true)); + + ASSERT_EQ(state->resetSimulation(state), OK); + ASSERT_EQ(state->getClassicalVariable(state, "c[0]", &v), OK); + ASSERT_TRUE(classicalEquals(v, false)); +} + /** * @test Test that parsing works correctly even if a custom gate name includes * the keyword `gate`.