|q⟩ Bad Qubits

advanced · Programming · Implementing Error-Correcting Codes

Encoding Circuits

An encoding circuit is the unitary UencU_{\text{enc}} that maps an unprotected qubit onto a logical codeword. Every code in this module begins with one. Get the encoder right and the rest of the pipeline — syndrome extraction, decoding, logical gates — has something well-defined to act on.

What an encoder must do

A code is fixed by its two logical basis states ∣0L⟩|0_L\rangle and ∣1L⟩|1_L\rangle. The encoder is any unitary satisfying

Uenc ∣0⟩∣0⋯0⟩=∣0L⟩,Uenc ∣1⟩∣0⋯0⟩=∣1L⟩,U_{\text{enc}}\,|0\rangle|0\cdots0\rangle = |0_L\rangle, \qquad U_{\text{enc}}\,|1\rangle|0\cdots0\rangle = |1_L\rangle,

where the data qubit carries the input and the remaining qubits start in ∣0⟩|0\rangle. Because UencU_{\text{enc}} is linear, it then automatically encodes any superposition:

Uenc(α∣0⟩+β∣1⟩)∣0⋯0⟩=α∣0L⟩+β∣1L⟩.U_{\text{enc}}\bigl(\alpha|0\rangle + \beta|1\rangle\bigr)|0\cdots0\rangle = \alpha|0_L\rangle + \beta|1_L\rangle.

This is the whole reason a single fixed circuit protects an unknown state.

The bit-flip encoder

The three-qubit bit-flip code uses

∣0L⟩=∣000⟩,∣1L⟩=∣111⟩.|0_L\rangle = |000\rangle, \qquad |1_L\rangle = |111\rangle.

Its encoder copies the data qubit onto two partners with CNOTs:

cx(0,1); cx(0,2).\texttt{cx(0,1)};\ \texttt{cx(0,2)}.

Check it on basis inputs: ∣000⟩↦∣000⟩|000\rangle \mapsto |000\rangle and ∣100⟩↦∣111⟩|100\rangle \mapsto |111\rangle. By linearity α∣000⟩+β∣100⟩↦α∣000⟩+β∣111⟩\alpha|000\rangle + \beta|100\rangle \mapsto \alpha|000\rangle + \beta|111\rangle.

Encoding a non-trivial state

To make the exercise checkable we encode the logical ∣+L⟩|+_L\rangle. First prepare ∣+⟩|+\rangle on the data qubit with a Hadamard, then run the bit-flip encoder:

H0: ∣000⟩↦12(∣000⟩+∣100⟩),H_0:\ |000\rangle \mapsto \tfrac{1}{\sqrt{2}}(|000\rangle + |100\rangle), cx(0,1), cx(0,2): ↦12(∣000⟩+∣111⟩)=∣+L⟩.\texttt{cx(0,1)},\ \texttt{cx(0,2)}:\ \mapsto \tfrac{1}{\sqrt{2}}(|000\rangle + |111\rangle) = |+_L\rangle.

The two nonzero amplitudes are each 1/2≈0.7071/\sqrt{2} \approx 0.707. Note this is a genuinely entangled three-qubit state — measuring any one qubit instantly fixes the other two.

Order matters

Apply the Hadamard before the CNOTs. Doing it after would put the redundancy qubits into superposition and produce a different (and unprotected) state. An encoder is a specific sequence, not a bag of gates.

Try it

Build the bit-flip encoder for ∣+L⟩|+_L\rangle on three qubits. The grader checks the full statevector: amplitudes of ∣000⟩|000\rangle and ∣111⟩|111\rangle must each be 1/21/\sqrt{2} and all six other amplitudes must be zero.

Run your code to see the quantum state.

After running, the statevector panel should show exactly two bars, at indices 0 (000) and 7 (111), each at height ≈0.707\approx 0.707.

Sign in on the full site to ask questions and join the discussion.