From b77aa66f861a68b9e7c5cf5fa5a68241b4d71f9f Mon Sep 17 00:00:00 2001 From: JPark1023 <98407462+JPark1023@users.noreply.github.com> Date: Mon, 27 May 2024 10:10:55 -0500 Subject: [PATCH] Add Measurements task 2.1 to Distinguishing States Kata (#1518) Added Common.qs, media folder, zero-plus folder containing task and updated index Resolves part of #1185 --------- Co-authored-by: Mariia Mykhailova --- katas/content/distinguishing_states/Common.qs | 40 +++++++++++++++++++ katas/content/distinguishing_states/index.md | 10 +++++ .../zero_plus/Placeholder.qs | 6 +++ .../zero_plus/Solution.qs | 8 ++++ .../zero_plus/Verification.qs | 14 +++++++ .../distinguishing_states/zero_plus/index.md | 7 ++++ .../zero_plus/solution.md | 33 +++++++++++++++ 7 files changed, 118 insertions(+) create mode 100644 katas/content/distinguishing_states/Common.qs create mode 100644 katas/content/distinguishing_states/zero_plus/Placeholder.qs create mode 100644 katas/content/distinguishing_states/zero_plus/Solution.qs create mode 100644 katas/content/distinguishing_states/zero_plus/Verification.qs create mode 100644 katas/content/distinguishing_states/zero_plus/index.md create mode 100644 katas/content/distinguishing_states/zero_plus/solution.md diff --git a/katas/content/distinguishing_states/Common.qs b/katas/content/distinguishing_states/Common.qs new file mode 100644 index 0000000000..151919f5a9 --- /dev/null +++ b/katas/content/distinguishing_states/Common.qs @@ -0,0 +1,40 @@ +namespace Kata.Verification { + open Microsoft.Quantum.Convert; + open Microsoft.Quantum.Random; + + operation DistinguishStates_MultiQubit_Threshold (nQubit : Int, + nState : Int, + threshold : Double, + statePrep : ((Qubit, Int) => Unit), + testImpl : (Qubit => Bool)) : Bool { + let nTotal = 1000; + mutable nOk = 0; + + use qs = Qubit[nQubit]; + for i in 1 .. nTotal { + // get a random integer to define the state of the qubits + let state = DrawRandomInt(0, nState - 1); + + // do state prep: convert |0⟩ to outcome with return equal to state + statePrep(qs[0], state); + + // get the solution's answer and verify that it's a match + let ans = testImpl(qs[0]); + if ans == (state == 0) { + set nOk += 1; + } + + // we're not checking the state of the qubit after the operation + ResetAll(qs); + } + + if IntAsDouble(nOk) < threshold * IntAsDouble(nTotal) { + Message($"{nTotal - nOk} test runs out of {nTotal} returned incorrect state, which does not meet the required threshold of at least {threshold * 100.0}%."); + Message("Incorrect."); + return false; + } else { + Message("Correct!"); + return true; + } + } +} diff --git a/katas/content/distinguishing_states/index.md b/katas/content/distinguishing_states/index.md index e2e6918732..d7dcabc1f3 100644 --- a/katas/content/distinguishing_states/index.md +++ b/katas/content/distinguishing_states/index.md @@ -46,6 +46,16 @@ This kata is designed to get you familiar with the concept of measurements and u "title": "Distinguishing Non-orthogonal States" }) +@[exercise]({ + "id": "distinguishing_states__zero_plus", + "title": "|0〉 or |+〉?", + "path": "./zero_plus/", + "qsDependencies": [ + "../KatasLibrary.qs", + "./Common.qs" + ] +}) + @[section]({ "id": "distinguishing_states__conclusion", "title": "Conclusion" diff --git a/katas/content/distinguishing_states/zero_plus/Placeholder.qs b/katas/content/distinguishing_states/zero_plus/Placeholder.qs new file mode 100644 index 0000000000..12b239cd41 --- /dev/null +++ b/katas/content/distinguishing_states/zero_plus/Placeholder.qs @@ -0,0 +1,6 @@ +namespace Kata { + operation IsQubitZeroOrPlus (q : Qubit) : Bool { + // Implement your solution here... + return true; + } +} diff --git a/katas/content/distinguishing_states/zero_plus/Solution.qs b/katas/content/distinguishing_states/zero_plus/Solution.qs new file mode 100644 index 0000000000..2c15fd6cb3 --- /dev/null +++ b/katas/content/distinguishing_states/zero_plus/Solution.qs @@ -0,0 +1,8 @@ +namespace Kata { + open Microsoft.Quantum.Math; + + operation IsQubitZeroOrPlus (q : Qubit) : Bool { + Ry(0.25 * PI(), q); + return M(q) == Zero; + } +} diff --git a/katas/content/distinguishing_states/zero_plus/Verification.qs b/katas/content/distinguishing_states/zero_plus/Verification.qs new file mode 100644 index 0000000000..a2c68bc2e6 --- /dev/null +++ b/katas/content/distinguishing_states/zero_plus/Verification.qs @@ -0,0 +1,14 @@ +namespace Kata.Verification { + open Microsoft.Quantum.Katas; + + operation SetQubitZeroOrPlus (q : Qubit, state : Int) : Unit { + if state != 0 { + H(q); + } + } + + @EntryPoint() + operation CheckSolution () : Bool { + return DistinguishStates_MultiQubit_Threshold(1, 2, 0.8, SetQubitZeroOrPlus, Kata.IsQubitZeroOrPlus); + } +} diff --git a/katas/content/distinguishing_states/zero_plus/index.md b/katas/content/distinguishing_states/zero_plus/index.md new file mode 100644 index 0000000000..b1e86d3db1 --- /dev/null +++ b/katas/content/distinguishing_states/zero_plus/index.md @@ -0,0 +1,7 @@ +**Input:** A qubit which is guaranteed to be in either the $\ket{0}$ or the $\ket{+}$ state. + +**Output:** `true` if the qubit was in the $\ket{0}$ state, or `false` if it was in the $\ket{+}$ state. The state of the qubit at the end of the operation does not matter. + +In this task your solution will be called multiple times, with one of the states picked with equal probability every time. You have to get overall accuracy of at least 80%. + +> This task is an example of quantum hypothesis testing, or state discrimination with minimum error. diff --git a/katas/content/distinguishing_states/zero_plus/solution.md b/katas/content/distinguishing_states/zero_plus/solution.md new file mode 100644 index 0000000000..e7f2f36949 --- /dev/null +++ b/katas/content/distinguishing_states/zero_plus/solution.md @@ -0,0 +1,33 @@ +Let ${\ket{E_a}, \ket{E_b}}$ be a measurement with two outcomes $a$ and $b$, which we identify with the answers, i.e., outcome "a" means we answer "state was $\ket{0}$" and outcome "b" means we answer "state was $\ket{+}$". Then we define + +* $P(a|0)$ = probability to observe first outcome given that the state was $\ket{0}$ +* $P(b|0)$ = probability to observe second outcome given that the state was $\ket{0}$ +* $P(a|+)$ = probability to observe first outcome given that the state was $\ket{+}$ +* $P(b|+)$ = probability to observe second outcome given that the state was $\ket{+}$ + +The task is to maximize the probability to be correct on a single shot experiment, which is the same as to minimize the probability to be wrong on a single shot. + +Since the task promises uniform prior distribution of the inputs $\ket{0}$ and $\ket{+}$, i.e., $P(+) = P(0) = \frac{1}{2}$, we get the following expression for the probability of giving a correct answer: + +$$P_{correct} = P(0) P(a|0) + P(+) P(b|+) = \frac{1}{2} (P(a|0) + P(b|+))$$ + +We can represent our measurement as a von Neumann measurement of the following form: + +$$\ket{E_a} = R_y(2\alpha) \begin{bmatrix} 1 \\ 0 \end{bmatrix} = \begin{bmatrix} \cos \alpha \\ \sin \alpha \end{bmatrix}$$ +$$\ket{E_b} = R_y(2\alpha) \begin{bmatrix} 0 \\ 1 \end{bmatrix} = \begin{bmatrix} - \sin \alpha \\ \cos \alpha \end{bmatrix}$$ + +Using this representation, we can express our probabilities as follows: + +$$P(a|0) = |\braket{E_a|0}|^2 = \cos^2 \alpha$$ + +$$P(b|+) = |\braket{E_b|+}|^2 = \frac{1}{2} - \cos \alpha \sin \alpha$$ + +$$P_{correct} = \frac{1}{2} (\cos^2 \alpha + \frac{1}{2} - \cos \alpha \sin \alpha)$$ + +Maximizing this for $\alpha$, we get max $P_{success} = \frac{1}{2} (1 + \frac{1}{\sqrt{2}}) = 0.8535...$, which is attained for $\alpha = -\pi/8$. + +This means that $\ket{E_a}$ and $\ket{E_b}$ are the result of rotating $\ket{0}$ and $\ket{1}$, respectively, by $-\pi/8$. If we rotate the whole system by $-\alpha = \pi/8$, we will get $\ket{E_a}=\ket{0}$ and $\ket{E_b}=\ket{1}$, and a measurement in the computational basis will give us the correct result with a probability of 85%. + +> In Q#, rotating the input state by some angle $\theta$ can be done by applying $Ry$ gate with angle parameter $2\theta$. + +@[solution]({ "id": "measurements_nonorthogonal_states_solution", "codePath": "./Solution.qs" })