Skip to content

Commit 876c10b

Browse files
* Add a method to convert vector<measure_result> to vector<bool>
Signed-off-by: Pradnya Khalate <[email protected]>
1 parent 7778cef commit 876c10b

File tree

2 files changed

+83
-52
lines changed

2 files changed

+83
-52
lines changed

runtime/cudaq/qis/execution_manager.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ class measure_result {
5858

5959
operator int() const { return result; }
6060
operator bool() const { return __nvqpp__MeasureResultBoolConversion(result); }
61+
62+
inline static std::vector<bool>
63+
to_bool_vector(const std::vector<measure_result> &results) {
64+
std::vector<bool> boolResults;
65+
boolResults.reserve(results.size());
66+
for (const auto &res : results)
67+
boolResults.push_back(static_cast<bool>(res));
68+
return boolResults;
69+
}
6170
};
6271
#else
6372
/// When compiling with MLIR, we default to a boolean.

targettests/execution/cudaq_run.cpp

Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,24 @@ __qpu__ std::vector<bool> vector_bool_test() {
4242
return vec;
4343
}
4444

45-
#ifndef CUDAQ_LIBRARY_MODE
46-
struct K9 {
47-
/// NOTE: This doesn't work in library mode - `error: no viable conversion
48-
/// from returned value of type 'vector<measure_result>' to function return
49-
/// type 'vector<bool>'`
45+
__qpu__ bool mz_test() {
46+
cudaq::qubit q;
47+
h(q);
48+
return mz(q);
49+
}
50+
51+
struct vector_mz_test {
5052
std::vector<bool> operator()() __qpu__ {
5153
cudaq::qvector q(5);
5254
cudaq::qubit p;
5355
x(q);
56+
#ifdef CUDAQ_LIBRARY_MODE
57+
return cudaq::measure_result::to_bool_vector(mz(q));
58+
#else
5459
return mz(q);
60+
#endif
5561
}
5662
};
57-
#endif
5863

5964
__qpu__ std::vector<int> vector_int_test() {
6065
std::vector<int> result(2);
@@ -90,27 +95,28 @@ auto struct_test = []() __qpu__ {
9095
};
9196

9297
int main() {
98+
std::size_t shots = 10;
9399
int c = 0;
94100
{
95-
const auto results = cudaq::run(100, nullary_test);
96-
if (results.size() != 100) {
97-
printf("FAILED! Expected 100 shots. Got %lu\n", results.size());
101+
const auto results = cudaq::run(shots, nullary_test);
102+
if (results.size() != shots) {
103+
printf("FAILED! Expected %lu shots. Got %lu\n", shots, results.size());
98104
} else {
99105
for (auto i : results)
100106
printf("%d: %d\n", c++, i);
101-
printf("success!\n");
107+
printf("success - nullary_test\n");
102108
}
103109
}
104110

105111
{
106-
const auto results = cudaq::run(50, unary_test, 4);
112+
const auto results = cudaq::run(shots, unary_test, 4);
107113
c = 0;
108-
if (results.size() != 50) {
109-
printf("FAILED! Expected 50 shots. Got %lu\n", results.size());
114+
if (results.size() != shots) {
115+
printf("FAILED! Expected %lu shots. Got %lu\n", shots, results.size());
110116
} else {
111117
for (auto i : results)
112118
printf("%d: %d\n", c++, i);
113-
printf("success!\n");
119+
printf("success - unary_test\n");
114120
}
115121
}
116122

@@ -138,111 +144,127 @@ int main() {
138144
// Run async
139145
{
140146
const auto results =
141-
cudaq::run_async(/*qpu_id=*/0, 100, nullary_test).get();
142-
if (results.size() != 100) {
143-
printf("FAILED! Expected 100 shots. Got %lu\n", results.size());
147+
cudaq::run_async(/*qpu_id=*/0, shots, nullary_test).get();
148+
if (results.size() != shots) {
149+
printf("FAILED! Expected %lu shots. Got %lu\n", shots, results.size());
144150
} else {
145151
for (auto i : results)
146152
printf("%d: %d\n", c++, i);
147-
printf("success!\n");
153+
printf("success async - nullary_test\n");
148154
}
149155
}
150156

151157
{
152158
const auto results =
153-
cudaq::run_async(/*qpu_id=*/0, 50, unary_test, 4).get();
159+
cudaq::run_async(/*qpu_id=*/0, shots, unary_test, 4).get();
154160
c = 0;
155-
if (results.size() != 50) {
156-
printf("FAILED! Expected 50 shots. Got %lu\n", results.size());
161+
if (results.size() != shots) {
162+
printf("FAILED! Expected %lu shots. Got %lu\n", shots, results.size());
157163
} else {
158164
for (auto i : results)
159165
printf("%d: %d\n", c++, i);
160-
printf("success!\n");
166+
printf("success async - unary_test\n");
161167
}
162168
}
163169

170+
shots = 5;
164171
{
165172
const std::vector<std::vector<bool>> results =
166-
cudaq::run(3, vector_bool_test);
173+
cudaq::run(shots, vector_bool_test);
167174
c = 0;
168-
if (results.size() != 3) {
169-
printf("FAILED! Expected 3 shots. Got %lu\n", results.size());
175+
if (results.size() != shots) {
176+
printf("FAILED! Expected %lu shots. Got %lu\n", shots, results.size());
170177
} else {
171178
for (auto i : results) {
172179
printf("%d: {%d , %d}\n", c++, (bool)i[0], (bool)i[1]);
173180
assert(i[0] == true);
174181
assert(i[1] == false);
175182
}
176-
printf("success!\n");
183+
printf("success - vector_bool_test\n");
177184
}
178185
}
179-
#ifndef CUDAQ_LIBRARY_MODE
186+
180187
{
181-
const std::vector<std::vector<bool>> results = cudaq::run(2, K9{});
188+
const std::vector<bool> results = cudaq::run(shots, mz_test);
182189
c = 0;
183-
if (results.size() != 2) {
184-
printf("FAILED! Expected 2 shots. Got %lu\n", results.size());
190+
if (results.size() != shots) {
191+
printf("FAILED! Expected %lu shots. Got %lu\n", shots, results.size());
192+
} else {
193+
for (auto i : results)
194+
printf("%d: %d\n", c++, (bool)i);
195+
printf("success - mz_test\n");
196+
}
197+
}
198+
199+
{
200+
const std::vector<std::vector<bool>> results =
201+
cudaq::run(shots, vector_mz_test{});
202+
c = 0;
203+
if (results.size() != shots) {
204+
printf("FAILED! Expected %lu shots. Got %lu\n", shots, results.size());
185205
} else {
186206
for (auto i : results) {
187207
printf("%d: {", c++);
188208
for (auto b : i)
189209
printf("%d ", (bool)b);
190210
printf("}\n");
191211
}
192-
printf("success!\n");
212+
printf("success - vector_mz_test\n");
193213
}
194214
}
195-
#endif
215+
196216
{
197217
const std::vector<std::vector<int>> results =
198-
cudaq::run(3, vector_int_test);
218+
cudaq::run(shots, vector_int_test);
199219
c = 0;
200-
if (results.size() != 3) {
201-
printf("FAILED! Expected 3 shots. Got %lu\n", results.size());
220+
if (results.size() != shots) {
221+
printf("FAILED! Expected %lu shots. Got %lu\n", shots, results.size());
202222
} else {
203223
for (auto i : results) {
204224
printf("%d: {%d , %d}\n", c++, i[0], i[1]);
205225
assert(i[0] == 42);
206226
assert(i[1] == -13);
207227
}
208-
printf("success!\n");
228+
printf("success - vector_int_test\n");
209229
}
210230
}
211231

212232
{
213233
const std::vector<std::vector<float>> results =
214-
cudaq::run(2, vector_float_test);
234+
cudaq::run(shots, vector_float_test);
215235
c = 0;
216-
if (results.size() != 2) {
217-
printf("FAILED! Expected 2 shots. Got %lu\n", results.size());
236+
if (results.size() != shots) {
237+
printf("FAILED! Expected %lu shots. Got %lu\n", shots, results.size());
218238
} else {
219239
for (auto i : results)
220240
printf("%d: {%f , %f , %f}\n", c++, i[0], i[1], i[2]);
221-
printf("success!\n");
241+
printf("success - vector_float_test\n");
222242
}
223243
}
224244

225245
{
226-
const auto results = cudaq::run(3, struct_test);
227-
if (results.size() != 3) {
228-
printf("FAILED! Expected 3 shots. Got %lu\n", results.size());
246+
const auto results = cudaq::run(shots, struct_test);
247+
if (results.size() != shots) {
248+
printf("FAILED! Expected %lu shots. Got %lu\n", shots, results.size());
229249
} else {
230250
c = 0;
231251
for (auto i : results)
232252
printf("%d: {%s, %ld, %f}\n", c++, i.boolVal ? "true" : "false",
233253
i.i64Val, i.f64Val);
234-
printf("success!\n");
254+
printf("success - struct_test\n");
235255
}
236256
}
237257

238258
return 0;
239259
}
240260

241-
// CHECK: success!
242-
// CHECK: success!
243-
// CHECK: success!
244-
// CHECK: success!
245-
// CHECK: success!
246-
// CHECK: success!
247-
// CHECK: success!
248-
// CHECK: success!
261+
// CHECK: success - nullary_test
262+
// CHECK: success - unary_test
263+
// CHECK: success async - nullary_test
264+
// CHECK: success async - unary_test
265+
// CHECK: success - vector_bool_test
266+
// CHECK: success - mz_test
267+
// CHECK: success - vector_mz_test
268+
// CHECK: success - vector_int_test
269+
// CHECK: success - vector_float_test
270+
// CHECK: success - struct_test

0 commit comments

Comments
 (0)