Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b0e8b68
Merge branch 'main' of github.com:microsoft/qsharp
Mar 26, 2024
4a2a62d
Merge branch 'main' of github.com:microsoft/qsharp into katas/superpo…
Mar 28, 2024
d1e217b
add ghz kata
Mar 28, 2024
9de6753
Merge branch 'main' of github.com:microsoft/qsharp into katas/superpo…
Apr 1, 2024
20efc32
add even odd superposition kata
Apr 1, 2024
c987029
add all basis vectors superposition kata
Apr 1, 2024
a2148db
Update katas/content/superposition/even_odd/Solution.qs
jkingdon-ms Apr 2, 2024
46bdc2e
Update katas/content/superposition/even_odd/Placeholder.qs
jkingdon-ms Apr 2, 2024
07d0116
Update katas/content/superposition/even_odd/Solution.qs
jkingdon-ms Apr 2, 2024
4d64d8a
pr suggestions
Apr 2, 2024
44b630f
Merge branch 'katas/superposition-18-19-110' of github.com:jkingdon-m…
Apr 2, 2024
29d54dc
Update katas/content/superposition/all_basis_vectors/Placeholder.qs
jkingdon-ms Apr 2, 2024
7e679ad
Update katas/content/superposition/greenberger_horne_zeilinger/soluti…
jkingdon-ms Apr 2, 2024
ecb0905
pr suggestions
Apr 2, 2024
aa1a768
Merge branch 'katas/superposition-18-19-110' of github.com:jkingdon-m…
Apr 2, 2024
160b7bd
remove superposition from index.json
Apr 2, 2024
3d2c47b
fix even odd table
Apr 2, 2024
f03e0f2
Update katas/content/superposition/greenberger_horne_zeilinger/soluti…
jkingdon-ms Apr 2, 2024
fb25a57
Update katas/content/superposition/greenberger_horne_zeilinger/soluti…
jkingdon-ms Apr 2, 2024
f8d1980
Update katas/content/superposition/even_odd/solution.md
jkingdon-ms Apr 2, 2024
9cc1c08
Update katas/content/superposition/all_basis_vectors/solution.md
jkingdon-ms Apr 2, 2024
e65cbc2
bold first qubits in even odd table
Apr 2, 2024
2993e4b
Merge branch 'katas/superposition-18-19-110' of github.com:jkingdon-m…
Apr 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Kata {
operation AllBasisVectorsSuperposition (qs : Qubit[]) : Unit is Adj + Ctl {
// Implement your solution here...

}
}

7 changes: 7 additions & 0 deletions katas/content/superposition/all_basis_vectors/Solution.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Kata {
operation AllBasisVectorsSuperposition (qs : Qubit[]) : Unit is Adj + Ctl {
for q in qs {
H(q);
}
}
}
26 changes: 26 additions & 0 deletions katas/content/superposition/all_basis_vectors/Verification.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Kata.Verification {
open Microsoft.Quantum.Katas;

operation AllBasisVectorsSuperposition_Reference (qs : Qubit[]) : Unit is Adj + Ctl {
for q in qs {
H(q);
}
}

@EntryPoint()
operation CheckSolution() : Bool {
for i in 1 .. 5 {
Message($"Testing {i} qubit(s)...");
if not CheckOperationsEquivalenceOnZeroStateWithFeedback(
Kata.AllBasisVectorsSuperposition,
AllBasisVectorsSuperposition_Reference,
i) {
return false;
}
}

return true;


}
}
5 changes: 5 additions & 0 deletions katas/content/superposition/all_basis_vectors/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**Input:** $N$ ($N \ge 1$) qubits in the $|0 \dots 0\rangle$ state.

**Goal:** Change the state of the qubits to an equal superposition of all basis vectors $\frac{1}{\sqrt{2^N}} \big (|0 \dots 0\rangle + \dots + |1 \dots 1\rangle\big)$.

> For example, for $N = 2$ the final state should be $\frac{1}{2} \big (|00\rangle + |01\rangle + |10\rangle + |11\rangle\big)$.
22 changes: 22 additions & 0 deletions katas/content/superposition/all_basis_vectors/solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
As we've seen in the 'Superposition of all basis vectors on two qubits' task, to prepare a superposition of all basis vectors on 2 qubits we need to apply a Hadamard gate to each of the qubits.

It seems that the solution for the general case might be to apply a Hadamard gate to every qubit as well. Let's check the first few examples:


