Basic Gates quantum kata is a series of exercises designed to get you familiar with the basic quantum gates in Q#. It covers the following topics:
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).
Most tasks in this kata can be done using exactly one gate. None of the tasks require measurement, and the tests are written so as to fail if qubit state is measured.
The tasks are given in approximate order of increasing difficulty; harder ones are marked with asterisks.
In all tasks you need to implement the transformation exactly, without introducing any global phase. You can read more about the global phase of quantum states here.
Microsoft.Quantum.Intrinsic
namespace and are listed here.Note that all operations in this section have
is Adj+Ctl
in their signature.
This means that they should be implemented in a way that allows Q# to compute their adjoint and controlled variants automatically. Since each task is solved using only intrinsic gates, you should not need to put any special effort in this.
Input: A qubit in state $|\psi\rangle = \alpha |0\rangle + \beta |1\rangle$.
Goal: Change the state of the qubit to $\alpha |1\rangle + \beta |0\rangle$.
Example:
If the qubit is in state $|0\rangle$, change its state to $|1\rangle$.
If the qubit is in state $|1\rangle$, change its state to $|0\rangle$.
Note that this operation is self-adjoint: applying it for a second time returns the qubit to the original state.
%kata T101_StateFlip
operation StateFlip (q : Qubit) : Unit is Adj+Ctl {
// The Pauli X gate will change the |0⟩ state to the |1⟩ state and vice versa.
// Type X(q);
// Then run the cell using Ctrl+Enter (⌘+Enter on macOS).
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
Input: A qubit in state $|\psi\rangle = \alpha |0\rangle + \beta |1\rangle$.
Goal: Change the state of the qubit as follows:
Note:
$|+\rangle$ and $|-\rangle$ form a different basis for single-qubit states, called X basis.
$|0\rangle$ and $|1\rangle$ are called Z basis.
%kata T102_BasisChange
operation BasisChange (q : Qubit) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
Input: A qubit in state $|\psi\rangle = \alpha |0\rangle + \beta |1\rangle$.
Goal : Change the qubit state to $\alpha |0\rangle - \beta |1\rangle$ (flip the sign of $|1\rangle$ component of the superposition).
%kata T103_SignFlip
operation SignFlip (q : Qubit) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
Inputs:
Goal: Change the state of the qubit as follows:
This is the first operation in this kata that is not self-adjoint, i.e., applying it for a second time does not return the qubit to the original state.
%kata T104_AmplitudeChange
operation AmplitudeChange (alpha : Double, q : Qubit) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
Input: A qubit in state $|\psi\rangle = \alpha |0\rangle + \beta |1\rangle$.
Goal: Change the qubit state to $\alpha |0\rangle + {\color{red}i}\beta |1\rangle$ (add a relative phase $i$ to $|1\rangle$ component of the superposition).
%kata T105_PhaseFlip
operation PhaseFlip (q : Qubit) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
Inputs:
Goal: Change the state of the qubit as follows:
%kata T106_PhaseChange
operation PhaseChange (alpha : Double, q : Qubit) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
Input: A qubit in state $|\psi\rangle = \beta |0\rangle + \gamma |1\rangle$.
Goal: Change the state of the qubit to $- \beta |0\rangle - \gamma |1\rangle$.
Note: this change on its own is not observable - there is no experiment you can do on a standalone qubit to figure out whether it acquired the global phase or not. However, you can use a controlled version of this operation to observe the global phase it introduces. This is used in later katas as part of more complicated tasks.
%kata T107_GlobalPhaseChange
operation GlobalPhaseChange (q : Qubit) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
The next three tasks deal with multi-qubit systems, though they still only require single-qubit gates. We recommend going through the MultiQubitSystems tutorial before attempting these.
Input: Two entangled qubits in Bell state $|\Phi^{+}\rangle = \frac{1}{\sqrt{2}} \big(|00\rangle + |11\rangle\big)$.
Goal: Change the two-qubit state to $|\Phi^{-}\rangle = \frac{1}{\sqrt{2}} \big(|00\rangle - |11\rangle\big)$.
%kata T108_BellStateChange1
operation BellStateChange1 (qs : Qubit[]) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
Input: Two entangled qubits in Bell state $|\Phi^{+}\rangle = \frac{1}{\sqrt{2}} \big(|00\rangle + |11\rangle\big)$.
Goal: Change the two-qubit state to $|\Psi^{+}\rangle = \frac{1}{\sqrt{2}} \big(|01\rangle + |10\rangle\big)$.
%kata T109_BellStateChange2
operation BellStateChange2 (qs : Qubit[]) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
Input: Two entangled qubits in Bell state $|\Phi^{+}\rangle = \frac{1}{\sqrt{2}} \big(|00\rangle + |11\rangle\big)$.
Goal: Change the two-qubit state, without adding a global phase, to $|\Psi^{-}\rangle = \frac{1}{\sqrt{2}} \big(|01\rangle - |10\rangle\big)$.
Look for a solution, possibly with multiple gates, which has a very similar transformation as the Y gate but without adding a global phase.
%kata T110_BellStateChange3
operation BellStateChange3 (qs : Qubit[]) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
In the following tasks the order of qubit states in task description matches the order of qubits in the array (i.e., $|10\rangle$ state corresponds to
qs[0]
in state $|1\rangle$ andqs[1]
in state $|0\rangle$).Note also that the states shown in test output use little-endian notation (similarly to
DumpMachine
), see this tutorial section for a refresher on endianness.
Input: Two unentangled qubits (stored in an array of length 2). The first qubit will be in state $|\psi\rangle = \alpha |0\rangle + \beta |1\rangle$, the second - in state $|0\rangle$ (this can be written as two-qubit state $\big(\alpha |0\rangle + \beta |1\rangle \big) \otimes |0\rangle = \alpha |00\rangle + \beta |10\rangle$.
Goal: Change the two-qubit state to $\alpha |00\rangle + \beta |11\rangle$.
Note that unless the starting state of the first qubit was $|0\rangle$ or $|1\rangle$, the resulting two-qubit state can not be represented as a tensor product of the states of individual qubits any longer; thus the qubits become entangled.
%kata T201_TwoQubitGate1
operation TwoQubitGate1 (qs : Qubit[]) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
Input: Two unentangled qubits (stored in an array of length 2) in state $|+\rangle \otimes |+\rangle = \frac{1}{2} \big( |00\rangle + |01\rangle + |10\rangle {\color{blue}+} |11\rangle \big)$.
Goal: Change the two-qubit state to $\frac{1}{2} \big( |00\rangle + |01\rangle + |10\rangle {\color{red}-} |11\rangle \big)$.
Note that while the starting state can be represented as a tensor product of single-qubit states, the resulting two-qubit state can not be represented in such a way.
%kata T202_TwoQubitGate2
operation TwoQubitGate2 (qs : Qubit[]) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
Input: Two qubits (stored in an array of length 2) in an arbitrary two-qubit state $\alpha |00\rangle + {\color{blue}\beta} |01\rangle + {\color{blue}\gamma} |10\rangle + \delta |11\rangle$.
Goal: Change the two-qubit state to $\alpha |00\rangle + {\color{red}\gamma} |01\rangle + {\color{red}\beta} |10\rangle + \delta |11\rangle$.
This task can be solved using one intrinsic gate; as an exercise, try to express the solution using several (possibly controlled) Pauli gates.
%kata T203_TwoQubitGate3
operation TwoQubitGate3 (qs : Qubit[]) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
Input: Two qubits (stored in an array of length 2) in an arbitrary two-qubit state ${\color{blue}\alpha} |00\rangle + {\color{blue}\beta} |01\rangle + \gamma |10\rangle + \delta |11\rangle$.
Goal: Change the two-qubit state to ${\color{red}\beta} |00\rangle + {\color{red}\alpha} |01\rangle + \gamma |10\rangle + \delta |11\rangle$.
This task can be solved using one library function; as an exercise, try to express the solution using several (possibly controlled) Pauli gates.
ControlledOnInt function generates a controlled variant of the given gate, which is applied when the control register is in a state corresponding to a non-negative integer. For example,
ControlledOnInt(0, H)([qs[0]], qs[1])
applies the Hadamard gate to the qs[1]
qubit when the control qubit qs[0]
is in the $|0\rangle$ state.ControlledOnInt(1, X)([qs[0]], qs[1])
applies the X gate to the qs[1]
qubit when the control qubit qs[0]
is in the $|1\rangle$ state (equivalent to CNOT(qs[0], qs[1])
).Notice that the
ControlledOnInt
function requires the control qubit(s) to be passed as an array. This function generates a gate controlled by multiple qubits. The argument gate is applied if the control register is in the basis state corresponding to the specified non-negative integer. The little-endian binary representation of this integer defines a bit mask, which is compared with the control register.A similar function
ControlledOnBitString
is introduced in the Multi-Qubit Gates tutorial.
%kata T204_TwoQubitGate4
operation TwoQubitGate4 (qs : Qubit[]) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
Input: Three qubits (stored in an array of length 3) in an arbitrary three-qubit state $\alpha |000\rangle + \beta |001\rangle + \gamma |010\rangle + \delta |011\rangle + \epsilon |100\rangle + \zeta|101\rangle + {\color{blue}\eta}|110\rangle + {\color{blue}\theta}|111\rangle$.
Goal: Flip the state of the third qubit if the state of the first two is $|11\rangle$, i.e., change the three-qubit state to $\alpha |000\rangle + \beta |001\rangle + \gamma |010\rangle + \delta |011\rangle + \epsilon |100\rangle + \zeta|101\rangle + {\color{red}\theta}|110\rangle + {\color{red}\eta}|111\rangle$.
%kata T205_ToffoliGate
operation ToffoliGate (qs : Qubit[]) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.
Input: Three qubits (stored in an array of length 3) in an arbitrary three-qubit state $\alpha |000\rangle + \beta |001\rangle + \gamma |010\rangle + \delta |011\rangle + \epsilon |100\rangle + {\color{blue}\zeta}|101\rangle + {\color{blue}\eta}|110\rangle + \theta|111\rangle$.
Goal: Swap the states of second and third qubit if and only if the state of the first qubit is $|1\rangle$, i.e., change the three-qubit state to $\alpha |000\rangle + \beta |001\rangle + \gamma |010\rangle + \delta |011\rangle + \epsilon |100\rangle + {\color{red}\eta}|101\rangle + {\color{red}\zeta}|110\rangle + \theta|111\rangle$.
%kata T206_FredkinGate
operation FredkinGate (qs : Qubit[]) : Unit is Adj+Ctl {
// ...
}
Can't come up with a solution? See the explained solution in the Basic Gates Workbook.