Skip to content

Commit

Permalink
Migrate task 1.3 from Measurements to Distinguishing States kata (#1489)
Browse files Browse the repository at this point in the history
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
3 people authored May 9, 2024
1 parent 8e6015e commit 1621b26
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 0 deletions.
8 changes: 8 additions & 0 deletions katas/content/distinguishing_states/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions katas/content/distinguishing_states/plus_minus/Placeholder.qs
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;
}
}
6 changes: 6 additions & 0 deletions katas/content/distinguishing_states/plus_minus/SolutionA.qs
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;
}
}
5 changes: 5 additions & 0 deletions katas/content/distinguishing_states/plus_minus/SolutionB.qs
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 katas/content/distinguishing_states/plus_minus/Verification.qs
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
}
}
3 changes: 3 additions & 0 deletions katas/content/distinguishing_states/plus_minus/index.md
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 katas/content/distinguishing_states/plus_minus/solution.md
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"
})

0 comments on commit 1621b26

Please sign in to comment.