Skip to main content
Create your own
Lesson illustration

Implementing the 3-Qubit Bit-Flip Code

Hello! Welcome back to our exploration of quantum error correction.

In our last lesson, we derived the Knill-Laflamme theorem, which provides the abstract necessary and sufficient conditions for a quantum code to be correctable. We learned that error correction is possible if and only if the errors do not reveal any information about the logical state they corrupt.

Today, we will move from this abstract theory to concrete practice. Our goal is to implement the 3-qubit bit-flip code, the canonical first example of a quantum error-correcting code. We will walk through the three essential stages of any such code:

  1. Encoding: Storing the information of one logical qubit across three physical qubits.
  2. Syndrome Measurement: Detecting if and where a bit-flip error has occurred, without collapsing the precious quantum state.
  3. Correction: Applying a targeted operation to reverse the error and restore the original logical state.

This lesson will bridge the gap between the formal conditions we've learned and a working quantum algorithm.

1. Encoding: Redundancy is Key

The core idea behind error correction is redundancy. We protect a fragile state by encoding it into a larger, more robust system. For the bit-flip code, we encode a single logical qubit into a three-qubit state. The logical basis states, which we denote with a bar, are mapped as follows:

A general logical state is therefore encoded into the entangled state:

This is a Greenberger–Horne–Zeilinger (GHZ) state. It's crucial to note that this is an entangled state, not three copies of the original qubit. The no-cloning theorem forbids making multiple copies of an unknown quantum state.

How do we create this state? The circuit is remarkably simple. We start with our data qubit in the state and two ancillary qubits in the state. A sequence of two CNOT gates then generates the desired entangled state.

Stabilizer codes for quantum error correction

The PennyLane tutorial, which we will use for implementation, provides a clear animation of this encoding circuit. Watch this brief segment to see how it works.

Watch the segment titled 'Qubit encoding' to see the circuit diagram and the corresponding PennyLane code.

Three-qubit bit-flip encoding circuit

This diagram from the lecture notes by A. Quillen shows the encoding circuit. An initial state on the top qubit is encoded into a three-qubit entangled state using two CNOT gates with the first qubit as control.

2. Syndrome Measurement: The Art of Diagnosis

Now, suppose our encoded state is subjected to noise. We'll consider a simple noise model where at most one of the three qubits undergoes a bit-flip, which corresponds to a Pauli gate. There are four possibilities:

  1. No error: The state remains .
  2. Error on qubit 0: .
  3. Error on qubit 1: .
  4. Error on qubit 2: .

The challenge is to determine which of these four situations has occurred without measuring the data qubits directly, as that would collapse the superposition and destroy the logical information encoded in and .

The solution is syndrome measurement. We perform a measurement that reveals information about the error (the "syndrome") but nothing about the underlying logical state. There are two powerful ways to think about this.

Perspective 1: Parity Checks (The Circuit View)

Intuitively, we can detect a bit-flip by checking if the qubits are still in agreement. In the error-free states and , the parity (or XOR sum) of any two bits is 0. A single bit-flip will change the parity for any pair involving the flipped bit.

We can measure these parities without collapsing the state by using two ancillary qubits. For example, to check the parity of qubit 0 and qubit 1, we can CNOT them onto an ancilla. The ancilla's final state will tell us the parity. By measuring the parities of, say, (qubit 0, qubit 1) and (qubit 1, qubit 2), we can uniquely identify which qubit, if any, was flipped.

PHY265 Lecture notes: Introducing Quantum Error Correction

The Rochester lecture notes provide a clear circuit diagram and explanation for syndrome extraction using ancilla qubits.

Please read section 2.1, starting from 'How would we detect this kind of error?' up to the end of the table titled 'Syndrome computation circuit for a Bit Flip Error'. This will introduce the syndrome extraction circuit and the resulting syndrome-to-error mapping.

The key idea is that the measurement outcomes of the two ancilla qubits form a 2-bit syndrome that uniquely identifies the error, as summarized in the table from the notes.

Perspective 2: Stabilizer Measurements (The Operator View)

Given your physics background, you'll find this more formal perspective powerful. It forms the basis of the stabilizer formalism, a cornerstone of modern quantum error correction.

The code space can be defined as the set of states that are "stabilized" by (i.e., are +1 eigenvectors of) a set of commuting Pauli operators. For the bit-flip code, these operators are:

You can easily verify that and for any .

When an error occurs, it may commute or anti-commute with these stabilizers.

  • If an error commutes with a stabilizer , then . The eigenvalue remains +1.
  • If an error anti-commutes with a stabilizer , then . The eigenvalue flips to -1.

