Fixing Quantum Mistakes: The Bit-Flip Error Correction Code
Project Overview:
Real quantum computers are noisy, tiny disturbances from heat, vibration, or stray electromagnetic fields can randomly flip a qubit's value by accident. Classical computers solve this kind of problem with simple backup copies, but quantum states cannot be copied due to the no cloning theorem, so quantum error correction needs a cleverer trick. In this project you will build the simplest quantum error correcting code, the bit-flip code, encode a single qubit's state across three physical qubits, deliberately introduce an error, and watch the circuit detect and fix it automatically without ever directly measuring, and therefore destroying, the protected information.
Materials Required:
A computer with internet access
A free Google account, to use Google Colab, no installation needed
Basic comfort reading Python
Background: How the Bit-Flip Code Actually Works
Instead of copying a qubit, which is impossible, the bit-flip code spreads one qubit's information across three qubits using entanglement, so all three become correlated without any individual qubit holding a readable copy of the state.
If random noise flips exactly one of the three qubits, the three qubits briefly disagree with each other. By comparing pairs of qubits using special check operations, called parity checks, the circuit can figure out exactly which qubit was flipped, without ever measuring the actual protected information itself. It only checks whether pairs agree or disagree, never what the value actually is. Once the faulty qubit is identified, a simple corrective gate flips it back, restoring the original protected state perfectly, as if the error never happened.
Step by Step Instructions
Set up your notebook.
Go to Google Colab at colab.research.google.com and create a new notebook.
Install Qiskit.
Type the following into a code cell and run it:
pip install qiskit qiskit-aer
Encode one logical qubit into three physical qubits.
Qubit 0 holds the original state we want to protect, we will spread it onto qubits 1 and 2.
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
qc = QuantumCircuit(5, 2)
qc.h(0)
qc.barrier()
qc.cx(0, 1)
qc.cx(0, 2)
qc.barrier()
Deliberately introduce an error on one qubit.
This simulates random noise flipping qubit 1 by accident, pretend this happens without anyone knowing it occurred.
qc.x(1)
qc.barrier()
Add the parity check qubits.
Qubits 3 and 4 are helper qubits used only to detect disagreement between the three data qubits, without revealing their actual values.
qc.cx(0, 3)
qc.cx(1, 3)
qc.cx(1, 4)
qc.cx(2, 4)
qc.barrier()
qc.measure(3, 0)
qc.measure(4, 1)
Run the circuit and check the syndrome.
simulator = AerSimulator()
result = simulator.run(qc, shots=1000).result()
counts = result.get_counts()
print("Syndrome measurement results:", counts)
Interpret the syndrome bits.
You should see a single consistent result across all 1000 shots, something like 01 or 10 depending on which qubit you flipped. This 2 bit syndrome acts like an address, telling you exactly which of the three data qubits was flipped, without ever measuring qubit 0, 1, or 2 directly. A result of 00 means no error, 01 or 10 point to specific qubits, and 11 points to the remaining one.
Apply the correction based on your syndrome.
Add this after confirming your syndrome result, using an x gate on whichever qubit the syndrome identified, in this case qubit 1, since that is the one we manually flipped.
qc.x(1)
Verify the fix by checking the data qubits agree again.
qc2 = QuantumCircuit(5, 3)
qc2.h(0)
qc2.cx(0, 1)
qc2.cx(0, 2)
qc2.x(1)
qc2.cx(0, 1)
qc2.cx(0, 2)
qc2.measure(0, 0)
qc2.measure(1, 1)
qc2.measure(2, 2)
result2 = simulator.run(qc2, shots=1000).result()
counts2 = result2.get_counts()
print("Data qubits after correction:", counts2)
Check the final result.
All three measured bits should now agree with each other on every single shot, either all 0 or all 1 together across each run, confirming the three qubits were successfully restored to matching, correct values after the deliberate error was detected and reversed.
Congratulations, you just built a real quantum error correction circuit, the same foundational idea, scaled up dramatically, that every real quantum computer needs in order to run long, reliable calculations despite constant background noise.
Fun Fact:
Modern quantum computers need far more sophisticated codes than this simple example, some leading approaches require around 1000 or more noisy physical qubits just to create a single reliable logical qubit, which is a major reason building large scale, fully error corrected quantum computers remains one of the biggest engineering challenges in the entire field.