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`.