Skip to content

Plotly debug

build_plotly_debugger_figure

build_plotly_debugger_figure(
    mt: Method,
    arch_spec: ArchSpec,
    *,
    interactive: bool,
    pause_time: float,
    atom_marker: str,
    theme: Theme,
    height: int,
    show_circuit: bool = True,
    circuit_window: int = 24
) -> Figure

Build the Plotly figure without displaying it.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/visualize/plotly_debug.py
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
def build_plotly_debugger_figure(
    mt: ir.Method,
    arch_spec: ArchSpec,
    *,
    interactive: bool,
    pause_time: float,
    atom_marker: str,
    theme: Theme,
    height: int,
    show_circuit: bool = True,
    circuit_window: int = 24,
) -> Figure:
    """Build the Plotly figure without displaying it."""
    if pause_time < 0:
        raise ValueError("pause_time must be non-negative")
    if circuit_window < 1:
        raise ValueError("circuit_window must be at least 1")
    go = _plotly()
    steps = collect_debug_steps(mt, arch_spec)
    colors = _theme_colors(theme)
    circuit = (
        _CircuitPanel(
            columns=tuple(circuit_columns(steps, arch_spec)),
            qubit_ids=tuple(circuit_qubit_ids(steps)),
            window=circuit_window,
        )
        if show_circuit
        else None
    )
    route_groups_by_step = [
        _group_route_segments(_move_path_segments(step.state, arch_spec))
        for step in steps
    ]
    route_trace_count = max(
        1,
        max((len(groups) for groups in route_groups_by_step), default=0),
    )

    # Start with the complete interactive architecture rather than rebuilding
    # its sites and buses here. This preserves one source of truth for site
    # metadata, bus selectors, hover highlighting, and transport paths.
    figure = ArchVisualizer(arch_spec).plot_interactive(
        show_site_ids=False,
        show_all_buses=False,
        show_bus_legend=False,
        path_style="exact",
        site_lane_preview="hover",
        bus_line_style="dashed",
        theme=theme,
        height=height,
    )
    architecture_trace_count = len(cast(Any, figure.data))
    circuit_trace_indices: list[int] = []
    if circuit is not None:
        circuit_trace_indices = list(
            range(architecture_trace_count, architecture_trace_count + 3)
        )
        figure.add_traces(
            circuit_static_traces(circuit.columns, circuit.qubit_ids, colors)
        )
    dynamic_trace_start = len(cast(Any, figure.data))
    dynamic_trace_count = route_trace_count + (1 if circuit is not None else 0) + 2
    dynamic_trace_indices = list(
        range(dynamic_trace_start, dynamic_trace_start + dynamic_trace_count)
    )
    frames: list[Any] = []

    for step_index, step in enumerate(steps):
        frames.append(
            go.Frame(
                name=f"step-{step_index}",
                data=_dynamic_traces(
                    step,
                    step_index,
                    route_groups_by_step[step_index],
                    route_trace_count,
                    arch_spec,
                    colors,
                    atom_marker,
                    circuit,
                ),
                traces=dynamic_trace_indices,
                layout=_frame_layout(step, step_index, circuit),
            )
        )

    initial_data = (
        _dynamic_traces(
            steps[0],
            0,
            route_groups_by_step[0],
            route_trace_count,
            arch_spec,
            colors,
            atom_marker,
            circuit,
        )
        if steps
        else [
            *(_route_trace(None) for _ in range(route_trace_count)),
            *([_empty_trace("Circuit highlight")] if circuit is not None else []),
            _empty_trace("Gate highlight"),
            _empty_trace("Atoms"),
        ]
    )
    title = steps[0].title if steps else "Plotly move debugger: no atom-state steps"
    figure.add_traces(initial_data)
    figure.frames = frames

    sliders = []
    debugger_update_menus = []
    if interactive and frames:
        sliders = [
            {
                "active": 0,
                "currentvalue": {"prefix": "Step: "},
                "pad": {"t": 45},
                "steps": [
                    _slider_step([frame.name], str(index + 1))
                    for index, frame in enumerate(frames)
                ],
            }
        ]
        if len(frames) > 1:
            debugger_update_menus = [
                {
                    "type": "buttons",
                    "direction": "left",
                    "x": 0.0,
                    "xanchor": "left",
                    # Keep playback on the same row as the architecture
                    # controls in the upper margin.
                    "y": 1.09,
                    "yanchor": "top",
                    "showactive": False,
                    "buttons": [
                        {
                            "label": "Play steps",
                            "method": "animate",
                            "args": [
                                None,
                                {
                                    "fromcurrent": True,
                                    "mode": "immediate",
                                    "frame": {
                                        "duration": max(1, round(pause_time * 1000)),
                                        "redraw": True,
                                    },
                                    "transition": {"duration": 0},
                                },
                            ],
                        },
                        {
                            "label": "Pause",
                            "method": "animate",
                            "args": [
                                [None],
                                {
                                    "mode": "immediate",
                                    "frame": {"duration": 0, "redraw": True},
                                    "transition": {"duration": 0},
                                },
                            ],
                        },
                    ],
                }
            ]

    architecture_meta = dict(cast(Any, figure.layout.meta) or {})
    architecture_update_menus = list(figure.layout.updatemenus or ())
    # Make room for playback at the left, then center the complete four-group
    # control row over the plot. ``plot_interactive`` only has the latter
    # three groups, so this debugger-specific alignment belongs here.
    for menu, x_position in zip(
        architecture_update_menus,
        (0.18, 0.47, 0.75),
    ):
        menu.x = x_position
        menu.y = 1.09
    figure.layout.annotations = ()
    total_height = height
    circuit_layout: dict[str, Any] = {}
    if circuit is not None:
        total_height, circuit_layout = _circuit_panel_layout(circuit, height, colors)
    figure.update_layout(
        title={"text": title, "x": 0.01, "xanchor": "left"},
        height=total_height,
        margin=dict(_DEBUGGER_MARGIN),
        showlegend=False,
        uirevision="bloqade-plotly-debugger",
        sliders=sliders,
        updatemenus=[*architecture_update_menus, *debugger_update_menus],
        meta={
            **architecture_meta,
            "bloqadePlotlyDebugger": {
                "stepCount": len(steps),
                "frameCount": len(frames),
                "frameNames": [frame.name for frame in frames],
                "routeTraceCount": route_trace_count,
                "architectureTraceCount": architecture_trace_count,
                "circuitTraceIndices": circuit_trace_indices,
                "circuitColumnCount": circuit.column_count if circuit else 0,
                "circuitStepColumns": (
                    [column.step_index for column in circuit.columns] if circuit else []
                ),
            },
        },
        **circuit_layout,
    )
    return figure

