diagram
SVG diagram rendering for quantum circuits.
Diagram
Diagram(svg: str, html: str)
Wrapper for SVG diagram with Jupyter notebook display support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
svg | str | Raw SVG markup suitable for saving to | required |
html | str | HTML-wrapped version for interactive Jupyter display. | required |
Source code in src/tsim/utils/diagram.py
23 24 25 26 27 28 29 30 31 32 | |
__str__
__str__() -> str
Return the raw SVG string.
Source code in src/tsim/utils/diagram.py
34 35 36 | |
GateLabel dataclass
GateLabel(label: str, annotation: str | None = None)
Label for a gate in the SVG diagram.
placeholders_to_t
placeholders_to_t(
svg_string: str,
placeholder_id_to_labels: dict[float, GateLabel],
) -> str
Replace I_ERROR placeholder gates in an SVG diagram with actual gate names.
Supported gates are T, T†, R_Z, R_X, R_Y, U_3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
svg_string | str | The SVG string from stim's diagram() method containing I_ERROR placeholder gates whose p-value are used as identifiers. | required |
placeholder_id_to_labels | dict[float, GateLabel] | Mapping from identifier (float), i.e. the p values of I_ERROR gates, to GateLabel. | required |
Returns:
| Type | Description |
|---|---|
str | Modified SVG string with I_ERROR gates replaced by the actual gate names. |
Source code in src/tsim/utils/diagram.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
render_pyzx_d3
render_pyzx_d3(
stim_circ: Circuit, kwargs: dict[str, Any]
) -> GraphS
Render a stim circuit as a pyzx ZX diagram using d3.js.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stim_circ | Circuit | The stim circuit to render. | required |
kwargs | dict[str, Any] | Additional keyword arguments passed to the underlying diagram renderer. | required |
Returns:
| Type | Description |
|---|---|
GraphS | A pyzx ZX diagram. |
Source code in src/tsim/utils/diagram.py
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
render_svg
render_svg(
c: Circuit,
type: str,
*,
tick: int | range | None = None,
filter_coords: Iterable[Iterable[float] | DemTarget] = (
(),
),
rows: int | None = None,
width: float | None = None,
height: float | None = None,
zoomable: bool = True
) -> Diagram
Render a stim circuit timeline/timeslice diagram with custom labels.
Source code in src/tsim/utils/diagram.py
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | |
tagged_gates_to_placeholder
tagged_gates_to_placeholder(
circuit: Circuit,
) -> tuple[stim.Circuit, dict[float, GateLabel]]
Replace tagged gates with I_ERROR placeholder gates for rendering.
Converts S[T], S_DAG[T], I[R_X(...)], I[R_Y(...)], I[R_Z(...)], I[U3(...)] to I_ERROR placeholder gates whose p-values are used as identifiers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit | Circuit | The stim circuit to replace tagged gates with I_ERROR placeholder gates. | required |
Returns:
| Type | Description |
|---|---|
Circuit | A tuple containing the modified circuit and a dictionary mapping the p-values |
dict[float, GateLabel] | of the I_ERROR placeholder gates to the actual gate names. |
Source code in src/tsim/utils/diagram.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
wrap_svg
wrap_svg(
svg: str,
*,
width: float | None = None,
height: float | None = None
) -> str
Wrap an SVG string in a container div.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
svg | str | Raw SVG markup. | required |
width | float | None | Width of the container in pixels. | None |
height | float | None | Height of the container in pixels (unused, kept for API symmetry with :func: | None |
Source code in src/tsim/utils/diagram.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
wrap_svg_zoomable
wrap_svg_zoomable(
svg: str,
*,
width: float | None = None,
height: float = 700
) -> str
Wrap an SVG in a zoomable, scrollable container.
The container has the given pixel dimensions. When width is None the container fills the available width. Users can pan by scrolling and zoom with pinch-zoom (trackpad) or Ctrl/Cmd + wheel. Zoom is anchored at the cursor position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
svg | str | Raw SVG markup. | required |
width | float | None | Pixel width of the container, or | None |
height | float | Pixel height of the container. | 700 |
Source code in src/tsim/utils/diagram.py
102 103 104 105 106 107 108 109 110 111 112 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |