Skip to main content
In this tutorial, we will simulate a magic state distillation protocol using Tsim. We will simulate the magic state distillation circuit with 85 qubits introduced in Rodriguez et al. (2025). This showcases Tsim’s ability to simulate large non-Clifford circuits that would be impossible to simulate using a state vector simulator.

Magic states

We start by preparing a particular magic state, the eigenstate of the “SH” operator. To simulate noise, we add a depolarizing channel after state preparation:
By undoing the state preparation at the end, we can measure the infidelity:
Output
The noisy state preparation process produces a magic state with 97% fidelity.

Magic state distillation: The ideal case

Given multiple noisy magic states of low fidelity, we can distill a single high-fidelity magic state using a distillation circuit:
In this distillation circuit, qubits [q1, q2, q3, q4] are the distillation syndrome. If we measure them in the state [1,0,1,1], we know that distillation was successful and the first qubit q0 is in a magic state with higher fidelity. We can simulate this using Tsim. To measure the fidelity, we undo the magic state preparation on the first qubit and perform a measurement in the Z basis.
Output
After distillation, the fidelity has increased to 99.3%. This is much better than the original 97% of the individual input magic states. A downside is that the distillation process is only successful 14% of the time. This means we have to repeat the distillation process multiple times until we get a good output state. With Tsim, we can study other properties of the distillation circuit. For example, we can analyze the dependence of the fidelity on the rotation angle of the R_X gate:
Notebook output figure We see that the output fidelity and post-selection rate are peaked at the distillation angle θ\theta^*.

Magic state distillation: The realistic case (with noise)

On a real quantum computer, the distillation circuit is noisy itself. Here, the benefit of distillation is generally outweighed by the noise in the distillation circuit. We can explicitly see this using a simulation, where we add noise to the distillation circuit. Note that we consider two different noise scales:
  • p is the noise that captures the magic state preparation process.
  • noise is the strength of the depolarizing noise in the distillation circuit.
Output
We see that the addition of noise has reduced the output fidelity to ~96%. This is worse than the input state!

Encoding the distillation circuit

To address this issue, we need to make our distillation circuit resilient to noise. We do this by encoding it using a quantum error correction code, specifically the [17,1,5] color code. The helper class ColorEncoder5 provides a convenient way to encode the distillation circuit. Each qubit will be encoded into 17 physical qubits using an encoding circuit. After that, each gate of the distillation circuit will be applied transversally to the encoded qubits.
Output

The fully encoded protocol

The full circuit now contains 85 qubits: 5 logical qubits, constructed from 17 physical qubits each. Let us summarize the protocol we are going to simulate:
  • Prepare 5 encoded magic states at 97% fidelity. The input magic states are noisy, and we assume the encoding process to be noiseless. While this is a simplification for this tutorial, it does not introduce any qualitative changes. We can think of this as an abstract preparation protocol that produces 5 encoded magic states, each with 97% fidelity.
  • Distill 5 encoded magic states into a single logical qubit. We use the transversal encoding of the distillation circuit shown above. This includes depolarization noise with strength noise.
  • Perform post-selection We will perform two stages of post-selection: (a) post-select on perfect stabilizers, and (b) post-select on the [1,0,1,1] distillation syndrome of logical bits.
  • Quantum state tomography We will perform quantum state tomography on the output logical qubit to estimate its magic state fidelity.
Output
It would be impossible to simulate an 85-qubit circuit using a state vector simulator. But with Tsim, we can compute hundreds of thousands of shots in a few seconds. Finally, we construct a density matrix from the measured logical expectation values:
Next, we compute a statevector for the expected magic state:
Then, we compute the fidelity as ψexpectedρψexpected\langle\psi_{expected}|\rho|\psi_{expected}\rangle:
Output
We find that the overall fidelity has been increased from 97% of the individual encoded magic states to 99% of the output logical qubit, despite the presence of noise in the distillation circuit. This is an exciting result of quantum error correction! This concludes our tutorial. We have learned:
  • how to model noisy magic states and simulate fidelities using Tsim
  • how to perform magic state distillation and post-selection
  • how to encode a distillation circuit using the [17,1,5] color code
  • how to simulate hundreds of thousands of shots of this 85-qubit circuit within seconds on a laptop
  • how magic state distillation can improve the fidelity of noisy magic states, even when the distillation circuit is noisy itself