QEC 101
Lab 5- gates and Magic State Distillation
Fault tolerant quantum computing (FTQC) requires a universal gate set from which any quantum algorithm can be implemented. The T-gate is a challenging gate to implement fault tolerantly, yet holds the key to unlocking the power of quantum computing.
A common procedure for producing T-gates is called magic state distillation (MSD), and will likely consume the lion's share of the resources necessary to realize FTQC. Much quantum research is currently directed at finding ways to efficiently implement MSD.
In a recent paper titled Experimental Demonstration of the Logical Magic State Distillation, researchers from QuEra, MIT, and Harvard showcased MSD on QuEra's neutral atom quantum processor.
Prerequisites:
If you have completed the previous labs in this series (1-4), you should have a good foundation for the basics of QEC and the fundamentals of stabilizer codes, and have already coded up the Steane code which is used in the QuEra MSD implementation.
This lab will introduce the importance of T-gates, how MSD works, and allow you to implement a version of it yourself.
The list below outlines what you'll be doing in each section of this lab:
5.1 Learn about the Clifford + gate set.
5.2 Learn how fault tolerant gates are applied.
5.3 Learn how magic state distillation works.
5.4 Learn how QuEra performed MSD in their recent experimental paper.
5.5 Write a CUDA-Q code to perform MSD distillation.
Terminology and notation you'll use:
universal gate set, T-gates, transversal gates
Eastin-Knill Theorem
magic state, magic state distillation, [[5,1,3]] code.
color code
To get started, run the cell below to install the prerequisite libraries and then restart the kernel.
💻 Just a heads-up: This notebook is designed to be run on an environment with a GPU. If you don't have access to a GPU, feel free to read through the cells and explore the content without executing them. Enjoy learning! ⭐
5.1 Clifford + Gates
So far in this QEC series, all of the QEC procedures have been contextualized as critical for realizing FTQC. However, FTQC is broader than QEC and involves careful consideration of all operations such that errors are not propagated through a computation. That is to say, implementing syndrome extraction of a QEC code correctly is a key step for preserving quantum memory, but still requires a set of FT operations (state prep, gate, measurement, etc) to perform computations in a way that ensures a single errors do not propogate uncontrollably.
For example, proper FT implementation of the Steane code would require logically encoded ancilla qubits to measure the stabilizers to make sure an ancilla qubit error does not become an uncorrectable error itself.
There are many aspects of FTQC that will not be covered here. However, the concept of a universal gate set needs to be introduced before the MSD protocol can be understood. A universal gate set is a set of quantum gates from which any unitary operation can be approximated to arbitrary precision. The is analogous to classical computing where any digital computation can be constructed from AND, NOT, and OR gates.
Certain gates, known as transversal gates, are nice to have in a gate set, because they are already fault tolerant. A transversal gate can be applied individually to every data qubit encoding a logical qubit such that if any of them have an error, it only affects the target data qubit. An example of a transversal get would be the operation in the Steane code. A logical is obtained by performing an on each data qubit and if any of those result in an error, it is correctable and will not propagate to the other data qubits.
So, all that is needed for FTQC to be easy is a universal gate set consisting only of transversal gates. Unfortunately, no such set exists. The Eastin-Knill Theorem proves that no quantum error-correcting code can implement a universal set of gates using only transversal operations.
The most common universal gate set is the Clifford + T gates. The Clifford gates, such as , , and CNOT, are the gates that map Pauli operators to other Pauli operators under conjugation. The gate, while not part of the Clifford group, is necessary to promote the Clifford gate set to a universal gate set. In most quantum error-correcting codes, the gate cannot be implemented transversally. In the case of the Steane code, the Clifford gates can be implemented transversally (see figure below), but the gate is not.

Consider why the -gate is necessary for the universal gate set. For any logical operation , it must transform all stabilizers into another .
does this fine as and .
does transform into a valid stabilizer, but not the stabilizers.
The gate ends up being the key to any advantage realized by quantum algorithms. This is because without gates, the other gates could only jump between the six pole states of the Bloch sphere, which can be simulated efficiently with classical computing. Explore this fact with the widget linked here. Notice how the T gate is the only operation capable of producing a non-pole state.
5.2 Fault Tolerant Gate Application
Thankfully, there is a way to implement a fault-tolerant gate, it is just difficult and resource intensive. The procedure involves the circuit below.

It begins with an encoded qubit (bottom wire) and an arbitrary logical qubit state (top wire). Then, the bottom wire is prepared in a so called magic state, . A fault tolerant CNOT gate is then applied with the magic state qubit as the control. The target qubit is then measured and if the result is a 0, was successfully applied to . If measurement produces a 1, then a FT SX gate is applied to the qubit and the result becomes applied to .
The "Prepare Magic State" subcircuit is deceptively simple. It consists of a single logical Hadamard gate followed by a noisy gate to form . This process may seem circular as a gate is required in the process to apply a fault tolerant gate. However, the procedure is a bit more nuanced. A "good enough" (high enough fidelity) magic state is the output of an onerous process called magic state distillation (MSD). MSD will be discussed more in the next section.
Another distinction is that the magic state can only be produced from an easy to prepare logical 0 state. Only by performing the fault tolerant circuit above, can the gate act on an arbitrary qubit state .
The math is instructive for why the above circuit works. After the CNOT is applied to the magic state and qubits, the resulting state is
Then, if the second qubit is measured (fault tolerantly) and is a 0, the intended state is prepared.
Otherwise, if 1 is measured, the following state is prepared.
In the latter case, the objective state can be obtained with a Clifford correction consisting of an and operation to flip the bits and amplitudes. such a process ensures that the gate is implemented fault tolerantly and will not propagate through the computation if it introduces an error.
5.3 Magic State Distillation
Preparing the magic state is a very resource intensive process. First, the process will be explained at a high level and then in terms of the details used in Experimental Demonstration of the Logical Magic State Distillation.
Magic states are produced by magic state factories which follow a procedure like the one pictured below. This process is decoupled from any quantum application and can be used to build and store magic states which are consumed as an application needs gates applied.
Select a QEC code to encode a collection of logical qubits
Prepare each logical qubit in a noisy state.
Apply a second MSD QEC code on the logical qubits. In the figure below, the [[5,1,3]] code used by QuEra is represented.
Measure all but one of the logical qubits to produce a syndrome to determine if the remaining qubit is in a good magic state or not. If the syndrome indicates no error, the process worked and proceed to 5), if an error is flagged, the process restarts at 1).
A less noisy (high fidelity) magic state has been prepared. It can now be used, or input into another round of MSD.

