From af39c90d784811d5d855ce5e82868bf93b334496 Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Sat, 2 Dec 2023 14:44:59 +0000 Subject: [PATCH 1/2] Fix ghost qubit bug #981 Signed-off-by: Alex McCaskey --- runtime/nvqir/CircuitSimulator.h | 7 +++++-- unittests/integration/measure_reset_tester.cpp | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/runtime/nvqir/CircuitSimulator.h b/runtime/nvqir/CircuitSimulator.h index b4cb7e6fdd..856929caa9 100644 --- a/runtime/nvqir/CircuitSimulator.h +++ b/runtime/nvqir/CircuitSimulator.h @@ -413,8 +413,11 @@ class CircuitSimulatorBase : public CircuitSimulator { if (iter == registerNameToMeasuredQubit.end()) registerNameToMeasuredQubit.emplace(mutableName, std::vector{qubitIdx}); - else - iter->second.push_back(qubitIdx); + else { + if (std::find(iter->second.begin(), iter->second.end(), qubitIdx) == + iter->second.end()) + iter->second.push_back(qubitIdx); + } return true; } diff --git a/unittests/integration/measure_reset_tester.cpp b/unittests/integration/measure_reset_tester.cpp index 3319f9cfa3..56e954f7cd 100644 --- a/unittests/integration/measure_reset_tester.cpp +++ b/unittests/integration/measure_reset_tester.cpp @@ -42,4 +42,21 @@ TEST(MeasureResetTester, checkBug980) { EXPECT_EQ(2, result.size()); EXPECT_TRUE(result.count("0") > 0); EXPECT_TRUE(result.count("1") > 0); +} + +TEST(MeasureResetTester, checkBug981) { + + auto bar = []() __qpu__ { + cudaq::qubit a; + cudaq::x(a); + [[maybe_unused]] auto a0 = cudaq::mz(a); + cudaq::reset(a); + [[maybe_unused]] auto a1 = cudaq::mz(a); + }; + + std::cout << "Bar:\n"; + auto result = cudaq::sample(bar); + result.dump(); + EXPECT_EQ(1, result.size()); + EXPECT_TRUE(result.count("0") > 0); } \ No newline at end of file From ea300e43ef5be8995075c1b8a4303ec9845bad8b Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Sat, 2 Dec 2023 20:44:39 +0000 Subject: [PATCH 2/2] apply pr review comment Signed-off-by: Alex McCaskey --- unittests/integration/measure_reset_tester.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unittests/integration/measure_reset_tester.cpp b/unittests/integration/measure_reset_tester.cpp index 56e954f7cd..83ad81fd8e 100644 --- a/unittests/integration/measure_reset_tester.cpp +++ b/unittests/integration/measure_reset_tester.cpp @@ -55,7 +55,7 @@ TEST(MeasureResetTester, checkBug981) { }; std::cout << "Bar:\n"; - auto result = cudaq::sample(bar); + auto result = cudaq::sample(/*shots=*/10, bar); result.dump(); EXPECT_EQ(1, result.size()); EXPECT_TRUE(result.count("0") > 0);