-
Notifications
You must be signed in to change notification settings - Fork 136
/
tutorial_example.c
122 lines (82 loc) · 2.44 KB
/
tutorial_example.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/** @file
* A demo of QuEST
*
* @author Ania Brown
* @author Tyson Jones
*/
#include <stdio.h>
#include "QuEST.h"
int main (int narg, char *varg[]) {
/*
* PREPARE QuEST environment
* (Required only once per program)
*/
QuESTEnv env = createQuESTEnv();
printf("-------------------------------------------------------\n");
printf("Running QuEST tutorial:\n\t Basic circuit involving a system of 3 qubits.\n");
printf("-------------------------------------------------------\n");
/*
* PREPARE QUBIT SYSTEM
*/
Qureg qubits = createQureg(3, env);
initZeroState(qubits);
/*
* REPORT SYSTEM AND ENVIRONMENT
*/
printf("\nThis is our environment:\n");
reportQuregParams(qubits);
reportQuESTEnv(env);
/*
* APPLY CIRCUIT
*/
hadamard(qubits, 0);
controlledNot(qubits, 0, 1);
rotateY(qubits, 2, .1);
int targs[] = {0,1,2};
multiControlledPhaseFlip(qubits, targs, 3);
ComplexMatrix2 u = {
.real={{.5,.5},{.5,.5}},
.imag={{.5,-.5},{-.5,.5}}
};
unitary(qubits, 0, u);
Complex a, b;
a.real = .5; a.imag = .5;
b.real = .5; b.imag = -.5;
compactUnitary(qubits, 1, a, b);
Vector v;
v.x = 1; v.y = 0; v.z = 0;
rotateAroundAxis(qubits, 2, 3.14/2, v);
controlledCompactUnitary(qubits, 0, 1, a, b);
int ctrls[] = {0,1};
multiControlledUnitary(qubits, ctrls, 2, 2, u);
ComplexMatrixN toff = createComplexMatrixN(3);
toff.real[6][7] = 1;
toff.real[7][6] = 1;
for (int i=0; i<6; i++)
toff.real[i][i] = 1;
multiQubitUnitary(qubits, targs, 3, toff);
/*
* STUDY QUANTUM STATE
*/
printf("\nCircuit output:\n");
qreal prob;
prob = getProbAmp(qubits, 7);
printf("Probability amplitude of |111>: " REAL_STRING_FORMAT "\n", prob);
prob = calcProbOfOutcome(qubits, 2, 1);
printf("Probability of qubit 2 being in state 1: " REAL_STRING_FORMAT "\n", prob);
int outcome = measure(qubits, 0);
printf("Qubit 0 was measured in state %d\n", outcome);
outcome = measureWithStats(qubits, 2, &prob);
printf("Qubit 2 collapsed to %d with probability " REAL_STRING_FORMAT "\n", outcome, prob);
/*
* FREE MEMORY
*/
destroyQureg(qubits, env);
destroyComplexMatrixN(toff);
/*
* CLOSE QUEST ENVIRONMET
* (Required once at end of program)
*/
destroyQuESTEnv(env);
return 0;
}