forked from microsoft/QuantumKatas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTests.qs
361 lines (287 loc) · 15.4 KB
/
Tests.qs
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
//////////////////////////////////////////////////////////////////////
// This file contains testing harness for all tasks.
// You should not modify anything in this file.
// The tasks themselves can be found in Tasks.qs file.
//////////////////////////////////////////////////////////////////////
namespace Quantum.Kata.Superposition {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Random;
// ------------------------------------------------------
operation AssertEqualOnZeroState (N : Int,
testImpl : (Qubit[] => Unit),
refImpl : (Qubit[] => Unit is Adj),
verbose : Bool,
testStr : String) : Unit {
using (qs = Qubit[N]) {
if (verbose) {
if (testStr != "") {
Message($"The desired state for {testStr}");
} else {
Message("The desired state:");
}
refImpl(qs);
DumpMachine(());
ResetAll(qs);
}
// apply operation that needs to be tested
testImpl(qs);
if (verbose) {
Message("The actual state:");
DumpMachine(());
}
// apply adjoint reference operation and check that the result is |0⟩
Adjoint refImpl(qs);
// assert that all qubits end up in |0⟩ state
AssertAllZero(qs);
if (verbose) {
Message("Test case passed");
}
}
}
//////////////////////////////////////////////////////////////////
// Part I. Simple Gates
//////////////////////////////////////////////////////////////////
operation ArrayWrapperOperation (op : (Qubit => Unit), qs : Qubit[]) : Unit {
op(qs[0]);
}
operation ArrayWrapperOperationA (op : (Qubit => Unit is Adj), qs : Qubit[]) : Unit is Adj {
op(qs[0]);
}
operation T101_PlusState_Test () : Unit {
AssertEqualOnZeroState(1, ArrayWrapperOperation(PlusState, _), ArrayWrapperOperationA(PlusState_Reference, _), true, "");
}
// ------------------------------------------------------
operation T102_MinusState_Test () : Unit {
AssertEqualOnZeroState(1, ArrayWrapperOperation(MinusState, _), ArrayWrapperOperationA(MinusState_Reference, _), true, "");
}
// ------------------------------------------------------
operation T103_AllBasisVectors_TwoQubits_Test () : Unit {
// We only check for 2 qubits.
AssertEqualOnZeroState(2, AllBasisVectors_TwoQubits, AllBasisVectors_TwoQubits_Reference, true, "");
}
// ------------------------------------------------------
operation T104_AllBasisVectorWithPhaseFlip_TwoQubits_Test() : Unit {
AssertEqualOnZeroState(2, AllBasisVectorWithPhaseFlip_TwoQubits, AllBasisVectorWithPhaseFlip_TwoQubits_Reference, true, "");
}
// ------------------------------------------------------
operation T105_AllBasisVectorsWithPhases_TwoQubits_Test () : Unit {
// We only check for 2 qubits.
AssertEqualOnZeroState(2, AllBasisVectorsWithPhases_TwoQubits, AllBasisVectorsWithPhases_TwoQubits_Reference, true, "");
}
// ------------------------------------------------------
operation T106_BellState_Test () : Unit {
AssertEqualOnZeroState(2, BellState, BellState_Reference, true, "");
}
// ------------------------------------------------------
operation T107_AllBellStates_Test () : Unit {
for (i in 0 .. 3) {
AssertEqualOnZeroState(2, AllBellStates(_, i), AllBellStates_Reference(_, i), true, $"index = {i}");
}
}
// ------------------------------------------------------
operation T108_GHZ_State_Test () : Unit {
// for N = 1 it's just |+⟩
AssertEqualOnZeroState(1, GHZ_State, ArrayWrapperOperationA(PlusState_Reference, _), true, "N = 1");
// for N = 2 it's Bell state
AssertEqualOnZeroState(2, GHZ_State, BellState_Reference, true, "N = 2");
Message("Testing on hidden test cases...");
for (n in 3 .. 9) {
AssertEqualOnZeroState(n, GHZ_State, GHZ_State_Reference, false, "");
}
}
// ------------------------------------------------------
operation T109_AllBasisVectorsSuperposition_Test () : Unit {
// for N = 1 it's just |+⟩
AssertEqualOnZeroState(1, AllBasisVectorsSuperposition, ArrayWrapperOperationA(PlusState_Reference, _), true, "N = 1");
AssertEqualOnZeroState(2, AllBasisVectorsSuperposition, AllBasisVectorsSuperposition_Reference, true, "N = 2");
Message("Testing on hidden test cases...");
for (n in 3 .. 8) {
AssertEqualOnZeroState(n, AllBasisVectorsSuperposition, AllBasisVectorsSuperposition_Reference, false, "");
}
}
// ------------------------------------------------------
operation T110_EvenOddNumbersSuperposition_Test () : Unit {
for (n in 1 .. 2) {
for (isEven in [false, true]) {
AssertEqualOnZeroState(n, EvenOddNumbersSuperposition(_, isEven), EvenOddNumbersSuperposition_Reference(_, isEven), true, $"N = {n}, isEven = {isEven}");
}
}
Message("Testing on hidden test cases...");
for (n in 3 .. 8) {
for (isEven in [false, true]) {
AssertEqualOnZeroState(n, EvenOddNumbersSuperposition(_, isEven), EvenOddNumbersSuperposition_Reference(_, isEven), false, "");
}
}
}
// ------------------------------------------------------
operation T111_ZeroAndBitstringSuperposition_Test () : Unit {
// compare with results of previous operations
mutable b = [true];
AssertEqualOnZeroState(Length(b), ZeroAndBitstringSuperposition(_, b),
ArrayWrapperOperationA(PlusState_Reference, _), true, $"bits = {b}");
set b = [true, true];
AssertEqualOnZeroState(Length(b), ZeroAndBitstringSuperposition(_, b),
BellState_Reference, true, $"bits = {b}");
set b = [true, false];
AssertEqualOnZeroState(Length(b), ZeroAndBitstringSuperposition(_, b),
ZeroAndBitstringSuperposition_Reference(_, b), true, $"bits = {b}");
Message("Testing on hidden test cases...");
set b = [true, true, true];
AssertEqualOnZeroState(Length(b), ZeroAndBitstringSuperposition(_, b),
GHZ_State_Reference, false, "");
set b = [true, false, true];
AssertEqualOnZeroState(Length(b), ZeroAndBitstringSuperposition(_, b),
ZeroAndBitstringSuperposition_Reference(_, b), false, "");
set b = [true, false, true, true, false, false];
AssertEqualOnZeroState(Length(b), ZeroAndBitstringSuperposition(_, b),
ZeroAndBitstringSuperposition_Reference(_, b), false, "");
}
// ------------------------------------------------------
operation T112_TwoBitstringSuperposition_Test () : Unit {
// open tests
// diff in the first position
mutable b1 = [true];
mutable b2 = [false];
AssertEqualOnZeroState(Length(b1), TwoBitstringSuperposition(_, b1, b2),
ArrayWrapperOperationA(PlusState_Reference, _),
true, $"bits1 = {b1}, bits2 = {b2}");
// diff in both positions
set b1 = [false, true];
set b2 = [true, false];
AssertEqualOnZeroState(Length(b1), TwoBitstringSuperposition(_, b1, b2),
AllBellStates_Reference(_, 2),
true, $"bits1 = {b1}, bits2 = {b2}");
Message("Testing on hidden test cases...");
// compare with results of previous operations
set b1 = [false, false];
set b2 = [true, true];
AssertEqualOnZeroState(Length(b1), TwoBitstringSuperposition(_, b1, b2),
BellState_Reference, false, "");
set b1 = [true, true, true];
set b2 = [false, false, false];
AssertEqualOnZeroState(Length(b1), TwoBitstringSuperposition(_, b1, b2),
GHZ_State_Reference, false, "");
// compare with reference implementation
// diff in the first position
set b1 = [true, true, false];
set b2 = [false, true, true];
AssertEqualOnZeroState(Length(b1), TwoBitstringSuperposition(_, b1, b2),
TwoBitstringSuperposition_Reference(_, b1, b2), false, "");
// diff in the middle
set b1 = [true, false, false];
set b2 = [true, true, false];
AssertEqualOnZeroState(Length(b1), TwoBitstringSuperposition(_, b1, b2),
TwoBitstringSuperposition_Reference(_, b1, b2), false, "");
// diff in the last position
set b1 = [false, true, true, false];
set b2 = [false, true, true, true];
AssertEqualOnZeroState(Length(b1), TwoBitstringSuperposition(_, b1, b2),
TwoBitstringSuperposition_Reference(_, b1, b2), false, "");
}
// ------------------------------------------------------
operation T113_FourBitstringSuperposition_Test () : Unit {
// cross-tests
mutable bits = [[false, false], [false, true], [true, false], [true, true]];
AssertEqualOnZeroState(2, FourBitstringSuperposition(_, bits), ApplyToEachA(H, _), true, $"bits = {bits}");
Message("Testing on hidden test cases...");
set bits = [[false, false, false, true], [false, false, true, false], [false, true, false, false], [true, false, false, false]];
AssertEqualOnZeroState(4, FourBitstringSuperposition(_, bits), WState_Arbitrary_Reference, false, "");
// random tests
for (N in 3 .. 10) {
// generate 4 distinct numbers corresponding to the bit strings
mutable numbers = new Int[4];
repeat {
mutable ok = true;
for (i in 0 .. 3) {
set numbers w/= i <- DrawRandomInt(0, 1 <<< N - 1);
for (j in 0 .. i - 1) {
if (numbers[i] == numbers[j]) {
set ok = false;
}
}
}
}
until (ok);
// convert numbers to bit strings
for (i in 0 .. 3) {
set bits w/= i <- IntAsBoolArray(numbers[i], N);
}
AssertEqualOnZeroState(N, FourBitstringSuperposition(_, bits), FourBitstringSuperposition_Reference(_, bits), false, "");
}
}
// ------------------------------------------------------
operation T114_AllStatesWithParitySuperposition_Test () : Unit {
// remember to repeat the tests (for the small case of N = 2), lest the post-selection solution doesn't detect failure and retry
for (i in 1 .. 10) {
for (parity in 0 .. 1) {
AssertEqualOnZeroState(2, AllStatesWithParitySuperposition(_, parity), AllStatesWithParitySuperposition_Reference(_, parity), false, "");
}
}
for (N in 3 .. 6) {
for (parity in 0 .. 1) {
AssertEqualOnZeroState(N, AllStatesWithParitySuperposition(_, parity), AllStatesWithParitySuperposition_Reference(_, parity), false, "");
}
}
}
//////////////////////////////////////////////////////////////////
// Part II. Arbitrary Rotations
//////////////////////////////////////////////////////////////////
operation T201_UnequalSuperposition_Test () : Unit {
// cross-test
AssertEqualOnZeroState(1, ArrayWrapperOperation(UnequalSuperposition(_, 0.5 * PI()), _), ApplyToEachA(X, _), true, "α = 0.5 π");
AssertEqualOnZeroState(1, ArrayWrapperOperation(UnequalSuperposition(_, 0.25 * PI()), _), ArrayWrapperOperationA(PlusState_Reference, _), true, "α = 0.25 π");
AssertEqualOnZeroState(1, ArrayWrapperOperation(UnequalSuperposition(_, 0.75 * PI()), _), ArrayWrapperOperationA(MinusState_Reference, _), true, "α = 0.75 π");
Message("Testing on hidden test cases...");
AssertEqualOnZeroState(1, ArrayWrapperOperation(UnequalSuperposition(_, 0.0), _), ApplyToEachA(I, _), false, "");
for (i in 1 .. 36) {
let alpha = ((2.0 * PI()) * IntAsDouble(i)) / 36.0;
AssertEqualOnZeroState(1, ArrayWrapperOperation(UnequalSuperposition(_, alpha), _), ArrayWrapperOperationA(UnequalSuperposition_Reference(_, alpha), _), false, "");
}
}
// ------------------------------------------------------
operation T202_ControlledRotation_Test () : Unit {
AssertEqualOnZeroState(2, ControlledRotation, ControlledRotation_Reference, true, "");
}
// ------------------------------------------------------
operation T203_ThreeStates_TwoQubits_Test () : Unit {
AssertEqualOnZeroState(2, ThreeStates_TwoQubits, ThreeStates_TwoQubits_Reference, true, "");
}
// ------------------------------------------------------
operation T204_ThreeStates_TwoQubits_Phases_Test () : Unit {
AssertEqualOnZeroState(2, ThreeStates_TwoQubits_Phases, ThreeStates_TwoQubits_Phases_Reference, true, "");
}
// ------------------------------------------------------
operation T205_Hardy_State_Test () : Unit {
AssertEqualOnZeroState(2, Hardy_State, Hardy_State_Reference, true, "");
}
// ------------------------------------------------------
operation T206_WState_PowerOfTwo_Test () : Unit {
// separate check for N = 1 (return must be |1⟩)
AssertEqualOnZeroState(1, WState_PowerOfTwo, ApplyToEachA(X, _), true, "N = 1");
AssertEqualOnZeroState(2, WState_PowerOfTwo, TwoBitstringSuperposition_Reference(_, [false, true], [true, false]), true, "N = 2");
Message("Testing on hidden test cases...");
AssertEqualOnZeroState(4, WState_PowerOfTwo, WState_PowerOfTwo_Reference, false, "");
AssertEqualOnZeroState(8, WState_PowerOfTwo, WState_PowerOfTwo_Reference, false, "");
AssertEqualOnZeroState(16, WState_PowerOfTwo, WState_PowerOfTwo_Reference, false, "");
}
// ------------------------------------------------------
operation T207_WState_Arbitrary_Test () : Unit {
// separate check for N = 1 (return must be |1⟩)
AssertEqualOnZeroState(1, WState_Arbitrary, ApplyToEachA(X, _), true, "N = 1");
// cross-tests for powers of 2
AssertEqualOnZeroState(2, WState_Arbitrary, WState_PowerOfTwo_Reference, true, "N = 2");
Message("Testing on hidden test cases...");
for (n in [4, 8, 16]) {
AssertEqualOnZeroState(n, WState_Arbitrary, WState_PowerOfTwo_Reference, false, "");
}
for (i in 2 .. 16) {
AssertEqualOnZeroState(i, WState_Arbitrary, WState_Arbitrary_Reference, false, "");
}
}
}