Skip to content

Commit 0e4e5fb

Browse files
[testing] Fix test behavior for missing subset of measured qubits (#3516)
Modify the test to not fail if sub-register values are not available. When parsing from QIR output log, it is not expected to populate the sub-registers. Using `run` API is appropriate for such cases. Signed-off-by: Pradnya Khalate <[email protected]>
1 parent 5efa9d2 commit 0e4e5fb

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

targettests/execution/sudoku_2x2-bit_names.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
// RUN: if %qci_avail; then nvq++ %cpp_std --target qci --emulate %s -o %t && %t | FileCheck %s; fi
1818
// clang-format on
1919

20-
#include <cudaq.h>
2120
#include <algorithm>
21+
#include <cudaq.h>
2222
#include <iostream>
2323
#include <unordered_set>
2424

@@ -55,19 +55,11 @@ __qpu__ void grover() {
5555
auto groverQubits3 = mz(qubits[3]);
5656
};
5757

58-
int main() {
59-
auto result = cudaq::sample(1000, grover);
60-
result.dump();
61-
62-
auto& platform = cudaq::get_platform();
63-
if (platform.is_remote() || platform.is_emulated()) {
64-
// Make sure that the get_marginal() results for the individual register names
65-
// match the subset of the bits from the global register.
66-
// Note that this will fail if you only compile this in library mode.
67-
auto numBits = result.begin()->first.size();
68-
std::cout << "Checking " << numBits << " bits against global register\n";
69-
for (size_t b = 0; b < numBits; b++) {
70-
auto regName = "groverQubits" + std::to_string(b);
58+
bool validateMarginals(const cudaq::sample_result &result) {
59+
size_t numBits = result.begin()->first.size();
60+
for (size_t b = 0; b < numBits; b++) {
61+
auto regName = "groverQubits" + std::to_string(b);
62+
try {
7163
auto valFromRegName = result.get_marginal({0}, regName);
7264
auto valFromGlobal = result.get_marginal({b});
7365
if (valFromRegName.to_map() != valFromGlobal.to_map()) {
@@ -77,6 +69,24 @@ int main() {
7769
// Mark test failure
7870
assert(valFromRegName.to_map() == valFromGlobal.to_map());
7971
}
72+
} catch (const std::exception &e) {
73+
return false;
74+
}
75+
}
76+
return true;
77+
}
78+
79+
int main() {
80+
auto result = cudaq::sample(1000, grover);
81+
result.dump();
82+
83+
auto &platform = cudaq::get_platform();
84+
if (platform.is_remote() || platform.is_emulated()) {
85+
// Make sure that the get_marginal() results for the individual register
86+
// names match the subset of the bits from the global register. Note that
87+
// this will fail if you only compile this in library mode.
88+
if (!validateMarginals(result)) {
89+
std::cout << "Unsupported API, use `run` instead!\n";
8090
}
8191
}
8292

@@ -85,7 +95,7 @@ int main() {
8595
for (auto &&[bits, count] : result) {
8696
strings.push_back(bits);
8797
}
88-
std::sort(strings.begin(), strings.end(), [&](auto& a, auto& b) {
98+
std::sort(strings.begin(), strings.end(), [&](auto &a, auto &b) {
8999
return result.count(a) > result.count(b);
90100
});
91101
std::cout << strings[0] << '\n';

0 commit comments

Comments
 (0)