quantum-circuit-drawer 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. quantum_circuit_drawer/__init__.py +100 -0
  2. quantum_circuit_drawer/_draw_managed.py +496 -0
  3. quantum_circuit_drawer/_draw_managed_slider.py +95 -0
  4. quantum_circuit_drawer/_draw_managed_viewport.py +233 -0
  5. quantum_circuit_drawer/_draw_managed_zoom.py +190 -0
  6. quantum_circuit_drawer/_draw_pipeline.py +228 -0
  7. quantum_circuit_drawer/_draw_request.py +257 -0
  8. quantum_circuit_drawer/_matrix_support.py +326 -0
  9. quantum_circuit_drawer/_version.py +5 -0
  10. quantum_circuit_drawer/adapters/__init__.py +16 -0
  11. quantum_circuit_drawer/adapters/_cudaq_quake_parser.py +411 -0
  12. quantum_circuit_drawer/adapters/_helpers.py +423 -0
  13. quantum_circuit_drawer/adapters/base.py +49 -0
  14. quantum_circuit_drawer/adapters/cirq_adapter.py +486 -0
  15. quantum_circuit_drawer/adapters/cudaq_adapter.py +85 -0
  16. quantum_circuit_drawer/adapters/ir_adapter.py +23 -0
  17. quantum_circuit_drawer/adapters/myqlm_adapter.py +495 -0
  18. quantum_circuit_drawer/adapters/pennylane_adapter.py +355 -0
  19. quantum_circuit_drawer/adapters/qiskit_adapter.py +443 -0
  20. quantum_circuit_drawer/adapters/registry.py +130 -0
  21. quantum_circuit_drawer/api.py +207 -0
  22. quantum_circuit_drawer/exceptions.py +29 -0
  23. quantum_circuit_drawer/hover.py +109 -0
  24. quantum_circuit_drawer/ir/__init__.py +21 -0
  25. quantum_circuit_drawer/ir/circuit.py +78 -0
  26. quantum_circuit_drawer/ir/classical_conditions.py +31 -0
  27. quantum_circuit_drawer/ir/measurements.py +41 -0
  28. quantum_circuit_drawer/ir/operations.py +161 -0
  29. quantum_circuit_drawer/ir/wires.py +36 -0
  30. quantum_circuit_drawer/layout/__init__.py +8 -0
  31. quantum_circuit_drawer/layout/_classical_conditions.py +29 -0
  32. quantum_circuit_drawer/layout/_layering.py +68 -0
  33. quantum_circuit_drawer/layout/_layout_scaffold.py +263 -0
  34. quantum_circuit_drawer/layout/_operation_layout.py +691 -0
  35. quantum_circuit_drawer/layout/_operation_text.py +61 -0
  36. quantum_circuit_drawer/layout/engine.py +90 -0
  37. quantum_circuit_drawer/layout/engine_3d.py +745 -0
  38. quantum_circuit_drawer/layout/routing.py +24 -0
  39. quantum_circuit_drawer/layout/scene.py +163 -0
  40. quantum_circuit_drawer/layout/scene_3d.py +129 -0
  41. quantum_circuit_drawer/layout/spacing.py +90 -0
  42. quantum_circuit_drawer/layout/topology_3d.py +380 -0
  43. quantum_circuit_drawer/py.typed +1 -0
  44. quantum_circuit_drawer/renderers/__init__.py +28 -0
  45. quantum_circuit_drawer/renderers/_matplotlib_figure.py +290 -0
  46. quantum_circuit_drawer/renderers/_matplotlib_hover.py +308 -0
  47. quantum_circuit_drawer/renderers/_matplotlib_page_projection.py +111 -0
  48. quantum_circuit_drawer/renderers/_render_support.py +122 -0
  49. quantum_circuit_drawer/renderers/base.py +31 -0
  50. quantum_circuit_drawer/renderers/matplotlib_primitives.py +1209 -0
  51. quantum_circuit_drawer/renderers/matplotlib_renderer.py +445 -0
  52. quantum_circuit_drawer/renderers/matplotlib_renderer_3d.py +1545 -0
  53. quantum_circuit_drawer/style/__init__.py +7 -0
  54. quantum_circuit_drawer/style/defaults.py +37 -0
  55. quantum_circuit_drawer/style/theme.py +88 -0
  56. quantum_circuit_drawer/style/validators.py +73 -0
  57. quantum_circuit_drawer/typing.py +73 -0
  58. quantum_circuit_drawer/utils/__init__.py +5 -0
  59. quantum_circuit_drawer/utils/formatting.py +40 -0
  60. quantum_circuit_drawer-0.2.1.dist-info/METADATA +153 -0
  61. quantum_circuit_drawer-0.2.1.dist-info/RECORD +65 -0
  62. quantum_circuit_drawer-0.2.1.dist-info/WHEEL +5 -0
  63. quantum_circuit_drawer-0.2.1.dist-info/licenses/LICENSE +21 -0
  64. quantum_circuit_drawer-0.2.1.dist-info/licenses/THIRD_PARTY_LICENSES.md +67 -0
  65. quantum_circuit_drawer-0.2.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,100 @@
