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_view(
    x_indices: Sequence[int], y_indices: IList[int, Ny]
) -> Grid[Any, Ny]
get_view(
    x_indices: IList[int, Nx], y_indices: Sequence[int]
) -> Grid[Nx, Any]
get_view(
    x_indices: Sequence[int], y_indices: Sequence[int]
) -> Grid[Any, Any]
get_view(x_indices, y_indices) -> Grid

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

Parameters:

Name Type Description Default
x_indices Sequence[int]

The x indices to include in the sub-grid.

required
y_indices Sequence[int]

The y indices to include in the sub-grid.

required

Returns:

Name Type Description
Grid Grid

The sub-grid view.

Source code in src/bloqade/geometry/dialects/grid/types.py
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
def get_view(self, x_indices, y_indices) -> "Grid":
    """Get a sub-grid view based on the specified x and y indices.

    Args:
        x_indices (Sequence[int]): The x indices to include in the sub-grid.
        y_indices (Sequence[int]): The y indices to include in the sub-grid.

    Returns:
        Grid: The sub-grid view.
    """
    if isinstance(x_indices, ilist.IList):
        x_indices = x_indices.data

    if isinstance(y_indices, ilist.IList):
        y_indices = y_indices.data

    return SubGrid(
        parent=self,
        x_indices=ilist.IList(x_indices),
        y_indices=ilist.IList(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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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,
    )

shift_subgrid_x

shift_subgrid_x(
    x_indices: IList[int, Nx] | slice, x_shift: float
) -> Grid[NumX, NumY]

Shift a sub grid of grid in the x directions.

Parameters:

Name Type Description Default
grid Grid

a grid object

required
x_indices float

a list/ilist of x indices to shift

required
x_shift float

shift in the x direction

required

Returns: Grid: a new grid object that has been shifted

Source code in src/bloqade/geometry/dialects/grid/types.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
413
414
415
416
417
def shift_subgrid_x(
    self, x_indices: ilist.IList[int, Nx] | slice, x_shift: float
) -> "Grid[NumX, NumY]":
    """Shift a sub grid of grid in the x directions.

    Args:
        grid (Grid): a grid object
        x_indices (float): a list/ilist of x indices to shift
        x_shift (float): shift in the x direction
    Returns:
        Grid: a new grid object that has been shifted
    """
    indices = get_indices(len(self.x_spacing) + 1, x_indices)

    def shift_x(index):
        new_spacing = self.x_spacing[index]
        if index in indices and (index + 1) not in indices:
            new_spacing -= x_shift
        elif index not in indices and (index + 1) in indices:
            new_spacing += x_shift
        return new_spacing

    new_spacing = tuple(shift_x(i) for i in range(len(self.x_spacing)))

    assert all(
        x >= 0 for x in new_spacing
    ), "Invalid shift: column order changes after shift."

    x_init = self.x_init
    if x_init is not None and 0 in indices:
        x_init += x_shift

    return Grid(
        x_spacing=new_spacing,
        y_spacing=self.y_spacing,
        x_init=x_init,
        y_init=self.y_init,
    )

shift_subgrid_y

shift_subgrid_y(
    y_indices: IList[int, Ny] | slice, y_shift: float
) -> Grid[NumX, NumY]

Shift a sub grid of grid in the y directions.

Parameters:

Name Type Description Default
grid Grid

a grid object

required
y_indices float

a list/ilist of y indices to shift

required
y_shift float

shift in the y direction

required

Returns: Grid: a new grid object that has been shifted

Source code in src/bloqade/geometry/dialects/grid/types.py
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
456
def shift_subgrid_y(
    self, y_indices: ilist.IList[int, Ny] | slice, y_shift: float
) -> "Grid[NumX, NumY]":
    """Shift a sub grid of grid in the y directions.

    Args:
        grid (Grid): a grid object
        y_indices (float): a list/ilist of y indices to shift
        y_shift (float): shift in the y direction
    Returns:
        Grid: a new grid object that has been shifted
    """
    indices = get_indices(len(self.y_spacing) + 1, y_indices)

    def shift_y(index):
        new_spacing = self.y_spacing[index]
        if index in indices and (index + 1) not in indices:
            new_spacing -= y_shift
        elif index not in indices and (index + 1) in indices:
            new_spacing += y_shift
        return new_spacing

    new_spacing = tuple(shift_y(i) for i in range(len(self.y_spacing)))

    assert all(
        y >= 0 for y in new_spacing
    ), "Invalid shift: row order changes after shift."

    y_init = self.y_init
    if y_init is not None and 0 in indices:
        y_init += y_shift

    return Grid(
        x_spacing=self.x_spacing,
        y_spacing=new_spacing,
        x_init=self.x_init,
        y_init=y_init,
    )

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, y_indices)

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

Parameters:

Name Type Description Default
x_indices Sequence[int]

The x indices to include in the sub-grid.

required
y_indices Sequence[int]

The y indices to include in the sub-grid.

required

Returns:

Name Type Description
Grid Grid

The sub-grid view.

Source code in src/bloqade/geometry/dialects/grid/types.py
537
538
539
540
541
def get_view(self, x_indices, y_indices):
    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]),
    )