2

I made an svg diagram from a stim circuit:

import stim
circuit = stim.Circuit("""
    H 0
    CX 0 1
    M 0 1
""")
diagram = circuit.diagram("timeline-svg")
display(diagram)

enter image description here

I tried to write the diagram to a file, but I got an error:

with open('diagram.svg', 'w') as f:
    f.write(diagram)
TypeError: write() argument must be str, not stim._DiagramHelper

How do I save the diagram to an SVG image file?

Craig Gidney
  • 36,389
  • 1
  • 29
  • 95

1 Answers1

2

Do this:

with open('diagram.svg', 'w') as f:
    print(diagram, file=f)

Or this:

with open('diagram.svg', 'w') as f:
    f.write(str(diagram))

The stim._DiagramHelper class has a __str__ method that returns the contents of the SVG file. The diagram method could just return a string... but then tools like Jupyter notebook wouldn't display it as an image.

Craig Gidney
  • 36,389
  • 1
  • 29
  • 95