forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grover.cpp
63 lines (55 loc) · 1.56 KB
/
grover.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Compile and run with:
// ```
// nvq++ grover.cpp -o grover.x && ./grover.x
// ```
#include <cmath>
#include <cudaq.h>
#include <numbers>
__qpu__ void reflect_about_uniform(cudaq::qvector<> &qs) {
auto ctrlQubits = qs.front(qs.size() - 1);
auto &lastQubit = qs.back();
// Compute (U) Action (V) produces
// U V U::Adjoint
cudaq::compute_action(
[&]() {
h(qs);
x(qs);
},
[&]() { z<cudaq::ctrl>(ctrlQubits, lastQubit); });
}
struct run_grover {
template <typename CallableKernel>
__qpu__ auto operator()(const int n_qubits, CallableKernel &&oracle) {
int n_iterations = round(0.25 * std::numbers::pi * sqrt(2 ^ n_qubits));
cudaq::qvector qs(n_qubits);
h(qs);
for (int i = 0; i < n_iterations; i++) {
oracle(qs);
reflect_about_uniform(qs);
}
mz(qs);
}
};
struct oracle {
const long target_state;
void operator()(cudaq::qvector<> &qs) __qpu__ {
cudaq::compute_action(
[&]() {
for (int i = 1; i <= qs.size(); ++i) {
auto target_bit_set = (1 << (qs.size() - i)) & target_state;
if (!target_bit_set)
x(qs[i - 1]);
}
},
[&]() {
auto ctrlQubits = qs.front(qs.size() - 1);
z<cudaq::ctrl>(ctrlQubits, qs.back());
});
}
};
int main(int argc, char *argv[]) {
auto secret = 1 < argc ? strtol(argv[1], NULL, 2) : 0b1011;
oracle compute_oracle{.target_state = secret};
auto counts = cudaq::sample(run_grover{}, 4, compute_oracle);
printf("Found string %s\n", counts.most_probable().c_str());
}