Skip to content

Commit 6bfd223

Browse files
weinbe58Copilot
andcommitted
Get Bounding box of arch.Layout. (#79)
* Adding method to get total bounds of the layout * adding error when no positions are found * adding tests * changing condition of failure * adding failure check * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * run linter --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent d958f10 commit 6bfd223

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/bloqade/shuttle/arch.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import dataclass, field
2+
from itertools import chain
23
from typing import cast
34

45
import numpy as np
@@ -41,6 +42,31 @@ def __eq__(self, other):
4142
self.static_traps == other.static_traps and self.fillable == other.fillable
4243
)
4344

45+
def bounding_box(self) -> tuple[float, float, float, float]:
46+
"""Get the bounding box (xmin, xmax, ymin, ymax) of the layout."""
47+
xmin = float("inf")
48+
xmax = float("-inf")
49+
ymin = float("inf")
50+
ymax = float("-inf")
51+
52+
for zone in chain(self.static_traps.values(), self.special_grid.values()):
53+
if zone.x_init is not None:
54+
xmin = min(xmin, zone.x_init)
55+
xmax = max(xmax, zone.x_init + zone.width)
56+
if zone.y_init is not None:
57+
ymin = min(ymin, zone.y_init)
58+
ymax = max(ymax, zone.y_init + zone.height)
59+
60+
if (
61+
xmin == float("inf")
62+
or xmax == float("-inf")
63+
or ymin == float("inf")
64+
or ymax == float("-inf")
65+
):
66+
raise ValueError("Layout has incomplete bounding box data.")
67+
68+
return xmin, xmax, ymin, ymax
69+
4470
@staticmethod
4571
def _plot_zone(zone: Grid, ax, name: str, **plot_options):
4672
from matplotlib.axes import Axes # type: ignore

test/unit/test_arch.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from bloqade.geometry.dialects.grid import Grid
23

34
from bloqade.shuttle.arch import Layout
@@ -28,3 +29,16 @@ def test_layout():
2829
{"test"},
2930
)
3031
assert layout != 1
32+
assert layout.bounding_box() == (0.0, 15.0, 0.0, 15.0)
33+
34+
35+
def test_bounding_box_no_positions():
36+
layout = Layout(
37+
{"test": Grid.from_positions([], [])},
38+
{"test"},
39+
{"test"},
40+
{"test"},
41+
)
42+
43+
with pytest.raises(ValueError):
44+
layout.bounding_box()

0 commit comments

Comments
 (0)