Stim icon indicating copy to clipboard operation
Stim copied to clipboard

Add stim.Circuit.diagram

Open Strilanc opened this issue 1 year ago • 0 comments

Attempt at signature (oh boy it's a big one):

def stim.Circuit.diagram(
    self,
    *,
    style: Literal['timeline', 'layers', '3d'],
    format: Literal['ascii', 'utf-8', 'katex', 'svg', 'html', 'gltf'],
    file: Union[None, str, pathlib.Path, io.BaseTextIO] = None,
    highlights: Iterable[Union[str, CircuitErrorLocation]] = (),
    annotations: Iterable[Union[str, CircuitErrorLocation]] = ("detectors", "observables", "ticks"),
    time: Literal['right', 'up', 'down', 'left', 'toward', 'away'] = 'right',
    projection: Optional[Callable[[int, Tuple[float, ...]], Union[float, Iterable[float]]]] = None,
) -> Optional[str]:
    """Creates a diagram of the circuit.

    Args:
        style: The type of diagram to make. Available diagram styles are:
            "timeline": A traditional quantum circuit diagram. A musical staff of gates to play.
            "layers": Groups operations into 2d layers separated by TICKs.
            "3d": Produces a 3d model of the circuit.
        format: The desired format to save the diagram in. Available formats are:
            "ascii": Produces a minimalist text diagram that can be shown in any monospace font.
                Requires style="timeline" or style="layers".
            "utf-8": Produce a fancy text diagram with box drawing characters.
                Requires style="timeline" or style="layers".
                See: https://en.wikipedia.org/wiki/Box-drawing_character
                Beware that it's unfortunately common for "monospace" fonts to not correctly monospace
                all box drawing characters. If you look at this diagram in such a font, it will be a mess. It is
                especially frustrating that this behavior can be operating system specific or browser specific.
                For example, if you put this diagram in a markdown code block on github, it will look fine in
                [[[[X ON Y]]]] and be mangled by [[[[X ON Y]]]].
            "katex": Produce katex code that can be pasted into a latex document.
                Requires style="timeline" or style="layers".
                See: https://katex.org/
            "svg": Produce an SVG image.
                Requires style="timeline" or style="layers".
                See the SVG 2 specification: https://www.w3.org/TR/SVG2/
            "gltf": Produce a GL Transmission Format representation of the diagram.
                Requires style="3d".
                See the GLTF 2.0 specification: https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/Specification.adoc
            "html": Produce an HTML file with an embedded viewer for the diagram.
                Works with all styles.
                For example, if you don't have software to view 3d models, this is the easiest way to look at the 3d model.
                Note that this HTML can be embedded into a jupyter notebook result.
        file: Defaults to None. When None, the diagram is returned from the method as a string.
            When not None, the diagram is written to the given location and None is returned from the method.
        time: Specifies the time direction (right, up down, left, toward, or away. Defaults to "right".
            "toward" and "away" require style="3d".
        projection: A callable that takes a qubit index and coordinates and returns the coordinates used in the diagram.
        highlights: Specifies a list of things to highlight in the diagram. Examples of things that can be highlighted:
            "M#": Highlight the error sensitivity region of a measurement.
            "D#", "detectors", "gauge-detectors": Highlight the error sensitivity region of a detector.
            "L#", "observables", "gauge-observables": Highlight the error sensitivity region of an observable.
            stim.CircuitErrorLocation: Custom highlight region.
        annotations: Specifies a list of things to annotate in the diagram. Examples of things that can be annotated:
            "shortest-graphlike-error": Finds and annotates a shortest graphlike error.
            stim.CircuitErrorLocation: Annotate a custom error.
            "D#", "detectors", "gauge-detectors": Annotate a detector or set of detectors. Detectors are annotated by place "D#" next to each measurement used by the detector.
            "L#", "observables", "gauge-observables": Annotate a detector or set of detectors. Detectors are annotated by place "D#" next to each measurement used by the detector.

Strilanc avatar Aug 03 '22 14:08 Strilanc