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

Commit 13705d3

Browse files
ricardo-espinozatcNickolas
authored andcommitted
Adding new tasks to Superposition kata. (#40)
1 parent bbc8750 commit 13705d3

File tree

3 files changed

+117
-25
lines changed

3 files changed

+117
-25
lines changed

Superposition/ReferenceImplementation.qs

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,48 @@ namespace Quantum.Kata.Superposition
5555
adjoint auto;
5656
}
5757

58-
// Task 4. Bell state
58+
// Task 4. Superposition of all basis vectors on two qubits
59+
operation AllBasisVectors_TwoQubits_Reference (qs : Qubit[]) : ()
60+
{
61+
body
62+
{
63+
// Since a Hadamard gate will change |0⟩ into |+⟩ = (|0⟩ + |1⟩)/sqrt(2)
64+
// And the desired state is just a tensor product |+⟩|+⟩, we can apply
65+
// a Hadamard transformation to each qubit.
66+
H(qs[0]);
67+
H(qs[1]);
68+
}
69+
adjoint auto;
70+
}
71+
72+
// Task 5. Superposition of basis vectors with phases
73+
operation AllBasisVectorsWithPhases_TwoQubits_Reference (qs : Qubit[]) : ()
74+
{
75+
body
76+
{
77+
// Question:
78+
// Is this state separable?
79+
80+
// Answer:
81+
// Yes. It is. We can see that:
82+
// ((|0⟩ - |1⟩) / sqrt(2)) ⊗ ((|0⟩ + i*|1⟩) / sqrt(2)) is equal to the desired
83+
// state, so we can create it by doing operations on each qubit independently.
84+
85+
// We can see that the first qubit is in state |-⟩ and the second in state |i⟩,
86+
// so the transformations that we need are:
87+
88+
// Qubit 0 is taken into |+⟩ and then z-rotated into |-⟩.
89+
H(qs[0]);
90+
Z(qs[0]);
91+
92+
// Qubit 1 is taken into |+⟩ and then z-rotated into |i⟩.
93+
H(qs[1]);
94+
S(qs[1]);
95+
}
96+
adjoint auto;
97+
}
98+
99+
// Task 6. Bell state
59100
// Input: two qubits in |00⟩ state (stored in an array of length 2).
60101
// Goal: create a Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2) on these qubits.
61102
operation BellState_Reference (qs : Qubit[]) : ()
@@ -68,7 +109,7 @@ namespace Quantum.Kata.Superposition
68109
adjoint auto;
69110
}
70111

71-
// Task 5. All Bell states
112+
// Task 7. All Bell states
72113
// Inputs:
73114
// 1) two qubits in |00⟩ state (stored in an array of length 2)
74115
// 2) an integer index
@@ -95,7 +136,7 @@ namespace Quantum.Kata.Superposition
95136
adjoint auto;
96137
}
97138

98-
// Task 6. Greenberger–Horne–Zeilinger state
139+
// Task 8. Greenberger–Horne–Zeilinger state
99140
// Input: N qubits in |0...0⟩ state.
100141
// Goal: create a GHZ state (|0...0⟩ + |1...1⟩) / sqrt(2) on these qubits.
101142
operation GHZ_State_Reference (qs : Qubit[]) : ()
@@ -110,7 +151,7 @@ namespace Quantum.Kata.Superposition
110151
adjoint auto;
111152
}
112153

113-
// Task 7. Superposition of all basis vectors
154+
// Task 9. Superposition of all basis vectors
114155
// Input: N qubits in |0...0⟩ state.
115156
// Goal: create an equal superposition of all basis vectors from |0...0⟩ to |1...1⟩
116157
// (i.e. state (|0...0⟩ + ... + |1...1⟩) / sqrt(2^N) ).
@@ -125,7 +166,7 @@ namespace Quantum.Kata.Superposition
125166
adjoint auto;
126167
}
127168

128-
// Task 8. Superposition of |0...0⟩ and given bit string
169+
// Task 10. Superposition of |0...0⟩ and given bit string
129170
// Inputs:
130171
// 1) N qubits in |0...0⟩ state
131172
// 2) bit string represented as Bool[]
@@ -154,7 +195,7 @@ namespace Quantum.Kata.Superposition
154195
adjoint auto;
155196
}
156197

157-
// Task 9. Superposition of two bit strings
198+
// Task 11. Superposition of two bit strings
158199
// Inputs:
159200
// 1) N qubits in |0...0⟩ state
160201
// 2) two bit string represented as Bool[]s
@@ -207,7 +248,7 @@ namespace Quantum.Kata.Superposition
207248
adjoint auto;
208249
}
209250

