The three-qubit bit-flip code stores a qubit as a|000\rangle + b|111\rangle. A stray X gate (a bit flip) on one wire turns this into, for example, a|100\rangle + b|011\rangle. The code finds the damaged wire without looking at a or b: it measures two parity checks.
- Z_0Z_1 multiplies each basis state |b_0b_1b_2\rangle by (-1)^{b_0 \oplus b_1}; its bit s_1 is b_0 \oplus b_1 (
b0 ^ b1). - Z_1Z_2 multiplies it by (-1)^{b_1 \oplus b_2}; its bit s_2 is b_1 \oplus b_2.
When every basis state with a nonzero amplitude has the same two parity bits, the measurement gives those bits with certainty and leaves the state as it was. The pair (s_1, s_2) is the syndrome.
Write syndrome(state, flip) that first applies X to qubit flip (or does nothing when flip is None) and returns the syndrome (s_1, s_2) of the result as a pair of ints. state has 8 amplitudes and every basis state with a nonzero amplitude (above 1e-9 in size) has the same two parity bits, so the answer is well defined.
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. X on qubit k flips bit k of every basis index (in 3 qubits that is i ^ (1 << (2 - k))).
Examples
Input: state = [0.6, 0, 0, 0, 0, 0, 0, 0.8], flip = 0
Output: (1, 0)
Explanation: X on qubit 0 gives 0.6|100> + 0.8|011>; 1^0 = 1 and 0^0 = 0 (and 0^1 = 1, 1^1 = 0).
Input: state = [0.6, 0, 0, 0, 0, 0, 0, 0.8], flip = None
Output: (0, 0)
Input: state = [0, 0, 0.8j, 0, 0, -0.6, 0, 0], flip = None
Output: (1, 1)
Explanation: |010> and |101> both have b0^b1 = 1 and b1^b2 = 1.
Goals
- Apply an X error to one qubit of a register (big-endian order)
- Read the two parity checks $Z_0Z_1$ and $Z_1Z_2$ of a three-qubit state