Skip to main content

Qiskit Topological Codes

Project description


Qiskit Topological Codes



qtcodes is published on PyPI. So, to install, simply run:

pip install qtcodes

This will install a precompiled version of qtcodes into your python environment.

Building from source

To build qtcodes from source, pip install using:

git clone
cd qtcodes
pip install --upgrade .

To check if the installation was successful, run:

>>> import qtcodes as qtc


Quantum computation is an inherently noisy process. Scalable quantum computers will require fault-tolerance to implement useful computation. There are many proposed approaches to this, but one promising candidate is the family of topological quantum error correcting codes.

Currently, the qiskit.ignis.verification.topological_codes module provides a general framework for QEC and implements one specific example, the repetition code. Qiskit Topological Codes builds out the qtcodes module into a diverse family of QEC encoders and decoders, supporting the repetition code, XXXX/ZZZZ (XXZZ) rotated surface code, and the XZZX rotated surface code.

Inspired by the Qiskit Textbook, we've written a full set of jupyter notebook tutorials to demonstrate the circuit encoders, graph decoders, and benchmarking tools that compose Qiskit Topological Codes. These tutorials both demonstrate the elegance of QEC codes as well as the utility of this package -- please check them out!


surface code teaser

Fig 1. Rotated XXXX/ZZZZ (XXZZ) Surface Code. ZZZZ/ZZ syndromes in red, XXXX/XX syndromes in purple, physical errors in green, and syndrome hits in yellow.

Topological QEC codes disperse, and thus protect, one quantum bit of logical information across many physical qubits. The classical repetition code distributes 1 bit of logical information across multiple imperfect physical bits (e.g. logical 0 is 000...0 and logical 1 is 111...1). In the classical repetition logical 0 bit, for example, a few physical bits may flip to 1, but the majority will very likely stay in 0, thus preserving the logical 0 bit. Similarly, the surface code protects one logical qubit in a grid of imperfect physical qubits against Pauli errors.

The qtcodes module can be broken down into circuits (encoders) and fitters (decoders). Additionally, unittests can be found in tests and benchmarking tools in qtcodes/tools.

The rotated surface code is based on the earlier theoretical idea of a toric code, with periodic boundary conditions instead of open boundary conditions. This has been shown to be largely identical, but embedding a surface code on an actual device is much easier.


The qtcodes.circuits sub-module contains classes such as XXZZQubit, XZZXQubit, and RepetitionQubit, which each allow users to construct and manipulate circuits encoding one logical qubit using a particular QEC code.

For example, we can create and apply a logical X onto a RepetitionQubit as follows

from qtcodes import RepetitionQubit
qubit = RepetitionQubit({"d":3},"t")
qubit.draw(output='mpl', fold=150)

Repetition Code Qubit

qtcodes.circuits.circ also allows users to create TopologicalRegisters (treg: a collection of topological qubits) and TopologicalCircuits (tcirc: a circuit built using a treg), the analog of QuantumRegister and QuantumCircuit.

We can, for example, create a tcirc and treg out of two RepetitionQubits.

from qtcodes import TopologicalRegister, TopologicalCircuit
treg = TopologicalRegister(2, ctype="Repetition", params={"d": 3})
circ = TopologicalCircuit(treg)
circ.draw(output='mpl', fold=500)

Repetition Code TCirc

Learn more about circuits through encoder tutorials such as this one for the XXXX/ZZZZ rotated surface code.


Topological codes aim to build better (read: less noisy) logical qubits out of many imperfect physical qubits. This improvement is enabled by decoding schemes that can detect and thus correct for errors on a code's constituent physical qubits.

The Qiskit Topological Codes package leverages Minimum-Weight Perfect Matching Graph Decoding to efficiently correct logical qubit readout.

For example, we can decode the syndrome hits in Fig 1 and fine the most probable error chains (data qubit flips) corresponding to these syndrome hits.

#d: surface code side length, T: number of rounds
decoder = RotatedDecoder({"d":5,"T":1})
all_syndromes = {"X": [(0,1.5,.5),(0,.5,1.5)], "Z": [(0,0.5,0.5),(0,1.5,1.5),(0,1.5,3.5), (0,3.5,3.5)]}
matches = {}

for syndrome_key, syndromes in all_syndromes.items():
    print(f"{syndrome_key} Syndrome Graph")
    error_graph = decoder._make_error_graph(syndromes,syndrome_key)
    print("Error Graph")
    matches[syndrome_key] = decoder._run_mwpm(error_graph)
    matched_graph = decoder._run_mwpm_graph(error_graph)
    print("Matched Graph")
    print(f"Matches: {matches[syndrome_key]}")

In this way, Qiskit Topological Codes uses graph decoding to find and correct for the most probable set of errors (error chains).

The careful reader will notice that connecting syndrome hits in the most probable set of "error chains" does not uniquely specify the underlying physical qubits that underwent physical errors (i.e. there are multiple shortest paths between two syndrome hits). It turns out, by the nuances of how topological codes store logical information (i.e. codespace), in most cases the exact path across physical qubits doesn't matter when correcting for an error chain. Read more about this in this tutorial on Decoding for XXZZ Qubits!


Finally, the efficiency and efficacy of the Qiskit Topological Codes package is demonstrated through benchmark simulations achieving threshold for the Repetition, XXZZ, and XZZX topological codes. Here, threshold is defined as the maximum physical error rate (i.e. imperfection level of physical qubits) below which larger surface codes perform better than smaller surface codes.

Fig. 2 By simulating circuits with errors inserted between two rounds of stabilizing measurements, we are able to extract a logical error rate for each code for a given physical error rate (quality of physical qubit) and surface code size. In particular, threshold is shown for the repetition code (left), XXZZ code (center), and XZZX code (right).

Explore the benchmarking tools and simulations to see how the graphs in Fig. 2 were created.

Future Directions

Checkout issues to see what we are working on these days!


Core Devs: Shantanu Jha, Jessie Chen, Aaron Householder, Allen Mi

Thanks to our mentor James Wootton (IBM) for invaluable feedback and support since the inception of this project at the IBM Qiskit - Summer Jam Hackathon 2020.

Thanks also to Matthew Treinish from the retworkx team for helping onboard and support this project.

Alums: Henry Liu, Shraddha Singh, Will Sun, Andy Ding


Here's some reading material that we found particularly useful:

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for qtcodes, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size qtcodes-0.1.1.tar.gz (31.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page