diff --git a/katas/content/distinguishing_states/index.md b/katas/content/distinguishing_states/index.md index b37e78e256..e2e6918732 100644 --- a/katas/content/distinguishing_states/index.md +++ b/katas/content/distinguishing_states/index.md @@ -32,6 +32,14 @@ This kata is designed to get you familiar with the concept of measurements and u ] }) +@[exercise]({ + "id": "distinguishing_states__plus_minus", + "title": "|+〉 or |-〉?", + "path": "./plus_minus/", + "qsDependencies": [ + "../KatasLibrary.qs" + ] +}) @[section]({ "id": "distinguishing_states__nonorthogonal_states", diff --git a/katas/content/distinguishing_states/plus_minus/Placeholder.qs b/katas/content/distinguishing_states/plus_minus/Placeholder.qs new file mode 100644 index 0000000000..0cd4b4c21a --- /dev/null +++ b/katas/content/distinguishing_states/plus_minus/Placeholder.qs @@ -0,0 +1,7 @@ +namespace Kata { + operation IsQubitPlus(q : Qubit) : Bool { + // Implement your solution here... + + return false; + } +} diff --git a/katas/content/distinguishing_states/plus_minus/SolutionA.qs b/katas/content/distinguishing_states/plus_minus/SolutionA.qs new file mode 100644 index 0000000000..cdd0c38cf4 --- /dev/null +++ b/katas/content/distinguishing_states/plus_minus/SolutionA.qs @@ -0,0 +1,6 @@ +namespace Kata { + operation IsQubitPlus(q : Qubit) : Bool { + H(q); + return M(q) == Zero; + } +} diff --git a/katas/content/distinguishing_states/plus_minus/SolutionB.qs b/katas/content/distinguishing_states/plus_minus/SolutionB.qs new file mode 100644 index 0000000000..4cf9d3ab7c --- /dev/null +++ b/katas/content/distinguishing_states/plus_minus/SolutionB.qs @@ -0,0 +1,5 @@ +namespace Kata { + operation IsQubitPlus(q : Qubit) : Bool { + return Measure([PauliX], [q]) == Zero; + } +} diff --git a/katas/content/distinguishing_states/plus_minus/Verification.qs b/katas/content/distinguishing_states/plus_minus/Verification.qs new file mode 100644 index 0000000000..62a00bc7b3 --- /dev/null +++ b/katas/content/distinguishing_states/plus_minus/Verification.qs @@ -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 + } +} diff --git a/katas/content/distinguishing_states/plus_minus/index.md b/katas/content/distinguishing_states/plus_minus/index.md new file mode 100644 index 0000000000..d76f0ee406 --- /dev/null +++ b/katas/content/distinguishing_states/plus_minus/index.md @@ -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. diff --git a/katas/content/distinguishing_states/plus_minus/solution.md b/katas/content/distinguishing_states/plus_minus/solution.md new file mode 100644 index 0000000000..cf2bcd5e2a --- /dev/null +++ b/katas/content/distinguishing_states/plus_minus/solution.md @@ -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" +})