3
$\begingroup$

I'm trying to create a Stim.Circuit, given a list of qubits and operations on them. Does the API support adding comments to the circuit's string representation? I've tried using circuit.append_from_stim_program_text('# Some comment'), but such comments never appear when I later call print(repr(circuit)), so I presume this method only cares about appending actual circuit instructions. Any help / workarounds appreciated!

$\endgroup$

1 Answer 1

3
$\begingroup$

No, the python API doesn't support adding comments. Neither does the underlying C++ API, actually. Comments are completely discarded by the parser, and the circuit struct has no fields for storing them or related concepts such as whitespace. This is unlikely to change because such things tend to involve performance sacrifices (eg. additional memory allocations to store the comments during parsing or additional branches to deal with a more complex circuit struct during simulation).

(Similar performance-driven limitations are that operation names get canonicalized and adjacent operations get fused. For example, parsing "SQRT_Z 0\nSQRT_Z 1" then printing it gives you "S 0 1".)

If you need to output a circuit that contains comments, you'll have to track the comments for yourself. For example, you could keep a dictionary mapping instruction indices to strings and then print the circuit instruction-by-instruction while appending comments from the dictionary:

from typing import Dict
import stim

def print_commented_circuit(circuit: stim.Circuit, line_comments: Dict[int, str]):
    for i, instruction in enumerate(circuit):
        line = str(instruction)
        if i in line_comments:
            line += "  # " + line_comments[i]
        print(line)


print_commented_circuit(
    circuit=stim.Circuit("""
        H 0
        CX 0 1
        M 0 1
    """),
    line_comments = {
        1: "entangle",
    },
)

Which outputs:

H 0
CX 0 1  # entangle
M 0 1
$\endgroup$
1
  • $\begingroup$ Ahh okay, will add a method like this to a wrapper class. Thanks for the quick reply! Love the package so far. $\endgroup$
    – user18859
    Nov 17, 2021 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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