Quantum Error Correction: Bit flip code in Qiskit

Interested in learning how to program quantum computers? Then check out our Qiskit textbook Introduction to Quantum Computing with Qiskit.

Introduction

Qubits are very fragile and are prone to errors due to decoherence and other quantum noise. However using quantum error correction qubit errors can be corrected.

In this tutorial you will see how a specific type of error called a bit flip error can be corrected using a quantum circuit known as the bit flip code.

What is a Bit Flip error?

A bit flip error is specific type of error where the qubits computational state flips from 1 to 0 or vice versa.

However a bit flip can be corrected using the bit flip code. This is a 3 qubit circuit that makes use of 2 ancillary qubits to correct 1 qubit.

Circuit diagram of the bit flip code

Circuit diagram of the bit flip code

The code works by first using CNOT gates to transfer the computational state of the main qubit to the other ancillary qubits. Then if an error occurs the first qubits state will be flipped. To correct the bit flip CNOT gates are applied to the ancillary qubits again and then a toffoli gate is applied to the first qubit which will correct its state.

For example let’s say the main qubits state was 0. CNOT gates will be applied to the ancillary qubits which will leave them unchanged since the main qubits state was 0.

Next a bit flip occurs which flips the main qubits state to 1. After the bit flip CNOT gates are applied to the ancillary qubits which will flip their states to 1 since the main qubits state is 1.

Then a toffoli gate is applied to the main qubit which will flip the state of the qubit since the ancillary qubits states are 1. This flips the state of the main qubit to 0 thus correcting the error.

Implementation

Circuit diagram of bit flip code with simulated error using a NOT gate

Circuit diagram of bit flip code with simulated error using a NOT gate

Step 1: Initialise the quantum and classical registers

The first step is to initialise a 3 qubit register . This is done by the following code:

q = QuantumRegister(3,'q')

Next we initialise the 1 bit classical register with the following code:

c = ClassicalRegister(1,'c')

Step 2: Create the circuit

Next we create quantum circuit using the following code:

circuit = QuantumCircuit(q,c)

Step 3: Apply a CNOT gate to ancillary qubits

Next we will need to transfer the state of the first qubit to the ancillary qubits. This is done using CNOT gates where the ancillary qubits are the targets and the first qubit is the control qubit.

This is done using the following code:

circuit.cx(q[0],q[1]) 
circuit.cx(q[0],q[2])

Step 4: Simulate a bit flip

To show that the circuit corrects bit flips lets simulate a bit flip error. This can be done by applying a NOT gate to the first qubit:

circuit.x(q[0]) #Add this to simulate a bit flip error 

Step 5: Again apply CNOT gates to ancillary qubits

This is done using the following code:

circuit.cx(q[0],q[1]) 
circuit.cx(q[0],q[2])

Step 6: Apply a Toffoli gate to the main qubit

This is done using the following code:

circuit.ccx(q[2],q[1],q[0])

Step 7: Measure the qubits

After this we measure the qubits.

This is done with the following code:

circuit.measure(q,c) 

How to run the program

  1. Copy and paste the code below in to a python file

  2. Enter your API token in the IBMQ.enable_account('Insert API token here') part

  3. Save and run

Code

from qiskit import QuantumRegister
from qiskit import QuantumRegister, ClassicalRegister
from qiskit import QuantumCircuit, execute,IBMQ
from qiskit.tools.monitor import job_monitor

print('\nBit Flip Code')
print('----------------')

IBMQ.enable_account('ENTER API KEY HERE')
provider = IBMQ.get_provider(hub='ibm-q')

backend = provider.get_backend('ibmq_qasm_simulator')

q = QuantumRegister(3,'q')
c = ClassicalRegister(1,'c')

circuit = QuantumCircuit(q,c)

circuit.cx(q[0],q[1])
circuit.cx(q[0],q[2])
circuit.x(q[0]) #Add this to simulate a bit flip error
circuit.cx(q[0],q[1])
circuit.cx(q[0],q[2])
circuit.ccx(q[2],q[1],q[0])
circuit.measure(q[0],c[0])

job = execute(circuit, backend, shots=1000)

job_monitor(job)

counts = job.result().get_counts()

print("\nBit flip code with error")
print("----------------------")
print(counts)
input()

Output

Output showing the main qubit has been corrected to 0

Output showing the main qubit has been corrected to 0