-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate task 1.3 from Measurements to Distinguishing States kata (#1489)
Migrating task 1.3 from the Measurements workbook to Distinguishing States Kata. This resolves a part of the [Issue 1185](#1185). - Added folder plus_minus under distinguishing_states - Added index, placeholder, solution, and verification under plus_minus - Updated index file under distinguishing_states to contain the exercise plus_minus --------- Co-authored-by: Manvi-Agrawal <[email protected]> Co-authored-by: Mariia Mykhailova <[email protected]>
- Loading branch information
1 parent
8e6015e
commit 1621b26
Showing
7 changed files
with
82 additions
and
0 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
7 changes: 7 additions & 0 deletions
7
katas/content/distinguishing_states/plus_minus/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,7 @@ | ||
namespace Kata { | ||
operation IsQubitPlus(q : Qubit) : Bool { | ||
// Implement your solution here... | ||
|
||
return false; | ||
} | ||
} |
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 IsQubitPlus(q : Qubit) : Bool { | ||
H(q); | ||
return M(q) == Zero; | ||
} | ||
} |
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 IsQubitPlus(q : Qubit) : Bool { | ||
return Measure([PauliX], [q]) == Zero; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
katas/content/distinguishing_states/plus_minus/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,30 @@ | ||
namespace Kata.Verification { | ||
open Microsoft.Quantum.Katas; | ||
|
||
operation StatePrep_IsQubitPlus(q : Qubit, state : Int) : Unit is Adj { | ||
if state == 1 { | ||
// convert |0⟩ to |+⟩ | ||
H(q); | ||
} else { | ||
// convert |0⟩ to |-⟩ | ||
X(q); | ||
H(q); | ||
} | ||
} | ||
|
||
@EntryPoint() | ||
operation CheckSolution() : Bool { | ||
let isCorrect = DistinguishTwoStates_SingleQubit( | ||
StatePrep_IsQubitPlus, | ||
Kata.IsQubitPlus, | ||
["|-⟩", "|+⟩"], | ||
false | ||
); | ||
if isCorrect { | ||
Message("Correct!"); | ||
} else { | ||
Message("Incorrect."); | ||
} | ||
isCorrect | ||
} | ||
} |
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,3 @@ | ||
**Input:** A qubit which is guaranteed to be in either the $\ket{+}$ or the $\ket{-}$ state. | ||
|
||
**Output:** `true` if the qubit was in the $\ket{+}$ state, or `false` if it was in the $\ket{-}$ state. The state of the qubit at the end of the operation does not matter. |
23 changes: 23 additions & 0 deletions
23
katas/content/distinguishing_states/plus_minus/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,23 @@ | ||
Both input states are superposition states, with equal absolute values of amplitudes of both basis states. This means if the sate is measured in the Pauli $Z$ basis, there is a 50-50 chance of measuring `One` or `Zero`, which won't give us the necessary information. | ||
|
||
To determine in which state the input qubit is with certainty, we want to transform the qubit into a state where there is no superposition with respect to the basis in which we perform the measurement. | ||
|
||
Consider how we can prepare the input states, starting with basis states: $H\ket{0} = \ket{+}$ and $H\ket{1} = \ket{-}$. This transformation can also be undone by applying the $H$ gate again (remember that the $H$ gate is self-adjoint, i.e., it equals its own inverse): $H\ket{+} = \ket{0}$ and $H\ket{-} = \ket{1}$. | ||
|
||
Once we have the $\ket{0}$ or $\ket{1}$ state, we can use the same principle as in previous task $\ket{0}$ or $\ket{1}$ to measure the state and report the outcome. Note that in this task return value `true` corresponds to input state $\ket{+}$, so we compare the measurement result with `Zero`. | ||
|
||
@[solution]({ | ||
"id": "distinguishing_states__plus_minus_solution_a", | ||
"codePath": "SolutionA.qs" | ||
}) | ||
|
||
#### Alternative solution | ||
|
||
Another possible solution could be to measure in the Pauli $X$ basis ($\ket{+}, \ket{-}$ basis), this means a transformation with the $H$ gate before measurement is not needed. Again, measurement result `Zero` would correspond to state $\ket{+}$. | ||
|
||
In Q#, measuring in another Pauli basis can be done with the `Measure()` operation. | ||
|
||
@[solution]({ | ||
"id": "distinguishing_states__plus_minus_solution_b", | ||
"codePath": "SolutionB.qs" | ||
}) |