It should be noted that this is one approach to producing gates, but others exits.
Usually, one round of MSD is not enough, even if the resulting state does have higher fidelity. To fix this, the output from the first round can be combined with other output magic states and the procedure repeated. Each round exponentially grows the overhead required to produce a single magic state. This is likely the greatest challenge facing FTQC and will consume most of the resources for any quantum application.

5.4 Performing MSD with the Color Code
It is helpful to look at a concrete example of MSD in practice to better understand the above process. This section will follow the procedure from the QuEra paper and explain the steps they took, preparing you to code up a similar example in the following section.
Each magic state is prepared using the [[7,1,3]] color code, also known as the Steane code which was covered in the second lab of this QEC series. The image below shows the procedure which QuEra used based on the gate set of their neutral atom QPU. The bottom data qubit can be set to an arbitrary qubit state and will "inject" that state into the logical qubit. The circuit is slightly different from the circuit used to encode the Steane code in lab 2, but enforces equivalent stabilizer constraints.
In this case, the prepared state is a noisy state created by starting in the state and applying a rotation of about the (-1,1,0) axis.
This is repeated five times to create five logically encoded noisy magic states. They are then input into a [[5, 1, 3]] code which produces one magic state with high fidelity from five low fidelity ones. This is an example, like the Shor code, where QEC codes are concatenated. That is to say, the logical qubits are themselves used in a QEC code. The circuit below demonstrates what logical operations need to be performed on each logical qubit to complete the MSD process.
All but the first logical qubit are measured to produce as syndrome. If the correct syndrome (which can vary by implementation) is measured, a magic state with less error has been produced. QuEra's experimental results found that a raw logically encoded magic state had a fidelity of 94.1% which increased to 98.9% when postselected based on syndrome measurements, demonstrating the procedure worked.
Exercise 1:
Write a simple script to calculate the number of physical qubits needed to perform rounds of MSD given an arbitrary QEC code and MSD procedure. Assuming the [[5,1,3]] code is used for the MSD, plot how many data qubits are needed for up to 5 rounds of MSD when the Steane code is used and the other [[17,1,5]] color code shown below, also used by QuEra in the paper.
Is it clear why FTQC is so hard? This entire process must happen for every single gate applied in a quantum circuit, which could require millions or more gates.
5.5 Using CUDA-Q to perform MSD
This section will walk you through an example using CUDA-Q to perform MSD. You will be coding the [[7,1,3]] code and following the MSD protocol of Bravyi and Kitaev. Because this procedure only requires seven qubits, it can be simulated with a state vector simulator. However, remember that in practice each qubit would be a logical qubit and the procedure below would need to be performed with logical operations.
Exercise 2:
The first step of this protocol is to prepare a kernel that produces a noisy state.
Where . Let the kernel take a single float as an input which applies an error to . The code underneath the kernel creates a list of perturbed s over a suggested range.
Exercise 3:
Next, initialize a perfect state and a second state with some error (It is suggested to use element 30 from the list above to get a noticeable amount of noise but still allows MSD to work). Use CUDA-Q's and commands to compute the fidelity of the noisy state and visualize both states using the Bloch spheres below. Note that fidelity is the overlap squared.
Exercise 4:
Now prepare a kernel to perform the [[5,1,3]] QEC code. The kernel should prepare each qubit as a noisy state, all with the same angle perturbation. Then, implement the circuit below. This circuit essentially maps the stabilizer measurements of the code to qubits 1,2,3 and 4 which need to be measured to produce a syndrome. Note that this version of the [[5,1,3]] code is slightly different from the version used by QuEra, but is more amenable to gate operations native to CUDA-Q. It is the implementation from A study of the robustness of magic state distillation against Clifford gate faults.
One caveat to this code. It actually produces a distilled . Add a Pauli Y operation followed by a Hadamard gate to the first qubit to convert back to . Play around with this in the Bloch spheres above to understand what the transformation does.
Now, select an entry from the perturbed angle you created above and use it to run the code below. This will produce ten shots and for each print the syndrome, state, and fidelity with respect to the perfect state. How does the initial fidelity compare to the results? For results with improved fidelity, what is the resulting syndrome? If you do not get a 0000 syndrome, rerun the code. Take one of these improved states and visualize it using the Bloch sphere. Notice how it is closer to the perfect state than the state you started with.
Exercise 5:
Now, build a loop below that performs the MSD protocol for initial states corresponding to each error in the initial list of 's. Plot this against the line to determine the threshold required for the input state? That is, the minimum fidelity of the input state such that the procedure works and does not produce worse results. From the graph, estimate how many rounds of MSD would be needed (assuming the same procedure) to distill a state above .98 starting from .94.
Conclusion
after completing this notebook you should now have a sense of how MSD works, why it is needed, and what makes it so challenging. As you continue with QEC 101 content, remember that there are many aspects of FTQC beyond simply the QEC code selected.