210-
// Task 10. W state on 2^k qubits
251+
// Task 12. W state on 2^k qubits
211252
// Input: N = 2^k qubits in |0...0⟩ state.
212253
// Goal: create a W state (https://en.wikipedia.org/wiki/W_state) on these qubits.
213254
// W state is an equal superposition of all basis states on N qubits of Hamming weight 1.
@@ -241,7 +282,7 @@ namespace Quantum.Kata.Superposition
241282
adjoint auto;
242283
}
243284

244-
// Task 11. W state on arbitrary number of qubits
285+
// Task 13. W state on arbitrary number of qubits
245286
// Input: N qubits in |0...0⟩ state (N is not necessarily a power of 2).
246287
// Goal: create a W state (https://en.wikipedia.org/wiki/W_state) on these qubits.
247288
// W state is an equal superposition of all basis states on N qubits of Hamming weight 1.

Superposition/Tasks.qs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace Quantum.Kata.Superposition
3535
{
3636
// Hadamard gate H will convert |0⟩ state to |+⟩ state.
3737
// The first qubit of the array can be accessed as qs[0].
38-
// Type H(qs[0]);
38+
// Type the following: H(qs[0]);
3939
// Then rebuild the project and rerun the tests - T01_PlusState_Test should now pass!
4040

4141
// ...
@@ -71,7 +71,38 @@ namespace Quantum.Kata.Superposition
7171
}
7272
}
7373

74-
// Task 4. Bell state
74+
// Task 4. Superposition of all basis vectors on two qubits
75+
// Input: two qubits in |00⟩ state (stored in an array of length 2).
76+
// Goal: create the following state on these qubits: (|00⟩ + |01⟩ + |10⟩ + |11⟩) / 2.
77+
operation AllBasisVectors_TwoQubits (qs : Qubit[]) : ()
78+
{
79+
body
80+
{
81+
// The following lines enforce the constraints on the input that you are given.
82+
// You don't need to modify them. Feel free to remove them, this won't cause your code to fail.
83+
AssertIntEqual(Length(qs), 2, "The array should have exactly 2 qubits.");
84+
85+
// ...
86+
}
87+
}
88+
89+
// Task 5. Superposition of basis vectors with phases
90+
// Input: two qubits in |00⟩ state (stored in an array of length 2).
91+
// Goal: create the following state on these qubits: (|00⟩ + i*|01⟩ - |10⟩ - i*|11⟩) / 2.
92+
operation AllBasisVectorsWithPhases_TwoQubits (qs : Qubit[]) : ()
93+
{
94+
body
95+
{
96+
// The following lines enforce the constraints on the input that you are given.
97+
// You don't need to modify them. Feel free to remove them, this won't cause your code to fail.
98+
AssertIntEqual(Length(qs), 2, "The array should have exactly 2 qubits.");
99+
100+
// Hint: Is this state separable?
101+
// ...
102+
}
103+
}
104+
105+
// Task 6. Bell state
75106
// Input: two qubits in |00⟩ state (stored in an array of length 2).
76107
// Goal: create a Bell state |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2) on these qubits.
77108
operation BellState (qs : Qubit[]) : ()
@@ -82,7 +113,7 @@ namespace Quantum.Kata.Superposition
82113
}
83114
}
84115

85-
// Task 5. All Bell states
116+
// Task 7. All Bell states
86117
// Inputs:
87118
// 1) two qubits in |00⟩ state (stored in an array of length 2)
88119
// 2) an integer index
@@ -99,7 +130,7 @@ namespace Quantum.Kata.Superposition
99130
}
100131
}
101132

102-
// Task 6. Greenberger–Horne–Zeilinger state
133+
// Task 8. Greenberger–Horne–Zeilinger state
103134
// Input: N qubits in |0...0⟩ state.
104135
// Goal: create a GHZ state (|0...0⟩ + |1...1⟩) / sqrt(2) on these qubits.
105136
operation GHZ_State (qs : Qubit[]) : ()
@@ -112,7 +143,7 @@ namespace Quantum.Kata.Superposition
112143
}
113144
}
114145

115-
// Task 7. Superposition of all basis vectors
146+
// Task 9. Superposition of all basis vectors
116147
// Input: N qubits in |0...0⟩ state.
117148
// Goal: create an equal superposition of all basis vectors from |0...0⟩ to |1...1⟩
118149
// (i.e. state (|0...0⟩ + ... + |1...1⟩) / sqrt(2^N) ).
@@ -124,7 +155,7 @@ namespace Quantum.Kata.Superposition
124155
}
125156
}
126157

