Skip to content

Plotly circuit

Executed-circuit reconstruction and Plotly traces for the move debugger.

The move dialect program that :func:~bloqade.lanes.visualize.plotly_debug.plotly_debugger interprets already describes the executed circuit: every gate statement carries the physical locations (or zone) it acts on, and the interpreted atom state maps those locations back to qubit ids. This module turns the debugger steps into circuit columns (one per gate step) and renders them as a compact circuit diagram on a secondary Plotly axis pair, so the diagram stays in lockstep with the atom animation.

CircuitColumn dataclass

CircuitColumn(
    step_index: int,
    kind: GateKind,
    label: str,
    description: str,
    qubit_ids: tuple[int, ...],
    pairs: tuple[tuple[int, int], ...] = (),
)

One executed gate layer of the circuit, tied to a debugger step.

circuit_columns

circuit_columns(
    steps: Sequence[DebugStep], arch_spec: ArchSpec
) -> list[CircuitColumn]

Reconstruct the executed circuit, one column per gate step.

The qubit ids are resolved from the interpreted atom state exactly the way move2squin resolves them when it emits the final squin program, so the result is the executed circuit by construction (after ASAP/ALAP reordering and gate fusion).

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/visualize/plotly_circuit.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def circuit_columns(
    steps: Sequence[DebugStep], arch_spec: ArchSpec
) -> list[CircuitColumn]:
    """Reconstruct the executed circuit, one column per gate step.

    The qubit ids are resolved from the interpreted atom state exactly the way
    ``move2squin`` resolves them when it emits the final squin program, so the
    result is the executed circuit by construction (after ASAP/ALAP reordering
    and gate fusion).
    """
    columns: list[CircuitColumn] = []
    for step_index, step in enumerate(steps):
        statement = step.statement
        kind_and_label = _gate_kind_and_label(statement)
        if kind_and_label is None:
            continue
        kind, label = kind_and_label
        pairs: tuple[tuple[int, int], ...] = ()

        if isinstance(statement, (move.LocalR, move.LocalRz, move.StarRz)):
            qubit_ids = _qubits_at(step, statement.location_addresses)
        elif isinstance(statement, (move.GlobalR, move.GlobalRz)):
            qubit_ids = tuple(sorted(step.state.data.qubit_to_locations))
        elif isinstance(statement, move.CZ):
            controls, targets, _ = step.state.data.get_qubit_pairing(
                statement.zone_address, arch_spec
            )
            pairs = tuple(zip(controls, targets, strict=True))
            qubit_ids = tuple(sorted({*controls, *targets}))
        else:
            assert isinstance(statement, (move.EndMeasure, move.Measure))
            qubit_ids = _qubits_in_zones(
                step, {address.zone_id for address in statement.zone_addresses}
            )

        columns.append(
            CircuitColumn(
                step_index=step_index,
                kind=kind,
                label=label,
                description=gate_description(step),
                qubit_ids=qubit_ids,
                pairs=pairs,
            )
        )
    return columns

circuit_cursor

circuit_cursor(
    columns: Sequence[CircuitColumn], step_index: int
) -> float

Position of step_index along the circuit's column axis.

Gate steps sit on their own column. Steps without a gate (moves, fills, loads) sit halfway between the previously executed column and the next one, which is where the atoms are physically travelling to.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/visualize/plotly_circuit.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def circuit_cursor(columns: Sequence[CircuitColumn], step_index: int) -> float:
    """Position of ``step_index`` along the circuit's column axis.

    Gate steps sit on their own column. Steps without a gate (moves, fills,
    loads) sit halfway between the previously executed column and the next one,
    which is where the atoms are physically travelling to.
    """
    cursor = -0.5
    for column_index, column in enumerate(columns):
        if column.step_index == step_index:
            return float(column_index)
        if column.step_index < step_index:
            cursor = column_index + 0.5
        else:
            break
    return cursor

circuit_highlight_trace

circuit_highlight_trace(
    columns: Sequence[CircuitColumn],
    step_index: int,
    qubit_count: int,
    colors: dict[str, str],
) -> Any

Translucent band over the active column, or a cursor between columns.

