-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add task 2.1 to Superposition kata (#1395)
Co-authored-by: César Zaragoza Cortés <[email protected]>
- Loading branch information
1 parent
e874bfb
commit 2963b42
Showing
6 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
katas/content/superposition/unequal_superposition/Placeholder.qs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Kata { | ||
operation UnequalSuperposition(q : Qubit, alpha : Double) : Unit { | ||
// Implement your solution here... | ||
|
||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
katas/content/superposition/unequal_superposition/Solution.qs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace Kata { | ||
operation UnequalSuperposition(q : Qubit, alpha : Double) : Unit { | ||
Ry(2.0 * alpha, q); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
katas/content/superposition/unequal_superposition/Verification.qs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace Kata.Verification { | ||
open Microsoft.Quantum.Convert; | ||
open Microsoft.Quantum.Katas; | ||
open Microsoft.Quantum.Math; | ||
|
||
operation UnequalSuperposition_Reference(q : Qubit, alpha : Double) : Unit is Adj + Ctl { | ||
Ry(2.0 * alpha, q); | ||
} | ||
|
||
@EntryPoint() | ||
operation CheckSolution() : Bool { | ||
let limit = 36; | ||
for i in 0 .. limit { | ||
let alpha = 2.0 * PI() * IntAsDouble(i) / IntAsDouble(limit); | ||
let solution = Kata.UnequalSuperposition(_, alpha); | ||
let reference = UnequalSuperposition_Reference(_, alpha); | ||
Message($"Testing for alpha = {alpha}..."); | ||
if not CheckOperationsEquivalenceOnZeroStateWithFeedback( | ||
qs => solution(qs[0]), | ||
qs => reference(qs[0]), | ||
1) { | ||
return false; | ||
} | ||
} | ||
|
||
true | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
katas/content/superposition/unequal_superposition/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
**Inputs:** | ||
|
||
1. A qubit in the $|0\rangle$ state. | ||
2. Angle $\alpha$, in radians, represented as `Double`. | ||
|
||
**Goal**: Change the state of the qubit to $\cos{α} |0\rangle + \sin{α} |1\rangle$. | ||
|
||
<details> | ||
<summary><b>Need a hint?</b></summary> | ||
Experiment with rotation gates you learned about in the Single-Qubit Gates kata. | ||
Note that all rotation operators rotate the state by <i>half</i> of its angle argument. | ||
</details> |
20 changes: 20 additions & 0 deletions
20
katas/content/superposition/unequal_superposition/solution.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
We want to convert the $\ket{0}$ state to a parameterized superposition of $\ket{0}$ and $\ket{1}$, which suggests | ||
that we are looking for some kind of a rotation operation. There are three main gates that implement rotations around various axes of the Bloch Sphere: | ||
|
||
- $R_x(\theta) = \begin{bmatrix} \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\\ -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}$ | ||
- $R_y(\theta) = \begin{bmatrix} \cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}$ | ||
- $R_z(\theta) = \begin{bmatrix} e^{-i\theta/2} & 0 \\\ 0 & e^{i\theta/2} \end{bmatrix}$ | ||
|
||
If we were to apply the $R_x$ gate to a qubit in the $|0\rangle$ state, we would introduce complex coefficients to the amplitudes, which is clearly not what we're looking for. Similarly, the $R_z$ gate introduces only a global phase when applied to $|0\rangle$ state, so we can rule it out as well. This leaves only the $R_y$ as a starting point for the solution. | ||
|
||
Applying the $R_y$ gate to the $|0\rangle$ state, we get: | ||
$$R_y(\theta) |0\rangle = | ||
\begin{bmatrix} \cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix} \begin{bmatrix} 1 \\\ 0 \end{bmatrix} = | ||
\begin{bmatrix} \cos\frac{\theta}{2} \\\ \sin\frac{\theta}{2} \end{bmatrix} = \cos\frac{\theta}{2}|0\rangle + \sin\frac{\theta}{2}|1\rangle$$ | ||
|
||
Therefore, applying the $R_y(2\alpha)$ gate to $|0\rangle$ is the solution to our problem. | ||
|
||
@[solution]({ | ||
"id": "superposition__unequal_superposition_solution", | ||
"codePath": "./Solution.qs" | ||
}) |