-
Notifications
You must be signed in to change notification settings - Fork 137
/
test_gates.cpp
300 lines (233 loc) · 10.1 KB
/
test_gates.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "catch.hpp"
#include "QuEST.h"
#include "utilities.hpp"
/* allows concise use of Contains in catch's REQUIRE_THROWS_WITH */
using Catch::Matchers::Contains;
/** @sa collapseToOutcome
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "collapseToOutcome", "[gates]" ) {
Qureg vec = createQureg(NUM_QUBITS, QUEST_ENV);
Qureg mat = createDensityQureg(NUM_QUBITS, QUEST_ENV);
SECTION( "correctness" ) {
int qubit = GENERATE( range(0,NUM_QUBITS) );
int outcome = GENERATE( 0, 1 );
// repeat these random tests 10 times on every qubit, and for both outcomes
GENERATE( range(0,10) );
SECTION( "state-vector" ) {
// use a random L2 state for every qubit & outcome
QVector vecRef = getRandomStateVector(NUM_QUBITS);
toQureg(vec, vecRef);
// calculate prob of outcome
qreal prob = 0;
for (size_t ind=0; ind<vecRef.size(); ind++) {
int bit = (ind >> qubit) & 1; // target-th bit
if (bit == outcome)
prob += pow(abs(vecRef[ind]), 2);
}
// renormalise by the outcome prob
for (size_t ind=0; ind<vecRef.size(); ind++) {
int bit = (ind >> qubit) & 1; // target-th bit
if (bit == outcome)
vecRef[ind] /= sqrt(prob);
else
vecRef[ind] = 0;
}
qreal res = collapseToOutcome(vec, qubit, outcome);
REQUIRE( res == Approx(prob) );
REQUIRE( areEqual(vec, vecRef) );
}
SECTION( "density-matrix" ) {
// use a random density matrix for every qubit & outcome
QMatrix matRef = getRandomDensityMatrix(NUM_QUBITS);
toQureg(mat, matRef);
// prob is sum of diagonal amps (should be real) where target bit is outcome
qcomp tr = 0;
for (size_t ind=0; ind<matRef.size(); ind++) {
int bit = (ind >> qubit) & 1; // qubit-th bit
if (bit == outcome)
tr += matRef[ind][ind];
}
REQUIRE( imag(tr) == Approx(0).margin(REAL_EPS) );
qreal prob = real(tr);
// renorm (/prob) every |*outcome*><*outcome*| state, zeroing all others
for (size_t r=0; r<matRef.size(); r++) {
for (size_t c=0; c<matRef.size(); c++) {
int ketBit = (c >> qubit) & 1;
int braBit = (r >> qubit) & 1;
if (ketBit == outcome && braBit == outcome)
matRef[r][c] /= prob;
else
matRef[r][c] = 0;
}
}
qreal res = collapseToOutcome(mat, qubit, outcome);
REQUIRE( res == Approx(prob) );
REQUIRE( areEqual(mat, matRef) );
}
}
SECTION( "input validation" ) {
SECTION( "qubit index" ) {
int qubit = GENERATE( -1, NUM_QUBITS );
int outcome = 0;
REQUIRE_THROWS_WITH( collapseToOutcome(mat, qubit, outcome), Contains("Invalid target qubit") );
}
SECTION( "outcome value" ) {
int qubit = 0;
int outcome = GENERATE( -1, 2 );
REQUIRE_THROWS_WITH( collapseToOutcome(mat, qubit, outcome), Contains("Invalid measurement outcome") );
}
SECTION( "outcome probability" ) {
initZeroState(vec);
REQUIRE_THROWS_WITH( collapseToOutcome(vec, 0, 1), Contains("Can't collapse to state with zero probability") );
initClassicalState(vec, 1);
REQUIRE_THROWS_WITH( collapseToOutcome(vec, 0, 0), Contains("Can't collapse to state with zero probability") );
}
}
destroyQureg(vec, QUEST_ENV);
destroyQureg(mat, QUEST_ENV);
}
/** @sa measure
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "measure", "[gates]" ) {
Qureg vec = createQureg(NUM_QUBITS, QUEST_ENV);
Qureg mat = createDensityQureg(NUM_QUBITS, QUEST_ENV);
SECTION( "correctness" ) {
int qubit = GENERATE( range(0,NUM_QUBITS) );
// repeat these random tests 10 times on every qubit
GENERATE( range(0,10) );
SECTION( "state-vector" ) {
QVector vecRef = getRandomStateVector(NUM_QUBITS);
toQureg(vec, vecRef);
int outcome = measure(vec, qubit);
REQUIRE( (outcome == 0 || outcome == 1) );
// calculate prob of this outcome
qreal prob = 0;
for (size_t ind=0; ind<vecRef.size(); ind++) {
int bit = (ind >> qubit) & 1; // target-th bit
if (bit == outcome)
prob += pow(abs(vecRef[ind]), 2);
}
REQUIRE( prob > REAL_EPS );
// renormalise by the outcome prob
for (size_t ind=0; ind<vecRef.size(); ind++) {
int bit = (ind >> qubit) & 1; // target-th bit
if (bit == outcome)
vecRef[ind] /= sqrt(prob);
else
vecRef[ind] = 0;
}
REQUIRE( areEqual(vec, vecRef) );
}
SECTION( "density-matrix" ) {
QMatrix matRef = getRandomDensityMatrix(NUM_QUBITS);
toQureg(mat, matRef);
int outcome = measure(mat, qubit);
REQUIRE( (outcome == 0 || outcome == 1) );
// compute prob of this outcome
qreal prob = 0;
for (size_t ind=0; ind<matRef.size(); ind++) {
int bit = (ind >> qubit) & 1; // qubit-th bit
if (bit == outcome)
prob += real(matRef[ind][ind]);
}
REQUIRE( prob > REAL_EPS );
// renorm (/prob) every |*outcome*><*outcome*| state, zeroing all others
for (size_t r=0; r<matRef.size(); r++) {
for (size_t c=0; c<matRef.size(); c++) {
int ketBit = (c >> qubit) & 1;
int braBit = (r >> qubit) & 1;
if (ketBit == outcome && braBit == outcome)
matRef[r][c] /= prob;
else
matRef[r][c] = 0;
}
}
REQUIRE( areEqual(mat, matRef) );
}
}
SECTION( "input validation" ) {
SECTION( "qubit index" ) {
int qubit = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( measure(vec, qubit), Contains("Invalid target qubit") );
}
}
destroyQureg(vec, QUEST_ENV);
destroyQureg(mat, QUEST_ENV);
}
/** @sa measureWithStats
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "measureWithStats", "[gates]" ) {
Qureg vec = createQureg(NUM_QUBITS, QUEST_ENV);
Qureg mat = createDensityQureg(NUM_QUBITS, QUEST_ENV);
SECTION( "correctness" ) {
int qubit = GENERATE( range(0,NUM_QUBITS) );
// repeat these random tests 10 times on every qubit
GENERATE( range(0,10) );
SECTION( "state-vector" ) {
QVector vecRef = getRandomStateVector(NUM_QUBITS);
toQureg(vec, vecRef);
qreal res;
int outcome = measureWithStats(vec, qubit, &res);
REQUIRE( (outcome == 0 || outcome == 1) );
// calculate prob of this outcome
qreal prob = 0;
for (size_t ind=0; ind<vecRef.size(); ind++) {
int bit = (ind >> qubit) & 1; // target-th bit
if (bit == outcome)
prob += pow(abs(vecRef[ind]), 2);
}
REQUIRE( prob == Approx(res) );
// renormalise by the outcome prob
for (size_t ind=0; ind<vecRef.size(); ind++) {
int bit = (ind >> qubit) & 1; // target-th bit
if (bit == outcome)
vecRef[ind] /= sqrt(prob);
else
vecRef[ind] = 0;
}
REQUIRE( areEqual(vec, vecRef) );
}
SECTION( "density-matrix" ) {
QMatrix matRef = getRandomDensityMatrix(NUM_QUBITS);
toQureg(mat, matRef);
qreal res;
int outcome = measureWithStats(mat, qubit, &res);
REQUIRE( (outcome == 0 || outcome == 1) );
// compute prob of this outcome
qreal prob = 0;
for (size_t ind=0; ind<matRef.size(); ind++) {
int bit = (ind >> qubit) & 1; // qubit-th bit
if (bit == outcome)
prob += real(matRef[ind][ind]);
}
REQUIRE( prob == Approx(res) );
// renorm (/prob) every |*outcome*><*outcome*| state, zeroing all others
for (size_t r=0; r<matRef.size(); r++) {
for (size_t c=0; c<matRef.size(); c++) {
int ketBit = (c >> qubit) & 1;
int braBit = (r >> qubit) & 1;
if (ketBit == outcome && braBit == outcome)
matRef[r][c] /= prob;
else
matRef[r][c] = 0;
}
}
REQUIRE( areEqual(mat, matRef) );
}
}
SECTION( "input validation" ) {
SECTION( "qubit index" ) {
int qubit = GENERATE( -1, NUM_QUBITS );
qreal res;
REQUIRE_THROWS_WITH( measureWithStats(vec, qubit, &res), Contains("Invalid target qubit") );
}
}
destroyQureg(vec, QUEST_ENV);
destroyQureg(mat, QUEST_ENV);
}