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
22 23 24 25 26 27 28 29 30 31 | |
__str__
__str__() -> str
Return the raw SVG string.
Source code in src/tsim/utils/diagram.py
33 34 35 | |
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
202 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 | |
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
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 447 448 449 450 451 452 453 454 455 | |
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
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |
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
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
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
70 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 | |
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
101 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 | |