Skip to main content
This tutorial showcases the basic functionality of Tsim. tsim is a circuit sampler for Clifford+T circuits, based on stabilizer rank decomposition and ZX-calculus techniques. It closely follows the API of stim and directly uses stim’s circuit format. In contrast to Stim, Tsim supports T and T_DAG instructions. The following circuit demonstrates this by preparing and measuring the state HT+=eiπ/8[cos ⁣(π8)0    isin ⁣(π8)1].H\,T\,|+\rangle = e^{i\pi/8}\Big[\cos\!\left(\tfrac{\pi}{8}\right)\,|0\rangle \;-\; i\,\sin\!\left(\tfrac{\pi}{8}\right)\,|1\rangle\Big].
To sample from this circuit, we first compile it into a sampler:
We can now sample bitstrings from the measurement instructions:
Output
Let’s run a large number of shots to estimate the probability of measuring 1.
Output
As expected, the probability is close to sin(π/8)20.1464\sin(\pi/8)^2 \approx 0.1464.

Detectors and Observables

Next, we consider a more complex example: an encoding circuit for the [[7,1,3]] Steane code. This circuit prepares the logical state 12[(1+eiπ/4)0ˉ+(1eiπ/4)1ˉ]\frac{1}{2}\Big[(1 + e^{i\pi/4})|\bar{0}\rangle + (1 - e^{i\pi/4})|\bar{1}\rangle\Big]
tsim supports multiple visualization methods. The default makes use of stim’s “timeline-svg” visualization function:
Tsim also supports visualization as a ZX diagram, where measurement vertices are annotated with rec[i], and detector and observable vertices are annotated with det[i] and obs[i], respectively.
To sample detection events and logical observables, we can compile a detector sampler, similar to stim.
Output
Since the circuit is just a logical encoding of the 1-qubit circuit from the beginning of the tutorial, the logical observable should behave exactly like the physical qubit in the first example, i.e., the observable should be 1 with probability sin(π/8)20.1464\sin(\pi/8)^2 \approx 0.1464. Additionally, since the circuit is noiseless, we should not observe any non-zero detection events:
Output

Adding Noise

A core capability of tsim is its support for Pauli noise channels. Let’s look at a simple example. We’ll insert a depolarizing channel DEPOLARIZE1(0.01) before the final stabilizer measurements.
In the ZX diagram, the noise is represented by parametrized vertices with binary parameters e0, e1, etc. Since a depolarizing channel either applies X, Y, Z gates, each channel requires two bits, i.e. an X and a Z vertex.
Now we compile the sampler for the noisy circuit.
Sampling from the noisy circuit, we expect to see some non-zero detector events.
Output
We again calculate the probability of measuring a logical 1ˉ|\bar{1}\rangle. Due to the noise, it deviates from the ideal value of sin(π/8)20.1464\sin(\pi/8)^2 \approx 0.1464.
Output

Error detection

One simple error correction strategy is post-selection: we discard any shots where a detector fired (indicating an error occurred). This effectively projects us back to the code space, but reduces the success rate (yield).
Output

Error correction

To actively correct errors, we need a decoder. A decoder takes the detector syndrome and predicts whether the observable should be flipped. In this example, we will use the tesseract decoder. After correction, we see that the the probability of getting a logical 1ˉ|\bar{1}\rangle is close to the ideal value.
Output
Output

Monte Carlo Simulation with sinter

tsim is compatible with sinter, a tool for performing large Monte Carlo simulations. We can use sinter to sample and decode over a range of physical error rates.
sinter provides a number of convenient plotting tools. Here, we use them to plot the observable flip rate as a function of the physical error rate. We observe that the decoded probability approaches the expected value of sin(π/8)2\sin(\pi/8)^2 much faster.
Notebook output figure