Plotly animation merges frame trace attributes into the live trace instead of replacing them, so both variants set the same attribute keys.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/visualize/plotly_circuit.py
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
def circuit_highlight_trace(
    columns: Sequence[CircuitColumn],
    step_index: int,
    qubit_count: int,
    colors: dict[str, str],
) -> Any:
    """Translucent band over the active column, or a cursor between columns.

    Plotly animation merges frame trace attributes into the live trace instead
    of replacing them, so both variants set the same attribute keys.
    """
    go = _plotly()
    y_low, y_high = circuit_y_range(qubit_count)
    y_low += 0.2
    y_high -= 0.2
    cursor = circuit_cursor(columns, step_index)
    active_column = next(
        (column for column in columns if column.step_index == step_index), None
    )
    if active_column is None:
        x_values = [cursor, cursor]
        y_values = [y_low, y_high]
        line = {"color": colors["route"], "width": 2, "dash": "dot"}
        fill = "none"
        fillcolor = "rgba(0,0,0,0)"
        opacity = 1.0
    else:
        half_width = 0.45
        x_values = [
            cursor - half_width,
            cursor + half_width,
            cursor + half_width,
            cursor - half_width,
            cursor - half_width,
        ]
        y_values = [y_low, y_low, y_high, y_high, y_low]
        color = colors[_color_key(active_column.kind)]
        line = {"color": color, "width": 0, "dash": "solid"}
        fill = "toself"
        fillcolor = color
        opacity = 0.25

    return go.Scatter(
        x=x_values,
        y=y_values,
        mode="lines",
        line=line,
        fill=fill,
        fillcolor=fillcolor,
        opacity=opacity,
        hoverinfo="skip",
        name="Circuit highlight",
        showlegend=False,
        visible=True,
        meta={"bloqadeTraceKind": "circuitHighlight"},
        **_circuit_axes(),
    )

circuit_qubit_ids

circuit_qubit_ids(steps: Sequence[DebugStep]) -> list[int]

Every qubit id that occupies a site at any step, in ascending order.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/visualize/plotly_circuit.py
161
162
163
164
165
166
def circuit_qubit_ids(steps: Sequence[DebugStep]) -> list[int]:
    """Every qubit id that occupies a site at any step, in ascending order."""
    qubit_ids: set[int] = set()
    for step in steps:
        qubit_ids.update(step.state.data.qubit_to_locations)
    return sorted(qubit_ids)

circuit_static_traces

circuit_static_traces(
    columns: Sequence[CircuitColumn],
    qubit_ids: Sequence[int],
    colors: dict[str, str],
) -> list[Any]

Wires, CZ connectors, and gate markers for the executed circuit.

CZ layers draw one connector per pair, staggered into sub-columns when pairs overlap on the wire axis, so each two-qubit gate is legible. Gate markers carry their CZ partner in hover text.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/visualize/plotly_circuit.py
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
def circuit_static_traces(
    columns: Sequence[CircuitColumn],
    qubit_ids: Sequence[int],
    colors: dict[str, str],
) -> list[Any]:
    """Wires, CZ connectors, and gate markers for the executed circuit.

    CZ layers draw one connector per pair, staggered into sub-columns when
    pairs overlap on the wire axis, so each two-qubit gate is legible. Gate
    markers carry their CZ partner in hover text.
    """
    go = _plotly()
    rows = {qubit_id: index for index, qubit_id in enumerate(qubit_ids)}
    column_count = max(len(columns), 1)

    wire_x: list[float | None] = []
    wire_y: list[float | None] = []
    for row_index in range(len(qubit_ids)):
        wire_x.extend([-0.5, column_count - 0.5, None])
        wire_y.extend([circuit_row_y(row_index)] * 2 + [None])
    wires = go.Scatter(
        x=wire_x,
        y=wire_y,
        mode="lines",
        line={"color": colors["site_edge"], "width": 1},
        hoverinfo="skip",
        name="Circuit wires",
        showlegend=False,
        meta={"bloqadeTraceKind": "circuitWire"},
        **_circuit_axes(),
    )

    connector_x: list[float | None] = []
    connector_y: list[float | None] = []
    gate_x: list[float] = []
    gate_y: list[float] = []
    gate_text: list[str] = []
    gate_symbol: list[str] = []
    gate_size: list[int] = []
    gate_color: list[str] = []
    gate_customdata: list[list[Any]] = []
    for column_index, column in enumerate(columns):
        base = [column.step_index, column.step_index + 1, column.description]
        if column.kind == "cz":
            pairs = [
                (control, target)
                for control, target in column.pairs
                if control in rows and target in rows
            ]
            offsets = cz_pair_offsets(pairs, rows)
            for (control, target), offset in zip(pairs, offsets, strict=True):
                x_value = column_index + offset
                control_y = circuit_row_y(rows[control])
                target_y = circuit_row_y(rows[target])
                connector_x.extend([x_value, x_value, None])
                connector_y.extend([control_y, target_y, None])
                for qubit_id, y_value, partner, role in (
                    (control, control_y, target, "control"),
                    (target, target_y, control, "target"),
                ):
                    gate_x.append(x_value)
                    gate_y.append(y_value)
                    gate_text.append("")
                    gate_symbol.append("circle")
                    gate_size.append(11)
                    gate_color.append(colors["cz"])
                    gate_customdata.append(
                        [*base, qubit_id, f"CZ {role}, paired with qubit {partner}"]
                    )
            continue

        for qubit_id in column.qubit_ids:
            if qubit_id not in rows:
                continue
            gate_x.append(float(column_index))
            gate_y.append(circuit_row_y(rows[qubit_id]))
            if column.kind in ("global_r", "global_rz"):
                gate_text.append(column.label)
                gate_symbol.append("diamond")
                gate_size.append(26)
                role_text = "global pulse"
            else:
                gate_text.append(column.label)
                gate_symbol.append("square")
                gate_size.append(22)
                role_text = "local pulse"
            gate_color.append(colors[_color_key(column.kind)])
            gate_customdata.append([*base, qubit_id, role_text])

    connectors = go.Scatter(
        x=connector_x,
        y=connector_y,
        mode="lines",
        line={"color": colors["cz"], "width": 2},
        hoverinfo="skip",
        name="CZ connectors",
        showlegend=False,
        meta={"bloqadeTraceKind": "circuitConnector"},
        **_circuit_axes(),
    )
    gates = go.Scatter(
        x=gate_x,
        y=gate_y,
        text=gate_text,
        customdata=gate_customdata,
        mode="markers+text",
        textposition="middle center",
        textfont={"color": "white", "size": 10},
        marker={
            "symbol": gate_symbol,
            "size": gate_size,
            "color": gate_color,
            "line": {"color": colors["paper"], "width": 1},
        },
        name="Circuit gates",
        showlegend=False,
        meta={"bloqadeTraceKind": "circuitGate"},
        hovertemplate=(
            "<b>step %{customdata[1]}</b>: %{customdata[2]}<br>"
            "qubit %{customdata[3]} ยท %{customdata[4]}<br>"
            "<i>click to jump to this step</i><extra></extra>"
        ),
        **_circuit_axes(),
    )
    return [wires, connectors, gates]

