I'm trying to save and load large circuits from files in Stim. For example, if I use a generated surface code from the intro notebook

import stim

circuit = stim.Circuit.generated(

The way I would currently save this is with

with open('saved_circuit.stim', 'w') as f:

And then read it with

circuit_file = open('saved_circuit.stim', 'r')
circuit_text = circuit_file.read()

circuit = stim.Circuit()

This works fine and all, but I got the feeling that because they went through the trouble of defining the stim circuit file format (.stim), it seemed that there would be simple methods like circuit.save('filename.stim') or circuit.load('filename.stim'). But I'm not having any luck finding any methods of the like. Right now my only solution is to define functions that do this, which is not a challenge, but just wondering if it's already an option.


1 Answer 1


In stim v1.9+ there's stim.Circuit.to_file, stim.Circuit.from_file, stim.DetectorErrorModel.to_file, stim.DetectorErrorModel.from_file.

As of stim v1.8 there aren't methods that take a path to read/write a circuit from.

The __str__ method is guaranteed to produce the file format specified in that document, and the constructor is guaranteed to parse its argument as that format.

  • $\begingroup$ I suppose at some moment of finding time, I could do a merge request with these methods, since it seems trivial to implement as a simple wrapper. Might turn out to be a more delicate issue! Thanks for the response $\endgroup$
    – Bebotron
    Commented Jun 15, 2022 at 18:27

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.