forked from microsoft/QuantumKatas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tasks.qs
277 lines (203 loc) · 10.2 KB
/
Tasks.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
namespace Quantum.Kata.BasicGates {
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Extensions.Diagnostics;
open Microsoft.Quantum.Extensions.Convert;
open Microsoft.Quantum.Extensions.Math;
//////////////////////////////////////////////////////////////////
// Welcome!
//////////////////////////////////////////////////////////////////
// "Basic Gates" quantum kata is a series of exercises designed
// to get you familiar with the basic quantum gates in Q#.
// It covers the following topics:
// - basic single-qubit and multi-qubit gates,
// - adjoint and controlled gates,
// - using gates to modify the state of a qubit.
// Each task is wrapped in one operation preceded by the description of the task.
// Each task (except tasks in which you have to write a test) has a unit test associated with it,
// which initially fails. Your goal is to fill in the blank (marked with // ... comment)
// with some Q# code to make the failing test pass.
// Most tasks can be done using exactly one gate.
// None of the tasks require measurement, and the tests are written so as to fail if qubit state is measured.
// The tasks are given in approximate order of increasing difficulty; harder ones are marked with asterisks.
//////////////////////////////////////////////////////////////////
// Part I. Single-Qubit Gates
//////////////////////////////////////////////////////////////////
// Task 1.1. State flip: |0⟩ to |1⟩ and vice versa
// Input: A qubit in state |ψ⟩ = α |0⟩ + β |1⟩.
// Goal: Change the state of the qubit to α |1⟩ + β |0⟩.
// Example:
// If the qubit is in state |0⟩, change its state to |1⟩.
// If the qubit is in state |1⟩, change its state to |0⟩.
// Note that this operation is self-adjoint: applying it for a second time
// returns the qubit to the original state.
operation StateFlip (q : Qubit) : Unit {
body (...) {
// The Pauli X gate will change the |0⟩ state to the |1⟩ state and vice versa.
// Type X(q);
// Then rebuild the project and rerun the tests - T11_StateFlip_Test should now pass!
X(q);
}
adjoint self;
}
// Task 1.2. Basis change: |0⟩ to |+⟩ and |1⟩ to |-⟩ (and vice versa)
// Input: A qubit in state |ψ⟩ = α |0⟩ + β |1⟩.
// Goal: Change the state of the qubit as follows:
// If the qubit is in state |0⟩, change its state to |+⟩ = (|0⟩ + |1⟩) / sqrt(2).
// If the qubit is in state |1⟩, change its state to |-⟩ = (|0⟩ - |1⟩) / sqrt(2).
// If the qubit is in superposition, change its state according to the effect on basis vectors.
// Note: |+⟩ and |-⟩ form a different basis for single-qubit states, called X basis.
// |0⟩ and |1⟩ are called Z basis.
operation BasisChange (q : Qubit) : Unit {
body (...) {
H(q);
}
adjoint self;
}
// Task 1.3. Sign flip: |+⟩ to |-⟩ and vice versa.
// Input: A qubit in state |ψ⟩ = α |0⟩ + β |1⟩.
// Goal: Change the qubit state to α |0⟩ - β |1⟩ (flip the sign of |1⟩ component of the superposition).
operation SignFlip (q : Qubit) : Unit {
body (...) {
Z(q);
}
adjoint self;
}
// Task 1.4*. Amplitude change: |0⟩ to cos(alpha)*|0⟩ + sin(alpha)*|1⟩.
// Inputs:
// 1) A qubit in state β|0⟩ + γ|1⟩.
// 2) Angle alpha, in radians, represented as Double
// Goal: Change the state of the qubit as follows:
// If the qubit is in state |0⟩, change its state to cos(alpha)*|0⟩ + sin(alpha)*|1⟩.
// If the qubit is in state |1⟩, change its state to -sin(alpha)*|0⟩ + cos(alpha)*|1⟩.
// If the qubit is in superposition, change its state according to the effect on basis vectors.
operation AmplitudeChange (q : Qubit, alpha : Double) : Unit {
body (...) {
Ry(2.0 * alpha, q);
}
adjoint invert;
}
// Task 1.5. Phase flip
// Input: A qubit in state |ψ⟩ = α |0⟩ + β |1⟩.
// Goal: Change the qubit state to α |0⟩ + iβ |1⟩ (flip the phase of |1⟩ component of the superposition).
operation PhaseFlip (q : Qubit) : Unit {
body (...) {
// https://algassert.com/quirk#circuit={%22cols%22:[[%22H%22],[%22Z^t%22]]}
S(q);
// alternatively Rz(0.5 * PI(), q);
}
adjoint invert;
}
// Task 1.6*. Phase change
// Inputs:
// 1) A qubit in state β|0⟩ + γ|1⟩.
// 2) Angle alpha, in radians, represented as Double
// Goal: Change the state of the qubit as follows:
// If the qubit is in state |0⟩, don't change its state.
// If the qubit is in state |1⟩, change its state to exp(i*alpha)|1⟩.
// If the qubit is in superposition, change its state according to the effect on basis vectors.
operation PhaseChange (q : Qubit, alpha : Double) : Unit {
body (...) {
Rz(alpha, q);
}
adjoint invert;
}
// Task 1.7. Bell state change - 1
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
// Goal: Change the two-qubit state to |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2).
operation BellStateChange1 (qs : Qubit[]) : Unit {
body (...) {
Z(qs[1]);
}
adjoint invert;
}
// Task 1.8. Bell state change - 2
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
// Goal: Change the two-qubit state to |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2).
operation BellStateChange2 (qs : Qubit[]) : Unit {
body (...) {
X(qs[1]);
}
adjoint invert;
}
// Task 1.9. Bell state change - 3
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
// Goal: Change the two-qubit state to |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2).
operation BellStateChange3 (qs : Qubit[]) : Unit {
body (...) {
X(qs[1]);
Z(qs[1]);
}
adjoint invert;
}
//////////////////////////////////////////////////////////////////
// Part II. Multi-Qubit Gates
//////////////////////////////////////////////////////////////////
// Task 2.1. Two-qubit gate - 1
// Input: Two unentangled qubits (stored in an array of length 2).
// The first qubit will be in state |ψ⟩ = α |0⟩ + β |1⟩, the second - in state |0⟩
// (this can be written as two-qubit state (α|0⟩ + β|1⟩) ⊗ |0⟩).
// Goal: Change the two-qubit state to α |00⟩ + β |11⟩.
// Note that unless the starting state of the first qubit was |0⟩ or |1⟩,
// the resulting two-qubit state can not be represented as a tensor product
// of the states of individual qubits any longer; thus the qubits become entangled.
operation TwoQubitGate1 (qs : Qubit[]) : Unit {
body (...) {
CNOT(qs[0], qs[1]);
}
adjoint self;
}
// Task 2.2. Two-qubit gate - 2
// Input: Two qubits (stored in an array of length 2)
// in state |+⟩ ⊗ |+⟩ = (|00⟩ + |01⟩ + |10⟩ + |11⟩) / 2.
// Goal: Change the two-qubit state to (|00⟩ + |01⟩ + |10⟩ - |11⟩) / 2.
// Note that while the starting state can be represented as a tensor product of single-qubit states,
// the resulting two-qubit state can not be represented in such a way.
operation TwoQubitGate2 (qs : Qubit[]) : Unit {
body (...) {
Controlled Z([qs[0]], qs[1]);
}
adjoint self;
}
// Task 2.3. Two-qubit gate - 3
// Input: Two qubits (stored in an array of length 2) in an arbitrary
// two-qubit state α|00⟩ + β|01⟩ + γ|10⟩ + δ|11⟩.
// Goal: Change the two-qubit state to α|00⟩ + γ|01⟩ + β|10⟩ + δ|11⟩.
operation TwoQubitGate3 (qs : Qubit[]) : Unit {
body (...) {
// Hint: this task can be solved using one primitive gate;
// as an exercise, try to express the solution using several
// (possibly controlled) Pauli gates.
// SWAP(qs[0], qs[1]);
CNOT(qs[0], qs[1]);
CNOT(qs[1], qs[0]);
CNOT(qs[0], qs[1]);
}
adjoint self;
}
// Task 2.4. Toffoli gate
// Input: Three qubits (stored in an array of length 3) in an arbitrary three-qubit state
// α|000⟩ + β|001⟩ + γ|010⟩ + δ|011⟩ + ε|100⟩ + ζ|101⟩ + η|110⟩ + θ|111⟩.
// Goal: Flip the state of the third qubit if the state of the first two is |11⟩:
// i.e., change the three-qubit state to
// α|000⟩ + β|001⟩ + γ|010⟩ + δ|011⟩ + ε|100⟩ + ζ|101⟩ + θ|110⟩ + η|111⟩.
operation ToffoliGate (qs : Qubit[]) : Unit {
body (...) {
CCNOT(qs[0], qs[1], qs[2]);
}
adjoint self;
}
// Task 2.5. Fredkin gate
// Input: Three qubits (stored in an array of length 3) in an arbitrary three-qubit state
// α|000⟩ + β|001⟩ + γ|010⟩ + δ|011⟩ + ε|100⟩ + ζ|101⟩ + η|110⟩ + θ|111⟩.
// Goal: Swap the states of second and third qubit if and only if the state of the first qubit is |1⟩:
// α|000⟩ + β|001⟩ + γ|010⟩ + δ|011⟩ + ε|100⟩ + η|101⟩ + ζ|110⟩ + θ|111⟩.
operation FredkinGate (qs : Qubit[]) : Unit {
body (...) {
(Controlled SWAP)([qs[0]], (qs[1], qs[2]));
}
adjoint self;
}
}