From 7e28994eeb19713dd6ff9b1e0a7c0d2c7473e78a Mon Sep 17 00:00:00 2001 From: Mariia Mykhailova Date: Tue, 16 Apr 2024 16:18:22 -0700 Subject: [PATCH 1/2] Add task 2.1 to Superposition kata --- katas/content/superposition/index.md | 15 +++++++++-- .../unequal_superposition/Placeholder.qs | 6 +++++ .../unequal_superposition/Solution.qs | 5 ++++ .../unequal_superposition/Verification.qs | 27 +++++++++++++++++++ .../unequal_superposition/index.md | 12 +++++++++ .../unequal_superposition/solution.md | 20 ++++++++++++++ 6 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 katas/content/superposition/unequal_superposition/Placeholder.qs create mode 100644 katas/content/superposition/unequal_superposition/Solution.qs create mode 100644 katas/content/superposition/unequal_superposition/Verification.qs create mode 100644 katas/content/superposition/unequal_superposition/index.md create mode 100644 katas/content/superposition/unequal_superposition/solution.md diff --git a/katas/content/superposition/index.md b/katas/content/superposition/index.md index 3f0ce10b63..f53780721e 100644 --- a/katas/content/superposition/index.md +++ b/katas/content/superposition/index.md @@ -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" diff --git a/katas/content/superposition/unequal_superposition/Placeholder.qs b/katas/content/superposition/unequal_superposition/Placeholder.qs new file mode 100644 index 0000000000..5803f99dd6 --- /dev/null +++ b/katas/content/superposition/unequal_superposition/Placeholder.qs @@ -0,0 +1,6 @@ +namespace Kata { + operation UnequalSuperposition(q : Qubit, alpha : Double) : Unit { + // Implement your solution here... + + } +} diff --git a/katas/content/superposition/unequal_superposition/Solution.qs b/katas/content/superposition/unequal_superposition/Solution.qs new file mode 100644 index 0000000000..225e0ffecb --- /dev/null +++ b/katas/content/superposition/unequal_superposition/Solution.qs @@ -0,0 +1,5 @@ +namespace Kata { + operation UnequalSuperposition(q : Qubit, alpha : Double) : Unit { + Ry(2.0 * alpha, q); + } +} diff --git a/katas/content/superposition/unequal_superposition/Verification.qs b/katas/content/superposition/unequal_superposition/Verification.qs new file mode 100644 index 0000000000..82cb7bc012 --- /dev/null +++ b/katas/content/superposition/unequal_superposition/Verification.qs @@ -0,0 +1,27 @@ +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 { + for i in 0 .. 36 { + let alpha = 2.0 * PI() * IntAsDouble(i) / 36.0; + 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 + } +} diff --git a/katas/content/superposition/unequal_superposition/index.md b/katas/content/superposition/unequal_superposition/index.md new file mode 100644 index 0000000000..decae4db3f --- /dev/null +++ b/katas/content/superposition/unequal_superposition/index.md @@ -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$. + +
+ Need a hint? + Experiment with rotation gates you learned about in the Single-Qubit Gates kata. + Note that all rotation operators rotate the state by half of its angle argument. +
diff --git a/katas/content/superposition/unequal_superposition/solution.md b/katas/content/superposition/unequal_superposition/solution.md new file mode 100644 index 0000000000..61604fc99f --- /dev/null +++ b/katas/content/superposition/unequal_superposition/solution.md @@ -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" +}) From d486594ff5cf75f1d45198a2e58bd8ae74851251 Mon Sep 17 00:00:00 2001 From: Mariia Mykhailova Date: Tue, 16 Apr 2024 17:34:12 -0700 Subject: [PATCH 2/2] Update katas/content/superposition/unequal_superposition/Verification.qs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: César Zaragoza Cortés --- .../superposition/unequal_superposition/Verification.qs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/katas/content/superposition/unequal_superposition/Verification.qs b/katas/content/superposition/unequal_superposition/Verification.qs index 82cb7bc012..9ebc411a98 100644 --- a/katas/content/superposition/unequal_superposition/Verification.qs +++ b/katas/content/superposition/unequal_superposition/Verification.qs @@ -9,8 +9,9 @@ namespace Kata.Verification { @EntryPoint() operation CheckSolution() : Bool { - for i in 0 .. 36 { - let alpha = 2.0 * PI() * IntAsDouble(i) / 36.0; + 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}...");