A qubit cannot be copied, but it can be spread out. Put the qubit a|0\rangle + b|1\rangle on wire 0, add n - 1 helper wires in |0\rangle, and apply CNOT(0, 1), CNOT(0, 2), ..., CNOT(0, n-1). Each CNOT copies the basis label of wire 0 onto its target, so the register ends in
This is the repetition code for qubits (n = 3 is the three-qubit bit-flip code). It is not n copies of the qubit: (a|0\rangle + b|1\rangle)^{\otimes 3} has 8 nonzero amplitudes in general, the code state only 2.
Write encode(state, n) that returns the 2^n amplitudes of the encoded register, where state = [a, b] is a normalised one-qubit state.
Conventions: amplitude i belongs to basis state i; qubit 0 is the leftmost bit, so in 3 qubits |100\rangle is index 4 and |011\rangle is index 3. CNOT(control c, target t) flips bit t of every basis index whose bit c is 1.
Examples
Input: state = [0.6, 0.8], n = 3
Output: [0.6, 0, 0, 0, 0, 0, 0, 0.8]
Input: state = [0.7071067811865476, 0.7071067811865476j], n = 2
Output: [0.7071067811865476, 0, 0, 0.7071067811865476j]
Input: state = [0, 1], n = 1
Output: [0, 1]
Explanation: with one wire there is nothing to spread to.
Amplitudes are compared with a tolerance of 1e-6. Any global phase is accepted: [0.6j, 0, 0, 0, 0, 0, 0, 0.8j] is the same state as the first answer.
Goals
- Encode $a|0\rangle + b|1\rangle$ as $a|0\dots0\rangle + b|1\dots1\rangle$ with CNOT gates
- See that the code state is not $n$ copies of the qubit