This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTasks.qs
289 lines (229 loc) · 12.1 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
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
namespace Quantum.Kata.Superposition {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
//////////////////////////////////////////////////////////////////
// Welcome!
//////////////////////////////////////////////////////////////////
// "Superposition" quantum kata is a series of exercises designed
// to get you familiar with programming in Q#.
// It covers the following topics:
// - basic single-qubit and multi-qubit gates,
// - superposition,
// - flow control and recursion in Q#.
// 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.
// The tasks are given in approximate order of increasing difficulty; harder ones are marked with asterisks.
//////////////////////////////////////////////////////////////////
// Part I. Simple Gates
//////////////////////////////////////////////////////////////////
// Task 1.1. Plus state
// Input: a qubit in the |0⟩ state.
// Goal: prepare a |+⟩ state on this qubit (|+⟩ = (|0⟩ + |1⟩) / sqrt(2)).
operation PlusState (q : Qubit) : Unit {
// Hadamard gate H will convert |0⟩ state to |+⟩ state.
// Type the following: H(q);
// Then rebuild the project and rerun the tests - T01_PlusState should now pass!
// ...
}
// Task 1.2. Minus state
// Input: a qubit in the |0⟩ state.
// Goal: prepare a |-⟩ state on this qubit (|-⟩ = (|0⟩ - |1⟩) / sqrt(2)).
operation MinusState (q : Qubit) : Unit {
// In this task, as well as in all subsequent ones, you have to come up with the solution yourself.
// ...
}
// Task 1.3. Superposition of all basis vectors on two qubits
// Input: two qubits in |00⟩ state (stored in an array of length 2).
// Goal: create the following state on these qubits: (|00⟩ + |01⟩ + |10⟩ + |11⟩) / 2.
operation AllBasisVectors_TwoQubits (qs : Qubit[]) : Unit {
// The following lines enforce the constraints on the input that you are given.
// You don't need to modify them. Feel free to remove them, this won't cause your code to fail.
Fact(Length(qs) == 2, "The array should have exactly 2 qubits.");
// ...
}
// Task 1.4. Superposition of basis vectors with phase flip.
// Input: two qubits in |00⟩ state (stored in an array of length 2).
// Goal: create the following state on these qubits: (|00⟩ + |01⟩ + |10⟩ - |11⟩) / 2.
operation AllBasisVectorWithPhaseFlip_TwoQubits (qs : Qubit[]) : Unit {
// ...
}
// Task 1.5. Superposition of basis vectors with phases
// Input: two qubits in |00⟩ state (stored in an array of length 2).
// Goal: create the following state on these qubits: (|00⟩ + i*|01⟩ - |10⟩ - i*|11⟩) / 2.
operation AllBasisVectorsWithPhases_TwoQubits (qs : Qubit[]) : Unit {
// The following lines enforce the constraints on the input that you are given.
// You don't need to modify them. Feel free to remove them, this won't cause your code to fail.
Fact(Length(qs) == 2, "The array should have exactly 2 qubits.");
// Hint: Is this state separable?
// ...
}
// Task 1.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 (qs : Qubit[]) : Unit {
// ...
}
// Task 1.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 (qs : Qubit[], index : Int) : Unit {
// ...
}
// Task 1.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 (qs : Qubit[]) : Unit {
// Hint: N can be found as Length(qs).
// ...
}
// Task 1.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 (qs : Qubit[]) : Unit {
// ...
}
// Task 1.10. Superposition of all even or all odd numbers
// Inputs:
// 1) N qubits in |0...0⟩ state.
// 2) A boolean isEven.
// Goal: create a superposition of all even numbers on N qubits if isEven is true,
// or a superposition of all odd numbers on N qubits if isEven is false.
//
// A basis state encodes an integer number using big-endian binary notation:
// state |01⟩ corresponds to the integer 1, and state |10⟩ - to the integer 2.
//
// Example: for N = 2 and isEven = true the required state is (|00⟩ + |10⟩) / sqrt(2),
// and for N = 2 and isEven = false - (|01⟩ + |11⟩) / sqrt(2).
operation EvenOddNumbersSuperposition (qs : Qubit[], isEven : Bool) : Unit {
// ...
}
// Task 1.11. 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 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 (qs : Qubit[], bits : Bool[]) : Unit {
// The following lines enforce the constraints on the input that you are given.
// You don't need to modify them. Feel free to remove them, this won't cause your code to fail.
Fact(Length(bits) == Length(qs), "Arrays should have the same length");
Fact(Head(bits), "First bit of the input bit string should be set to true");
// ...
}
// Task 1.12. 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 both bit strings have the same length as the qubit array,
// and that the bit strings will differ in at least one bit.
operation TwoBitstringSuperposition (qs : Qubit[], bits1 : Bool[], bits2 : Bool[]) : Unit {
// ...
}
// Task 1.13*. Superposition of four bit strings
// Inputs:
// 1) N qubits in |0...0⟩ state
// 2) four bit string represented as Bool[][] bits
// bits is an array of size 4 x N array which describes the bit strings as follows:
// bits[i] describes the i-th bit string and has N elements.
// All four bit strings will be distinct.
//
// Goal: create an equal superposition of the four basis states given by the bit strings.
//
// Example: for N = 3 and bits = [[false, true, false], [true, false, false], [false, false, true], [true, true, false]]
// the state you need to prepare is (|010⟩ + |100⟩ + |001⟩ + |110⟩) / 2.
operation FourBitstringSuperposition (qs : Qubit[], bits : Bool[][]) : Unit {
// Hint: remember that you can allocate extra qubits.
// ...
}
// Task 1.14. Superposition of all bit strings of the given parity
// Inputs:
// 1) N qubits in |0..0⟩ state (stored in an array of length N).
// 2) An int "parity".
// Goal: change the state to an equal superposition of all basis states that have
// an even number of 1s in them if "parity" = 0, or
// an odd number of 1s in them if "parity" = 1.
// Example: for N = 2, the goal state would be (|00⟩ + |11⟩) / sqrt(2) if "parity" = 0,
// and (|01⟩ + |10⟩) / sqrt(2) if "parity" = 1.
operation AllStatesWithParitySuperposition (qs : Qubit[], parity : Int) : Unit {
// Hint: remember that you can call the solution recursively.
// You are allowed to modify the signature of the method to include adjoint and/or controlled variants.
// ...
}
//////////////////////////////////////////////////////////////////
// Part II. Arbitrary Rotations
//////////////////////////////////////////////////////////////////
// Task 2.1. Unequal superposition
// Inputs:
// 1) a qubit in the |0⟩ state.
// 2) angle alpha, in radians, represented as Double.
// Goal: prepare a cos(alpha) * |0⟩ + sin(alpha) * |1⟩ state on this qubit.
operation UnequalSuperposition (q : Qubit, alpha : Double) : Unit {
// Hint: Experiment with rotation gates from the Microsoft.Quantum.Intrinsic namespace.
// Note that all rotation operators rotate the state by _half_ of its angle argument.
// ...
}
// Task 2.2. 1/sqrt(2)|00⟩ + 1/2|01⟩ + 1/2|10⟩ state
// Input: two qubits in |00⟩ state (stored in an array of length 2).
// Goal: change the state to 1/sqrt(2)|00⟩ + 1/2|10⟩ + 1/2|11⟩.
operation ControlledRotation (qs : Qubit[]) : Unit {
//...
}
// Task 2.3*. |00⟩ + |01⟩ + |10⟩ state
// Input: 2 qubits in |00⟩ state (stored in an array of length 2).
// Goal: change the state to (|00⟩ + |01⟩ + |10⟩) / sqrt(3).
operation ThreeStates_TwoQubits (qs : Qubit[]) : Unit {
// ...
}
// Task 2.4*. (|00⟩ + ω |01⟩ + ω² |10⟩) / sqrt(3)
// Input: two qubits in |00⟩ state (stored in an array of length 2).
// Goal: change the state to (|00⟩ + ω |01⟩ + ω² |10⟩) / sqrt(3) where ω is exp(2πi/3).
operation ThreeStates_TwoQubits_Phases (qs : Qubit[]) : Unit {
// ...
}
// Task 2.5*. Hardy State
// Input: 2 qubits in |00⟩ state.
// Goal: create the state (3|00⟩ + |01⟩ + |10⟩ + |11⟩) / sqrt(12) on these qubits.
operation Hardy_State (qs : Qubit[]) : Unit {
// ...
}
// Task 2.6*. W state on 2ᵏ qubits
// Input: N = 2ᵏ 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 (qs : Qubit[]) : Unit {
// Hint: you can use Controlled modifier to perform arbitrary controlled gates.
// ...
}
// Task 2.7**. 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).
operation WState_Arbitrary (qs : Qubit[]) : Unit {
// ...
}
}