Skip to content

Commit

Permalink
Add task 2.1 to Superposition kata (#1395)
Browse files Browse the repository at this point in the history
Co-authored-by: César Zaragoza Cortés <[email protected]>
  • Loading branch information
tcNickolas and cesarzc authored Apr 17, 2024
1 parent e874bfb commit 2963b42
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
15 changes: 13 additions & 2 deletions katas/content/superposition/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,21 @@ This kata is designed to get you familiar with the concept of superposition and
})

@[section]({
"id": "superposition__uneven_superpositions",
"title": "Uneven superpositions"
"id": "superposition__arbitrary_rotations",
"title": "Arbitrary Rotations"
})

@[exercise]({
"id": "superposition__unequal_superposition",
"title": "Unequal Superposition",
"path": "./unequal_superposition/",
"qsDependencies": [
"../KatasLibrary.qs",
"./Common.qs"
]
})


@[section]({
"id": "superposition__conclusion",
"title": "Conclusion"
Expand Down
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 katas/content/superposition/unequal_superposition/Solution.qs
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 katas/content/superposition/unequal_superposition/Verification.qs
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 katas/content/superposition/unequal_superposition/index.md
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 katas/content/superposition/unequal_superposition/solution.md
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"
})

0 comments on commit 2963b42

Please sign in to comment.