-
Notifications
You must be signed in to change notification settings - Fork 171
Add sign flip, basis change, amplitude change tasks to Single-Qubit Gates kata #1352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6d4dc9e
Add task1.2to1.4 to Single-Qubit Gates kata
WWhitedogi cfd0da2
Update katas/content/single_qubit_gates/sign_flip/Placeholder.qs
WWhitedogi 8251050
Update katas/content/single_qubit_gates/sign_flip/index.md
WWhitedogi 46e71b9
Update katas/content/single_qubit_gates/sign_flip/solution.md
WWhitedogi ed3b89e
Update katas/content/single_qubit_gates/sign_flip/solution.md
WWhitedogi db3eaeb
Update katas/content/single_qubit_gates/sign_flip/solution.md
WWhitedogi a5c2682
Update katas/content/single_qubit_gates/amplitude_change/index.md
WWhitedogi fb4bb67
Update katas/content/single_qubit_gates/amplitude_change/solution.md
WWhitedogi 024e120
Update katas/content/single_qubit_gates/amplitude_change/solution.md
WWhitedogi 3bb096a
Update katas/content/single_qubit_gates/amplitude_change/Verification.qs
WWhitedogi e251061
Update katas/content/single_qubit_gates/amplitude_change/solution.md
WWhitedogi 6fb7605
Update katas/content/single_qubit_gates/amplitude_change/index.md
WWhitedogi c55032f
Update katas/content/single_qubit_gates/amplitude_change/index.md
WWhitedogi 6041b19
Update Verification.qs of basis_change task
WWhitedogi 19d5e73
Update Verification.qs of amplitude_change
WWhitedogi 5551ff4
Update amplitude_change task's order in index.md
WWhitedogi 69f72be
Update index.md
WWhitedogi 4f890b9
Delete katas/content/single_qubit_gates/Common.qs
WWhitedogi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
katas/content/single_qubit_gates/amplitude_change/Placeholder.qs
This file contains hidden or 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,7 @@ | ||
| namespace Kata { | ||
| operation AmplitudeChange (alpha : Double, q : Qubit) : Unit is Adj + Ctl { | ||
| // Implement your solution here... | ||
|
|
||
| } | ||
| } | ||
|
|
5 changes: 5 additions & 0 deletions
5
katas/content/single_qubit_gates/amplitude_change/Solution.qs
This file contains hidden or 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 AmplitudeChange (alpha : Double, q : Qubit) : Unit is Adj + Ctl { | ||
| Ry(2.0 * alpha, q); | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
katas/content/single_qubit_gates/amplitude_change/Verification.qs
This file contains hidden or 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,32 @@ | ||
| namespace Kata.Verification { | ||
| open Microsoft.Quantum.Katas; | ||
| open Microsoft.Quantum.Math; | ||
| open Microsoft.Quantum.Convert; | ||
|
|
||
| operation AmplitudeChange (alpha : Double, q : Qubit) : Unit is Adj+Ctl { | ||
| Ry(2.0 * alpha, q); | ||
| } | ||
|
|
||
| operation CheckSolution() : Bool { | ||
| for i in 0 .. 36 { | ||
| let alpha = ((2.0 * PI()) * IntAsDouble(i)) / 36.0; | ||
| let solution = register => Kata.AmplitudeChange(alpha, register[0]); | ||
| let reference = register => AmplitudeChange(alpha, register[0]); | ||
| let isCorrect = CheckOperationsEquivalenceStrict(solution, reference, 1); | ||
| if not isCorrect { | ||
| Message("Incorrect."); | ||
| Message($"The solution was incorrect for the test case alpha = {alpha}."); | ||
| Message("Hint: examine the effect your solution has on the state 0.6|0〉 + 0.8|1〉 and compare it with the effect it " + | ||
| "is expected to have."); | ||
| use initial = Qubit(); // |0〉 | ||
| Ry(ArcTan2(0.8, 0.6) * 2.0, initial); // 0.6|0〉 + 0.8|1〉 | ||
| ShowQuantumStateComparison([initial], solution, reference); | ||
| Reset(initial); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| Message("Correct!"); | ||
| true | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
katas/content/single_qubit_gates/amplitude_change/index.md
This file contains hidden or 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,10 @@ | ||
| **Inputs:** | ||
|
|
||
| 1. Angle $\alpha$, in radians, represented as Double. | ||
| 2. A qubit in state $|\psi\rangle = \beta |0\rangle + \gamma |1\rangle$. | ||
|
|
||
| **Goal:** Change the state of the qubit as follows: | ||
|
|
||
| - If the qubit is in state $|0\rangle$, change its state to $\cos \alpha |0\rangle + \sin \alpha |1\rangle$. | ||
| - If the qubit is in state $|1\rangle$, change its state to $-\sin \alpha |0\rangle + \cos \alpha |1\rangle$. | ||
| - If the qubit is in superposition, change its state according to the effect on basis vectors. |
41 changes: 41 additions & 0 deletions
41
katas/content/single_qubit_gates/amplitude_change/solution.md
This file contains hidden or 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,41 @@ | ||
| We can recognize that we need to use one of the rotation gates Rx, Ry, and Rz (named because they "rotate" the qubit state in the three dimensional space visualized as the Bloch sphere about the x, y, and z axes, respectively), since they involve angle parameters. Of these three gates, only Ry rotates the basis states $|0\rangle$ and $|1\rangle$ to have real amplitudes (the other two gates introduce complex coefficients). | ||
|
|
||
| As a reminder, | ||
|
|
||
| $$ | ||
| R_{y}(\theta) = | ||
| \begin{bmatrix}\cos \frac{\theta}{2} & -\sin \frac{\theta}{2}\\\ \sin \frac{\theta}{2} & \cos \frac{\theta}{2}\end{bmatrix} | ||
| $$ | ||
|
|
||
| Let's see its effect on the $|0\rangle$ state: | ||
|
|
||
| $$ | ||
| 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}\cdot1 - \sin \frac{\theta}{2}\cdot0\\\ \sin \frac{\theta}{2}\cdot1 + \cos \frac{\theta}{2}\cdot0 | ||
| \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 | ||
| $$ | ||
|
|
||
| Recall that when applying a gate, you can tell what its matrix does to the basis states by looking at its columns: the first column of the matrix is the state into which it will transform the $|0\rangle$ state, and the second column is the state into which it will transform the $|1\rangle$ state. | ||
| One of the examples used by the testing harness has $\beta = 0.6, \gamma = 0.8$ and $\alpha = 1.0471975511965976 = \frac{\pi}{3}$. | ||
| Since $\cos \frac{\pi}{3} = 0.5$ and $\sin \frac{\pi}{3} = 0.8660$, working to 4 decimal places, we can use $\frac{\theta}{2} = \alpha$ to compute: | ||
|
|
||
| $$ | ||
| R_{y}(\theta) |\psi\rangle= | ||
| \begin{bmatrix}\cos \frac{\theta}{2} & -\sin \frac{\theta}{2}\\\ \sin \frac{\theta}{2} & \cos \frac{\theta}{2} \end{bmatrix} | ||
| \begin{bmatrix}\beta\\\ \gamma\\\ \end{bmatrix}= | ||
| \begin{bmatrix}\cos \frac{\theta}{2}\cdot\beta - \sin \frac{\theta}{2}\cdot\gamma\\\ \sin \frac{\theta}{2}\cdot\beta +\cos \frac{\theta}{2}\cdot\gamma \end{bmatrix}= | ||
| \begin{bmatrix} 0.6\cdot\cos \frac{\pi}{3} -0.8\cdot\sin \frac{\pi}{3}\\\0.6\cdot\sin \frac{\pi}{3} +0.8\cdot\cos \frac{\pi}{3}\end{bmatrix}= | ||
| \begin{bmatrix}0.3 - 0.6928\\\ 0.5196 + 0.4\end{bmatrix}= | ||
| \begin{bmatrix}-0.3928\\\ 0.9196\\\ \end{bmatrix} | ||
| $$ | ||
|
|
||
| Notice that we used $\frac{\theta}{2} = \alpha$; this means that in the Q# code we need to pass the angle $\theta = 2\alpha$. | ||
|
|
||
| @[solution]({ | ||
| "id": "single_qubit_gates__amplitude_change_solution", | ||
| "codePath": "./Solution.qs" | ||
| }) |
This file contains hidden or 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,7 @@ | ||
| namespace Kata { | ||
| operation BasisChange (q : Qubit) : Unit is Adj + Ctl { | ||
| // Implement your solution here... | ||
|
|
||
| } | ||
| } | ||
|
|
This file contains hidden or 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 BasisChange (q : Qubit) : Unit is Adj + Ctl { | ||
| H(q); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
katas/content/single_qubit_gates/basis_change/Verification.qs
This file contains hidden or 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.Katas; | ||
| open Microsoft.Quantum.Math; | ||
|
|
||
| operation BasisChange(q : Qubit) : Unit is Adj + Ctl { | ||
| H(q); | ||
| } | ||
|
|
||
| operation CheckSolution() : Bool { | ||
| let solution = register => Kata.BasisChange(register[0]); | ||
| let reference = register => BasisChange(register[0]); | ||
| let isCorrect = CheckOperationsEquivalenceStrict(solution, reference, 1); | ||
|
|
||
| // Output different feedback to the user depending on whether the solution was correct. | ||
| if isCorrect { | ||
| Message("Correct!"); | ||
| } else { | ||
| Message("Incorrect."); | ||
| Message("Hint: examine the effect your solution has on the state 0.6|0〉 + 0.8|1〉 and compare it with the effect it " + | ||
| "is expected to have."); | ||
| use initial = Qubit(); // |0〉 | ||
| Ry(ArcTan2(0.8, 0.6) * 2.0, initial); // 0.6|0〉 + 0.8|1〉 | ||
| ShowQuantumStateComparison([initial], solution, reference); | ||
| Reset(initial); | ||
| } | ||
| isCorrect | ||
| } | ||
| } |
This file contains hidden or 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,7 @@ | ||
| **Input**: A qubit in state $|\psi\rangle = \alpha |0\rangle + \beta |1\rangle$. | ||
|
|
||
| **Goal**: Change the state of the qubit as follows: | ||
|
|
||
| - If the qubit is in state $|0\rangle$, change its state to $|+\rangle = \frac{1}{\sqrt{2}} \big(|0\rangle + |1\rangle\big)$. | ||
| - If the qubit is in state $|1\rangle$, change its state to $|-\rangle = \frac{1}{\sqrt{2}} \big(|0\rangle - |1\rangle\big)$. | ||
| - If the qubit is in superposition, change its state according to the effect on basis vectors. |
This file contains hidden or 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,36 @@ | ||
| We can recognize that the Hadamard gate changes states $|0\rangle$ and $|1\rangle$ to $|+\rangle$ and $|-\rangle$, respectively, and vice versa. | ||
|
|
||
| As a reminder, the Hadamard gate is defined by the following matrix: | ||
|
|
||
| $$ | ||
| \frac{1}{\sqrt{2}}\begin{bmatrix}1 & 1 \\\1 & -1\end{bmatrix} | ||
| $$ | ||
|
|
||
| For example, we can work out $H|1\rangle$ as follows: | ||
|
|
||
| $$ | ||
| H|1\rangle= | ||
| \frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\\1 & -1\end{bmatrix} | ||
| \begin{bmatrix} 0\\\ 1\end{bmatrix}= | ||
| \frac{1}{\sqrt{2}}\begin{bmatrix}1 \cdot 0 + 1 \cdot 1 \\\1 \cdot 0 + (-1) \cdot 1\end{bmatrix}= | ||
| \frac{1}{\sqrt{2}}\begin{bmatrix}1\\\ -1\\\ \end{bmatrix}= | ||
| \frac{1}{\sqrt{2}} \big(|0\rangle - |1\rangle\big) = |-\rangle | ||
| $$ | ||
|
|
||
| Similarly, we can consider the effect of the Hadamard gate on the superposition state $|\psi\rangle = 0.6|0\rangle + 0.8|1\rangle$ (rounding the numbers to 4 decimal places): | ||
|
|
||
| $$ | ||
| H|\psi⟩ = | ||
| \frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\\ 1 & -1 \end{bmatrix} | ||
| \begin{bmatrix} \alpha\\\ \beta\\\ \end{bmatrix} = | ||
| \frac{1}{\sqrt{2}}\begin{bmatrix} \alpha + \beta\\\ \alpha - \beta\\\ \end{bmatrix}= | ||
| 0.7071\begin{bmatrix} 1.4\\\ -0.2\\\ \end{bmatrix} = | ||
| \begin{bmatrix} | ||
| 0.98994\\\ -0.14142\\\ \end{bmatrix} = | ||
| 0.9899|0\rangle - 0.1414|1\rangle | ||
| $$ | ||
|
|
||
| @[solution]({ | ||
| "id": "single_qubit_gates__basis_change_solution", | ||
| "codePath": "./Solution.qs" | ||
| }) |
This file contains hidden or 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
This file contains hidden or 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,7 @@ | ||
| namespace Kata { | ||
| operation SignFlip (q : Qubit) : Unit is Adj + Ctl { | ||
| // Implement your solution here... | ||
|
|
||
| } | ||
| } | ||
|
|
This file contains hidden or 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 SignFlip (q : Qubit) : Unit is Adj + Ctl { | ||
| Z(q); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
katas/content/single_qubit_gates/sign_flip/Verification.qs
This file contains hidden or 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.Katas; | ||
| open Microsoft.Quantum.Math; | ||
|
|
||
| operation SignFlip(q : Qubit) : Unit is Adj + Ctl { | ||
| Z(q); | ||
| } | ||
|
|
||
| operation CheckSolution() : Bool { | ||
| let solution = register => Kata.SignFlip(register[0]); | ||
| let reference = register => SignFlip(register[0]); | ||
| let isCorrect = CheckOperationsEquivalenceStrict(solution, reference, 1); | ||
|
|
||
| // Output different feedback to the user depending on whether the solution was correct. | ||
| if isCorrect { | ||
| Message("Correct!"); | ||
| } else { | ||
| Message("Incorrect."); | ||
| Message("Hint: examine the effect your solution has on the state 0.6|0〉 + 0.8|1〉 and compare it with the effect it " + | ||
| "is expected to have."); | ||
| use initial = Qubit(); // |0〉 | ||
| Ry(ArcTan2(0.8, 0.6) * 2.0, initial); // 0.6|0〉 + 0.8|1〉 | ||
| ShowQuantumStateComparison([initial], solution, reference); | ||
| Reset(initial); | ||
| } | ||
| isCorrect | ||
| } | ||
| } |
This file contains hidden or 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,3 @@ | ||
| **Input**: A qubit in state $|\psi\rangle = \alpha |0\rangle + \beta |1\rangle$. | ||
|
|
||
| **Goal** : Change the qubit state to $\alpha |0\rangle - \beta |1\rangle$ (i.e., flip the sign of the $|1\rangle$ component of the superposition). |
This file contains hidden or 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,37 @@ | ||
| The action of the Pauli Z gate is exactly what is required by this question. | ||
| This gate leaves the sign of the $|0\rangle$ component of the superposition unchanged but flips the sign of the $|1\rangle$ component of the superposition. | ||
|
|
||
| As a reminder, the Pauli Z gate is defined by the following matrix: | ||
|
|
||
| $$ | ||
| Z = | ||
| \begin{bmatrix}1 & 0\\\0 & -1 \end{bmatrix} | ||
| $$ | ||
|
|
||
| Let's see its effect on the only computational basis state that it changes, $|1\rangle$: | ||
|
|
||
| $$ | ||
| Z|1\rangle = | ||
| \begin{bmatrix} 1 & 0\\\0 & -1 \end{bmatrix} | ||
| \begin{bmatrix}0\\\ 1\\\ \end{bmatrix}= | ||
| \begin{bmatrix}1 \cdot 0 + 0 \cdot1\\\0 \cdot 1 + (-1) \cdot 1\\\ \end{bmatrix}= | ||
| \begin{bmatrix}0\\\ -1\\\ \end{bmatrix}= | ||
| -\begin{bmatrix}0\\\ 1\\\ \end{bmatrix}= | ||
| -|1\rangle | ||
| $$ | ||
|
|
||
| In general applying the Z gate to a single qubit superposition state $|\psi\rangle = \alpha |0\rangle + \beta |1\rangle$ gives | ||
|
|
||
| $$ | ||
| Z|\psi\rangle = | ||
| \begin{bmatrix}1 & 0 \\\0 & -1\end{bmatrix} | ||
| \begin{bmatrix}\alpha\\\ \beta\\\ \end{bmatrix}= | ||
| \begin{bmatrix}1\cdot\alpha + 0\cdot\beta\\\0\cdot\alpha + (-1)\cdot\beta\\\ \end{bmatrix}= | ||
| \begin{bmatrix}\alpha\\\ -\beta\\\ \end{bmatrix}= | ||
| \alpha |0\rangle -\beta |1\rangle | ||
| $$ | ||
|
|
||
| @[solution]({ | ||
| "id": "single_qubit_gates__sign_flip_solution", | ||
| "codePath": "./Solution.qs" | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.