Skip to content

Commit

Permalink
Finish Superposition/Preparing quantum states kata (#1465)
Browse files Browse the repository at this point in the history
Add the last task (2.7) from the classic Superposition kata, polish task
names / descriptions for uniformity.
  • Loading branch information
tcNickolas authored May 2, 2024
1 parent 663df6a commit 25bc315
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 31 deletions.
1 change: 1 addition & 0 deletions katas/content/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"single_qubit_gates",
"multi_qubit_systems",
"multi_qubit_gates",
"superposition",
"single_qubit_measurements",
"multi_qubit_measurements",
"random_numbers",
Expand Down
2 changes: 1 addition & 1 deletion katas/content/superposition/even_odd/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
**Inputs:**

1. $N$ ($N \ge 1$) qubits in the $|0 \dots 0\rangle$ state (stored in an array of length $N$).
1. $N$ ($N \ge 1$) qubits in the $|0 \dots 0\rangle$ state.
2. A boolean `isEven`.

**Goal:** Prepare a superposition of all *even* numbers if `isEven` is `true`, or of all *odd* numbers if `isEven` is `false`.
Expand Down
4 changes: 1 addition & 3 deletions katas/content/superposition/four_bitstrings/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@

**Goal:** Create an equal superposition of the four basis states given by the bit strings.

**Example:**

For $N = 3$ and `bits = [[false, true, false], [true, false, false], [false, false, true], [true, true, false]]`, the state you need to prepare is $\frac{1}{2} \big(|010\rangle + |100\rangle + |001\rangle + |110\rangle\big)$.
> For example, for $N = 3$ and `bits = [[false, true, false], [true, false, false], [false, false, true], [true, true, false]]`, the state you need to prepare is $\frac{1}{2} \big(|010\rangle + |100\rangle + |001\rangle + |110\rangle\big)$.
2 changes: 1 addition & 1 deletion katas/content/superposition/hardy_state/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
**Input:** 2 qubits in the $|00\rangle$ state.
**Input:** Two qubits in the $|00\rangle$ state.

**Goal:** Change the state of the qubits to $\frac{1}{\sqrt{12}} \big(3|00\rangle + |01\rangle + |10\rangle + |11\rangle \big)$.
51 changes: 33 additions & 18 deletions katas/content/superposition/index.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Superposition
# Preparing Quantum States

@[section]({
"id": "superposition__overview",
"title": "Overview"
})

This kata is designed to get you familiar with the concept of superposition and with Q# programming.
This kata is designed to get you familiar with quantum state preparation and to help you practice using gates to change quantum states.

**This kata covers the following topics:**

Expand All @@ -20,12 +20,14 @@ This kata is designed to get you familiar with the concept of superposition and

@[section]({
"id": "superposition__even_superpositions",
"title": "Even superpositions"
"title": "Even Superpositions"
})

In this lesson, you'll practice preparing states that are even superpositions of specific sets of basis states, possibly with some relative phases on some of the basis states. These exercises can be solved using some sequences of $H$ and $X$ gates, their controlled variants, and some phase-introducing gates.

@[exercise]({
"id": "superposition__plus_state",
"title": "The Plus State",
"title": "Plus State",
"path": "./plus_state/",
"qsDependencies": [
"../KatasLibrary.qs",
Expand All @@ -35,7 +37,7 @@ This kata is designed to get you familiar with the concept of superposition and

@[exercise]({
"id": "superposition__minus_state",
"title": "The Minus State",
"title": "Minus State",
"path": "./minus_state/",
"qsDependencies": [
"../KatasLibrary.qs",
Expand All @@ -45,7 +47,7 @@ This kata is designed to get you familiar with the concept of superposition and

@[exercise]({
"id": "superposition__even_sup_two_qubits",
"title": "Even superposition of all basis vectors on two qubits",
"title": "All Two-Qubit Basis Vectors",
"path": "./even_sup_two_qubits/",
"qsDependencies": [
"../KatasLibrary.qs",
Expand All @@ -55,7 +57,7 @@ This kata is designed to get you familiar with the concept of superposition and

@[exercise]({
"id": "superposition__even_sup_two_qubits_phase_flip",
"title": "Even superposition of all basis vectors on two qubits with phase flip",
"title": "All Two-Qubit Basis Vectors with Phase Flip",
"path": "./even_sup_two_qubits_phase_flip/",
"qsDependencies": [
"../KatasLibrary.qs",
Expand All @@ -65,7 +67,7 @@ This kata is designed to get you familiar with the concept of superposition and

@[exercise]({
"id": "superposition__even_sup_two_qubits_complex_phases",
"title": "Even superposition of all basis vectors on two qubits with complex phases",
"title": "All Two-Qubit Basis Vectors with Complex Phases",
"path": "./even_sup_two_qubits_complex_phases/",
"qsDependencies": [
"../KatasLibrary.qs",
Expand Down Expand Up @@ -105,7 +107,7 @@ This kata is designed to get you familiar with the concept of superposition and

@[exercise]({
"id": "superposition__all_basis_vectors",
"title": "All Basis Vectors",
"title": "All N-Qubit Basis Vectors",
"path": "./all_basis_vectors/",
"qsDependencies": [
"../KatasLibrary.qs",
Expand All @@ -115,7 +117,7 @@ This kata is designed to get you familiar with the concept of superposition and

@[exercise]({
"id": "superposition__even_odd",
"title": "Even and Odd Numbers",
"title": "Even or Odd Basis Vectors",
"path": "./even_odd/",
"qsDependencies": [
"../KatasLibrary.qs",
Expand Down Expand Up @@ -163,11 +165,17 @@ This kata is designed to get you familiar with the concept of superposition and
]
})


@[section]({
"id": "superposition__arbitrary_rotations",
"title": "Arbitrary Rotations"
})

In this lesson, you'll practice preparing more interesting states that require the use of arbitrary rotation gates.

> Some of the alternative solutions rely on clever tricks such as partial measurement of the system.
> To fully appreciate them, come back to this kata after you've familiarized yourself with quantum measurements!
@[exercise]({
"id": "superposition__unequal_superposition",
"title": "Unequal Superposition",
Expand All @@ -190,7 +198,7 @@ This kata is designed to get you familiar with the concept of superposition and

@[exercise]({
"id": "superposition__three_states_two_qubits",
"title": "Three States Two Qubits",
"title": "Three Two-Qubit Basis States",
"path": "./three_states_two_qubits/",
"qsDependencies": [
"../KatasLibrary.qs",
Expand All @@ -200,7 +208,7 @@ This kata is designed to get you familiar with the concept of superposition and

@[exercise]({
"id": "superposition__three_states_two_qubits_phases",
"title": "Three States Two Qubits Phases",
"title": "Three Two-Qubit Basis States with Complex Phases",
"path": "./three_states_two_qubits_phases/",
"qsDependencies": [
"../KatasLibrary.qs",
Expand All @@ -220,19 +228,26 @@ This kata is designed to get you familiar with the concept of superposition and

@[exercise]({
"id": "superposition__wstate_power_of_two",
"title": "W State Power Of Two",
"title": "W State on Power of Two Number of Qubits",
"path": "./wstate_power_of_two/",
"qsDependencies": [
"../KatasLibrary.qs",
"./Common.qs"
"../KatasLibrary.qs"
]
})

@[exercise]({
"id": "superposition__wstate_arbitrary",
"title": "W State on Arbitrary Number of Qubits",
"path": "./wstate_arbitrary/",
"qsDependencies": [
"../KatasLibrary.qs"
]
})


@[section]({
"id": "superposition__conclusion",
"title": "Conclusion"
})

Congratulations! In this kata you learned to use the basic quantum computing gates to prepare quantum states. Here are a few key concepts to keep in mind:

- TODO
Congratulations! In this kata you learned to use the basic quantum computing gates to prepare quantum states.
4 changes: 1 addition & 3 deletions katas/content/superposition/parity_bitstrings/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
* an even number of 1s in them if `parity = 0`, or
* an odd number of 1s in them if `parity = 1`.

**Example:**

For $N = 2$ the required state is $\frac{1}{\sqrt{2}}\big(|00\rangle + |11\rangle\big)$ if `parity = 0`, or $\frac{1}{\sqrt{2}}\big(|01\rangle + |10\rangle\big)$ if `parity = 1`.
> For example, for $N = 2$ the required state is $\frac{1}{\sqrt{2}}\big(|00\rangle + |11\rangle\big)$ if `parity = 0`, or $\frac{1}{\sqrt{2}}\big(|01\rangle + |10\rangle\big)$ if `parity = 1`.
<details>
<summary><b>Need a hint?</b></summary>
Expand Down
6 changes: 6 additions & 0 deletions katas/content/superposition/wstate_arbitrary/Placeholder.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Kata {
operation WState_Arbitrary (qs : Qubit[]) : Unit {
// Implement your solution here...

}
}
13 changes: 13 additions & 0 deletions katas/content/superposition/wstate_arbitrary/SolutionA.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Kata {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;

operation WState_Arbitrary (qs : Qubit[]) : Unit is Adj + Ctl {
let N = Length(qs);
Ry(2.0 * ArcSin(Sqrt(1.0 / IntAsDouble(N))), qs[0]);
for i in 1 .. N - 1 {
ApplyControlledOnInt(0, Ry(2.0 * ArcSin(Sqrt(1.0 / IntAsDouble(N - i))), _),
qs[0 .. i - 1], qs[i]);
}
}
}
12 changes: 12 additions & 0 deletions katas/content/superposition/wstate_arbitrary/SolutionB.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Kata {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;

operation WState_Arbitrary (qs : Qubit[]) : Unit is Adj + Ctl {
let N = Length(qs);
Ry(2.0 * ArcSin(Sqrt(1.0 / IntAsDouble(N))), qs[0]);
if N > 1 {
ApplyControlledOnInt(0, WState_Arbitrary, [qs[0]], qs[1 ...]);
}
}
}
48 changes: 48 additions & 0 deletions katas/content/superposition/wstate_arbitrary/SolutionC.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace Kata {
operation WState_Arbitrary (qs : Qubit[]) : Unit {
let N = Length(qs);
if N == 1 {
X(qs[0]);
} else {
mutable P = 1;
for i in 1 .. 6 {
if P < N {
set P *= 2;
}
}

use anc = Qubit[P - N];
mutable allZeros = true;
repeat {
WState_PowerOfTwo(qs + anc);

set allZeros = true;
for i in 0 .. (P - N) - 1 {
if MResetZ(anc[i]) == One {
set allZeros = false;
}
}
}
until (allZeros);
}
}

operation WState_PowerOfTwo (qs : Qubit[]) : Unit is Adj + Ctl {
let N = Length(qs);

if N == 1 {
X(qs[0]);
} else {
let K = N / 2;
use anc = Qubit();
H(anc);

ApplyControlledOnInt(0, WState_PowerOfTwo, [anc], qs[0 .. K - 1]);
ApplyControlledOnInt(1, WState_PowerOfTwo, [anc], qs[K .. N - 1]);

for i in K .. N - 1 {
CNOT(qs[i], anc);
}
}
}
}
28 changes: 28 additions & 0 deletions katas/content/superposition/wstate_arbitrary/Verification.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Kata.Verification {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Katas;
open Microsoft.Quantum.Math;

operation WState_Arbitrary_Reference (qs : Qubit[]) : Unit is Adj + Ctl {
let N = Length(qs);
Ry(2.0 * ArcSin(Sqrt(1.0/IntAsDouble(N))), qs[0]);
for i in 1 .. N - 1 {
ApplyControlledOnInt(0, Ry(2.0 * ArcSin(Sqrt(1.0/IntAsDouble(N - i))), _), qs[0 .. i-1], qs[i]);
}
}

@EntryPoint()
operation CheckSolution() : Bool {
for n in 1 .. 6 {
Message($"Testing for N = {n}...");
if not CheckOperationsEquivalenceOnZeroStateWithFeedback(
Kata.WState_Arbitrary,
WState_Arbitrary_Reference,
n) {
return false;
}
}

return true;
}
}
10 changes: 10 additions & 0 deletions katas/content/superposition/wstate_arbitrary/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
**Input:** $N$ qubits in the $|0 \dots 0\rangle$ state ($N$ is not necessarily a power of two).

**Goal:** Change the state of the qubits to the [W state](https://en.wikipedia.org/wiki/W_state) - an equal superposition of $N$ basis states on $N$ qubits which have Hamming weight of 1.

For example, for $N = 3$ the required state is $\frac{1}{\sqrt{3}}\big(|100\rangle + |010\rangle + |001\rangle\big)$.

<details>
<summary><b>Need a hint?</b></summary>
You can modify the signature of the given operation to specify its controlled specialization.
</details>
80 changes: 80 additions & 0 deletions katas/content/superposition/wstate_arbitrary/solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
This problem allows a variety of solutions that rely on techniques from arbitrary rotations to recursion to postselection.

### Iterative Solution

The first approach we will describe relies on performing a sequence of controlled rotations.

To prepare a weighted superposition $\cos \theta |0\rangle + \sin \theta |1\rangle$ on a single qubit, we need to start with the $|0\rangle$ state and apply the $R_y$ gate to it with the angle parameter equal to $2 \theta$.
We'll apply the $R_y$ gate with angle $2 \theta_1 = 2\arcsin \frac{1}{\sqrt{N}}$ to the first qubit of the register to prepare the following state:

$$(\cos \theta_1 |0\rangle + \sin \theta_1 |1\rangle) \otimes |0 \dots 0\rangle = \frac{1}{\sqrt{N}}|10 \dots 0\rangle + \frac{\sqrt{N-1}}{\sqrt{N}}|00 \dots 0\rangle $$

The first term $\frac{1}{\sqrt{N}}|10 \dots 0\rangle$ already matches the first term of the $|W_N\rangle$ state; now we need to convert the second term $\frac{\sqrt{N-1}}{\sqrt{N}}|00 \dots 0\rangle$ into the rest of the $|W_N\rangle$ terms.

To prepare a term that matches the second term of the $|W_N\rangle$ state, we can apply another $R_y$ gate to the term $|00 \dots 0\rangle$, this time to the second qubit, with an angle $2 \theta_2 = 2\arcsin \frac{1}{\sqrt{N-1}}$.
To make sure it doesn't affect the term that we're already happy with, we will apply a controlled version of the $R_y$ gate, with the first qubit of the register in state $|0\rangle$ as control.
This will change our state to

$$\frac{1}{\sqrt{N}}|10 \dots 0\rangle + \frac{\sqrt{N-1}}{\sqrt{N}} |0\rangle \otimes (\cos \theta_2 |0\rangle + \sin \theta_2 |1\rangle) \otimes |0 \dots 0\rangle = $$
$$= \frac{1}{\sqrt{N}}|10 \dots 0\rangle + \frac{\sqrt{N-1}}{\sqrt{N}} \frac{1}{\sqrt{N-1}} |010 \dots 0\rangle + \frac{\sqrt{N-1}}{\sqrt{N}} \frac{\sqrt{N-2}}{\sqrt{N-1}} |000 \dots 0\rangle$$

Now we have two terms that match the terms of the $|W_N\rangle$ state, and need to convert the third term $\frac{\sqrt{N-2}}{\sqrt{N}}|00 \dots 0\rangle$ into the rest of terms.

We will keep going like this, preparing one term of the $|W_N\rangle$ state at a time, until the rotation on the last qubit will be an $X$ gate, controlled on all previous qubits being in the $|0 \dots 0\rangle$ state.

@[solution]({
"id": "superposition__wstate_arbitrary_solution_a",
"codePath": "./SolutionA.qs"
})

### Recursive Solution

We can express the same sequence of gates using recursion, if we notice that

$$|W_N\rangle = \frac{1}{\sqrt{N}}|10 \dots 0\rangle + \frac{\sqrt{N-1}}{\sqrt{N}}|0\rangle \otimes |W_{N-1}\rangle$$

The first step of the solution would still be applying the $R_y$ gate with angle $2 \theta_1 = 2\arcsin \frac{1}{\sqrt{N}}$ to the first qubit of the register to prepare the following state:

$$\frac{1}{\sqrt{N}}|10 \dots 0\rangle + \frac{\sqrt{N-1}}{\sqrt{N}}|00 \dots 0\rangle $$

But we would express the rest of the controlled rotations as the operation that prepares the $|W_{N-1}\rangle$ state, controlled on the $|0\rangle$ state of the first qubit.

> Note that you don't have to implement the controlled version of this operation yourself; it is sufficient to add `is Adj + Ctl` to the signature of the operation `WState_Arbitrary` to specify that controlled variant has to be generated automatically.
@[solution]({
"id": "superposition__wstate_arbitrary_solution_b",
"codePath": "./SolutionB.qs"
})

### Post-selection Solution

Let's assume that we know how to prepare the $W$ state for $N = 2^k$ (we've discussed this in the previous task), and figure out how to use this knowledge as a building block for solving this task.

Let's look at the smallest possible case for which $N \neq 2^k$: $N = 3$ (we'll be able to generalize our solution for this case to an arbitrary number of qubits). The target $W$ state looks like this:

$$|W_3\rangle = \frac{1}{3}\big(|100\rangle + |010\rangle + |001\rangle\big)$$

We will start by finding the smallest power of 2 $P$ which is greater than or equal to $N$; for our case $N = 3$ this power will be $P = 4$. We will allocate an extra $P - N$ qubits and use the solution of the previous task to prepare the $W_P$ state that looks as follows (with the state of the extra qubit highlighted in bold):

$$|W_4\rangle = \frac{1}{2}\big |100\textbf{0}\rangle + |010\textbf{0}\rangle + |001\textbf{0}\rangle + |000\textbf{1}\rangle \big) = $$
$$= \frac{\sqrt3}{2} \cdot \frac{1}{\sqrt3}\big(|100\rangle + |010\rangle + |001\rangle \big) \otimes |\textbf{0}\rangle + \frac{1}{2}|000\rangle \otimes |\textbf{1}\rangle = $$
$$= \frac{\sqrt3}{2} |W_3\rangle \otimes |\textbf{0}\rangle + \frac{1}{2}|000\rangle \otimes |\textbf{1}\rangle$$

As we can see, if the extra qubit is in the $|0\rangle$ state, the main 3 qubits that we are concerned about are in the right $|W_3\rangle$ state.

What happens if 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 $|0\rangle$, the system collapses to the $|W_3\rangle$ state - which is exactly what we wanted to achieve.
* If the result is $|1\rangle$, the system collapses to a state $|000\rangle$, so our goal is not achieved. The good thing is, this only happens in 25% of the cases, and we can just try again.

If we generalize this approach to an arbitrary $N$, we'll have

$$|W_P\rangle = \frac{\sqrt{N}}{\sqrt{P}} |W_N\rangle \otimes |\textbf{0}\rangle^{\otimes P-N} + \frac{\sqrt{P-N}}{\sqrt{P}} |0\rangle^{\otimes N} \otimes |W_{P-N}\rangle$$

Measuring the extra $P-N$ qubits gives us two possibilities:
* All the extra qubits are in the $|0\rangle$ state; this means the main qubits collapse to the $|W_N\rangle$ state.
* One of the extra qubits is in the $|1\rangle$ state; this means that the main qubits collapse to the $|0\rangle^{\otimes N}$ state, which is **not** the desired state. In this case we will reset and try again until all the extra qubits are in the $|0\rangle$ state.

@[solution]({
"id": "superposition__wstate_arbitrary_solution_c",
"codePath": "./SolutionC.qs"
})
Loading

0 comments on commit 25bc315

Please sign in to comment.