$$\begin{align*}
H|0\rangle &= \frac{1}{\sqrt2}\big(|0\rangle + |1\rangle\big) \\\\
H|0\rangle \otimes H|0\rangle &= \frac{1}{\sqrt2} \big(|0\rangle + |1\rangle\big) \otimes \frac{1}{\sqrt2} \big(|0\rangle + |1\rangle\big) \\\\
&= \frac{1}{\sqrt{2^2}}\big(|00\rangle + |01\rangle+ |10\rangle+ |11\rangle\big) \\\\
H|0\rangle \otimes H|0\rangle \otimes H|0\rangle &= \frac{1}{\sqrt{2^2}}\big(|00\rangle + |01\rangle+ |10\rangle+ |11\rangle\big) \otimes \frac{1}{\sqrt2}\big(|0\rangle + |1\rangle\big) \\\\
&= \frac{1}{\sqrt{2^3}}\big(|000\rangle + |001\rangle + |010\rangle+ |100\rangle+ |110\rangle + |101\rangle+ |011\rangle+ |111\rangle\big) \\\\
\underset{N}{\underbrace{H|0\rangle \otimes \dots \otimes H|0\rangle}}
&= \frac{1}{\sqrt{2^{N-1}}} \big( |\underset{N-1}{\underbrace{0 \cdots 0}}\rangle + \cdots + |\underset{N-1}{\underbrace{1 \cdots 1}}\rangle \big) \otimes \frac{1}{\sqrt2}\big(|0\rangle + |1\rangle\big) = \\\\
&= \frac{1}{\sqrt{2^N}} \big( |\underset{N}{\underbrace{0 \cdots 0}}\rangle + \cdots + |\underset{N}{\underbrace{1 \cdots 1}}\rangle \big) \\\\
\end{align*}$$

Thus, the solution requires us to iterate over the qubit array and to apply the Hadamard gate to each element.

@[solution]({
"id": "superposition__all_basis_vectors_solution",
"codePath": "./Solution.qs"
})
7 changes: 7 additions & 0 deletions katas/content/superposition/even_odd/Placeholder.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Kata {
operation EvenOddNumbersSuperposition(qs : Qubit[], isEven : Bool) : Unit is Adj + Ctl {
// Implement your solution here...

}
}

14 changes: 14 additions & 0 deletions katas/content/superposition/even_odd/Solution.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Kata {
operation EvenOddNumbersSuperposition(qs : Qubit[], isEven : Bool) : Unit is Adj + Ctl {
let N = Length(qs);

for i in 0 .. N-2 {
H(qs[i]);
}

// for odd numbers, flip the last bit to 1
if not isEven {
X(qs[N-1]);
}
}
}
32 changes: 32 additions & 0 deletions katas/content/superposition/even_odd/Verification.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Kata.Verification {
open Microsoft.Quantum.Katas;

operation EvenOddNumbersSuperposition_Reference(qs : Qubit[], isEven : Bool) : Unit is Adj + Ctl {
let N = Length(qs);

for i in 0..N-2 {
H(qs[i]);
}

if not isEven {
X(qs[N-1]);
}
}

@EntryPoint()
operation CheckSolution() : Bool {
for i in 1 .. 5 {
for boolVal in [false, true] {
Message($"Testing {i} qubit(s) where isEven = {boolVal}...");
if not CheckOperationsEquivalenceOnZeroStateWithFeedback(
Kata.EvenOddNumbersSuperposition(_, boolVal),
EvenOddNumbersSuperposition_Reference(_, boolVal),
i) {
return false;
}
}
}

return true;
}
}
10 changes: 10 additions & 0 deletions katas/content/superposition/even_odd/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
**Inputs:**

