Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in sampling a kernel with mz-reset-mz pattern (overwriting sample_result register) #997

Merged
merged 2 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion runtime/common/MeasureCounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,13 @@ sample_result::sample_result(double preComputedExp,
}

void sample_result::append(ExecutionResult &result) {
sampleResults.insert({result.registerName, result});
// If given a result corresponding to the same register name,
// replace the existing one if in the map.
auto iter = sampleResults.find(result.registerName);
if (iter != sampleResults.end())
sampleResults[result.registerName] = result;
amccaskey marked this conversation as resolved.
Show resolved Hide resolved
else
sampleResults.insert({result.registerName, result});
if (!totalShots)
for (auto &[bits, count] : result.counts)
totalShots += count;
Expand Down
1 change: 1 addition & 0 deletions unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(CUDAQ_RUNTIME_TEST_SOURCES
integration/observe_result_tester.cpp
integration/noise_tester.cpp
integration/get_state_tester.cpp
integration/measure_reset_tester.cpp
qir/NVQIRTester.cpp
qis/QubitQISTester.cpp
integration/kernels_tester.cpp
Expand Down
45 changes: 45 additions & 0 deletions unittests/integration/measure_reset_tester.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*******************************************************************************
* Copyright (c) 2022 - 2023 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
* This source code and the accompanying materials are made available under *
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/

#include "CUDAQTestUtils.h"

#include <cudaq.h>
#include <iostream>

TEST(MeasureResetTester, checkBug980) {
auto foo = []() __qpu__ {
cudaq::qubit a;
cudaq::mz(a);
cudaq::reset(a); // properly reset the qubit!
cudaq::h(a);
cudaq::mz(a);
};

auto bar = []() __qpu__ {
cudaq::qubit a;
cudaq::x(a);
[[maybe_unused]] auto a0 = cudaq::mz(a);
cudaq::reset(a); // properly reset the qubit!
cudaq::h(a);
[[maybe_unused]] auto a1 = cudaq::mz(a);
};

std::cout << "Foo:\n";
auto result = cudaq::sample(foo);
result.dump();
EXPECT_EQ(2, result.size());
EXPECT_TRUE(result.count("0") > 0);
EXPECT_TRUE(result.count("1") > 0);

std::cout << "Bar:\n";
result = cudaq::sample(bar);
result.dump();
EXPECT_EQ(2, result.size());
EXPECT_TRUE(result.count("0") > 0);
EXPECT_TRUE(result.count("1") > 0);
}
Loading