Skip to content

Commit

Permalink
Katas UI review - Multiqubit measurements (#1844)
Browse files Browse the repository at this point in the history
Co-authored-by: Mariia Mykhailova <[email protected]>
Co-authored-by: Scott Carda <[email protected]>
Co-authored-by: César Zaragoza Cortés <[email protected]>
  • Loading branch information
4 people authored Aug 20, 2024
1 parent 7ed76eb commit 10fa095
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
* 2 if they were in the $\ket{10}$ state,
* 3 if they were in the $\ket{11}$ state.

In this task and the subsequent ones the order of qubit states in task description matches the order of qubits in the array (i.e., $\ket{10}$ state corresponds to `qs[0]` in state $\ket{1}$ and `qs[1]` in state $\ket{0}$).
In this task and the subsequent ones the order of qubit states in task description matches the order of qubits in the array (that is, $\ket{10}$ state corresponds to `qs[0]` in state $\ket{1}$ and `qs[1]` in state $\ket{0}$).

The state of the qubits at the end of the operation does not matter.
The state of the qubits at the end of the operation doesn't matter.
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
First, we measure each of the qubits in the input array, convert the measurement results into integers
and store them in variables `m1` and `m2`. We can decode these results like this:
First, you measure each of the qubits in the input array, convert the measurement results into integers
and store them in variables `m1` and `m2`. You can decode these results like this:

- `m1` is $\ket{0}$ and `m2` is $\ket{0}$: we return $0\cdot2+0 = 0$
- `m1` is $\ket{0}$ and `m2` is $\ket{1}$: we return $0\cdot2+1 = 1$
- `m1` is $\ket{1}$ and `m2` is $\ket{0}$: we return $1\cdot2+0 = 2$
- `m1` is $\ket{1}$ and `m2` is $\ket{1}$: we return $1\cdot2+1 = 3$
- `m1` is $\ket{0}$ and `m2` is $\ket{0}$: the return value is $0\cdot2+0 = 0$
- `m1` is $\ket{0}$ and `m2` is $\ket{1}$: the return value is $0\cdot2+1 = 1$
- `m1` is $\ket{1}$ and `m2` is $\ket{0}$: the return value is $1\cdot2+0 = 2$
- `m1` is $\ket{1}$ and `m2` is $\ket{1}$: the return value is $1\cdot2+1 = 3$

In other words, we treat the measurement results as the binary notation of the return value in big-endian notation.
In other words, you treat the measurement results as the binary notation of the return value in big-endian notation.

@[solution]({
"id": "multi_qubit_measurements__full_measurements_solution",
"codePath": "Solution.qs"
})

We can generalize this code to read out an integer in big-endian notation from a qubit array of arbitrary length using several library operations and functions:
You can generalize this code to read out an integer in big-endian notation from a qubit array of arbitrary length using several library operations and functions:

- `MeasureEachZ` measures each of the qubits in the array in the computational basis and returns an array of `Result` data type.
- `Reversed` reverses the given array.
- `ResultArrayAsInt` converts an array of bits given as `Result` to an integer, assuming little-endian notation (that's why we have to reverse the array before converting it).
- `ResultArrayAsInt` converts an array of bits given as `Result` to an integer, assuming little-endian notation (that's why you have to reverse the array before converting it).

@[solution]({
"id": "multi_qubit_measurements__full_measurements_solution_alt",
Expand Down
105 changes: 53 additions & 52 deletions katas/content/multi_qubit_measurements/index.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
If we were not asked to maintain the state of the qubits, one approach would be to measure both the qubits separately in the computational basis, and check if the result is the same for both the measurements. If the measurement results are equal, the input state must have been a superposition of $\ket{00}$ and $\ket{11}$, while different measurement outcomes will imply that the input state must have been a superposition of $\ket{01}$ and $\ket{10}$. However, in these measurements we will lose the information about the original superposition states: a state $\alpha \ket{00} + \beta \ket{11}$ will collapse to either $\ket{00}$ or $\ket{11}$, and we won't be able to recover the information about the coefficients $\alpha$ and $\beta$.
If you weren't asked to maintain the state of the qubits, one approach would be to measure both the qubits separately in the computational basis, and check if the result is the same for both the measurements. If the measurement results are equal, the input state must have been a superposition of $\ket{00}$ and $\ket{11}$, while different measurement outcomes will imply that the input state must have been a superposition of $\ket{01}$ and $\ket{10}$. However, in these measurements you'll lose the information about the original superposition states: a state $\alpha \ket{00} + \beta \ket{11}$ will collapse to either $\ket{00}$ or $\ket{11}$, and you won't be able to recover the information about the coefficients $\alpha$ and $\beta$.

We need to measure the *parity* of the state without collapsing it all the way to the basis states. Pauli measurements can be used for joint measurements involving multiple qubits. For this task we need to do the $Z \otimes Z$ measurement on both qubits.
You need to measure the *parity* of the state without collapsing it all the way to the basis states. Pauli measurements can be used for joint measurements involving multiple qubits. For this task we need to do the $Z \otimes Z$ measurement on both qubits.

A joint measurement using $Z \otimes Z$ operator can be thought of as projecting the measured state to one of the two eigenspaces of $Z \otimes Z$ with $+1$ and $-1$ as the corresponding eigenvalues. The measurement returns `Zero` if the measured state is projected to the space with an eigenvalue of $+1$, and a result of `One` if projected to the space with an eigenvalue of $-1$.

As we've seen in the tutorial, the state $\alpha \ket{00} + \beta \ket{11}$ is an eigenstate of the $Z \otimes Z$ operator with the eigenvalue $+1$, and the state $\alpha \ket{01} + \beta \ket{10}$ is an eigenstate with the eigenvalue $-1$.
Hence, we can use this joint measurement to recognize which of the superposition states we were given while preserving the initial superposition state.
As you've seen in the tutorial, the state $\alpha \ket{00} + \beta \ket{11}$ is an eigenstate of the $Z \otimes Z$ operator with the eigenvalue $+1$, and the state $\alpha \ket{01} + \beta \ket{10}$ is an eigenstate with the eigenvalue $-1$.
Hence, you can use this joint measurement to recognize which of the superposition states you were given while preserving the initial superposition state.

In Q#, the operation `Measure` can be used to measure multiple qubits using an array of `Pauli` constants (`PauliI`, `PauliX`, `PauliY`, or `PauliZ`) that define the basis for measurement.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Since the state of the first qubit is different in these states ($\ket +$ and $\ket -$, respectively), it is sufficient to measure only the first qubit in the Pauli X basis in order to distinguish the two states. Furthermore, this measurement will not change the state of the system, since the post-projection state is identical to the original state regardless of the input state.
Since the state of the first qubit is different in these states ($\ket +$ and $\ket -$, respectively), it's sufficient to measure only the first qubit in the Pauli X basis in order to distinguish the two states. Furthermore, this measurement won't change the state of the system, since the post-projection state is identical to the original state regardless of the input state.

@[solution]({
"id": "multi_qubit_measurements__partial_measurements_for_system_solution",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ where the constants $a$ and $b$ satisfying $|a|^2 + |b|^2 = 1$ are unknown.
- If $ind$ equals 0, convert the state of the second qubit into $a\ket{0} + b\ket{1}$
- If $ind$ equals 1, convert the state of the second qubit into $b\ket{0} + a\ket{1}$.

The state of the first qubit at the end does not matter (it has to be not entangled with the second qubit).
The state of the first qubit at the end doesn't matter (it has to be not entangled with the second qubit).
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
We note that if we measure the first qubit in the computational basis, then an outcome of $0$ collapses the second qubit to the state $a\ket 0 + b \ket 1$, while an outcome of $1$ collapses the second qubit to the state $b\ket 0 + a \ket 1$.
Note that if you measure the first qubit in the computational basis, then an outcome of $0$ collapses the second qubit to the state $a\ket 0 + b \ket 1$, while an outcome of $1$ collapses the second qubit to the state $b\ket 0 + a \ket 1$.

Thus, if $ind=0$ and we measure $0$ or if $ind=1$ and we measure $1$, then after the measurement the second qubit will be in the desired state. On the other hand, if $ind=1$ and we measure $0$, or if $ind=0$ and we measure $1$, then the state of the second qubit after the measurement is not what we're looking for, but we can adjust it using the Pauli X gate.
Thus, if $ind=0$ and you measure $0$ or if $ind=1$ and we measure $1$, then after the measurement the second qubit will be in the desired state. On the other hand, if $ind=1$ and you measure $0$, or if $ind=0$ and you measure $1$, then the state of the second qubit after the measurement isn't what you're looking for, but you can adjust it using the Pauli X gate.

@[solution]({
"id": "multi_qubit_measurements__state_modification_solution",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<details>
<summary><b>Need a hint?</b></summary>
Consider a 3-qubit state $\frac{1}{2}(\ket{00} + \ket{01} + \ket{11}) \otimes \ket{0} + \frac{1}{2} \ket{11} \otimes \ket{1}$.
What happens when one measures the third qubit?
What happens when you measure the third qubit?
</details>
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
While it is possible to prepare the state $\ket \psi$ directly using unitary rotations, it is simpler to use post-selection for preparing it.
While it's possible to prepare the state $\ket \psi$ directly using unitary rotations, it's simpler to use post-selection for preparing it.

Initially we will prepare an equal superposition of all basis states on the first two qubits by applying the **H** gate to each of them, and allocate an extra qubit in the $\ket{0}$ state:
Initially, you prepare an equal superposition of all basis states on the first two qubits by applying the $H$ gate to each of them, and allocate an extra qubit in the $\ket{0}$ state:
$$\frac{1}{2} \big(\ket{00} + \ket{01} + \ket{10} + \ket{11}\big) \otimes \ket 0$$

The state of the first two qubits is a superposition of the state we want to prepare and the $\ket{11}$ state that we would like to discard.
The state of the first two qubits is a superposition of the state you want to prepare and the $\ket{11}$ state that you want to discard.

Now, we want to separate the first three basis states from the last one and to store this separation in the extra qubit.
For example, we can keep the state of the extra qubit $\ket{0}$ for the basis states that we want to keep, and switch it to $\ket{1}$ for the basis states that we would like to discard.
Now, you want to separate the first three basis states from the last one and to store this separation in the extra qubit.
For example, you can keep the state of the extra qubit $\ket{0}$ for the basis states that you want to keep, and switch it to $\ket{1}$ for the basis states that you want to discard.
A $CCNOT$ gate can do this, with the first two qubits used as control qubits and the extra qubit as target.
When the gate is applied, the state of the extra qubit will only change to $\ket{1}$ if both control qubits are in the $\ket{11}$ state, which marks exactly the state that we want to discard:
When the gate is applied, the state of the extra qubit will only change to $\ket{1}$ if both control qubits are in the $\ket{11}$ state, which marks exactly the state that you want to discard:

$$CCNOT\frac{1}{2} \big(\ket{00\textbf{0}} + \ket{01\textbf{0}} + \ket{10\textbf{0}} + \ket{11\textbf{0}}\big) =
\frac{1}{2}\big(\ket{00} + \ket{01} + \ket{10} \big) \otimes \ket{\textbf{0}} + \frac{1}{2}\ket{11} \otimes \ket{\textbf{1}} $$

Finally, we measure just the extra qubit; this causes a partial collapse of the system to the state defined by the measurement result:
* If the result is $\ket{0}$, the first two qubits collapse to a state that is a linear combination of basis states which had the extra qubit in state $\ket{0}$, i.e., they end up in the target state $\frac{1}{\sqrt3}\big(\ket{00} + \ket{01} + \ket{10}\big)$.
* If the result is $\ket{1}$, the first two qubits collapse to a state $\ket{11}$, so our goal is not achieved. The good thing is, this only happens in 25% of the cases, and we can just reset our qubits to the $\ket{00}$ state and try again.
Finally, you measure just the extra qubit; this causes a partial collapse of the system to the state defined by the measurement result:
* If the result is $\ket{0}$, the first two qubits collapse to a state that is a linear combination of basis states which had the extra qubit in state $\ket{0}$, that is, they end up in the target state $\frac{1}{\sqrt3}\big(\ket{00} + \ket{01} + \ket{10}\big)$.
* If the result is $\ket{1}$, the first two qubits collapse to a state $\ket{11}$, so your goal is not achieved. The good thing is, this only happens in 25% of the cases, and you can just reset our qubits to the $\ket{00}$ state and try again.

> Q# has a built-in <a href="https://learn.microsoft.com/azure/quantum/user-guide/language/expressions/conditionalloops#repeat-expression" target="_blank">repeat-until-success (RUS) loop</a>, which comes in handy in this case.
> * We will describe the main operations (applying $H$ and $CCNOT$ gates and the measurement) in the `repeat` part of the loop, which specifies its body.
> * `until` section specifies the condition which will break the loop. In this case the result of the measurement needs to be `Zero` to indicate our success.
> * Finally, the `fixup` section allows us to clean up the results of the loop body execution before trying again if the success criteria is not met. In this case we reset the first two qubits back to the $\ket{00}$ state.
> * The main operations (applying $H$ and $CCNOT$ gates and the measurement) are described in the `repeat` part of the loop, which specifies its body.
> * `until` section specifies the condition which will break the loop. In this case, the result of the measurement needs to be `Zero` to indicate your success.
> * Finally, the `fixup` section allows you to clean up the results of the loop body execution before trying again if the success criteria isn't met. In this case, you reset the first two qubits back to the $\ket{00}$ state.
@[solution]({
"id": "multi_qubit_measurements__state_preparation_solution",
Expand Down

0 comments on commit 10fa095

Please sign in to comment.