This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
forked from microsoft/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatePreparationTests.qs
267 lines (226 loc) · 13.6 KB
/
StatePreparationTests.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Tests {
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Extensions.Convert;
open Microsoft.Quantum.Extensions.Math;
// number of qubits, abs(amplitude), phase
newtype StatePreparationTestCase = (Int, Double[], Double[]);
operation StatePreparationPositiveCoefficientsTest () : () {
body{
let tolerance = 10e-10;
mutable testCases = new StatePreparationTestCase[100];
mutable nTests = 0;
// Test positive coefficients.
set testCases[nTests] = StatePreparationTestCase(1, [0.773761; 0.633478], [0.0; 0.0]);
set nTests = nTests + 1;
set testCases[nTests] = StatePreparationTestCase(2, [0.183017; 0.406973; 0.604925; 0.659502], [0.0; 0.0; 0.0; 0.0]);
set nTests = nTests + 1;
set testCases[nTests] = StatePreparationTestCase(3, [0.0986553; 0.359005; 0.465689; 0.467395; 0.419893; 0.118445; 0.461883; 0.149609], [0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0]);
set nTests = nTests + 1;
set testCases[nTests] = StatePreparationTestCase(4, [0.271471; 0.0583654; 0.11639; 0.36112; 0.307383; 0.193371; 0.274151; 0.332542; 0.130172; 0.222546; 0.314879; 0.210704; 0.212429; 0.245518; 0.30666; 0.22773], [0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0]);
set nTests = nTests + 1;
// Test negative coefficients. Should give same probabilities as positive coefficients.
set testCases[nTests] = StatePreparationTestCase(1, [-0.773761; 0.633478], [0.0; 0.0]);
set nTests = nTests + 1;
set testCases[nTests] = StatePreparationTestCase(2, [0.183017; -0.406973; 0.604925; 0.659502], [0.0; 0.0; 0.0; 0.0]);
set nTests = nTests + 1;
set testCases[nTests] = StatePreparationTestCase(3, [0.0986553; -0.359005; 0.465689; -0.467395; 0.419893; 0.118445; -0.461883; 0.149609], [0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0]);
set nTests = nTests + 1;
set testCases[nTests] = StatePreparationTestCase(4, [-0.271471; 0.0583654; 0.11639; 0.36112; -0.307383; 0.193371; -0.274151; 0.332542; 0.130172; 0.222546; 0.314879; -0.210704; 0.212429; 0.245518; -0.30666; -0.22773], [0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0]);
set nTests = nTests + 1;
// Test unnormalized coefficients
set testCases[nTests] = StatePreparationTestCase(3, [1.0986553; 0.359005; 0.465689; -0.467395; 0.419893; 0.118445; 0.461883; 0.149609], new Double[0]);
set nTests = nTests + 1;
// Test missing coefficients
set testCases[nTests] = StatePreparationTestCase(3, [1.0986553; 0.359005; 0.465689; -0.467395; 0.419893; 0.118445], new Double[0]);
set nTests = nTests + 1;
// Loop over multiple qubit tests
for(idxTest in 0..nTests-1){
let (nQubits, coefficientsAmplitude, coefficientsPhase) = testCases[idxTest];
let nCoefficients = Length(coefficientsAmplitude);
// Test negative coefficients. Should give same results as positive coefficients.
using(qubits = Qubit[nQubits]){
let qubitsBE = BigEndian(qubits);
let op = StatePreparationPositiveCoefficients(coefficientsAmplitude);
op(qubitsBE);
let normalizedCoefficients = PNormalize(2.0, coefficientsAmplitude);
for(idxCoeff in 0..(nCoefficients-1)){
let amp = normalizedCoefficients[idxCoeff];
let prob = amp * amp;
AssertProbIntBE(idxCoeff, prob, qubitsBE, tolerance);
}
ResetAll(qubits);
}
}
}
}
// Test phase factor on 1-qubit uniform superposition.
operation StatePreparationComplexCoefficientsQubitPhaseTest () : () {
body{
let tolerance = 10e-10;
mutable testCases = new StatePreparationTestCase[10];
mutable nTests = 0;
// Test phase factor on uniform superposition.
set testCases[nTests] = StatePreparationTestCase(1, [1.0; 1.0], [0.01; -0.01]);
set nTests = nTests + 1;
set testCases[nTests] = StatePreparationTestCase(1, [1.0; 1.0], [0.01; -0.05]);
set nTests = nTests + 1;
// Loop over tests
for(idxTest in 0..nTests-1){
let (nQubits, coefficientsAmplitude, coefficientsPhase) = testCases[idxTest];
Message($"Test case {idxTest}");
let nCoefficients = Length(coefficientsAmplitude);
using(qubits = Qubit[nQubits]){
let qubitsBE = BigEndian(qubits);
mutable coefficients = new ComplexPolar[nCoefficients];
mutable coefficientsPositive = new Double[nCoefficients];
for(idxCoeff in 0..nCoefficients-1){
set coefficients[idxCoeff] = ComplexPolar(coefficientsAmplitude[idxCoeff], coefficientsPhase[idxCoeff]);
set coefficientsPositive[idxCoeff] = coefficientsAmplitude[idxCoeff];
}
let normalizedCoefficients = PNormalize(2.0, coefficientsAmplitude);
// Test phase factor on uniform superposition
let phase = 0.5 * (coefficientsPhase[0]-coefficientsPhase[1]);
let amp = normalizedCoefficients[0];
let prob = amp * amp;
let op = StatePreparationComplexCoefficients(coefficients);
op(qubitsBE);
AssertProbIntBE(0, prob, qubitsBE, tolerance);
AssertProbIntBE(1, prob, qubitsBE, tolerance);
AssertPhase(phase, qubitsBE[0], tolerance);
ResetAll(qubits);
}
}
}
}
// Test probabilities and phases factor of multi-qubit uniform superposition.
operation StatePreparationComplexCoefficientsMultiQubitPhaseTest () : () {
body{
let tolerance = 10e-10;
mutable testCases = new StatePreparationTestCase[10];
mutable nTests = 0;
// Test probability and phases of uniform superposition.
set testCases[nTests] = StatePreparationTestCase(1, [1.0; 1.0], [0.01; -0.01]);
set nTests = nTests + 1;
set testCases[nTests] = StatePreparationTestCase(3, [1.0; 1.0; 1.0; 1.0; 1.0; 1.0; 1.0; 1.0], [0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0]);
set nTests = nTests + 1;
set testCases[nTests] = StatePreparationTestCase(3, [1.0; 1.0; 1.0; 1.0; 1.0; 1.0; 1.0; 1.0], [PI(); PI(); PI(); PI(); PI(); PI(); PI(); PI()]);
set nTests = nTests + 1;
set testCases[nTests] = StatePreparationTestCase(3, [1.0; 1.0; 1.0; 1.0; 1.0; 1.0; 1.0; 1.0], [0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.0; 0.01]);
set nTests = nTests + 1;
set testCases[nTests] = StatePreparationTestCase(3, [1.0; 1.0; 1.0; 1.0; 1.0; 1.0; 1.0; 1.0], [1.0986553; 0.359005; 0.465689; -0.467395; 0.419893; 0.118445; 0.461883; 0.149609]);
set nTests = nTests + 1;
// Loop over tests
for(idxTest in 0..nTests-1){
let (nQubits, coefficientsAmplitude, coefficientsPhase) = testCases[idxTest];
Message($"Test case {idxTest}");
let nCoefficients = Length(coefficientsAmplitude);
using(qubits = Qubit[nQubits]){
let qubitsBE = BigEndian(qubits);
mutable coefficients = new ComplexPolar[nCoefficients];
mutable coefficientsPositive = new Double[nCoefficients];
for(idxCoeff in 0..nCoefficients-1){
set coefficients[idxCoeff] = ComplexPolar(coefficientsAmplitude[idxCoeff], coefficientsPhase[idxCoeff]);
set coefficientsPositive[idxCoeff] = coefficientsAmplitude[idxCoeff];
}
let normalizedCoefficients = PNormalize(2.0, coefficientsAmplitude);
// Test probability and phases of uniform superposition
let op = StatePreparationComplexCoefficients(coefficients);
using(control = Qubit[1]){
// Test probability
H(control[0]);
(Controlled op)(control, qubitsBE);
X(control[0]);
(Controlled ApplyToEachCA(H, _))(control, qubitsBE);
X(control[0]);
for(idxCoeff in 0..(nCoefficients-1)){
let amp = normalizedCoefficients[idxCoeff];
let prob = amp * amp;
AssertProbIntBE(idxCoeff, prob, qubitsBE, tolerance);
}
ResetAll(control);
ResetAll(qubits);
//Test phase
for(repeats in 0..nCoefficients/2){
H(control[0]);
(Controlled op)(control, qubitsBE);
X(control[0]);
(Controlled ApplyToEachCA(H, _))(control, qubitsBE);
X(control[0]);
let indexMeasuredInteger = MeasureIntegerBE(qubitsBE);
let phase = coefficientsPhase[indexMeasuredInteger];
Message($"StatePreparationComplexCoefficientsTest: expected phase = {phase}.");
AssertPhase(-0.5 * phase, control[0], tolerance);
ResetAll(control);
ResetAll(qubits);
}
}
}
}
}
}
// Test probabilities and phases of arbitrary multi-qubit superposition.
operation StatePreparationComplexCoefficientsArbitraryMultiQubitPhaseTest () : () {
body{
let tolerance = 10e-10;
mutable testCases = new StatePreparationTestCase[10];
mutable nTests = 0;
set testCases[nTests] = StatePreparationTestCase(1, [1.0986553; 0.359005], [0.419893; 0.118445]);
set nTests = nTests + 1;
set testCases[nTests] = StatePreparationTestCase(2, [1.0986553; 0.359005; - 0.123; 9.238], [0.419893; 0.118445; -0.467395; 0.419893]);
set nTests = nTests + 1;
set testCases[nTests] = StatePreparationTestCase(3, [1.0986553; 0.359005; 0.465689; 0.467395; 0.419893; 0.118445; 0.123; 9.238], [1.0986553; 0.359005; 0.465689; -0.467395; 0.419893; 0.118445; 0.461883; 0.149609]);
set nTests = nTests + 1;
// Loop over tests
for(idxTest in 0..nTests-1){
let (nQubits, coefficientsAmplitude, coefficientsPhase) = testCases[idxTest];
Message($"Test case {idxTest}");
let nCoefficients = Length(coefficientsAmplitude);
using(qubits = Qubit[nQubits]){
let qubitsBE = BigEndian(qubits);
mutable coefficients = new ComplexPolar[nCoefficients];
mutable coefficientsPositive = new Double[nCoefficients];
for(idxCoeff in 0..nCoefficients-1){
set coefficients[idxCoeff] = ComplexPolar(coefficientsAmplitude[idxCoeff], coefficientsPhase[idxCoeff]);
set coefficientsPositive[idxCoeff] = coefficientsAmplitude[idxCoeff];
}
let normalizedCoefficients = PNormalize(2.0, coefficientsAmplitude);
// Test probability and phases of arbitrary superposition
let opComplex = StatePreparationComplexCoefficients(coefficients);
let opReal = StatePreparationPositiveCoefficients(coefficientsPositive);
using(control = Qubit[1]){
// Test probability
H(control[0]);
(Controlled opComplex)(control, qubitsBE);
X(control[0]);
(Controlled opReal)(control, qubitsBE);
X(control[0]);
for(idxCoeff in 0..(nCoefficients-1)){
let amp = normalizedCoefficients[idxCoeff];
let prob = amp * amp;
AssertProbIntBE(idxCoeff, prob, qubitsBE, tolerance);
}
ResetAll(control);
ResetAll(qubits);
// Test phase
for(repeats in 0..nCoefficients/2){
H(control[0]);
(Controlled opComplex)(control, qubitsBE);
X(control[0]);
(Controlled opReal)(control, qubitsBE);
X(control[0]);
let indexMeasuredInteger = MeasureIntegerBE(qubitsBE);
let phase = coefficientsPhase[indexMeasuredInteger];
Message($"StatePreparationComplexCoefficientsTest: expected phase = {phase}.");
AssertPhase(-0.5 * phase, control[0], tolerance);
ResetAll(control);
ResetAll(qubits);
}
}
}
}
}
}
}