forked from microsoft/QuantumKatas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTests.qs
More file actions
213 lines (186 loc) · 7.41 KB
/
Tests.qs
File metadata and controls
213 lines (186 loc) · 7.41 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
//////////////////////////////////////////////////////////////////////
// This file contains parts of the testing harness.
// You should not modify anything in this file.
// The tasks themselves can be found in Tasks.qs file.
//////////////////////////////////////////////////////////////////////
namespace Quantum.Kata.SimonsAlgorithm
{
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Extensions.Testing;
// ------------------------------------------------------
operation ApplyOracleA (qs : Qubit[], oracle : ((Qubit[], Qubit) => () : Adjoint)) : ()
{
body
{
let N = Length(qs);
oracle(qs[0..N-2], qs[N-1]);
}
adjoint auto;
}
operation ApplyOracleWithOutputArrA (qs : Qubit[], oracle : ((Qubit[], Qubit[]) => () : Adjoint), outputSize : Int) : ()
{
body
{
let N = Length(qs);
oracle(qs[0..N-1-outputSize], qs[N-outputSize..N-1]);
}
adjoint auto;
}
// ------------------------------------------------------
operation AssertTwoOraclesAreEqual (
nQubits : Range,
oracle1 : ((Qubit[], Qubit) => () : Adjoint),
oracle2 : ((Qubit[], Qubit) => () : Adjoint)) : ()
{
body
{
let sol = ApplyOracleA(_, oracle1);
let refSol = ApplyOracleA(_, oracle2);
for (i in nQubits) {
AssertOperationsEqualReferenced(sol, refSol, i+1);
}
}
}
operation AssertTwoOraclesWithOutputArrAreEqual (
inputSize : Int,
outputSize : Int,
oracle1 : ((Qubit[], Qubit[]) => () : Adjoint),
oracle2 : ((Qubit[], Qubit[]) => () : Adjoint)) : ()
{
body
{
let sol = ApplyOracleWithOutputArrA(_, oracle1, outputSize);
let refSol = ApplyOracleWithOutputArrA(_, oracle2, outputSize);
AssertOperationsEqualReferenced(sol, refSol, inputSize + outputSize);
}
}
// ------------------------------------------------------
operation Q11_Oracle_CountBits_Test () : ()
{
body
{
AssertTwoOraclesAreEqual(1..10, Oracle_CountBits, Oracle_CountBits_Reference);
}
}
// ------------------------------------------------------
operation Q12_Oracle_BitwiseRightShift_Test () : ()
{
body
{
for (n in 2..6) {
AssertTwoOraclesWithOutputArrAreEqual(n, n, Oracle_BitwiseRightShift, Oracle_BitwiseRightShift_Reference);
}
}
}
// ------------------------------------------------------
operation AssertTwoOraclesWithIntArrAreEqual (
A : Int[],
oracle1 : ((Qubit[], Qubit, Int[]) => () : Adjoint),
oracle2 : ((Qubit[], Qubit, Int[]) => () : Adjoint)) : ()
{
body
{
AssertTwoOraclesAreEqual(Length(A)..Length(A), oracle1(_, _, A), oracle2(_, _, A));
}
}
operation Q13_Oracle_OperatorOutput_Test () : ()
{
body
{
// cross-tests
// the mask for all 1's should behave the same as Oracle_CountBits
mutable A = [1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1];
let L = Length(A);
for (i in 2..L)
{
AssertTwoOraclesAreEqual(i..i, Oracle_OperatorOutput(_, _, A[0..i-1]), Oracle_OperatorOutput_Reference(_, _, A[0..i-1]));
}
set A = [1; 1; 0; 0];
AssertTwoOraclesWithIntArrAreEqual(A, Oracle_OperatorOutput, Oracle_OperatorOutput_Reference);
set A = [0; 0; 0; 0; 0];
AssertTwoOraclesWithIntArrAreEqual(A, Oracle_OperatorOutput, Oracle_OperatorOutput_Reference);
set A = [1; 0; 1; 1; 1];
AssertTwoOraclesWithIntArrAreEqual(A, Oracle_OperatorOutput, Oracle_OperatorOutput_Reference);
set A = [0; 1; 0; 0];
AssertTwoOraclesWithIntArrAreEqual(A, Oracle_OperatorOutput, Oracle_OperatorOutput_Reference);
}
}
// ------------------------------------------------------
operation AssertTwoOraclesWithIntMatrixAreEqual (
A : Int[][],
oracle1 : ((Qubit[], Qubit[], Int[][]) => () : Adjoint),
oracle2 : ((Qubit[], Qubit[], Int[][]) => () : Adjoint)) : ()
{
body
{
let inputSize = Length(A[0]);
let outputSize = Length(A);
AssertTwoOraclesWithOutputArrAreEqual(inputSize, outputSize, oracle1(_, _, A), oracle2(_, _, A));
}
}
operation AssertTwoOraclesWithDifferentOutputsAreEqual (
inputSize : Int,
oracle1 : ((Qubit[], Qubit[]) => () : Adjoint),
oracle2 : ((Qubit[], Qubit) => () : Adjoint)) : ()
{
body
{
let sol = ApplyOracleWithOutputArrA(_, oracle1, 1);
let refSol = ApplyOracleA(_, oracle2);
AssertOperationsEqualReferenced(sol, refSol, inputSize+1);
}
}
operation Q14_Oracle_MultidimensionalOperatorOutput_Test () : ()
{
body
{
mutable A = [[1; 1]; [0; 0]];
AssertTwoOraclesWithIntMatrixAreEqual(A, Oracle_MultidimensionalOperatorOutput, Oracle_MultidimensionalOperatorOutput_Reference);
set A = [[1; 0]; [0; 1]; [1; 1]];
AssertTwoOraclesWithIntMatrixAreEqual(A, Oracle_MultidimensionalOperatorOutput, Oracle_MultidimensionalOperatorOutput_Reference);
set A = [[0; 1; 0]; [1; 0; 1]];
AssertTwoOraclesWithIntMatrixAreEqual(A, Oracle_MultidimensionalOperatorOutput, Oracle_MultidimensionalOperatorOutput_Reference);
// cross-test for bitwise right shift oracle
set A = [[0; 0; 0; 0];
[1; 0; 0; 0];
[0; 1; 0; 0];
[0; 0; 1; 0]];
AssertTwoOraclesWithOutputArrAreEqual(4, 4, Oracle_MultidimensionalOperatorOutput(_, _, A), Oracle_BitwiseRightShift_Reference);
// cross-test for 1-dimensional output
mutable B = [1; 0; 1; 0; 1];
AssertTwoOraclesWithDifferentOutputsAreEqual(5, Oracle_MultidimensionalOperatorOutput(_, _, [B]), Oracle_OperatorOutput_Reference(_, _, B));
// cross-test for bit counting oracle
set B = [1; 1; 1; 1; 1];
AssertTwoOraclesWithDifferentOutputsAreEqual(5, Oracle_MultidimensionalOperatorOutput(_, _, [B]), Oracle_CountBits_Reference);
}
}
operation Q21_StatePrep_Test () : ()
{
body
{
for (N in 1..10) {
using (qs = Qubit[N])
{
// apply operation that needs to be tested
SA_StatePrep(qs[0..N-1]);
// apply adjoint reference operation
(Adjoint SA_StatePrep_Reference)(qs[0..N-1]);
// assert that all qubits end up in |0⟩ state
AssertAllZero(qs);
}
}
}
}
// ------------------------------------------------------
operation cs_helper(N: Int, Matrix: Int[][]) : (Int[], ((Qubit[], Qubit[]) => ()))
{
body
{
let Uf = Oracle_MultidimensionalOperatorOutput_Reference(_, _, Matrix);
return (Simon_Algorithm(N, Uf), Uf);
}
}
}