5
$\begingroup$

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

$\endgroup$

2 Answers 2

5
$\begingroup$

Since v1.10, you can use stim.Circuit.diagram to visualize the circuit.

import 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
""")

print(circuit.diagram())
q0: -QUBIT_COORDS(0,0)-R-H-@-M:rec[0]-
                           |
q1: -QUBIT_COORDS(0,1)-R---X-M:rec[1]-

q2: -QUBIT_COORDS(1,0)-R-H-@-M:rec[2]-
                           |
q3: -QUBIT_COORDS(1,1)-R---X-M:rec[3]-

You can also get an SVG image of the timeline:

circuit.to_diagram(type="timeline-svg")

In Jupyter notebooks this SVG will display inline. Otherwise you have to print it to a .svg file then open it.

enter image description here

or a 3d model, optionally embedded into a viewer that can be opened in a web browser:

# for this one you really want to have specified qubit coords
circuit.to_diagram(type="timeline-3d-html")

enter image description here

In Jupyter notebooks this viewer will display inline in the cell output. Otherwise you have to print it to a .html file then open it.

The diagram types that are currently available are:

    timeline-text
    timeline-svg
    timeline-3d
    timeline-3d-html
    match-graph-svg
    match-graph-3d
    match-graph-3d-html
    detector-slice-text
    detector-slice-svg

You can also use the stimcirq package to convert into a cirq circuit and get diagrams that way (e.g. with the cirq_web package).

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$
    – Inm
    Commented Apr 1, 2022 at 6:33
  • $\begingroup$ @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. $\endgroup$ Commented Apr 1, 2022 at 8:21
  • $\begingroup$ @Inm I reported the bug: github.com/quantumlib/Cirq/issues/5172 $\endgroup$ Commented Apr 1, 2022 at 8:28
2
$\begingroup$

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)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.