plotly_debugger

plotly_debugger(
    mt: Method,
    arch_spec: ArchSpec,
    interactive: bool = True,
    pause_time: float = 1.0,
    atom_marker: str = "o",
    *,
    show: bool = True,
    renderer: str | None = None,
    theme: Theme = "light",
    height: int = 720,
    show_circuit: bool = True,
    circuit_window: int = 24
) -> Figure | None

Display a browser-native, discrete-step move-program debugger.

This is a Plotly alternative to :func:debugger; it does not replace the existing Matplotlib debugger or :func:animated_debugger. In a notebook, Plotly embeds the controls in the cell output. In a regular Python process, the default renderer opens the figure in a browser.

Hover over an empty site or an atom to preview the transport lanes available at that site, using the architecture's exact/cartoon path view.

Parameters:

Name Type Description Default
mt Method

Compiled physical move program to visualize.

required
arch_spec ArchSpec

Architecture used to compile mt.

required
interactive bool

Include the step slider and play/pause controls. When false, displaying the figure automatically plays all steps.

True
pause_time float

Seconds between steps during playback.

1.0
atom_marker str

Matplotlib-style marker name for atoms (for example, "o" or "s"), or a Plotly marker symbol.

'o'
show bool

Display the figure immediately. Set false to customize it first.

True
renderer str | None

Explicit Plotly renderer. The default embeds in Jupyter and uses "browser" in a regular Python process.

None
theme Theme

Initial light or dark theme.

'light'
height int

Height of the architecture panel in pixels. The executed circuit panel, when shown, is added above it.

720
show_circuit bool

Render the executed circuit above the architecture, one column per gate step. The active step's column is highlighted and kept in view; clicking a gate jumps the debugger to its step.

True
circuit_window int

Maximum number of circuit columns visible at once. Longer circuits scroll horizontally to follow the active step.

24

Returns:

Type Description
Figure | None

The constructed Plotly figure when show=False; otherwise None.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/visualize/plotly_debug.py
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
def plotly_debugger(
    mt: ir.Method,
    arch_spec: ArchSpec,
    interactive: bool = True,
    pause_time: float = 1.0,
    atom_marker: str = "o",
    *,
    show: bool = True,
    renderer: str | None = None,
    theme: Theme = "light",
    height: int = 720,
    show_circuit: bool = True,
    circuit_window: int = 24,
) -> Figure | None:
    """Display a browser-native, discrete-step move-program debugger.

    This is a Plotly alternative to :func:`debugger`; it does not replace the
    existing Matplotlib debugger or :func:`animated_debugger`. In a notebook,
    Plotly embeds the controls in the cell output. In a regular Python process,
    the default renderer opens the figure in a browser.

    Hover over an empty site or an atom to preview the transport lanes
    available at that site, using the architecture's exact/cartoon path view.

    Args:
        mt: Compiled physical move program to visualize.
        arch_spec: Architecture used to compile ``mt``.
        interactive: Include the step slider and play/pause controls. When
            false, displaying the figure automatically plays all steps.
        pause_time: Seconds between steps during playback.
        atom_marker: Matplotlib-style marker name for atoms (for example,
            ``"o"`` or ``"s"``), or a Plotly marker symbol.
        show: Display the figure immediately. Set false to customize it first.
        renderer: Explicit Plotly renderer. The default embeds in Jupyter and
            uses ``"browser"`` in a regular Python process.
        theme: Initial light or dark theme.
        height: Height of the architecture panel in pixels. The executed
            circuit panel, when shown, is added above it.
        show_circuit: Render the executed circuit above the architecture, one
            column per gate step. The active step's column is highlighted and
            kept in view; clicking a gate jumps the debugger to its step.
        circuit_window: Maximum number of circuit columns visible at once.
            Longer circuits scroll horizontally to follow the active step.

    Returns:
        The constructed Plotly figure when ``show=False``; otherwise ``None``.
    """
    figure = build_plotly_debugger_figure(
        mt,
        arch_spec,
        interactive=interactive,
        pause_time=pause_time,
        atom_marker=atom_marker,
        theme=theme,
        height=height,
        show_circuit=show_circuit,
        circuit_window=circuit_window,
    )
    if show:
        _show_plotly_debugger(
            figure,
            renderer=renderer,
            auto_play=not interactive,
            frame_duration_ms=max(1, round(pause_time * 1000)),
        )
        return None
    return figure