What is this workbook? A workbook is a collection of problems, accompanied by solutions to them. The explanations focus on the logical steps required to solve a problem; they illustrate the concepts that need to be applied to come up with a solution to the problem, explaining the mathematical steps required.
Note that a workbook should not be the primary source of knowledge on the subject matter; it assumes that you've already read a tutorial or a textbook and that you are now seeking to improve your problem-solving skills. You should attempt solving the tasks of the respective kata first, and turn to the workbook only if stuck. While a textbook emphasizes knowledge acquisition, a workbook emphasizes skill acquisition.
This workbook describes the solutions to the problems offered in the Single-Qubit System Measurements tutorial. Since the tasks are offered as programming problems, the explanations also cover some elements of Q# that might be non-obvious for a first-time user.
What you should know for this workbook
You should be familiar with the following concepts before tackling the Single-Qubit System Measurements tutorial (and this workbook):
$\renewcommand{\ket}[1]{\left\lvert#1\right\rangle}$ $\renewcommand{\bra}[1]{\left\langle#1\right\rvert}$
The qubit is in the following state: $$\ket \psi = 0.6 \ket 0 + 0.8 \ket 1 \equiv \begin{bmatrix} 0.6 \\ 0.8\end{bmatrix}.$$
If this qubit is measured in the computational basis, what are the outcome probabilities?
The given state $\ket \psi$ is normalized, since $0.6^2 + 0.8^2 = 1$. Hence, the probability of measuring $0$ is $|0.6|^2 = 0.36$, and the probability of measuring $1$ is $|0.8|^2 = 0.64$.
Input: A qubit which is guaranteed to be in either the $|0\rangle$ or the $|1\rangle$ state.
Output: true
if the qubit was in the $|0\rangle$ state, or false
if it was in the $|1\rangle$ state. The state of the qubit at the end of the operation does not matter.
The input qubit is guaranteed to be either in basis state $|0\rangle$ or $|1\rangle$. This means that when measuring the qubit in the computational basis, the measurement will report the input state without any doubt.
In Q# the operation M
can be used to measure a single qubit in the computational basis. The measurement result is a value of type Result
: the operation M
will return One
if the input qubit was in the $|1\rangle$ state and Zero
if the input qubit was in the $|0\rangle$ state. Since we need to encode the first case as false
and the second one as true
, we can return the result of equality comparison between measurement result and Zero
.
%kata T2_IsQubitZero
operation IsQubitZero (q : Qubit) : Bool {
return M(q) == Zero;
}
Measure
operation¶Input: A qubit which is guaranteed to be in either the $\ket +$ state, or the $\ket -$ state.
Output: true
if the qubit is in the $\ket +$ state, or false
if it was in the $\ket -$ state.
The input qubit is guaranteed to be either in basis state $|+\rangle$ or $|-\rangle$. This means that when measuring the qubit in the Pauli $X$ basis, the measurement will report the input state without any doubt. (Recall that these states are eigenstates for the Pauli $X$ matrix).
In Q# the operation Measure
can be used to measure a qubit in Pauli basis of the user's choice. The operation returns a value of type Result
, and is Zero
if the measured state corresponds to the eigenvalue $+1$, and One
if it corresponds to the eigenvalue $-1$ of the Pauli operator.
Since the states $\ket +$ and $\ket -$ correspond to the eigenvalues $+1$ and $-1$ of the Pauli X operator, we can return the result of equality comparison between the measurement result and One
.
Note that since Measure
operation generally works with multiple qubits to perform multi-qubit measurements, it takes array parameters. To do a single-qubit measurement, you need to pass two arrays of one element, [PauliX]
and [q]
, rather than individual values.
%kata T3_IsQubitMinus
operation IsQubitMinus (q : Qubit) : Bool {
return Measure([PauliX], [q]) == One;
}
Thus, the probability of measuring $+$ is $|\frac1{\sqrt2}|^2 = 0.5$, and similarly, the probability of measuring $-$ is $0.5$.
First, we recall that the states $\ket{\pm i}$ are given by $$ \ket{\pm i} = \frac1{\sqrt2} (\ket 0 \pm i \ket 1). $$ We can now construct the two projectors $P_{\pm i}$ onto states $\ket {\pm i}$ as follows: \begin{align} P_{i} &= \ket{i}\bra{i} = \frac{1}{2} \begin{bmatrix} 1 \\ i \end{bmatrix} \begin{bmatrix} 1 & -i \end{bmatrix} = \frac{1}{2} \begin{bmatrix}1 & -i \\ i & 1\end{bmatrix}; \\ P_{-i} &=\ket{-i}\bra{-i} = \frac{1}{2} \begin{bmatrix} 1 \\ -i \end{bmatrix} \begin{bmatrix} 1 & i \end{bmatrix} = \frac{1}{2} \begin{bmatrix}1 & i \\ -i & 1\end{bmatrix} \end{align} Recalling that the probabilities of measuring $\pm i$ are equal to the norm of the vectors $P_{\pm i}\ket \psi$, we now apply $P_{\pm i}$ to $\ket \psi$: \begin{align} P_{+i} \ket \psi &= \frac{1}{2} \begin{bmatrix}1 & -i \\ i & 1\end{bmatrix} \begin{bmatrix} 0.6 \\ 0.8 \end{bmatrix} = \frac{1}{2} \begin{bmatrix} 0.6 - 0.8i \\ 0.8 + 0.6i \end{bmatrix} ;\\ P_{-i} \ket \psi &= \frac{1}{2} \begin{bmatrix}1 & i \\ -i & 1\end{bmatrix} \begin{bmatrix} 0.6 \\ 0.8 \end{bmatrix} = \frac{1}{2} \begin{bmatrix} 0.6 + 0.8i \\ 0.8 - 0.6i \end{bmatrix}. \end{align} Hence, the probabilities of measuring $\pm i$, which we denote by $p(\pm i)$, are \begin{align} p(+i)& = |P_{+i} \ket \psi|^2 = \frac{1}{4}(|0.6 - 0.8i|^2 + |0.8 + 0.6i|^2) = \frac{1}{2}; \\ p(-i)& = |P_{-i} \ket \psi|^2 = \frac{1}{4}(|0.6 + 0.8i|^2 + |0.8 - 0.6i|^2) = \frac{1}{2}. \end{align}
Inputs: A qubit which is guaranteed to be in either the $\ket {\psi_+}$ or the $\ket{\psi_-} $ state, where $\ket {\psi_+} = 0.6\ket 0 + 0.8 \ket 1 $ and $\ket {\psi_-} = 0.8\ket 0 - 0.6 \ket 1$.
Output: true
if the qubit was in the $\ket {\psi_+}$ state, or false
if it was in the $\ket{\psi_-} $ state. The state of the qubit at the end of the operation does not matter.
We can distinguish between the states $\ket{\psi_\pm}$ if we implement a measurement in the $\{ \ket{\psi_+}, \ket{\psi_-}\}$ basis. This can be done if we construct a unitary transformation which maps the $\ket{\psi_+}$ state to the $\ket{0}$ state, and the $\ket{\psi_{-}}$ state to the $\ket{1}$ state.
We can notice that the $R_y$ rotation gate with $\theta = 2 \arctan \frac{0.8}{0.6}$ is an appropriate transformation:
\begin{align} R_y(\theta) \ket 0 &= 0.6 \ket 0 + 0.8 \ket 1 = \ket {\psi_+} \\ R_y(\theta) \ket 1 &= -0.8 \ket 0 + 0.6 \ket 1 = \ket{\psi_-} \end{align}Thus, the inverse (adjoint) transformation $R_y(-\theta)$ maps the $\ket{\psi_\pm}$ basis to the computational basis, i.e., \begin{align} R_y(-\theta) \ket {\psi_+} &= \ket 0 \\ R_y(-\theta) \ket {\psi_-} &= \ket 1 \end{align}
Hence, if we apply $R_y(-\theta)$ to the qubit, its state will be transformed to one of the computational basis states, at which point we can measure it using M
. If M
returns Zero
, the rotated state is $\ket{0}$, which means that the original state of the qubit was $\ket{\psi_+}$. Similarly, an output of One
indicates that the qubit was originally in the state $\ket{\psi_-}$.
%kata T5_IsQubitPsiPlus
open Microsoft.Quantum.Math;
operation IsQubitPsiPlus (q : Qubit) : Bool {
Ry(-2.0 * ArcTan2(0.8, 0.6), q);
return M(q) == Zero;
}
Inputs:
Double
.Output: true
if the qubit was in the $|A\rangle$ state, or false
if it was in the $|B\rangle$ state. The state of the qubit at the end of the operation does not matter.
We can distinguish between the states $\ket{A}$ and $\ket B$ if we implement a measurement in the $\{ \ket{A}, \ket{B}\}$ basis.
We can notice that the $R_x$ rotation gate with $\theta = 2 \alpha$ is an appropriate transformation which maps the $\ket 0 $ state to the $\ket A$ state, and the $\ket 1$ state to the $\ket B$ state:
\begin{align} R_x(\theta) \ket 0 &= \cos \alpha \ket 0 -i \sin \alpha \ket 1 = \ket {A} \\ R_x(\theta) \ket 1 &= -i \sin \alpha \ket 0 + \cos \alpha \ket 1 = \ket{B} \end{align}Thus, the inverse transformation $R_x(-\theta)$ maps the $A/B$ basis to the $0/1$ basis.
Therefore, if we apply $R_x(-\theta)$ to the qubit and measure it using M
, a measurement result of Zero
will correspond to the qubit's original state being $\ket{A}$, while a result of One
will correspond to the qubit's original state being $\ket B$.
%kata T6_IsQubitA
operation IsQubitA (alpha: Double, q : Qubit) : Bool {
Rx(-2.0 * alpha, q);
return M(q) == Zero;
}
Inputs:
Double
.Output: Implement a measurement in the $\{\ket A$, $\ket B\}$ basis. Same as in the previous exercise, $|A\rangle = \cos \alpha |0\rangle - i \sin \alpha |1\rangle$ and $|B\rangle = - i \sin \alpha |0\rangle + \cos \alpha |1\rangle$. Return Zero
if the measurement outcome is $A$, and One
if the outcome is $B$.
The state of the qubit after the measurement should correspond to the measurement result.
For this problem, we follow the procedure given in the arbitrary basis measurement section of the tutorial.
As noted in the solution of problem 6, the gate $R_x(-\theta)$, with $\theta = 2\alpha$ transforms the $\ket A/\ket B$ states to the $\ket 0/\ket 1$ states:
\begin{align}
R_x(-\theta) \ket A &= \ket 0 \\
R_X(-\theta) \ket B &= \ket 1
\end{align}
Hence, we first apply $R_x(-\theta)$ to the qubit. Next, we measure in the computational basis using the M
operation.
If the M
operation returned Zero
, we get measurement outcome $A$, and if it returned One
, we get measurement outcome $B$.
After the measurement, we apply the inverse of the $R_x(-\theta)$ gate, which is the $R_x(\theta)$ gate. The final rotation ensures that the state of the qubit is in the state corresponding to the measurement outcome.
%kata T7_MeasureInABBasis
operation MeasureInABBasis (alpha: Double, q : Qubit) : Result {
Rx(-2.0 * alpha, q);
let measurementResult = M(q);
Rx(2.0 * alpha, q);
return measurementResult;
}