The Same Code, Turned Sideways

phase-flip code · Hadamard basis · syndrome · Pauli Z · error correction

The bit-flip code cannot see a Z gate (a phase flip, which negates every amplitude whose index has that qubit's bit set). The phase-flip code turns the bit-flip code sideways. With |+\rangle = (|0\rangle + |1\rangle)/\sqrt2 and |-\rangle = (|0\rangle - |1\rangle)/\sqrt2 it stores a|0\rangle + b|1\rangle as

a\,|{+}{+}{+}\rangle + b\,|{-}{-}{-}\rangle,

which is what you get by applying H to every qubit of a|000\rangle + b|111\rangle. Since HZH = X, a Z on one qubit of this state becomes, after H on every qubit, an X on the same qubit of a bit-flip code state.

The syndrome of the phase-flip code is measured that way: apply H to every qubit, then take s_1 = b_0 \oplus b_1 and s_2 = b_1 \oplus b_2 from the bits b_0 b_1 b_2 of any basis state with a nonzero amplitude, then apply H to every qubit again.

Write fix_phase_flip(state) that returns the repaired register a|{+}{+}{+}\rangle + b|{-}{-}{-}\rangle (8 amplitudes). state is a phase-flip code state with a Z on at most one qubit.

The tests build their inputs with the helper phase_code(qubit, z_on), which returns the 8 amplitudes of the code state of qubit = [a, b] with Z applied to qubit z_on (or no damage when z_on is None). You may use it with Run.

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. H = \frac{1}{\sqrt2}\begin{pmatrix}1&1\\1&-1\end{pmatrix}.

Examples

Input:  state = phase_code([1, 0], 1)
        (= [0.3535533905932738, 0.3535533905932738, -0.3535533905932738, -0.3535533905932738,
            0.3535533905932738, 0.3535533905932738, -0.3535533905932738, -0.3535533905932738])
Output: [0.3535533905932738, 0.3535533905932738, 0.3535533905932738, 0.3535533905932738,
         0.3535533905932738, 0.3535533905932738, 0.3535533905932738, 0.3535533905932738]
Explanation: |+++> with Z on qubit 1, repaired.

Input:  state = phase_code([0.7071067811865476, 0.7071067811865476], 2)
        (= [0.5, 0, 0, -0.5, 0, -0.5, 0.5, 0])
Output: [0.5, 0, 0, 0.5, 0, 0.5, 0.5, 0]

Input:  state = phase_code([0, 1], None)
Output: [0.3535533905932738, -0.3535533905932738, -0.3535533905932738, 0.3535533905932738,
         -0.3535533905932738, 0.3535533905932738, 0.3535533905932738, -0.3535533905932738]
Explanation: |---> is undamaged; its amplitude i has the sign (-1)^(number of 1 bits of i).

Amplitudes are compared with a tolerance of 1e-6. Any global phase is accepted: the negative of an answer is the same state.

Goals

  • Repair a phase flip by moving to the Hadamard basis, where it becomes a bit flip
  • Reuse the bit-flip syndrome and correction table in a rotated basis
Starting Python…