-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathbernstein_vazirani_circuit.c
91 lines (58 loc) · 1.79 KB
/
bernstein_vazirani_circuit.c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/** @file
* Implements the Bernstien--Vazirani circuit
*
* @author Tyson Jones
*/
# include <stdio.h>
# include <math.h>
# include <time.h>
# include <stdlib.h>
# include "QuEST.h"
void applyOracle(Qureg qureg, int numQubits, int secret) {
int bits = secret;
for (int q=1; q<numQubits; q++) {
// extract the (q-1)-th bit of secret
int bit = bits % 2;
bits /= 2;
// NOT the ancilla, controlling on the q-th qubit
if (bit)
controlledNot(qureg, q, 0);
}
}
void measureResult(Qureg qureg, int secret) {
// |ind> = |s>|1>
int ind = 2*secret + 1;
qreal prob = getProbAmp(qureg, ind);
printf("success probability: " REAL_QASM_FORMAT " \n", prob);
}
void applyBernsteinVazirani(Qureg qureg, int numQubits, int secret) {
// start in |0>
initZeroState(qureg);
// NOT the ancilla
pauliX(qureg, 0);
// H all qubits, including the ancilla
for (int q=0; q<numQubits; q++)
hadamard(qureg, q);
applyOracle(qureg, numQubits, secret);
for (int q=0; q<numQubits; q++)
hadamard(qureg, q);
// infer the output basis state
measureResult(qureg, secret);
}
int main() {
// prepare the hardware-agnostic QuEST environment
QuESTEnv env = createQuESTEnv();
// choose the register size
int numQubits = 15;
// randomly choose the secret parameter
srand(time(NULL));
int secret = rand() % (int) pow(2, numQubits - 1);
// prepare our register in the |0> state
Qureg qureg = createQureg(numQubits, env);
// search for s using BV's algorithm
applyBernsteinVazirani(qureg, numQubits, secret);
// tidy up
destroyQureg(qureg, env);
destroyQuESTEnv(env);
return 0;
}