1. $N$ ($N \ge 1$) qubits in the $|0 \dots 0\rangle$ state (stored in an array of length $N$).
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`.
A basis state encodes an integer number using [big-endian](https://en.wikipedia.org/wiki/Endianness) binary notation: state $|01\rangle$ corresponds to the integer $1$, and state $|10 \rangle$ - to the integer $2$.

> For example, for $N = 2$ and `isEven = false` you need to prepare superposition $\frac{1}{\sqrt{2}} \big (|01\rangle + |11\rangle\big )$,
and for $N = 2$ and `isEven = true` - superposition $\frac{1}{\sqrt{2}} \big (|00\rangle + |10\rangle\big )$.
42 changes: 42 additions & 0 deletions katas/content/superposition/even_odd/solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Let’s look at some examples of basis states to illustrate the binary numbering system.

The 4 basis states on $N = 2$ qubits can be split in two columns, where the left column represents the basis states that form the required superposition state for `isEven = true` and the right column - the basis states that form the required superposition state for `isEven = false`.

| even | odd |
| -------- | ------- |
| **0**0 | **0**1 |
| **1**0 | **1**1 |

If we do the same basis state split for $N = 3$ qubits, the pattern becomes more obvious.

| even | odd |
| -------- | ------- |
| **00**0 | **00**1 |
| **01**0 | **01**1 |
| **10**0 | **10**1 |
| **11**0 | **11**1 |

The two leftmost qubits go through all possible basis states for `isEven = true` and for `isEven = false`, and the rightmost qubit stays in the $|0\rangle$ state for `isEven = true` and in the $|1\rangle$ state for `isEven = false`.

A quick sanity check for $N = 4$ qubits re-confirms the pattern.

| even | odd |
| --------- | -------- |
| **000**0 | **000**1 |
| **001**0 | **001**1 |
| **010**0 | **010**1 |
| **011**0 | **011**1 |
| **100**0 | **100**1 |
| **101**0 | **101**1 |
| **110**0 | **110**1 |
| **111**0 | **111**1 |

Again, the three leftmost qubits go through all possible basis states in both columns, and the rightmost qubit stays in the same state in each column.

The solution is to put all qubits except the rightmost one into an equal superposition (similar to what we did in the 'Superposition of all basis vectors' task) and to set the rightmost qubit to $|0\rangle$ or $|1\rangle$ depending on the `isEven` flag, using the X operator to convert $|0\rangle$ to $|1\rangle$ if `isEven = false`.


@[solution]({
"id": "superposition__even_odd_solution",
"codePath": "./Solution.qs"
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Kata {
open Microsoft.Quantum.Arrays;

operation GHZ_State (qs : Qubit[]) : Unit is Adj + Ctl {
// Implement your solution here...

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Kata {
open Microsoft.Quantum.Arrays;

operation GHZ_State (qs : Qubit[]) : Unit is Adj + Ctl {
H(qs[0]);

// Library function Rest returns all array elements except for the first one
for q in Rest(qs) {
CNOT(qs[0], q);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Kata.Verification {
open Microsoft.Quantum.Katas;
open Microsoft.Quantum.Arrays;

operation GHZ_State_Reference (qs : Qubit[]) : Unit is Adj + Ctl {
H(qs[0]);

for q in Rest(qs) {
CNOT(qs[0], q);
}
}

@EntryPoint()
operation CheckSolution() : Bool {
for i in 1 .. 5 {
Message($"Testing {i} qubit(s)...");
if not CheckOperationsEquivalenceOnZeroStateWithFeedback(
Kata.GHZ_State,
GHZ_State_Reference,
i) {
return false;
}
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Input:** $N$ ($N \ge 1$) qubits in the $|0 \dots 0\rangle$ state (stored in an array of length $N$).

**Goal:** Change the state of the qubits to the GHZ state $\frac{1}{\sqrt{2}} \big (|0\dots0\rangle + |1\dots1\rangle\big)$.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The single-qubit GHZ state is the plus state $\frac{1}{\sqrt{2}} \big (|0\rangle + |1\rangle\big)$ that we've discussed in the first task. As a reminder, that state is prepared by applying a Hadamard gate.

The 2-qubit GHZ state is the Bell state $\frac{1}{\sqrt{2}} \big (|00\rangle + |11\rangle\big)$ that we've discussed in the two previous tasks.

The next one is the 3-qubit GHZ state:
$$|GHZ\rangle = \frac{1}{\sqrt{2}} \big (|000\rangle + |111\rangle\big)$$

Let's use the 2-qubit state as a building block to construct the state for 3 qubits. First, let's add a third qubit to the above state (on the right from the first two qubits).
Comparing this state with the desired end state, we see that they differ only in the third (rightmost) qubit:

$$|\Phi^+\rangle |0\rangle = \frac{1}{\sqrt{2}} \big (|000\rangle + |11\textbf{0}\rangle\big)$$
$$|GHZ\rangle = \frac{1}{\sqrt{2}} \big (|000\rangle + |11\textbf{1}\rangle\big)$$

Applying a controlled NOT operation using the first (leftmost) qubit as the control bit and the third (rightmost) qubit as the target qubit allows us to fix this difference.

Thus we can come to the general solution: apply Hadamard gate to the first qubit and do a series of CNOT gates with the first qubit as control and each of the other qubits as targets.

@[solution]({
"id": "superposition__ghz_solution",
"codePath": "./Solution.qs"
})
30 changes: 30 additions & 0 deletions katas/content/superposition/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,36 @@ This kata is designed to get you familiar with the concept of superposition and
]
})

@[exercise]({
"id": "superposition__ghz_state",
"title": "Greenberger-Horne-Zellinger State",
"path": "./greenberger_horne_zeilinger/",
"qsDependencies": [
"../KatasLibrary.qs",
"./Common.qs"
]
})

@[exercise]({
"id": "superposition__all_basis_vectors",
"title": "All Basis Vectors",
"path": "./all_basis_vectors/",
"qsDependencies": [
"../KatasLibrary.qs",
"./Common.qs"
]
})

@[exercise]({
"id": "superposition__even_odd",
"title": "Even and Odd Numbers",
"path": "./even_odd/",
"qsDependencies": [
"../KatasLibrary.qs",
"./Common.qs"
]
})

@[section]({
"id": "superposition__uneven_superpositions",
"title": "Uneven superpositions"
Expand Down