1
+ """Public package exports for :mod:`quantum_circuit_drawer`.
2
+
3
+ The package-level namespace intentionally stays small: the convenience
4
+ ``draw_quantum_circuit(...)`` entrypoint, style types, version information,
5
+ and the exception types that callers are most likely to handle directly.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from collections.abc import Mapping
11
+ from typing import TYPE_CHECKING, Literal
12
+
13
+ from ._version import __version__
14
+ from .exceptions import (
15
+ LayoutError,
16
+ QuantumCircuitDrawerError,
17
+ RenderingError,
18
+ StyleValidationError,
19
+ UnsupportedBackendError,
20
+ UnsupportedFrameworkError,
21
+ UnsupportedOperationError,
22
+ )
23
+ from .hover import HoverOptions
24
+ from .style import DrawStyle, DrawTheme
25
+
26
+ if TYPE_CHECKING:
27
+ from matplotlib.axes import Axes
28
+
29
+ from .typing import LayoutEngine3DLike, LayoutEngineLike, OutputPath, RenderResult
30
+
31
+
32
+ def draw_quantum_circuit(
33
+ circuit: object,
34
+ framework: str | None = None,
35
+ *,
36
+ style: DrawStyle | Mapping[str, object] | None = None,
37
+ layout: LayoutEngineLike | LayoutEngine3DLike | None = None,
38
+ backend: str = "matplotlib",
39
+ ax: Axes | None = None,
40
+ output: OutputPath | None = None,
41
+ show: bool = True,
42
+ figsize: tuple[float, float] | None = None,
43
+ page_slider: bool = False,
44
+ composite_mode: str = "compact",
45
+ view: Literal["2d", "3d"] = "2d",
46
+ topology: Literal["line", "grid", "star", "star_tree", "honeycomb"] = "line",
47
+ direct: bool = True,
48
+ hover: bool | HoverOptions | Mapping[str, object] = False,
49
+ **options: object,
50
+ ) -> RenderResult:
51
+ """Draw a supported circuit through the package-level convenience API.
52
+
53
+ This wrapper preserves the full public signature while deferring the
54
+ internal API import until call time.
55
+
56
+ Returns:
57
+ ``(figure, axes)`` when the library manages the Matplotlib figure, or
58
+ the caller-provided axes when ``ax=...`` is used.
59
+
60
+ Notes:
61
+ ``page_slider=True`` requires a managed 2D figure.
62
+ ``view="3d"`` requires a 3D Matplotlib axes when ``ax`` is provided.
63
+ """
64
+
65
+ from .api import draw_quantum_circuit as _draw_quantum_circuit
66
+
67
+ return _draw_quantum_circuit(
68
+ circuit,
69
+ framework,
70
+ style=style,
71
+ layout=layout,
72
+ backend=backend,
73
+ ax=ax,
74
+ output=output,
75
+ show=show,
76
+ figsize=figsize,
77
+ page_slider=page_slider,
78
+ composite_mode=composite_mode,
79
+ view=view,
80
+ topology=topology,
81
+ direct=direct,
82
+ hover=hover,
83
+ **options,
84
+ )
85
+
86
+
87
+ __all__ = [
88
+ "DrawStyle",
89
+ "DrawTheme",
90
+ "HoverOptions",
91
+ "LayoutError",
92
+ "QuantumCircuitDrawerError",
93
+ "RenderingError",
94
+ "StyleValidationError",
95
+ "UnsupportedBackendError",
96
+ "UnsupportedFrameworkError",
97
+ "UnsupportedOperationError",
98
+ "__version__",
99
+ "draw_quantum_circuit",
100
+ ]
@@ -0,0 +1,496 @@
1
+ """Managed-figure rendering helpers for the public drawing API."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from collections.abc import Callable
7
+ from typing import TYPE_CHECKING, TypeGuard, cast
8
+
9
+ from ._draw_managed_slider import (
10
+ configure_page_slider as _configure_page_slider_impl,
11
+ )
12
+ from ._draw_managed_slider import (
13
+ page_slider_figsize as _page_slider_figsize_impl,
14
+ )
15
+ from ._draw_managed_slider import (
16
+ set_slider_view as _set_slider_view_impl,
17
+ )
18
+ from ._draw_managed_slider import (
19
+ slider_viewport_width as _slider_viewport_width_impl,
20
+ )
21
+ from ._draw_managed_viewport import (
22
+ auto_paging_matches as _auto_paging_matches_impl,
23
+ )
24
+ from ._draw_managed_viewport import (
25
+ axes_viewport_pixels as _axes_viewport_pixels_impl,
26
+ )
27
+ from ._draw_managed_viewport import (
28
+ axes_viewport_ratio as _axes_viewport_ratio_impl,
29
+ )
30
+ from ._draw_managed_viewport import (
31
+ build_continuous_slider_scene as _build_continuous_slider_scene_impl,
32
+ )
33
+ from ._draw_managed_viewport import (
34
+ compute_paged_scene as _compute_paged_scene_impl,
35
+ )
36
+ from ._draw_managed_viewport import (
37
+ scene_aspect_ratio as _scene_aspect_ratio_impl,
38
+ )
39
+ from ._draw_managed_viewport import (
40
+ viewport_adaptive_paged_scene as _viewport_adaptive_paged_scene_impl,
41
+ )
42
+ from ._draw_managed_viewport import (
43
+ viewport_scene_score as _viewport_scene_score_impl,
44
+ )
45
+ from ._draw_managed_viewport import (
46
+ viewport_signature as _viewport_signature_impl,
47
+ )
48
+ from ._draw_managed_zoom import (
49
+ configure_zoom_text_scaling as _configure_zoom_text_scaling_impl,
50
+ )
51
+ from ._draw_managed_zoom import (
52
+ current_text_scale as _current_text_scale_impl,
53
+ )
54
+ from ._draw_managed_zoom import (
55
+ current_view_size as _current_view_size_impl,
56
+ )
57
+ from ._draw_pipeline import PreparedDrawPipeline
58
+ from .renderers._render_support import should_use_managed_agg_canvas, show_figure_if_supported
59
+ from .style import DrawStyle
60
+ from .typing import LayoutEngineLike, OutputPath
61
+
62
+ if TYPE_CHECKING:
63
+ from matplotlib.axes import Axes
64
+ from matplotlib.figure import Figure
65
+
66
+ from .ir.circuit import CircuitIR
67
+ from .layout.scene import LayoutScene
68
+ from .layout.scene_3d import LayoutScene3D
69
+
70
+ _PAGE_SLIDER_MAIN_AXES_BOTTOM = 0.18
71
+ _MANAGED_3D_VIEWPORT_BOUNDS_ATTR = "_quantum_circuit_drawer_managed_3d_viewport_bounds"
72
+ logger = logging.getLogger(__name__)
73
+
74
+
75
+ def render_managed_draw_pipeline(
76
+ pipeline: PreparedDrawPipeline,
77
+ *,
78
+ output: OutputPath | None,
79
+ show: bool,
80
+ figsize: tuple[float, float] | None,
81
+ page_slider: bool,
82
+ ) -> tuple[Figure, Axes]:
83
+ """Render a prepared pipeline on a managed figure."""
84
+
85
+ from .renderers._matplotlib_figure import (
86
+ create_managed_figure,
87
+ set_page_slider,
88
+ set_viewport_width,
89
+ )
90
+
91
+ hover_enabled = (
92
+ pipeline.paged_scene.hover_enabled
93
+ if is_3d_scene(pipeline.paged_scene)
94
+ else cast("LayoutScene", pipeline.paged_scene).hover.enabled
95
+ )
96
+ use_agg_canvas = should_use_managed_agg_canvas(
97
+ show=show,
98
+ output=output,
99
+ prefer_offscreen_when_hidden=not hover_enabled,
100
+ )
101
+
102
+ if is_3d_scene(pipeline.paged_scene):
103
+ scene_3d = pipeline.paged_scene
104
+ figure_width, figure_height = figsize or (
105
+ max(4.6, scene_3d.width * 0.95),
106
+ max(2.1, scene_3d.height * 0.72),
107
+ )
108
+ figure, axes = create_managed_figure(
109
+ scene_3d,
110
+ figure_width=figure_width,
111
+ figure_height=figure_height,
112
+ use_agg=use_agg_canvas,
113
+ projection="3d",
114
+ )
115
+ setattr(axes, _MANAGED_3D_VIEWPORT_BOUNDS_ATTR, (0.0, 0.0, 1.0, 1.0))
116
+ pipeline.renderer.render(scene_3d, ax=axes, output=output)
117
+ logger.debug("Rendered managed 3D figure without page slider")
118
+ show_figure_if_supported(figure, show=show)
119
+ return figure, axes
120
+
121
+ if page_slider:
122
+ scene_2d = cast("LayoutScene", pipeline.paged_scene)
123
+ layout_engine = cast(LayoutEngineLike, pipeline.layout_engine)
124
+ slider_scene = build_continuous_slider_scene(
125
+ pipeline.ir,
126
+ layout_engine,
127
+ pipeline.normalized_style,
128
+ hover_enabled=scene_2d.hover.enabled,
129
+ )
130
+ slider_scene.hover = scene_2d.hover
131
+ initial_viewport_width = min(scene_2d.width, slider_scene.width)
132
+ figure_width, figure_height = page_slider_figsize(
133
+ initial_viewport_width,
134
+ slider_scene.height,
135
+ )
136
+ if figsize is not None:
137
+ figure_width, figure_height = figsize
138
+ if output is None:
139
+ figure, axes = create_managed_figure(
140
+ slider_scene,
141
+ figure_width=figure_width,
142
+ figure_height=figure_height,
143
+ use_agg=use_agg_canvas,
144
+ )
145
+ else:
146
+ figure, axes = create_managed_figure(
147
+ scene_2d,
148
+ figure_width=figure_width,
149
+ figure_height=figure_height,
150
+ use_agg=use_agg_canvas,
151
+ )
152
+ pipeline.renderer.render(scene_2d, ax=axes, output=output)
153
+ axes.clear()
154
+ figure.subplots_adjust(bottom=_PAGE_SLIDER_MAIN_AXES_BOTTOM)
155
+ viewport_width = slider_viewport_width(axes, slider_scene)
156
+ set_viewport_width(figure, viewport_width=viewport_width)
157
+ pipeline.renderer.render(slider_scene, ax=axes)
158
+ configure_page_slider(
159
+ figure=figure,
160
+ axes=axes,
161
+ scene=slider_scene,
162
+ viewport_width=viewport_width,
163
+ set_page_slider=set_page_slider,
164
+ )
165
+ logger.debug(
166
+ "Rendered managed figure with page slider viewport_width=%.2f pages=%d",
167
+ viewport_width,
168
+ len(scene_2d.pages),
169
+ )
170
+ else:
171
+ scene_2d = cast("LayoutScene", pipeline.paged_scene)
172
+ figure_width, figure_height = figsize or (
173
+ max(4.6, scene_2d.width * 0.95),
174
+ max(2.1, scene_2d.page_height * 0.72),
175
+ )
176
+ figure, axes = create_managed_figure(
177
+ scene_2d,
178
+ figure_width=figure_width,
179
+ figure_height=figure_height,
180
+ use_agg=use_agg_canvas,
181
+ )
182
+ render_draw_pipeline_on_axes(
183
+ pipeline,
184
+ axes=axes,
185
+ output=output,
186
+ enable_auto_paging=True,
187
+ reconcile_on_first_draw=not (use_agg_canvas and figsize is not None),
188
+ )
189
+ logger.debug("Rendered managed figure without page slider")
190
+
191
+ show_figure_if_supported(figure, show=show)
192
+ return figure, axes
193
+
194
+
195
+ def render_draw_pipeline_on_axes(
196
+ pipeline: PreparedDrawPipeline,
197
+ *,
198
+ axes: Axes,
199
+ output: OutputPath | None,
200
+ enable_auto_paging: bool,
201
+ reconcile_on_first_draw: bool = False,
202
+ ) -> Axes:
203
+ """Render a prepared pipeline on existing 2D axes, with optional auto paging."""
204
+
205
+ from .renderers._matplotlib_figure import (
206
+ AutoPagingState,
207
+ clear_auto_paging_state,
208
+ clear_text_scaling_state,
209
+ set_auto_paging_state,
210
+ set_viewport_width,
211
+ )
212
+
213
+ clear_auto_paging_state(axes)
214
+ clear_text_scaling_state(axes)
215
+
216
+ if is_3d_scene(pipeline.paged_scene) or not enable_auto_paging:
217
+ axes.clear()
218
+ pipeline.renderer.render(pipeline.paged_scene, ax=axes, output=output)
219
+ return axes
220
+
221
+ prepared_scene = cast("LayoutScene", pipeline.paged_scene)
222
+ scene_2d, effective_page_width = viewport_adaptive_paged_scene(
223
+ pipeline.ir,
224
+ cast(LayoutEngineLike, pipeline.layout_engine),
225
+ pipeline.normalized_style,
226
+ axes,
227
+ hover_enabled=prepared_scene.hover.enabled,
228
+ )
229
+ scene_2d.hover = prepared_scene.hover
230
+ axes.clear()
231
+ pipeline.renderer.render(scene_2d, ax=axes, output=output)
232
+ set_viewport_width(axes.figure, viewport_width=scene_2d.width)
233
+
234
+ auto_paging_state = AutoPagingState(
235
+ ir=pipeline.ir,
236
+ layout_engine=cast(LayoutEngineLike, pipeline.layout_engine),
237
+ renderer=pipeline.renderer,
238
+ normalized_style=pipeline.normalized_style,
239
+ scene=scene_2d,
240
+ effective_page_width=effective_page_width,
241
+ hover_enabled=prepared_scene.hover.enabled,
242
+ last_viewport_signature=viewport_signature(axes),
243
+ needs_initial_draw_reconcile=reconcile_on_first_draw,
244
+ )
245
+ set_auto_paging_state(axes, auto_paging_state)
246
+ configure_auto_paging(axes, auto_paging_state)
247
+ return axes
248
+
249
+
250
+ def build_continuous_slider_scene(
251
+ circuit: CircuitIR,
252
+ layout_engine: LayoutEngineLike,
253
+ style: DrawStyle,
254
+ *,
255
+ hover_enabled: bool = True,
256
+ ) -> LayoutScene:
257
+ """Compute a continuous-width 2D scene for page-slider mode."""
258
+
259
+ return _build_continuous_slider_scene_impl(
260
+ circuit,
261
+ layout_engine,
262
+ style,
263
+ hover_enabled=hover_enabled,
264
+ )
265
+
266
+
267
+ def viewport_adaptive_paged_scene(
268
+ circuit: CircuitIR,
269
+ layout_engine: LayoutEngineLike,
270
+ style: DrawStyle,
271
+ axes: Axes,
272
+ *,
273
+ hover_enabled: bool = True,
274
+ ) -> tuple[LayoutScene, float]:
275
+ """Return the paged scene whose aspect best matches the current axes viewport."""
276
+
277
+ return _viewport_adaptive_paged_scene_impl(
278
+ circuit,
279
+ layout_engine,
280
+ style,
281
+ axes,
282
+ hover_enabled=hover_enabled,
283
+ )
284
+
285
+
286
+ def compute_paged_scene(
287
+ circuit: CircuitIR,
288
+ layout_engine: LayoutEngineLike,
289
+ style: DrawStyle,
290
+ *,
291
+ hover_enabled: bool = True,
292
+ ) -> LayoutScene:
293
+ """Compute a paged 2D scene using the provided normalized style."""
294
+
295
+ return _compute_paged_scene_impl(
296
+ circuit,
297
+ layout_engine,
298
+ style,
299
+ hover_enabled=hover_enabled,
300
+ )
301
+
302
+
303
+ def configure_auto_paging(axes: Axes, state: object) -> None:
304
+ """Attach resize-aware paging callbacks to the provided axes."""
305
+
306
+ from matplotlib.backend_bases import Event
307
+
308
+ from .renderers._matplotlib_figure import (
309
+ AutoPagingState,
310
+ get_auto_paging_state,
311
+ set_viewport_width,
312
+ )
313
+
314
+ if not isinstance(state, AutoPagingState):
315
+ return
316
+
317
+ canvas = axes.figure.canvas
318
+ if canvas is None:
319
+ return
320
+
321
+ def request_redraw(_event: Event) -> None:
322
+ if state.is_updating or state.redraw_queued:
323
+ return
324
+ state.redraw_queued = True
325
+ canvas.draw_idle()
326
+
327
+ def redraw_if_needed(_event: Event) -> None:
328
+ current_state = get_auto_paging_state(axes)
329
+ if current_state is None or current_state is not state or state.is_updating:
330
+ return
331
+ state.redraw_queued = False
332
+
333
+ current_signature = viewport_signature(axes)
334
+ if current_signature is None:
335
+ return
336
+ if (
337
+ not state.needs_initial_draw_reconcile
338
+ and current_signature == state.last_viewport_signature
339
+ ):
340
+ return
341
+
342
+ cached_candidate = state.viewport_scene_cache.get(current_signature)
343
+ if cached_candidate is None:
344
+ candidate_scene, candidate_page_width = viewport_adaptive_paged_scene(
345
+ state.ir,
346
+ state.layout_engine,
347
+ state.normalized_style,
348
+ axes,
349
+ hover_enabled=state.hover_enabled,
350
+ )
351
+ state.viewport_scene_cache[current_signature] = (candidate_scene, candidate_page_width)
352
+ if len(state.viewport_scene_cache) > 6:
353
+ state.viewport_scene_cache.pop(next(iter(state.viewport_scene_cache)))
354
+ else:
355
+ candidate_scene, candidate_page_width = cached_candidate
356
+ candidate_scene.hover = state.scene.hover
357
+ state.last_viewport_signature = current_signature
358
+ state.needs_initial_draw_reconcile = False
359
+ if auto_paging_matches(
360
+ current_state=state,
361
+ candidate_scene=candidate_scene,
362
+ candidate_page_width=candidate_page_width,
363
+ ):
364
+ return
365
+
366
+ state.is_updating = True
367
+ try:
368
+ axes.clear()
369
+ state.renderer.render(candidate_scene, ax=axes)
370
+ set_viewport_width(axes.figure, viewport_width=candidate_scene.width)
371
+ state.scene = candidate_scene
372
+ state.effective_page_width = candidate_page_width
373
+ finally:
374
+ state.is_updating = False
375
+ canvas.draw_idle()
376
+
377
+ state.resize_callback_id = canvas.mpl_connect("resize_event", request_redraw)
378
+ state.draw_callback_id = canvas.mpl_connect("draw_event", redraw_if_needed)
379
+
380
+
381
+ def auto_paging_matches(
382
+ *,
383
+ current_state: object,
384
+ candidate_scene: LayoutScene,
385
+ candidate_page_width: float,
386
+ ) -> bool:
387
+ """Return whether a candidate scene would keep the current paging unchanged."""
388
+
389
+ return _auto_paging_matches_impl(
390
+ current_state=current_state,
391
+ candidate_scene=candidate_scene,
392
+ candidate_page_width=candidate_page_width,
393
+ )
394
+
395
+
396
+ def viewport_signature(axes: Axes) -> tuple[int, int] | None:
397
+ """Return an integer pixel signature for the current visible axes viewport."""
398
+
399
+ return _viewport_signature_impl(axes)
400
+
401
+
402
+ def axes_viewport_ratio(axes: Axes) -> float:
403
+ """Return the current visible axes aspect ratio in pixels."""
404
+
405
+ return _axes_viewport_ratio_impl(axes)
406
+
407
+
408
+ def axes_viewport_pixels(axes: Axes) -> tuple[float, float]:
409
+ """Return the current axes viewport size in pixels."""
410
+
411
+ return _axes_viewport_pixels_impl(axes)
412
+
413
+
414
+ def scene_aspect_ratio(scene: LayoutScene) -> float:
415
+ """Return the width-to-height ratio of a 2D scene."""
416
+
417
+ return _scene_aspect_ratio_impl(scene)
418
+
419
+
420
+ def viewport_scene_score(scene: LayoutScene, viewport_ratio: float) -> float:
421
+ """Return how closely the scene aspect matches the viewport aspect."""
422
+
423
+ return _viewport_scene_score_impl(scene, viewport_ratio)
424
+
425
+
426
+ def configure_zoom_text_scaling(axes: Axes, *, scene: LayoutScene) -> None:
427
+ """Attach zoom-responsive text scaling to the provided 2D axes."""
428
+
429
+ _configure_zoom_text_scaling_impl(axes, scene=scene)
430
+
431
+
432
+ def current_view_size(axes: Axes) -> tuple[float, float]:
433
+ """Return the current visible data window for the axes."""
434
+
435
+ return _current_view_size_impl(axes)
436
+
437
+
438
+ def current_text_scale(axes: Axes, state: object) -> float:
439
+ """Return the current zoom scale factor for 2D text."""
440
+
441
+ return _current_text_scale_impl(axes, state)
442
+
443
+
444
+ def configure_page_slider(
445
+ *,
446
+ figure: Figure,
447
+ axes: Axes,
448
+ scene: LayoutScene,
449
+ viewport_width: float,
450
+ set_page_slider: Callable[[Figure, object], None],
451
+ ) -> None:
452
+ """Attach and wire a slider that scrolls the rendered circuit horizontally."""
453
+
454
+ _configure_page_slider_impl(
455
+ figure=figure,
456
+ axes=axes,
457
+ scene=scene,
458
+ viewport_width=viewport_width,
459
+ set_page_slider=set_page_slider,
460
+ )
461
+
462
+
463
+ def page_slider_figsize(viewport_width: float, scene_height: float) -> tuple[float, float]:
464
+ """Return a readable managed figure size for page-slider mode."""
465
+
466
+ return _page_slider_figsize_impl(viewport_width, scene_height)
467
+
468
+
469
+ def slider_viewport_width(axes: Axes, scene: LayoutScene) -> float:
470
+ """Estimate the visible scene width for the current axes aspect ratio."""
471
+
472
+ return _slider_viewport_width_impl(axes, scene)
473
+
474
+
475
+ def set_slider_view(
476
+ axes: Axes,
477
+ scene: LayoutScene,
478
+ *,
479
+ x_offset: float,
480
+ viewport_width: float,
481
+ ) -> None:
482
+ """Set the 2D axes limits used for the slider viewport."""
483
+
484
+ _set_slider_view_impl(axes, scene, x_offset=x_offset, viewport_width=viewport_width)
485
+
486
+
487
+ def is_3d_axes(ax: Axes) -> bool:
488
+ """Return whether the provided axes is a Matplotlib 3D axes."""
489
+
490
+ return getattr(ax, "name", "") == "3d"
491
+
492
+
493
+ def is_3d_scene(scene: LayoutScene | LayoutScene3D) -> TypeGuard[LayoutScene3D]:
494
+ """Return whether a prepared scene is a 3D scene."""
495
+
496
+ return hasattr(scene, "depth")
@@ -0,0 +1,95 @@
1
+ """Page-slider helpers for managed 2D rendering."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Callable
6
+
7
+ from matplotlib.axes import Axes
8
+ from matplotlib.figure import Figure
9
+
10
+ from ._draw_managed_viewport import axes_viewport_pixels
11
+ from .layout.scene import LayoutScene
12
+
13
+
14
+ def configure_page_slider(
15
+ *,
16
+ figure: Figure,
17
+ axes: Axes,
18
+ scene: LayoutScene,
19
+ viewport_width: float,
20
+ set_page_slider: Callable[[Figure, object], None],
21
+ ) -> None:
22
+ """Attach and wire a slider that scrolls the rendered circuit horizontally."""
23
+
24
+ max_scroll = max(0.0, scene.width - viewport_width)
25
+ if max_scroll <= 0.0:
26
+ return
27
+
28
+ from matplotlib.widgets import Slider
29
+
30
+ slider_axes = figure.add_axes(
31
+ (0.12, 0.045, 0.76, 0.055),
32
+ facecolor=scene.style.theme.axes_facecolor,
33
+ )
34
+ slider = Slider(
35
+ ax=slider_axes,
36
+ label="Scroll",
37
+ valmin=0.0,
38
+ valmax=max_scroll,
39
+ valinit=0.0,
40
+ color=scene.style.theme.gate_edgecolor,
41
+ track_color=scene.style.theme.classical_wire_color,
42
+ handle_style={
43
+ "facecolor": scene.style.theme.accent_color,
44
+ "edgecolor": scene.style.theme.text_color,
45
+ "size": 16,
46
+ },
47
+ )
48
+ slider.label.set_color(scene.style.theme.text_color)
49
+ slider.valtext.set_visible(False)
50
+ slider.track.set_y(0.12)
51
+ slider.track.set_height(0.76)
52
+ slider.track.set_alpha(0.45)
53
+ slider.poly.set_alpha(0.75)
54
+ slider.vline.set_linewidth(3.0)
55
+
56
+ set_slider_view(axes, scene, x_offset=0.0, viewport_width=viewport_width)
57
+
58
+ def update_scroll(x_offset: float) -> None:
59
+ set_slider_view(axes, scene, x_offset=x_offset, viewport_width=viewport_width)
60
+ if figure.canvas is not None:
61
+ figure.canvas.draw_idle()
62
+
63
+ slider.on_changed(update_scroll)
64
+ set_page_slider(figure, slider)
65
+
66
+
67
+ def page_slider_figsize(viewport_width: float, scene_height: float) -> tuple[float, float]:
68
+ """Return a readable managed figure size for page-slider mode."""
69
+
70
+ width = max(4.8, viewport_width * 0.98)
71
+ height = max(2.0, scene_height * 0.68) + 1.0
72
+ return width, height
73
+
74
+
75
+ def slider_viewport_width(axes: Axes, scene: LayoutScene) -> float:
76
+ """Estimate the visible scene width for the current axes aspect ratio."""
77
+
78
+ axes_width_pixels, axes_height_pixels = axes_viewport_pixels(axes)
79
+ if axes_width_pixels <= 0.0 or axes_height_pixels <= 0.0:
80
+ return scene.width
81
+ viewport_width = scene.height * (axes_width_pixels / axes_height_pixels)
82
+ return min(scene.width, viewport_width)
83
+
84
+
85
+ def set_slider_view(
86
+ axes: Axes,
87
+ scene: LayoutScene,
88
+ *,
89
+ x_offset: float,
90
+ viewport_width: float,
91
+ ) -> None:
92
+ """Set the 2D axes limits used for the slider viewport."""
93
+
94
+ axes.set_xlim(x_offset, x_offset + viewport_width)
95
+ axes.set_ylim(scene.height, 0.0)