-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReferenceImplementation.qs
361 lines (331 loc) · 12.8 KB
/
ReferenceImplementation.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 reference solutions to all tasks.
// The tasks themselves can be found in Tasks.qs file.
// We recommend that you try to solve the tasks yourself first,
// but feel free to look up the solution if you get stuck.
//////////////////////////////////////////////////////////////////////
namespace Quantum.Kata.Superposition
{
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Extensions.Convert;
open Microsoft.Quantum.Extensions.Math;
// Task 1. Plus state
// Input: a qubit in |0⟩ state (stored in an array of length 1).
// Goal: create a |+⟩ state on this qubit (|+⟩ = (|0⟩ + |1⟩) / sqrt(2)).
operation PlusState_Reference (qs : Qubit[]) : ()
{
body
{
H(qs[0]);
}
adjoint auto;
}
// Task 2. Minus state
// Input: a qubit in |0⟩ state (stored in an array of length 1).
// Goal: create a |-⟩ state on this qubit (|-⟩ = (|0⟩ - |1⟩) / sqrt(2)).
operation MinusState_Reference (qs : Qubit[]) : ()
{
body
{
X(qs[0]);
H(qs[0]);
}
adjoint auto;
}
// Task 3. Unequal superposition
// Inputs:
// 1) a qubit in |0⟩ state (stored in an array of length 1).
// 2) angle alpha, in radians, represented as Double
// Goal: create a cos(alpha) * |0⟩ + sin(alpha) * |1⟩ state on this qubit.
operation UnequalSuperposition_Reference (qs : Qubit[], alpha : Double) : ()
{
body
{
// Hint: Experiment with rotation gates from Microsoft.Quantum.Primitive
Ry(2.0 * alpha, qs[0]);
}
adjoint auto;
}
// Task 4. Superposition of all basis vectors on two qubits
operation AllBasisVectors_TwoQubits_Reference (qs : Qubit[]) : ()
{
body
{
// Since a Hadamard gate will change |0⟩ into |+⟩ = (|0⟩ + |1⟩)/sqrt(2)
// And the desired state is just a tensor product |+⟩|+⟩, we can apply
// a Hadamard transformation to each qubit.
H(qs[0]);
H(qs[1]);
}
adjoint auto;
}
// Task 5. Superposition of basis vectors with phases
operation AllBasisVectorsWithPhases_TwoQubits_Reference (qs : Qubit[]) : ()
{
body
{
// Question:
// Is this state separable?
// Answer:
// Yes. It is. We can see that:
// ((|0⟩ - |1⟩) / sqrt(2)) ⊗ ((|0⟩ + i*|1⟩) / sqrt(2)) is equal to the desired
// state, so we can create it by doing operations on each qubit independently.
// We can see that the first qubit is in state |-⟩ and the second in state |i⟩,
// so the transformations that we need are:
// Qubit 0 is taken into |+⟩ and then z-rotated into |-⟩.
H(qs[0]);
Z(qs[0]);
// Qubit 1 is taken into |+⟩ and then z-rotated into |i⟩.
H(qs[1]);
S(qs[1]);
}
adjoint auto;
}
// Task 6. Bell state
// Input: two qubits in |00⟩ state (stored in an array of length 2).
// Goal: create a Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2) on these qubits.
operation BellState_Reference (qs : Qubit[]) : ()
{
body
{
H(qs[0]);
CNOT(qs[0], qs[1]);
}
adjoint auto;
}
// Task 7. All Bell states
// Inputs:
// 1) two qubits in |00⟩ state (stored in an array of length 2)
// 2) an integer index
// Goal: create one of the Bell states based on the value of index:
// 0: |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)
// 1: |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)
// 2: |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2)
// 3: |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2)
operation AllBellStates_Reference (qs : Qubit[], index : Int) : ()
{
body
{
H(qs[0]);
CNOT(qs[0], qs[1]);
// now we have |00⟩ + |11⟩ - modify it based on index arg
if (index % 2 == 1) {
// negative phase
Z(qs[1]);
}
if (index / 2 == 1) {
X(qs[1]);
}
}
adjoint auto;
}
// Task 8. Greenberger–Horne–Zeilinger state
// Input: N qubits in |0...0⟩ state.
// Goal: create a GHZ state (|0...0⟩ + |1...1⟩) / sqrt(2) on these qubits.
operation GHZ_State_Reference (qs : Qubit[]) : ()
{
body
{
H(qs[0]);
for (i in 1 .. Length(qs)-1) {
CNOT(qs[0], qs[i]);
}
}
adjoint auto;
}
// Task 9. Superposition of all basis vectors
// Input: N qubits in |0...0⟩ state.
// Goal: create an equal superposition of all basis vectors from |0...0⟩ to |1...1⟩
// (i.e. state (|0...0⟩ + ... + |1...1⟩) / sqrt(2^N) ).
operation AllBasisVectorsSuperposition_Reference (qs : Qubit[]) : ()
{
body
{
for (i in 0 .. Length(qs)-1) {
H(qs[i]);
}
}
adjoint auto;
}
// Task 10. Superposition of |0...0⟩ and given bit string
// Inputs:
// 1) N qubits in |0...0⟩ state
// 2) bit string represented as Bool[]
// Goal: create an equal superposition of |0...0⟩ and basis state given by the second bit string.
// Bit values false and true correspond to |0⟩ and |1⟩ states.
// You are guaranteed that the qubit array and the bit string have the same length.
// You are guaranteed that the first bit of the bit string is true.
// Example: for bit string = [true; false] the qubit state required is (|00⟩ + |10⟩) / sqrt(2).
operation ZeroAndBitstringSuperposition_Reference (qs : Qubit[], bits : Bool[]) : ()
{
body
{
AssertIntEqual(Length(bits), Length(qs), "Arrays should have the same length");
AssertBoolEqual(bits[0], true, "First bit of the input bit string should be set to true");
// Hadamard first qubit
H(qs[0]);
// iterate through the bit string and CNOT to qubits corresponding to true bits
for (i in 1..Length(qs)-1) {
if (bits[i]) {
CNOT(qs[0], qs[i]);
}
}
}
adjoint auto;
}
// Task 11. Superposition of two bit strings
// Inputs:
// 1) N qubits in |0...0⟩ state
// 2) two bit string represented as Bool[]s
// Goal: create an equal superposition of two basis states given by the bit strings.
// Bit values false and true correspond to |0⟩ and |1⟩ states.
// Example: for bit strings [false; true; false] and [false; false; true]
// the qubit state required is (|010⟩ + |001⟩) / sqrt(2).
// You are guaranteed that the two bit strings will be different.
// helper function for TwoBitstringSuperposition_Reference
function FindFirstDiff_Reference (bits1 : Bool[], bits2 : Bool[]) : Int
{
mutable firstDiff = -1;
for (i in 0 .. Length(bits1)-1) {
if (bits1[i] != bits2[i] && firstDiff == -1) {
set firstDiff = i;
}
}
return firstDiff;
}
operation TwoBitstringSuperposition_Reference (qs : Qubit[], bits1 : Bool[], bits2 : Bool[]) : ()
{
body
{
// find the index of the first bit at which the bit strings are different
let firstDiff = FindFirstDiff_Reference(bits1, bits2);
// Hadamard corresponding qubit to create superposition
H(qs[firstDiff]);
// iterate through the bit strings again setting the final state of qubits
for (i in 0 .. Length(qs)-1) {
if (bits1[i] == bits2[i]) {
// if two bits are the same apply X or nothing
if (bits1[i]) {
X(qs[i]);
}
} else {
// if two bits are different, set their difference using CNOT
if (i > firstDiff) {
CNOT(qs[firstDiff], qs[i]);
if (bits1[i] != bits1[firstDiff]) {
X(qs[i]);
}
}
}
}
}
adjoint auto;
}
// Task 12. W state on 2^k qubits
// Input: N = 2^k qubits in |0...0⟩ state.
// Goal: create a W state (https://en.wikipedia.org/wiki/W_state) on these qubits.
// W state is an equal superposition of all basis states on N qubits of Hamming weight 1.
// Example: for N = 4, W state is (|1000⟩ + |0100⟩ + |0010⟩ + |0001⟩) / 2.
operation WState_PowerOfTwo_Reference (qs : Qubit[]) : ()
{
body
{
let N = Length(qs);
if (N == 1) {
// base of recursion: |1⟩
X(qs[0]);
} else {
let K = N / 2;
// create W state on the first K qubits
WState_PowerOfTwo_Reference(qs[0..K-1]);
// the next K qubits are in |0...0⟩ state
// allocate ancilla in |+⟩ state
using (anc = Qubit[1]) {
H(anc[0]);
for (i in 0..K-1) {
(Controlled SWAP)(anc, (qs[i], qs[i+K]));
}
for (i in K..N-1) {
CNOT(qs[i], anc[0]);
}
}
}
}
adjoint auto;
}
// Task 13. W state on arbitrary number of qubits
// Input: N qubits in |0...0⟩ state (N is not necessarily a power of 2).
// Goal: create a W state (https://en.wikipedia.org/wiki/W_state) on these qubits.
// W state is an equal superposition of all basis states on N qubits of Hamming weight 1.
// Example: for N = 3, W state is (|100⟩ + |010⟩ + |001⟩) / sqrt(3).
// general solution based on rotations and recursive application of controlled generation routine
operation WState_Arbitrary_Reference (qs : Qubit[]) : ()
{
body
{
let N = Length(qs);
if (N == 1) {
// base case of recursion: |1⟩
X(qs[0]);
} else {
// |W_N> = |0⟩|W_(N-1)> + |1⟩|0...0⟩
// do a rotation on the first qubit to split it into |0⟩ and |1⟩ with proper weights
// |0⟩ -> sqrt((N-1)/N) |0⟩ + 1/sqrt(N) |1⟩
let theta = ArcSin(1.0 / Sqrt(ToDouble(N)));
Ry(2.0 * theta, qs[0]);
// do a zero-controlled W-state generation for qubits 1..N-1
X(qs[0]);
(Controlled WState_Arbitrary_Reference)(qs[0..0], qs[1..N-1]);
X(qs[0]);
}
}
adjoint auto;
controlled auto;
adjoint controlled auto;
}
// solution based on generation for 2^k and post-selection using measurements
operation WState_Arbitrary_Postselect (qs : Qubit[]) : ()
{
body
{
let N = Length(qs);
if (N == 1) {
// base case of recursion: |1⟩
X(qs[0]);
} else {
// find the smallest power of 2 which is greater than or equal to N
// as a hack, we know we're not doing it on more than 64 qubits
mutable P = 1;
for (i in 1..6) {
if (P < N) {
set P = P * 2;
}
}
if (P == N) {
// prepare as a power of 2 (previous task)
WState_PowerOfTwo_Reference(qs);
} else {
// allocate extra qubits
using (ans = Qubit[P-N]) {
let all_qubits = qs + ans;
repeat {
// prepare state W_P on original + ancilla qubits
WState_PowerOfTwo_Reference(all_qubits);
// measure ancilla qubits; if all of the results are Zero, we get the right state on main qubits
mutable allZeros = true;
for (i in 0..P-N-1) {
set allZeros = allZeros && IsResultZero(M(ans[i]));
}
} until allZeros
fixup {
ResetAll(ans);
}
}
}
}
}
}
}