Skip to content

types

Grid dataclass

Grid(
    x_spacing: tuple[float, ...],
    y_spacing: tuple[float, ...],
    x_init: float | None,
    y_init: float | None,
)

Bases: Data['Grid'], Generic[NumX, NumY]

height cached property

height

Height of the grid, which is the sum of y_spacing.

positions cached property

positions: IList[tuple[float, float], Any]

All positions in the grid as a list of tuples (x, y) in lexicographic order.

shape cached property

shape: tuple[int, int]

Shape of the grid, which is (num_x, num_y).

Note

if x_init or y_init is None, num_x or num_y will be 0 respectively.

width cached property

width

Width of the grid, which is the sum of x_spacing.

x_init instance-attribute

x_init: float | None

The initial x position of the grid, or None if not set.

x_positions cached property

x_positions: tuple[float, ...]

X positions of the grid.

Note

If x_init is None, returns an empty tuple.

x_spacing instance-attribute

x_spacing: tuple[float, ...]

A tuple of x spacings between grid points.

y_init instance-attribute

y_init: float | None

The initial y position of the grid, or None if not set.

y_positions cached property

y_positions: tuple[float, ...]

Y positions of the grid.

Note

If y_init is None, returns an empty tuple.

y_spacing instance-attribute

y_spacing: tuple[float, ...]

A tuple of y spacings between grid points.

from_positions classmethod

from_positions(
    x_positions: Sequence[float],
    y_positions: Sequence[float],
)

Create a grid from sequence of x and y positions.

Args:

x_positions (Sequence[float]): The x positions.
y_positions (Sequence[float]): The y positions.

Returns:

Name Type Description
Grid

A grid object with the specified x and y positions.

Source code in src/bloqade/geometry/dialects/grid/types.py
 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
100
101
102
103
104
105
106
@classmethod
def from_positions(
    cls,
    x_positions: Sequence[float],
    y_positions: Sequence[float],
):
    """Create a grid from sequence of x and y positions.

    Args:

        x_positions (Sequence[float]): The x positions.
        y_positions (Sequence[float]): The y positions.

    Returns:
        Grid: A grid object with the specified x and y positions.
    """
    x_init = x_positions[0] if len(x_positions) > 0 else None
    y_init = y_positions[0] if len(y_positions) > 0 else None

    if len(x_positions) > 1:
        x_spacing = tuple(
            x_positions[i + 1] - x_positions[i] for i in range(len(x_positions) - 1)
        )
    else:
        x_spacing = ()

    if len(y_positions) > 1:
        y_spacing = tuple(
            y_positions[i + 1] - y_positions[i] for i in range(len(y_positions) - 1)
        )
    else:
        y_spacing = ()

    return cls(x_spacing, y_spacing, x_init, y_init)

get

get(idx: tuple[int, int]) -> tuple[float, float]

Get the (x, y) position at the specified grid index.

Parameters:

Name Type Description Default
idx tuple[int, int]

The (x, y) index in the grid.

required

Returns:

Type Description
tuple[float, float]

tuple[float, float]: The (x, y) position in the grid.

Source code in src/bloqade/geometry/dialects/grid/types.py
194
195
196
197
198
199
200
201
202
203
def get(self, idx: tuple[int, int]) -> tuple[float, float]:
    """Get the (x, y) position at the specified grid index.

    Args:
        idx (tuple[int, int]): The (x, y) index in the grid.

    Returns:
        tuple[float, float]: The (x, y) position in the grid.
    """
    return (self.x_positions[idx[0]], self.y_positions[idx[1]])

get_view

get_view(
    x_indices: IList[int, Nx], y_indices: IList[int, Ny]
) -> Grid[Nx, Ny]

Get a sub-grid view based on the specified x and y indices.

Parameters:

Name Type Description Default
x_indices IList[int, Nx]

The x indices to include in the sub-grid.

required
y_indices IList[int, Ny]

The y indices to include in the sub-grid.

required

Returns:

Type Description
Grid[Nx, Ny]

Grid[Nx, Ny]: The sub-grid view.

Source code in src/bloqade/geometry/dialects/grid/types.py
208
209
210
211
212
213
214
215
216
217
218
219
220
def get_view(
    self, x_indices: ilist.IList[int, Nx], y_indices: ilist.IList[int, Ny]
) -> "Grid[Nx, Ny]":
    """Get a sub-grid view based on the specified x and y indices.

    Args:
        x_indices (ilist.IList[int, Nx]): The x indices to include in the sub-grid.
        y_indices (ilist.IList[int, Ny]): The y indices to include in the sub-grid.

    Returns:
        Grid[Nx, Ny]: The sub-grid view.
    """
    return SubGrid(parent=self, x_indices=x_indices, y_indices=y_indices)

