Lesson 14 of 15

Quantum Error Correction

Protecting Quantum Information

Quantum computers are fragile — qubits interact with their environment, causing decoherence and bit-flip errors. Quantum error correction protects information by encoding a single logical qubit into multiple physical qubits.

The simplest scheme is the 3-qubit bit-flip code:

  • Logical 0angle|0 angle is encoded as physical 000angle|000 angle
  • Logical 1angle|1 angle is encoded as physical 111angle|111 angle

If one of the three physical qubits flips, we can detect and correct the error by majority vote — two qubits always agree on the original value.

def encode(bit):
    return [bit, bit, bit]

def introduce_error(state, position):
    result = state[:]
    result[position] = 1 - result[position]
    return result

def decode_and_correct(state):
    return 1 if sum(state) >= 2 else 0

encoded = encode(0)             # [0, 0, 0]
with_error = introduce_error(encoded, 1)  # [0, 1, 0]
corrected = decode_and_correct(with_error)  # 0 — correct!

The code can correct any single-bit error but fails if two or more qubits flip.

Your Task

Implement encode(bit), introduce_error(state, position), and decode_and_correct(state).

Python runtime loading...
Loading...
Click "Run" to execute your code.