Skip to content

Commit

Permalink
Add Measurements task 2.1 to Distinguishing States Kata (#1518)
Browse files Browse the repository at this point in the history
Added Common.qs, media folder, zero-plus folder containing task and
updated index

Resolves part of #1185

---------

Co-authored-by: Mariia Mykhailova <[email protected]>
  • Loading branch information
JPark1023 and tcNickolas authored May 27, 2024
1 parent 6bed5a9 commit b77aa66
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 0 deletions.
40 changes: 40 additions & 0 deletions katas/content/distinguishing_states/Common.qs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
10 changes: 10 additions & 0 deletions katas/content/distinguishing_states/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions katas/content/distinguishing_states/zero_plus/Placeholder.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Kata {
operation IsQubitZeroOrPlus (q : Qubit) : Bool {
// Implement your solution here...
return true;
}
}
8 changes: 8 additions & 0 deletions katas/content/distinguishing_states/zero_plus/Solution.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Kata {
open Microsoft.Quantum.Math;

operation IsQubitZeroOrPlus (q : Qubit) : Bool {
Ry(0.25 * PI(), q);
return M(q) == Zero;
}
}
14 changes: 14 additions & 0 deletions katas/content/distinguishing_states/zero_plus/Verification.qs
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 7 additions & 0 deletions katas/content/distinguishing_states/zero_plus/index.md
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 33 additions & 0 deletions katas/content/distinguishing_states/zero_plus/solution.md
Original file line number Diff line number Diff line change
@@ -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" })

0 comments on commit b77aa66

Please sign in to comment.