By measuring the eigenvalues of and , we obtain a syndrome that identifies the error. For example, an error anti-commutes with both and . A measurement of these stabilizers would yield the syndrome , pinpointing the error on the first qubit (using 0-based indexing).

PHY265 Lecture notes: Introducing Quantum Error Correction

The Rochester notes provide a concise derivation of this operator-based syndrome extraction.

Please read the subsection 'Projection operators and measurements for detecting bit-flip errors' and the subsequent table 'Syndrome for the 3-bit bit flip error code'. Focus on how the commutation relations between the error operators (X1, X2, X3) and the stabilizer generators (Z1Z2, Z2Z3) determine the syndrome.

The parity-check circuit we saw earlier is, in fact, the physical implementation for measuring these stabilizer operators. Each pair of CNOTs onto an ancilla followed by a measurement of that ancilla constitutes a measurement of one stabilizer generator.

Test your understanding!

Based on the stabilizer measurement approach, what syndrome (i.e., pair of eigenvalues for and ) would you expect if an error occurs?

Hint: Check if commutes or anti-commutes with and .

Show answer

Let's check the commutation relations:

  1. For : acts on a different qubit from and , so it commutes with both. Therefore, . The eigenvalue for remains +1.
  2. For : commutes with (different qubits) but anti-commutes with (Pauli matrices on the same qubit anti-commute, e.g., ). Therefore, anti-commutes with . The eigenvalue for flips to -1.

The expected syndrome is (+1, -1). This uniquely identifies the error as .

3. Correction: Reversing the Damage

Once we have the syndrome, correction is straightforward. The syndrome tells us exactly which qubit was flipped. Since the bit-flip error is a Pauli gate, and , we can correct the error by simply applying another gate to the identified qubit.

  • Syndrome says "error on qubit 0" apply .
  • Syndrome says "error on qubit 1" apply .
  • Syndrome says "error on qubit 2" apply .
  • Syndrome says "no error" do nothing.

This correction step is a conditional operation. Modern quantum processors and simulators can implement this using "mid-circuit measurements," where the outcome of measuring the ancilla qubits classically controls whether a subsequent gate is applied.

4. Implementation with PennyLane

Now let's translate this entire process into code. The PennyLane tutorial provides a complete, runnable implementation of the 3-qubit bit-flip code, using mid-circuit measurement and conditional operations for the correction step.

Your task is to implement this code. The tutorial will serve as an excellent guide.

Stabilizer codes for quantum error correction

This PennyLane tutorial provides the full code for implementing the three stages of the bit-flip code. We will use it as a reference to build our implementation.

Review the sections 'Qubit encoding', 'Error detection', and 'Error Correction'. Pay close attention to the qml.cond function, which implements the conditional logic for the correction step based on the measured syndrome bits m3 and m4.

Your Implementation Task

Using the tutorial as a guide, construct a single PennyLane QNode that performs the full error correction sequence:

  1. Encode an arbitrary state into three qubits.
  2. Manually inject a single bit-flip error on one of the three data qubits to simulate noise.
  3. Perform the syndrome measurement using two ancilla qubits.
  4. Use mid-circuit measurements and qml.cond to apply the appropriate correction gate.
  5. Return the final density matrix of the three data qubits.
  6. Finally, calculate the fidelity between the final, corrected state and the initial, error-free encoded state. You should find the fidelity to be 1.0, demonstrating perfect correction.

This exercise will solidify your understanding by having you build the complete circuit from the components we've discussed.

Conclusion

In this lesson, we have constructed our first quantum error-correcting code. We have seen how to use redundancy to protect a quantum state and how the principles of syndrome measurement allow us to detect and correct errors without destroying the logical information.

Key Takeaways:

  • The 3-qubit bit-flip code encodes one logical qubit into the three-qubit GHZ state .
  • Syndrome measurement is the key to non-destructive error detection. It can be viewed operationally as measuring parities with ancillas, or more formally as measuring the eigenvalues of stabilizer operators ().
  • The measured syndrome uniquely identifies a single bit-flip error.
  • Correction is achieved by applying a conditional Pauli gate on the qubit identified by the syndrome.

Preview of the next lesson:
The bit-flip code is a great start, but it's incomplete. It protects against errors but is completely vulnerable to phase-flip errors ( errors). In the next lesson, we will analyze the 3-qubit phase-flip code. We will discover that it is intimately related to the bit-flip code through a simple change of basis, moving us one step closer to building a code that can handle all types of single-qubit errors.

Can't find a good explanation? Sign up and we'll make it for you

Sign up