Put the whole three-qubit bit-flip code together and test it against different kinds of damage. Starting from a one-qubit state state = [a, b]:
- Encode. Start from (a|0\rangle + b|1\rangle)|00\rangle (qubit 0 holds the state) and apply CNOT(0, 1), then CNOT(0, 2). This gives a|000\rangle + b|111\rangle.
- Damage. Apply the gates of
errorsin order. Each error is a pair(kind, k):("x", k)applies X to qubitk(flips bitkof every basis index),("z", k)applies Z to qubitk(negates every amplitude whose index has bitkset). - Syndrome. Measure s_1 = b_0 \oplus b_1 and s_2 = b_1 \oplus b_2, where b_0 b_1 b_2 are the bits of any basis state with a nonzero amplitude (after these errors all of them give the same two bits).
- Repair. Apply X to the qubit this syndrome blames under the assumption that at most one qubit was flipped: none for (0, 0), qubit 0 for (1, 0), qubit 1 for (1, 1), qubit 2 for (0, 1).
- Decode. Apply CNOT(0, 1) and CNOT(0, 2) again. Qubits 1 and 2 are now back in |00\rangle.
Write bit_flip_round_trip(state, errors) that returns the final state of qubit 0: the two amplitudes of |000\rangle and |100\rangle (indices 0 and 4). With no error or one bit flip the result is the original state; find out what happens otherwise.
Conventions: amplitude i belongs to basis state i; qubit 0 is the leftmost bit, so |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], errors = [("x", 1)]
Output: [0.6, 0.8]
Explanation: one flip, syndrome (1, 1), repaired.
Input: state = [0.6, 0.8], errors = [("x", 0), ("x", 2)]
Output: [0.8, 0.6]
Explanation: 0.6|101> + 0.8|010> has syndrome (1, 1); the repair flips qubit 1 and makes 0.6|111> + 0.8|000>.
Input: state = [0.6, 0.8], errors = [("z", 2)]
Output: [0.6, -0.8]
Amplitudes are compared with a tolerance of 1e-6. Any global phase is accepted: [-0.6, -0.8] is the same state as [0.6, 0.8], but [0.6, -0.8] is a different state.
Goals
- Run the whole bit-flip code: encode, damage, syndrome, repair, decode
- See that two bit flips turn into one logical flip, and that a phase flip passes straight through