Superposition quantum kata is a series of exercises designed to get you familiar with the concept of superposition and with programming in Q#. It covers the following topics:
It is recommended to complete the BasicGates kata before this one to get familiar with the basic gates used in quantum computing. The list of basic gates available in Q# can be found at Microsoft.Quantum.Intrinsic.
Each task is wrapped in one operation preceded by the description of the task.
Your goal is to fill in the blank (marked with // ...
comments)
with some Q# code that solves the task. To verify your answer, run the cell using Ctrl+Enter (⌘+Enter on macOS).
The tasks are given in approximate order of increasing difficulty; harder ones are marked with asterisks.
Input: A qubit in the $|0\rangle$ state.
Goal: Change the state of the qubit to $|+\rangle = \frac{1}{\sqrt{2}} \big(|0\rangle + |1\rangle\big)$.
%kata T101_PlusState
operation PlusState (q : Qubit) : Unit {
// Hadamard gate H will convert |0⟩ state to |+⟩ state.
// Type the following: H(q);
// Then run the cell using Ctrl+Enter (⌘+Enter on macOS).
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Input: A qubit in the $|0\rangle$ state.
Goal: Change the state of the qubit to $|-\rangle = \frac{1}{\sqrt{2}} \big(|0\rangle - |1\rangle\big)$.
%kata T102_MinusState
operation MinusState (q : Qubit) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Input: Two qubits in the $|00\rangle$ state (stored in an array of length 2).
Goal: Change the state of the qubits to $|+\rangle \otimes |+\rangle = \frac{1}{2} \big(|00\rangle + |01\rangle + |10\rangle + |11\rangle\big)$.
%kata T103_AllBasisVectors_TwoQubits
operation AllBasisVectors_TwoQubits (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Input: Two qubits in the $|00\rangle$ sate (stored in an array of length 2).
Goal: Change the state of the qubits to $\frac{1}{2}\big(|00\rangle+|01\rangle+|10\rangle-|11\rangle \big)$.
%kata T104_AllBasisVectorWithPhaseFlip_TwoQubits
operation AllBasisVectorWithPhaseFlip_TwoQubits (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Input: Two qubits in the $|00\rangle$ state (stored in an array of length 2).
Goal: Change the state of the qubits to $\frac{1}{2} \big(|00\rangle + i|01\rangle - |10\rangle - i|11\rangle\big)$.
%kata T105_AllBasisVectorsWithPhases_TwoQubits
operation AllBasisVectorsWithPhases_TwoQubits (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Input: Two qubits in the $|00\rangle$ state (stored in an array of length 2).
Goal: Change the state of the qubits to $|\Phi^{+}\rangle = \frac{1}{\sqrt{2}} \big (|00\rangle + |11\rangle\big)$.
You can find detailed coverage of Bell states and their creation in this blog post.
%kata T106_BellState
operation BellState (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Inputs:
Goal: Change the state of the qubits to one of the Bell states, based on the value of index:
Index | State |
---|---|
0 | $|\Phi^{+}\rangle = \frac{1}{\sqrt{2}} \big (|00\rangle + |11\rangle\big)$ |
1 | $|\Phi^{-}\rangle = \frac{1}{\sqrt{2}} \big (|00\rangle - |11\rangle\big)$ |
2 | $|\Psi^{+}\rangle = \frac{1}{\sqrt{2}} \big (|01\rangle + |10\rangle\big)$ |
3 | $|\Psi^{-}\rangle = \frac{1}{\sqrt{2}} \big (|01\rangle - |10\rangle\big)$ |
%kata T107_AllBellStates
operation AllBellStates (qs : Qubit[], index : Int) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
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)$.
For the syntax of flow control statements in Q#, see Q# iterations and Q# conditional branching documentation.
%kata T108_GHZ_State
operation GHZ_State (qs : Qubit[]) : Unit {
// You can find N as Length(qs).
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
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)$.
%kata T109_AllBasisVectorsSuperposition
operation AllBasisVectorsSuperposition (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Inputs:
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 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 )$.
%kata T110_EvenOddNumbersSuperposition
operation EvenOddNumbersSuperposition (qs : Qubit[], isEven : Bool) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Inputs:
Bool[]
. Bit values false
and true
correspond to $|0\rangle$ and $|1\rangle$ states. You are guaranteed that the first bit of the bit string is true
.Goal: Change the state of the qubits to an equal superposition of $|0 \dots 0\rangle$ and the basis state given by the bit string.
For example, for the bit string
[true, false]
the state required is $\frac{1}{\sqrt{2}}\big(|00\rangle + |10\rangle\big)$.
%kata T111_ZeroAndBitstringSuperposition
operation ZeroAndBitstringSuperposition (qs : Qubit[], bits : Bool[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Inputs:
Bool[]
s. Bit values false
and true
correspond to $|0\rangle$ and $|1\rangle$ states. You are guaranteed that the two bit strings differ in at least one bit.Goal: Change the state of the qubits to an equal superposition of the basis states given by the bit strings.
For example, for bit strings
[false, true, false]
and[false, false, true]
the state required is $\frac{1}{\sqrt{2}}\big(|010\rangle + |001\rangle\big)$.
If you need to define any helper functions, you'll need to create an extra code cell for it and execute it before returning to this cell.
Use ControlledOnInt
and ControlledOnBitString
library functions to apply a controlled gate on a target qubit(s).
ControlledOnInt
generates a gate which is applied when the control register is in a state corresponding to a non-negative integer (e.g., for integer 0, the control register must be in state $|0\rangle$). Read more in the documentation.
ControlledOnBitString
is similar, but the state is specified with a bit string (e.g., for bit string [false, true]
, the control register must be in state $|01\rangle$). Read more in the documentation.
%kata T112_TwoBitstringSuperposition
operation TwoBitstringSuperposition (qs : Qubit[], bits1 : Bool[], bits2 : Bool[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Inputs:
Bool[][]
bits
. bits
is an $4 \times N$ array which describes the bit strings as follows: bits[i]
describes the i
-th bit string and has $N$ elements. You are guaranteed that all four bit strings will be distinct.Goal: Change the state of the qubits to an equal superposition of the four basis states given by the bit strings.
For example, for $N = 3$ and
bits = [[false, true, false], [true, false, false], [false, false, true], [true, true, false]]
the state required is $\frac{1}{2}\big(|010\rangle + |100\rangle + |001\rangle + |110\rangle\big)$.
%kata T113_FourBitstringSuperposition
operation FourBitstringSuperposition (qs : Qubit[], bits : Bool[][]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Inputs:
Int
parity
.Goal: change the state to an equal superposition of all basis states that have
parity = 0
, orparity = 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)$ ifparity = 1
.
%kata T114_AllStatesWithParitySuperposition
operation AllStatesWithParitySuperposition (qs : Qubit[], parity : Int) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Inputs:
Double
.Goal : Change the state of the qubit to $\cos{α} |0\rangle + \sin{α} |1\rangle$.
%kata T201_UnequalSuperposition
operation UnequalSuperposition (q : Qubit, alpha : Double) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Input: Two qubits in the $|00\rangle$ state.
Goal: Change the state of the qubits to $\frac{1}{\sqrt{2}}|00\rangle+\frac{1}{2}|10\rangle+\frac{1}{2}|11\rangle$.
%kata T202_ControlledRotation
operation ControlledRotation (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Input: Two qubits in the $|00\rangle$ state.
Goal: Change the state of the qubits to $\frac{1}{\sqrt{3}} \big(|00\rangle + |01\rangle + |10\rangle\big)$.
open Microsoft.Quantum.Math;to the code before the operation definition.
%kata T203_ThreeStates_TwoQubits
operation ThreeStates_TwoQubits (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Input: Two qubits in $|0\rangle$ state (stored in an array of length 2).
Output: Change the state of the qubits to $\frac{1}{\sqrt{3}} \big( |00\rangle + \omega |01\rangle + \omega^2 |10\rangle \big)$.
Here $\omega = e^{2\pi i/3}$.
open Microsoft.Quantum.Math;to the code before the operation definition.
%kata T204_ThreeStates_TwoQubits_Phases
operation ThreeStates_TwoQubits_Phases (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
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)$.
open Microsoft.Quantum.Math;to the code before the operation definition.
%kata T205_Hardy_State
operation Hardy_State (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Input: $N = 2^k$ qubits in the $|0 \dots 0\rangle$ state.
Goal: Change the state of the qubits to the W state - an equal superposition of $N$ basis states on $N$ qubits which have Hamming weight of 1.
For example, for $N = 4$ the required state is $\frac{1}{2}\big(|1000\rangle + |0100\rangle + |0010\rangle + |0001\rangle\big)$.
%kata T206_WState_PowerOfTwo
operation WState_PowerOfTwo (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.
Input: $N$ qubits in the $|0 \dots 0\rangle$ state ($N$ is not necessarily a power of 2).
Goal: Change the state of the qubits to the 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)$.
%kata T207_WState_Arbitrary
operation WState_Arbitrary (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Superposition Workbook.