Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit 8621795

Browse files
adutchengineertcNickolas
authored andcommitted
BasicGates: Add new task for adding global phase -1 (#177)
1 parent 47dfa99 commit 8621795

File tree

4 files changed

+93
-38
lines changed

4 files changed

+93
-38
lines changed

BasicGates/BasicGates.ipynb

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
"metadata": {},
108108
"outputs": [],
109109
"source": [
110-
"%kata T11_StateFlip_Test \n",
110+
"%kata T101_StateFlip_Test \n",
111111
"\n",
112112
"operation StateFlip (q : Qubit) : Unit is Adj+Ctl {\n",
113113
" // The Pauli X gate will change the |0⟩ state to the |1⟩ state and vice versa.\n",
@@ -142,7 +142,7 @@
142142
"metadata": {},
143143
"outputs": [],
144144
"source": [
145-
"%kata T12_BasisChange_Test \n",
145+
"%kata T102_BasisChange_Test \n",
146146
"\n",
147147
"operation BasisChange (q : Qubit) : Unit is Adj+Ctl {\n",
148148
" // ...\n",
@@ -166,7 +166,7 @@
166166
"metadata": {},
167167
"outputs": [],
168168
"source": [
169-
"%kata T13_SignFlip_Test \n",
169+
"%kata T103_SignFlip_Test \n",
170170
"\n",
171171
"operation SignFlip (q : Qubit) : Unit is Adj+Ctl {\n",
172172
" // ...\n",
@@ -199,7 +199,7 @@
199199
"metadata": {},
200200
"outputs": [],
201201
"source": [
202-
"%kata T14_AmplitudeChange_Test\n",
202+
"%kata T104_AmplitudeChange_Test\n",
203203
"\n",
204204
"operation AmplitudeChange (alpha : Double, q : Qubit) : Unit is Adj+Ctl {\n",
205205
" // ...\n",
@@ -223,7 +223,7 @@
223223
"metadata": {},
224224
"outputs": [],
225225
"source": [
226-
"%kata T15_PhaseFlip_Test\n",
226+
"%kata T105_PhaseFlip_Test\n",
227227
"\n",
228228
"operation PhaseFlip (q : Qubit) : Unit is Adj+Ctl {\n",
229229
" // ...\n",
@@ -253,7 +253,7 @@
253253
"metadata": {},
254254
"outputs": [],
255255
"source": [
256-
"%kata T16_PhaseChange_Test\n",
256+
"%kata T106_PhaseChange_Test\n",
257257
"\n",
258258
"operation PhaseChange (alpha : Double, q : Qubit) : Unit is Adj+Ctl {\n",
259259
" // ...\n",
@@ -264,7 +264,34 @@
264264
"cell_type": "markdown",
265265
"metadata": {},
266266
"source": [
267-
"### Task 1.7. Bell state change - 1\n",
267+
"### Task 1.7. Global phase change\n",
268+
"**Input:** A qubit in state $|\\psi\\rangle = \\beta |0\\rangle + \\gamma |1\\rangle$.\n",
269+
"\n",
270+
"**Goal**: Change the state of the qubit to $- \\beta |0\\rangle - \\gamma |1\\rangle$.\n",
271+
"\n",
272+
"> Note: this change on its own is not observable - there is no experiment you can do on a standalone qubit to figure out whether it acquired the global phase or not. \n",
273+
"> However, you can use a controlled version of this operation to observe the global phase it introduces. \n",
274+
"> This is used in later katas as part of more complicated tasks."
275+
]
276+
},
277+
{
278+
"cell_type": "code",
279+
"execution_count": null,
280+
"metadata": {},
281+
"outputs": [],
282+
"source": [
283+
"%kata T107_GlobalPhaseChange_Test\n",
284+
"\n",
285+
"operation GlobalPhaseChange (q : Qubit) : Unit is Adj+Ctl {\n",
286+
" // ...\n",
287+
"}"
288+
]
289+
},
290+
{
291+
"cell_type": "markdown",
292+
"metadata": {},
293+
"source": [
294+
"### Task 1.8. Bell state change - 1\n",
268295
"\n",
269296
"**Input:** Two entangled qubits in Bell state $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big(|00\\rangle + |11\\rangle\\big)$.\n",
270297
"\n",
@@ -277,7 +304,7 @@
277304
"metadata": {},
278305
"outputs": [],
279306
"source": [
280-
"%kata T17_BellStateChange1_Test\n",
307+
"%kata T108_BellStateChange1_Test\n",
281308
"\n",
282309
"operation BellStateChange1 (qs : Qubit[]) : Unit is Adj+Ctl {\n",
283310
" // ...\n",
@@ -288,7 +315,7 @@
288315
"cell_type": "markdown",
289316
"metadata": {},
290317
"source": [
291-
"### Task 1.8. Bell state change - 2\n",
318+
"### Task 1.9. Bell state change - 2\n",
292319
"\n",
293320
"**Input:** Two entangled qubits in Bell state $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big(|00\\rangle + |11\\rangle\\big)$.\n",
294321
"\n",
@@ -301,7 +328,7 @@
301328
"metadata": {},
302329
"outputs": [],
303330
"source": [
304-
"%kata T18_BellStateChange2_Test\n",
331+
"%kata T109_BellStateChange2_Test\n",
305332
"\n",
306333
"operation BellStateChange2 (qs : Qubit[]) : Unit is Adj+Ctl {\n",
307334
" // ...\n",
@@ -312,7 +339,7 @@
312339
"cell_type": "markdown",
313340
"metadata": {},
314341
"source": [
315-
"### Task 1.9. Bell state change - 3\n",
342+
"### Task 1.10. Bell state change - 3\n",
316343
"\n",
317344
"**Input:** Two entangled qubits in Bell state $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big(|00\\rangle + |11\\rangle\\big)$.\n",
318345
"\n",
@@ -325,7 +352,7 @@
325352
"metadata": {},
326353
"outputs": [],
327354
"source": [
328-
"%kata T19_BellStateChange3_Test\n",
355+
"%kata T110_BellStateChange3_Test\n",
329356
"\n",
330357
"operation BellStateChange3 (qs : Qubit[]) : Unit is Adj+Ctl {\n",
331358
" // ...\n",
@@ -367,7 +394,7 @@
367394
"metadata": {},
368395
"outputs": [],
369396
"source": [
370-
"%kata T21_TwoQubitGate1_Test\n",
397+
"%kata T201_TwoQubitGate1_Test\n",
371398
"\n",
372399
"operation TwoQubitGate1 (qs : Qubit[]) : Unit is Adj {\n",
373400
" // ...\n",
@@ -395,7 +422,7 @@
395422
"metadata": {},
396423
"outputs": [],
397424
"source": [
398-
"%kata T22_TwoQubitGate2_Test\n",
425+
"%kata T202_TwoQubitGate2_Test\n",
399426
"\n",
400427
"operation TwoQubitGate2 (qs : Qubit[]) : Unit is Adj {\n",
401428
" // ...\n",
@@ -422,7 +449,7 @@
422449
"metadata": {},
423450
"outputs": [],
424451
"source": [
425-
"%kata T23_TwoQubitGate3_Test\n",
452+
"%kata T203_TwoQubitGate3_Test\n",
426453
"\n",
427454
"operation TwoQubitGate3 (qs : Qubit[]) : Unit is Adj {\n",
428455
" // ...\n",
@@ -447,7 +474,7 @@
447474
"metadata": {},
448475
"outputs": [],
449476
"source": [
450-
"%kata T24_ToffoliGate_Test\n",
477+
"%kata T204_ToffoliGate_Test\n",
451478
"\n",
452479
"operation ToffoliGate (qs : Qubit[]) : Unit is Adj {\n",
453480
" // ...\n",
@@ -472,7 +499,7 @@
472499
"metadata": {},
473500
"outputs": [],
474501
"source": [
475-
"%kata T25_FredkinGate_Test\n",
502+
"%kata T205_FredkinGate_Test\n",
476503
"\n",
477504
"operation FredkinGate (qs : Qubit[]) : Unit is Adj {\n",
478505
" // ...\n",

BasicGates/ReferenceImplementation.qs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Quantum.Kata.BasicGates {
1212
open Microsoft.Quantum.Intrinsic;
13+
open Microsoft.Quantum.Math;
1314

1415

1516
//////////////////////////////////////////////////////////////////
@@ -81,8 +82,14 @@ namespace Quantum.Kata.BasicGates {
8182
R1(alpha, q);
8283
}
8384

85+
// Task 1.7. Global Phase Change
86+
// Input: A qubit in state β|0⟩ + γ|1⟩.
87+
// Goal: Change the state of the qubit to - β|0⟩ - γ|1⟩.
88+
operation GlobalPhaseChange_Reference (q: Qubit) : Unit is Adj+Ctl {
89+
R(PauliI, 2.0 * PI(), q);
90+
}
8491

85-
// Task 1.7. Bell state change - 1
92+
// Task 1.8. Bell state change - 1
8693
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
8794
// Goal: Change the two-qubit state to |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2).
8895
operation BellStateChange1_Reference (qs : Qubit[]) : Unit is Adj+Ctl {
@@ -91,7 +98,7 @@ namespace Quantum.Kata.BasicGates {
9198
}
9299

93100

94-
// Task 1.8. Bell state change - 2
101+
// Task 1.9. Bell state change - 2
95102
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
96103
// Goal: Change the two-qubit state to |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2).
97104
operation BellStateChange2_Reference (qs : Qubit[]) : Unit is Adj+Ctl {
@@ -100,7 +107,7 @@ namespace Quantum.Kata.BasicGates {
100107
}
101108

102109

103-
// Task 1.9. Bell state change - 3
110+
// Task 1.10. Bell state change - 3
104111
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
105112
// Goal: Change the two-qubit state to |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2).
106113
operation BellStateChange3_Reference (qs : Qubit[]) : Unit is Adj+Ctl {

BasicGates/Tasks.qs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,38 @@ namespace Quantum.Kata.BasicGates {
111111
// ...
112112
}
113113

114+
// Task 1.7. Global phase change
115+
// Input: A qubit in state β|0⟩ + γ|1⟩.
116+
// Goal: Change the state of the qubit to - β|0⟩ - γ|1⟩.
117+
//
118+
// Note: This change on its own is not observable -
119+
// there is no experiment you can do on a standalone qubit
120+
// to figure out whether it acquired the global phase or not.
121+
// However, you can use a controlled version of this operation
122+
// to observe the global phase it introduces. This is used
123+
// in later katas as part of more complicated tasks.
124+
operation GlobalPhaseChange (q: Qubit) : Unit is Adj+Ctl {
125+
// ...
126+
}
127+
114128

115-
// Task 1.7. Bell state change - 1
129+
// Task 1.8. Bell state change - 1
116130
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
117131
// Goal: Change the two-qubit state to |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2).
118132
operation BellStateChange1 (qs : Qubit[]) : Unit is Adj+Ctl {
119133
// ...
120134
}
121135

122136

123-
// Task 1.8. Bell state change - 2
137+
// Task 1.9. Bell state change - 2
124138
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
125139
// Goal: Change the two-qubit state to |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2).
126140
operation BellStateChange2 (qs : Qubit[]) : Unit is Adj+Ctl {
127141
// ...
128142
}
129143

130144

131-
// Task 1.9. Bell state change - 3
145+
// Task 1.10. Bell state change - 3
132146
// Input: Two entangled qubits in Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2).
133147
// Goal: Change the two-qubit state to |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2).
134148
operation BellStateChange3 (qs : Qubit[]) : Unit is Adj+Ctl {

BasicGates/Tests.qs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,25 @@ namespace Quantum.Kata.BasicGates {
3535

3636

3737
// ------------------------------------------------------
38-
operation T11_StateFlip_Test () : Unit {
38+
operation T101_StateFlip_Test () : Unit {
3939
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(StateFlip, _), ArrayWrapperOperation(StateFlip_Reference, _));
4040
}
4141

4242

4343
// ------------------------------------------------------
44-
operation T12_BasisChange_Test () : Unit {
44+
operation T102_BasisChange_Test () : Unit {
4545
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(BasisChange, _), ArrayWrapperOperation(BasisChange_Reference, _));
4646
}
4747

4848

4949
// ------------------------------------------------------
50-
operation T13_SignFlip_Test () : Unit {
50+
operation T103_SignFlip_Test () : Unit {
5151
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(SignFlip, _), ArrayWrapperOperation(SignFlip_Reference, _));
5252
}
5353

5454

5555
// ------------------------------------------------------
56-
operation T14_AmplitudeChange_Test () : Unit {
56+
operation T104_AmplitudeChange_Test () : Unit {
5757
for (i in 0 .. 36) {
5858
let alpha = ((2.0 * PI()) * IntAsDouble(i)) / 36.0;
5959
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(AmplitudeChange(alpha, _), _), ArrayWrapperOperation(AmplitudeChange_Reference(alpha, _), _));
@@ -62,20 +62,26 @@ namespace Quantum.Kata.BasicGates {
6262

6363

6464
// ------------------------------------------------------
65-
operation T15_PhaseFlip_Test () : Unit {
65+
operation T105_PhaseFlip_Test () : Unit {
6666
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(PhaseFlip, _), ArrayWrapperOperation(PhaseFlip_Reference, _));
6767
}
6868

6969

7070
// ------------------------------------------------------
71-
operation T16_PhaseChange_Test () : Unit {
71+
operation T106_PhaseChange_Test () : Unit {
7272
for (i in 0 .. 36) {
7373
let alpha = ((2.0 * PI()) * IntAsDouble(i)) / 36.0;
7474
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(PhaseChange(alpha, _), _), ArrayWrapperOperation(PhaseChange_Reference(alpha, _), _));
7575
}
7676
}
7777

7878

79+
// ------------------------------------------------------
80+
operation T107_GlobalPhaseChange_Test () : Unit {
81+
AssertOperationsEqualReferenced(2, ArrayWrapperOperation(GlobalPhaseChange, _), ArrayWrapperOperation(GlobalPhaseChange_Reference, _));
82+
}
83+
84+
7985
// ------------------------------------------------------
8086
// 0 - |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)
8187
// 1 - |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)
@@ -119,22 +125,23 @@ namespace Quantum.Kata.BasicGates {
119125
AssertAllZero(qs);
120126
}
121127
}
128+
122129

123-
130+
124131
// ------------------------------------------------------
125-
operation T17_BellStateChange1_Test () : Unit {
132+
operation T108_BellStateChange1_Test () : Unit {
126133
VerifyBellStateConversion(BellStateChange1, 0, 1);
127134
}
128135

129136

130137
// ------------------------------------------------------
131-
operation T18_BellStateChange2_Test () : Unit {
138+
operation T109_BellStateChange2_Test () : Unit {
132139
VerifyBellStateConversion(BellStateChange2, 0, 2);
133140
}
134141

135142

136143
// ------------------------------------------------------
137-
operation T19_BellStateChange3_Test () : Unit {
144+
operation T110_BellStateChange3_Test () : Unit {
138145
VerifyBellStateConversion(BellStateChange3, 0, 3);
139146
}
140147

@@ -147,7 +154,7 @@ namespace Quantum.Kata.BasicGates {
147154

148155

149156
// ------------------------------------------------------
150-
operation T21_TwoQubitGate1_Test () : Unit {
157+
operation T201_TwoQubitGate1_Test () : Unit {
151158

152159
// Note that the way the problem is formulated, we can't just compare two unitaries,
153160
// we need to create an input state |A⟩ and check that the output state is correct
@@ -180,7 +187,7 @@ namespace Quantum.Kata.BasicGates {
180187

181188

182189
// ------------------------------------------------------
183-
operation T22_TwoQubitGate2_Test () : Unit {
190+
operation T202_TwoQubitGate2_Test () : Unit {
184191
using (qs = Qubit[2]) {
185192
// prepare |+⟩ ⊗ |+⟩ state
186193
StatePrep_PlusPlus(qs);
@@ -204,20 +211,20 @@ namespace Quantum.Kata.BasicGates {
204211
}
205212

206213

207-
operation T23_TwoQubitGate3_Test () : Unit {
214+
operation T203_TwoQubitGate3_Test () : Unit {
208215
AssertOperationsEqualReferenced(2, SwapWrapper, TwoQubitGate3_Reference);
209216
AssertOperationsEqualReferenced(2, TwoQubitGate3, TwoQubitGate3_Reference);
210217
}
211218

212219

213220
// ------------------------------------------------------
214-
operation T24_ToffoliGate_Test () : Unit {
221+
operation T204_ToffoliGate_Test () : Unit {
215222
AssertOperationsEqualReferenced(3, ToffoliGate, ToffoliGate_Reference);
216223
}
217224

218225

219226
// ------------------------------------------------------
220-
operation T25_FredkinGate_Test () : Unit {
227+
operation T205_FredkinGate_Test () : Unit {
221228
AssertOperationsEqualReferenced(3, FredkinGate, FredkinGate_Reference);
222229
}
223230

0 commit comments

Comments
 (0)