# How can I visualize a stim circuit?

Is there any function I can call to visualize a stim circuit?

There's not an "official" viewer, but there's a few different things you can do. They're all a bit janky.

You can combine the stimcirq and cirq_web packages to get a 3d visualization. This works much better if you're providing coordinate metadata for the qubits:

import stim
import stimcirq
import cirq_web

stim_circuit = stim.Circuit("""
QUBIT_COORDS(0, 0) 0
QUBIT_COORDS(0, 1) 1
QUBIT_COORDS(1, 0) 2
QUBIT_COORDS(1, 1) 3
R 0 1 2 3
H 0 2
CNOT 0 1
CNOT 2 3
M 0 1 2 3
""")

cirq_circuit = stimcirq.stim_circuit_to_cirq_circuit(stim_circuit)
cirq_web.Circuit3D(cirq_circuit).generate_html_file(
file_name="stim_circuit_viewer.html",
open_in_browser=True,
)


The 3d view can be a bit cumbersome. The perspective makes it hard to see the exact locations of operations. But it gives you a good overview of the general shape of the circuit.

In my own projects I've also written viewers that turn the circuit into a series of timeslice svg images. These tend to be specific to my needs, so it'll do things like not show noise and won't work for all operations. But maybe it's good enough for your needs:

https://github.com/Strilanc/honeycomb-boundaries/blob/dfb1f8f78f6db2aafffd024aa643c0b59d341377/src/hcb/tools/gen/viewer.py#L388

• I notice when I have detectors in a stim circuit, the 3d visualization of cirq_web can not work properly. Is that true I must delete all the detectors for visualization with cirq_web?
– Inm
Apr 1 at 6:33
• @Inm That sounds like a bug in cirq_web to me; it should be ignoring operations with no targets. But yes, filtering out the bad instructions would avoid the problem. Apr 1 at 8:21
• @Inm I reported the bug: github.com/quantumlib/Cirq/issues/5172 Apr 1 at 8:28

You can use stimcirq to convert your Stim circuit into a Cirq circuit then visualize it:

import stim
import stimcirq

cirq_circuit = stimcirq.stim_circuit_to_cirq_circuit(stim_circuit)
print(cirq_circuit)