127-
// Task 8. Superposition of |0...0⟩ and given bit string
158+
// Task 10. Superposition of |0...0⟩ and given bit string
128159
// Inputs:
129160
// 1) N qubits in |0...0⟩ state
130161
// 2) bit string represented as Bool[]
@@ -146,7 +177,7 @@ namespace Quantum.Kata.Superposition
146177
}
147178
}
148179

149-
// Task 9. Superposition of two bit strings
180+
// Task 11. Superposition of two bit strings
150181
// Inputs:
151182
// 1) N qubits in |0...0⟩ state
152183
// 2) two bit string represented as Bool[]s
@@ -164,7 +195,7 @@ namespace Quantum.Kata.Superposition
164195
}
165196
}
166197

167-
// Task 10**. W state on 2^k qubits
198+
// Task 12**. W state on 2^k qubits
168199
// Input: N = 2^k qubits in |0...0⟩ state.
169200
// Goal: create a W state (https://en.wikipedia.org/wiki/W_state) on these qubits.
170201
// W state is an equal superposition of all basis states on N qubits of Hamming weight 1.
@@ -179,7 +210,7 @@ namespace Quantum.Kata.Superposition
179210
}
180211
}
181212

182-
// Task 11**. W state on arbitrary number of qubits
213+
// Task 13**. W state on arbitrary number of qubits
183214
// Input: N qubits in |0...0⟩ state (N is not necessarily a power of 2).
184215
// Goal: create a W state (https://en.wikipedia.org/wiki/W_state) on these qubits.
185216
// W state is an equal superposition of all basis states on N qubits of Hamming weight 1.

Superposition/Tests.qs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,27 @@ namespace Quantum.Kata.Superposition
7272
}
7373

7474
// ------------------------------------------------------
75-
operation T04_BellState_Test () : ()
75+
operation T04_AllBasisVectors_TwoQubits_Test () : ()
76+
{
77+
body
78+
{
79+
// We only check for 2 qubits.
80+
AssertEqualOnZeroState(2, AllBasisVectors_TwoQubits, AllBasisVectors_TwoQubits_Reference);
81+
}
82+
}
83+
84+
// ------------------------------------------------------
85+
operation T05_AllBasisVectorsWithPhases_TwoQubits_Test () : ()
86+
{
87+
body
88+
{
89+
// We only check for 2 qubits.
90+
AssertEqualOnZeroState(2, AllBasisVectorsWithPhases_TwoQubits, AllBasisVectorsWithPhases_TwoQubits_Reference);
91+
}
92+
}
93+
94+
// ------------------------------------------------------
95+
operation T06_BellState_Test () : ()
7696
{
7797
body
7898
{
@@ -81,7 +101,7 @@ namespace Quantum.Kata.Superposition
81101
}
82102

83103
// ------------------------------------------------------
84-
operation T05_AllBellStates_Test () : ()
104+
operation T07_AllBellStates_Test () : ()
85105
{
86106
body
87107
{
@@ -92,7 +112,7 @@ namespace Quantum.Kata.Superposition
92112
}
93113

94114
// ------------------------------------------------------
95-
operation T06_GHZ_State_Test () : ()
115+
operation T08_GHZ_State_Test () : ()
96116
{
97117
body
98118
{
@@ -107,7 +127,7 @@ namespace Quantum.Kata.Superposition
107127
}
108128

109129
// ------------------------------------------------------
110-
operation T07_AllBasisVectorsSuperposition_Test () : ()
130+
operation T09_AllBasisVectorsSuperposition_Test () : ()
111131
{
112132
body
113133
{
@@ -121,7 +141,7 @@ namespace Quantum.Kata.Superposition
121141
}
122142

123143
// ------------------------------------------------------
124-
operation T08_ZeroAndBitstringSuperposition_Test () : ()
144+
operation T10_ZeroAndBitstringSuperposition_Test () : ()
125145
{
126146
body
127147
{
@@ -149,7 +169,7 @@ namespace Quantum.Kata.Superposition
149169
}
150170

151171
// ------------------------------------------------------
152-
operation T09_TwoBitstringSuperposition_Test () : ()
172+
operation T11_TwoBitstringSuperposition_Test () : ()
153173
{
154174
body
155175
{
@@ -194,7 +214,7 @@ namespace Quantum.Kata.Superposition
194214
}
195215

196216
// ------------------------------------------------------
197-
operation T10_WState_PowerOfTwo_Test () : ()
217+
operation T12_WState_PowerOfTwo_Test () : ()
198218
{
199219
body
200220
{
@@ -213,7 +233,7 @@ namespace Quantum.Kata.Superposition
213233
}
214234

215235
// ------------------------------------------------------
216-
operation T11_WState_Arbitrary_Test () : ()
236+
operation T13_WState_Arbitrary_Test () : ()
217237
{
218238
body
219239
{

0 commit comments

Comments
 (0)