Skip to main content
Create your own
Lesson illustration

Phase-Flip Code: Basis Transformation and Bit-Flip Equivalence

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

In the last lesson, we successfully built and implemented the 3-qubit bit-flip code. We saw how to encode a logical qubit into a GHZ state and how to use stabilizer measurements () to detect and correct single bit-flip () errors.

However, our code is only half the story. While it diligently protects against bit flips, it is completely exposed to another fundamental type of error: the phase flip. Today, we will address this vulnerability. Our learning goal is to implement the 3-qubit phase-flip code and analyze its relationship to the bit-flip code via a change of basis.

We will discover a beautiful symmetry in quantum error correction: the phase-flip code is not a new invention from scratch, but rather the dual of the bit-flip code, accessible through a simple basis transformation. This concept of duality is a powerful theme you'll encounter repeatedly in quantum information.

1. The Vulnerability of the Bit-Flip Code

Let's start by confirming why our previous code fails. Recall the encoded state . Now, consider a phase-flip error ( gate) on the first qubit, . Its action is:

The error has flipped the relative phase between the and components, corrupting the logical state into .

Worse still, our syndrome measurement based on the stabilizers and fails to detect this. The error commutes with both stabilizers, so the syndrome measurement will report "no error," leaving the corruption in place.

2. The Duality of Errors: A Change of Basis

The key to solving this lies in the Hadamard gate (). As you know from your physics background, the Hadamard gate performs a change of basis between the computational basis (eigenstates of ) and the basis (eigenstates of ).

This basis change has a profound effect on the Pauli operators themselves. It swaps and errors:

This identity is the theoretical cornerstone of our lesson. It suggests a powerful strategy: if we want to correct a error, we can transform it into an error and use the machinery we already built for the bit-flip code.

3. Constructing the Phase-Flip Code

This "transform-and-correct" strategy leads directly to the construction of the 3-qubit phase-flip code.

Encoding

The encoding process follows three logical steps:

  1. Start with the logical qubit .
  2. Change its basis with a Hadamard gate: .
  3. Now, use the bit-flip repetition principle on this new basis:

The encoded logical states for the phase-flip code are therefore:

  • , where is the parity (sum of bits) of .

The full encoding circuit is simply the bit-flip encoding circuit, but with Hadamard gates applied to all data qubits both before and after the CNOTs.

PHY265 Lecture notes: Introducing Quantum Error Correction

The following section from the Rochester lecture notes by A. Quillen provides an excellent, concise explanation of the phase-flip code, its encoding, and its relation to the bit-flip code.

Please read section 2.2, 'Correcting phase flip errors with a three qubit code'. Pay close attention to Figure 6, which shows the encoding circuit, and how the Pauli Z gate acts on the |+> and |-> states.

Syndrome Measurement and Correction

Now, let's see what a phase-flip error does. A gate flips . So, a error on the -th qubit of turns it into, for example, . This is perfectly analogous to an error flipping a bit in .

Since the code itself is just the bit-flip code in the Hadamard basis, the stabilizers must also be the bit-flip stabilizers transformed into the Hadamard basis.

  • Bit-flip stabilizers: ,
  • Phase-flip stabilizers:

To detect a phase-flip error, we must measure the eigenvalues of the operators and . A error will anti-commute with one or both of these, flipping the measured eigenvalue from +1 to -1 and generating a non-trivial syndrome. The resource you just read contains a table detailing the syndromes for each error.

Correction is then straightforward: if the syndrome indicates a phase-flip on qubit , we apply another gate to reverse it, since .

Test your understanding!

A phase-flip error occurs. What is the expected syndrome from measuring the stabilizers and ?

Show answer

We check the commutation relations:

  1. For : The error commutes with (different qubits) but anti-commutes with (same qubit). Therefore, anti-commutes with . The measured eigenvalue will be -1.
  2. For : The error anti-commutes with but commutes with . Therefore, anti-commutes with . The measured eigenvalue will also be -1.

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

4. Implementation Task

Your task is to implement the full 3-qubit phase-flip code in PennyLane. You will adapt the code from our previous lesson. The core logic remains the same—encode, inject error, measure syndrome, correct—but the specific gates will change.

Here is your guide:

  1. Encoder: Construct the encoding circuit for the phase-flip code. This is the bit-flip encoder surrounded by Hadamard gates.

    H(0), H(1), H(2) -> CNOT(0,1) -> CNOT(0,2)

  2. Error Injection: Manually apply a Pauli gate to one of the three data qubits to simulate a phase-flip error.

  3. Syndrome Measurement: This is the most interesting part. How do you measure an operator like ? You can use the basis-change principle again!

    • To measure stabilizers , first apply Hadamard gates to all data qubits. This transforms the state into a basis where the operators become operators.
    • Now, you can use the exact same syndrome measurement circuit as you did for the bit-flip code (the one with two ancillas and CNOTs). The ancilla measurement outcomes will give you the syndrome for the phase-flip error.
  4. Correction: Based on the 2-bit syndrome measured from the ancillas, use qml.cond to apply a conditional Pauli gate to the correct data qubit.

  5. Verification: Just as before, construct a QNode that performs this entire sequence. Start with an arbitrary logical state (e.g., prepared by applying RY to the first qubit before encoding), and return the final density matrix of the three data qubits. Calculate the fidelity between the final state and the initial, error-free encoded state. If your implementation is correct, the fidelity will be 1.0.

This implementation exercise beautifully demonstrates the power of abstraction in quantum computing. By changing the basis, we can repurpose an entire circuit to solve a different, but dual, problem.

Conclusion

Today we have successfully constructed and analyzed the 3-qubit phase-flip code. We saw that it's not an entirely new concept but is deeply related to the bit-flip code we already knew.

Key Takeaways:

  • The 3-qubit bit-flip code is vulnerable to phase () errors, as they commute with the stabilizers.
  • The Hadamard gate provides a change of basis that swaps and operators ().
  • The phase-flip code is the bit-flip code transformed into the Hadamard basis. Its logical states are and .
  • Its stabilizers are and , and it corrects phase-flip errors by applying a conditional gate.

Preview of the next lesson:
We now have a code that corrects bit flips and another that corrects phase flips. But what about an arbitrary error, which can be a combination of both (like a error)? Neither code alone is sufficient.

In our next lesson, we will see how to combine these two ideas. This will first lead us to the 9-qubit Shor code, which concatenates the bit-flip and phase-flip codes. More importantly, it will motivate us to generalize our understanding beyond these specific examples into the elegant and powerful stabilizer formalism, which provides a unified framework for constructing and analyzing a vast family of quantum error-correcting codes.

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

Sign up