is_equal

is_equal(other: Any) -> bool

Check if two grid geometry are equal.

Source code in src/bloqade/geometry/dialects/grid/types.py
62
63
64
65
66
67
68
69
70
71
def is_equal(self, other: Any) -> bool:
    """Check if two grid geometry are equal."""
    if not isinstance(other, Grid):
        return False
    return (
        self.x_spacing == other.x_spacing
        and self.y_spacing == other.y_spacing
        and self.x_init == other.x_init
        and self.y_init == other.y_init
    )

repeat

repeat(
    x_times: int, y_times: int, x_gap: float, y_gap: float
) -> Grid[NumX, NumY]

Repeat the grid in both x and y directions with specified gaps.

Parameters:

Name Type Description Default
x_times int

The number of times to repeat the grid in the x direction.

required
y_times int

The number of times to repeat the grid in the y direction.

required
x_gap float

The gap between repeated grids in the x direction.

required
y_gap float

The gap between repeated grids in the y direction.

required

Returns:

Type Description
Grid[NumX, NumY]

Grid[NumX, NumY]: A new grid with the specified repetitions and gaps.

Source code in src/bloqade/geometry/dialects/grid/types.py
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
def repeat(
    self, x_times: int, y_times: int, x_gap: float, y_gap: float
) -> "Grid[NumX, NumY]":
    """Repeat the grid in both x and y directions with specified gaps.

    Args:
        x_times (int): The number of times to repeat the grid in the x direction.
        y_times (int): The number of times to repeat the grid in the y direction.
        x_gap (float): The gap between repeated grids in the x direction.
        y_gap (float): The gap between repeated grids in the y direction.

    Returns:
        Grid[NumX, NumY]: A new grid with the specified repetitions and gaps.

    """

    if x_times < 1 or y_times < 1:
        raise ValueError("x_times and y_times must be non-negative")

    return Grid(
        x_spacing=sum((self.x_spacing + (x_gap,) for _ in range(x_times - 1)), ())
        + self.x_spacing,
        y_spacing=sum((self.y_spacing + (y_gap,) for _ in range(y_times - 1)), ())
        + self.y_spacing,
        x_init=self.x_init,
        y_init=self.y_init,
    )

scale

scale(x_scale: float, y_scale: float) -> Grid[NumX, NumY]

Scale the grid spacings by the specified x and y factors with fixed x and y initial positions.

Parameters:

Name Type Description Default
x_scale float

The scaling factor for the x spacings.

required
y_scale float

The scaling factor for the y spacings.

required

Returns:

Type Description
Grid[NumX, NumY]

Grid[NumX, NumY]: A new grid with scaled x and y spacings

Source code in src/bloqade/geometry/dialects/grid/types.py
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
def scale(self, x_scale: float, y_scale: float) -> "Grid[NumX, NumY]":
    """Scale the grid spacings by the specified x and y factors with fixed x and y initial positions.

    Args:
        x_scale (float): The scaling factor for the x spacings.
        y_scale (float): The scaling factor for the y spacings.

    Returns:
        Grid[NumX, NumY]: A new grid with scaled x and y spacings

    """
    return Grid(
        x_spacing=tuple(spacing * x_scale for spacing in self.x_spacing),
        y_spacing=tuple(spacing * y_scale for spacing in self.y_spacing),
        x_init=self.x_init,
        y_init=self.y_init,
    )

set_init

set_init(
    x_init: float | None, y_init: float | None
) -> Grid[NumX, NumY]

Set the initial positions of the grid.

Parameters:

Name Type Description Default
x_init float | None

The new initial x position. If None, the grid will not have an initial x position.

required
y_init float | None

The new initial y position. If None, the grid will not have an initial y position.

required

Returns:

Type Description
Grid[NumX, NumY]

Grid[NumX, NumY]: A new grid with the specified initial positions.

Source code in src/bloqade/geometry/dialects/grid/types.py
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
def set_init(
    self, x_init: float | None, y_init: float | None
) -> "Grid[NumX, NumY]":
    """Set the initial positions of the grid.

    Args:
        x_init (float | None): The new initial x position. If None, the grid
            will not have an initial x position.
        y_init (float | None): The new initial y position. If None, the grid
            will not have an initial y position.

    Returns:
        Grid[NumX, NumY]: A new grid with the specified initial positions.

    """
    return Grid(self.x_spacing, self.y_spacing, x_init, y_init)

