Skip to content
Open
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
5 changes: 5 additions & 0 deletions src/backend/dd/DDSimDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; });
}

/**
Expand Down
21 changes: 21 additions & 0 deletions test/test_custom_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
Loading