How the code works
The logical qubitα|0⟩ + β|1⟩ is encoded as:
α|100⟩ + β|011⟩. The syndrome measurements detect this without revealing α or β — they only identify which qubit flipped.
Error correction walkthrough
Create the logical qubit
Allocate a qubit in the state This is the qubit whose state we want to protect.
(0.2, 0.8) — unnormalized amplitudes that the simulator normalizes to 0.4472|0⟩ + 0.8944|1⟩.Encode into three physical qubits
Add two ancilla qubits After encoding:
q1 and q2 in |0⟩ and CNOT from q0 to each. This spreads the logical state across all three qubits.α|0⟩ → α|000⟩(CNOT copies 0 to q1 and q2)β|1⟩ → β|111⟩(CNOT copies 1 to q1 and q2)
α|000⟩ + β|111⟩.Introduce a bit-flip error on q0
Simulate a hardware error by applying X to The state is now
q0. This flips the first physical qubit.α|100⟩ + β|011⟩.Add syndrome measurement qubits
Add two fresh ancilla qubits
q3 and q4 in |0⟩. These will hold the error syndrome.Compute the error syndrome
Apply CNOTs to entangle the data qubits with the syndrome qubits. The syndrome circuit compares pairs of data qubits without reading their values.The syndrome table is:
q3measures the parity ofq0 XOR q1q4measures the parity ofq1 XOR q2
q3 and q4:| m3 | m4 | Flipped qubit |
|---|---|---|
| 1 | 0 | q0 |
| 1 | 1 | q1 |
| 0 | 1 | q2 |
| 0 | 0 | no error |
Apply conditional correction
Use the syndrome bits to conditionally apply X to the flipped qubit:Since the error was on
q0 (m3 = 1, m4 = 0), the first CondX fires and flips q0 back. The state returns to α|000⟩ + β|111⟩.Decode the logical qubit
Reverse the encoding CNOTs to disentangle After decoding,
q0 from q1 and q2.q0 holds the original logical state and q1, q2 return to |0⟩.