shift

shift(x_shift: float, y_shift: float) -> Grid[NumX, NumY]

Shift the grid by the specified x and y amounts.

Parameters:

Name Type Description Default
x_shift float

The amount to shift the grid in the x direction.

required
y_shift float

The amount to shift the grid in the y direction.

required

Returns:

Type Description
Grid[NumX, NumY]

Grid[NumX, NumY]: A new grid with the specified shifts applied to the initial positions.

Source code in src/bloqade/geometry/dialects/grid/types.py
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
def shift(self, x_shift: float, y_shift: float) -> "Grid[NumX, NumY]":
    """Shift the grid by the specified x and y amounts.

    Args:
        x_shift (float): The amount to shift the grid in the x direction.
        y_shift (float): The amount to shift the grid in the y direction.

    Returns:
        Grid[NumX, NumY]: A new grid with the specified shifts applied to the initial positions.

    """
    return Grid(
        x_spacing=self.x_spacing,
        y_spacing=self.y_spacing,
        x_init=self.x_init + x_shift if self.x_init is not None else None,
        y_init=self.y_init + y_shift if self.y_init is not None else None,
    )

x_bounds

x_bounds()

X bounds of the grid, which is (x_init, x_init + width).

Raises:

Type Description
ValueError

If x_init is None, cannot compute bounds.

Source code in src/bloqade/geometry/dialects/grid/types.py
130
131
132
133
134
135
136
137
138
139
140
def x_bounds(self):
    """X bounds of the grid, which is `(x_init, x_init + width)`.

    Raises:
        ValueError: If x_init is None, cannot compute bounds.

    """
    if self.x_init is None:
        raise ValueError("x_init is None, cannot compute bounds")

    return (self.x_init, self.x_init + self.width)

y_bounds

y_bounds()

Y bounds of the grid, which is (y_init, y_init + height).

Raises:

Type Description
ValueError

If y_init is None, cannot compute bounds.

Source code in src/bloqade/geometry/dialects/grid/types.py
142
143
144
145
146
147
148
149
150
151
152
def y_bounds(self):
    """Y bounds of the grid, which is `(y_init, y_init + height)`.

    Raises:
        ValueError: If y_init is None, cannot compute bounds.

    """
    if self.y_init is None:
        raise ValueError("y_init is None, cannot compute bounds")

    return (self.y_init, self.y_init + self.height)

SubGrid dataclass

SubGrid(
    x_spacing: tuple[float, ...],
    y_spacing: tuple[float, ...],
    x_init: float | None,
    y_init: float | None,
    parent: Grid[Any, Any],
    x_indices: IList[int, NumX],
    y_indices: IList[int, NumY],
)

Bases: Grid[NumX, NumY]

A sub-grid view of a parent grid with specified x and y indices.

For API documentation see the Grid class.

x_init class-attribute instance-attribute

x_init: float | None = field(init=False)

The initial x position of the grid, or None if not set.

x_spacing class-attribute instance-attribute

x_spacing: tuple[float, ...] = field(init=False)

A tuple of x spacings between grid points.

y_init class-attribute instance-attribute

y_init: float | None = field(init=False)

The initial y position of the grid, or None if not set.

y_spacing class-attribute instance-attribute

y_spacing: tuple[float, ...] = field(init=False)

A tuple of y spacings between grid points.

get_view

get_view(
    x_indices: IList[int, Any], y_indices: IList[int, Any]
)

Get a sub-grid view based on the specified x and y indices.

Parameters:

Name Type Description Default
x_indices IList[int, Nx]

The x indices to include in the sub-grid.

required
y_indices IList[int, Ny]

The y indices to include in the sub-grid.

required

Returns:

Type Description
Grid[Nx, Ny]

Grid[Nx, Ny]: The sub-grid view.

Source code in src/bloqade/geometry/dialects/grid/types.py
431
432
433
434
435
436
437
def get_view(
    self, x_indices: ilist.IList[int, Any], y_indices: ilist.IList[int, Any]
):
    return self.parent.get_view(
        x_indices=ilist.IList([self.x_indices[x_index] for x_index in x_indices]),
        y_indices=ilist.IList([self.y_indices[y_index] for y_index in y_indices]),
    )