-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
59 lines (45 loc) · 1.25 KB
/
main.cpp
File metadata and controls
59 lines (45 loc) · 1.25 KB
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
#include <stdio.h>
#include <iostream>
#include "quantum.hpp"
using namespace std;
void oracle(qureg& r) {
const uint32_t test_for = 5;
//apply function
for (int i = 0; i < 4; i++) r.op_X(2*i+1);
r.gate(qugate::increment(123, 8), bit_range(0, 7));
//do a conditional Z gate by turning |test_for> to |11...1>
for (int i = 0; i < 8; i++)
if (!(test_for & (1 << i))) r.op_X(i);
r.op_X(8);
r.gate(qugate::pauli_Z(), {8}, bit_range(0, 7));
r.op_X(8);
for (int i = 0; i < 8; i++)
if (!(test_for & (1 << i))) r.op_X(i);
//apply reversal of function, resetting ancilla bits
r.gate(qugate::increment(-123, 8), bit_range(0, 7));
for (int i = 0; i < 4; i++) r.op_X(2*i+1);
}
void grover(qureg& r) {
const int it = round(M_PI * 0.25 * sqrt(1 << 8));
r.op_hadamard_range(0, 7);
for (int i = 0; i < it; i++) {
//apply oracle
oracle(r);
//grover diffusion
r.op_hadamard_range(0, 7);
r.op_X_range(0, 8);
r.gate(qugate::pauli_Z(), {8}, bit_range(0, 7));
r.op_X_range(0, 8);
r.op_hadamard_range(0, 7);
}
}
int main(void) {
qureg r(9, 0);
grover(r);
cout << r.state << endl;
uint64_t x;
printf("measured: %lu\n", x = r.measure());
cout << print_bits(x, 6) << endl;
cout << "probability: " << r.probability(x) << endl;
return 0;
}