circuit_window_range

circuit_window_range(
    cursor: float, column_count: int, window: int
) -> tuple[float, float]

Visible column range keeping cursor centered inside a window.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/visualize/plotly_circuit.py
187
188
189
190
191
192
193
194
195
196
197
def circuit_window_range(
    cursor: float, column_count: int, window: int
) -> tuple[float, float]:
    """Visible column range keeping ``cursor`` centered inside a window."""
    left_edge = -0.6
    right_edge = max(column_count, 1) - 0.4
    span = right_edge - left_edge
    if window <= 0 or span <= window:
        return (left_edge, right_edge)
    low = min(max(cursor - window / 2, left_edge), right_edge - window)
    return (low, low + window)

cz_pair_offsets

cz_pair_offsets(
    pairs: Sequence[tuple[int, int]], rows: dict[int, int]
) -> list[float]

Horizontal offset within a column for each CZ pair.

Pairs whose wire spans overlap would draw collinear connectors and become indistinguishable, so they are assigned to separate sub-columns (a greedy interval colouring). Pairs with disjoint spans share a sub-column. The sub-columns are spread symmetrically around the column centre.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/visualize/plotly_circuit.py
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
def cz_pair_offsets(
    pairs: Sequence[tuple[int, int]], rows: dict[int, int]
) -> list[float]:
    """Horizontal offset within a column for each CZ pair.

    Pairs whose wire spans overlap would draw collinear connectors and become
    indistinguishable, so they are assigned to separate sub-columns (a greedy
    interval colouring). Pairs with disjoint spans share a sub-column. The
    sub-columns are spread symmetrically around the column centre.
    """
    spans = [
        (min(rows[control], rows[target]), max(rows[control], rows[target]))
        for control, target in pairs
    ]
    order = sorted(range(len(pairs)), key=lambda index: spans[index])
    sub_column_of: dict[int, int] = {}
    sub_column_end: list[int] = []  # last occupied row of each sub-column
    for index in order:
        low, high = spans[index]
        for sub_column, end in enumerate(sub_column_end):
            if end < low:
                sub_column_end[sub_column] = high
                sub_column_of[index] = sub_column
                break
        else:
            sub_column_of[index] = len(sub_column_end)
            sub_column_end.append(high)

    sub_column_count = len(sub_column_end)
    if sub_column_count <= 1:
        return [0.0] * len(pairs)
    spacing = 2 * _CZ_STAGGER_HALF_WIDTH / (sub_column_count - 1)
    return [
        -_CZ_STAGGER_HALF_WIDTH + sub_column_of[index] * spacing
        for index in range(len(pairs))
    ]

gate_description

gate_description(step: DebugStep) -> str

Format an interpreted gate with named, evaluated parameters.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/visualize/plotly_circuit.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def gate_description(step: DebugStep) -> str:
    """Format an interpreted gate with named, evaluated parameters."""
    if isinstance(step.statement, (move.EndMeasure, move.Measure)):
        zones = tuple(address.zone_id for address in step.statement.zone_addresses)
        return f"{type(step.statement).__name__}(zones={zones})"

    parameters = ", ".join(
        f"{name}={value!r}"
        for name, value in zip(
            gate_parameter_names(step.statement),
            step.parameter_values,
            strict=False,
        )
    )
    return f"{type(step.statement).__name__}({parameters})"