Skip to content

Commit

Permalink
Second batch of corrections, removed RandomLayout and TriangRectShape
Browse files Browse the repository at this point in the history
  • Loading branch information
soufianekaghad98 committed Apr 26, 2024
1 parent e8860de commit 8ef21d6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 133 deletions.
1 change: 1 addition & 0 deletions pulser-core/pulser/json/supported.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"RectangularLatticeLayout",
"SquareLatticeLayout",
"TriangularLatticeLayout",
# "RectangularLatticeLayout",
),
"pulser.register.mappable_reg": ("MappableRegister",),
"pulser.register.weight_maps": ("DetuningMap",),
Expand Down
7 changes: 2 additions & 5 deletions pulser-core/pulser/register/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
from pulser.register.register3d import Register3D
from pulser.register.register_layout import RegisterLayout
from pulser.register.special_layouts import (
RectangularLatticeLayout,
SquareLatticeLayout,
TriangularLatticeLayout,
TriangularLatticeLayoutRectShape,
RandomLayout,
RectangularLatticeLayout,
)

__all__ = [
Expand All @@ -32,6 +30,5 @@
"RegisterLayout",
"SquareLatticeLayout",
"TriangularLatticeLayout",
"TriangularLatticeLayoutRectShape",
"RandomLayout",
"RectangularLatticeLayout",
]
42 changes: 9 additions & 33 deletions pulser-core/pulser/register/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def rectangle(
spacing: float = 4.0,
prefix: Optional[str] = None,
) -> Register:
"""Initializes register, qubits in a rectangular array, square lattice.
"""Creates a rectangular array of qubits on a square lattice.
Args:
rows: Number of rows.
Expand All @@ -102,41 +102,18 @@ def rectangle(
Returns:
A register with qubits placed in a rectangular array.
"""
# Check rows
if rows < 1:
raise ValueError(
f"The number of rows (`rows` = {rows})"
" must be greater than or equal to 1."
)

# Check columns
if columns < 1:
raise ValueError(
f"The number of columns (`columns` = {columns})"
" must be greater than or equal to 1."
)

# Check spacing
if spacing <= 0.0:
raise ValueError(
f"Spacing between atoms (`spacing` = {spacing})"
" must be greater than 0."
)

coords = patterns.square_rect(rows, columns) * spacing

return cls.from_coordinates(coords, center=True, prefix=prefix)
return cls.rectangular_lattice(rows, columns, spacing, spacing, prefix)

@classmethod
def rectangle_rect_lattice(
def rectangular_lattice(
cls,
rows: int,
columns: int,
row_spacing: float = 4.0,
col_spacing: float = 2.0,
prefix: Optional[str] = None,
) -> Register:
"""Initializes register, qubits in rectangle array, rectangle lattice.
"""Creates a rectangular array of qubits on a rectangular lattice.
Args:
rows: Number of rows.
Expand All @@ -148,7 +125,8 @@ def rectangle_rect_lattice(
(e.g. prefix='q' -> IDs: 'q0', 'q1', 'q2', ...)
Returns:
Register, qubits placed in a rectangular array, rectangle lattice.
Register with qubits placed in a rectangular array on a
rectangular lattice.
"""
# Check rows
if rows < 1:
Expand All @@ -166,13 +144,11 @@ def rectangle_rect_lattice(

# Check spacing
if row_spacing <= 0.0 or col_spacing <= 0.0:
raise ValueError(
"Spacing between atoms must be greater than 0."
)
raise ValueError("Spacing between atoms must be greater than 0.")

coords = patterns.square_rect(rows, columns)
coords[:, 0] = coords[:, 0]*col_spacing
coords[:, 1] = coords[:, 1]*row_spacing
coords[:, 0] = coords[:, 0] * col_spacing
coords[:, 1] = coords[:, 1] * row_spacing

return cls.from_coordinates(coords, center=True, prefix=prefix)

Expand Down
82 changes: 5 additions & 77 deletions pulser-core/pulser/register/special_layouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
if TYPE_CHECKING:
from pulser.register import Register

import numpy as np
from scipy.spatial.distance import cdist


class RectangularLatticeLayout(RegisterLayout):
"""RegisterLayout with rectangular lattice pattern in a rectangular shape.
Expand Down Expand Up @@ -108,8 +105,11 @@ def rectangular_register(

def _to_dict(self) -> dict[str, Any]:
return obj_to_dict(
self, self._rows, self._columns, self._col_spacing,
self._row_spacing
self,
self._rows,
self._columns,
self._col_spacing,
self._row_spacing,
)


Expand Down Expand Up @@ -215,75 +215,3 @@ def rectangular_register(

def _to_dict(self) -> dict[str, Any]:
return obj_to_dict(self, self.number_of_traps, self._spacing)


class TriangularLatticeLayoutRectShape(RegisterLayout):
"""RegisterLayout with a triangular lattice pattern in a rectangular shape.
Args:
n_traps: The number of traps in the layout.
spacing: The distance between neighbouring traps (in µm).
"""

def __init__(self, columns: int, rows: int, spacing: float = 5):
"""Initializes a TriangularLatticeLayout."""
self._spacing = float(spacing)
self._columns = int(columns)
self._rows = int(rows)
slug = (
f"TriangularLatticeLayoutRectshape({self._rows}x{self._columns}, "
f"{self._spacing}µm)"
)
super().__init__(
patterns.triangular_rect(self._rows, self._columns)
* self._spacing,
slug=slug,
)


class RandomLayout(RegisterLayout):
"""A RegisterLayout generated randomly.
Args :
n_traps : the number of traps in the layout
radius : radius defining the working area
min_dist : minimum distance between traps
max_iter = maximum number of iterations to compute the random layout
"""

def __init__(
self,
n_traps: int,
radius: float = 35,
min_spacing: float = 5,
max_iter: int = 1000,
):
"""Initializes a random layout."""
self._pts : np.ndarray = []
self._n_traps = int(n_traps)
self._min_spacing = float(min_spacing)
i = 0
while len(self._pts) < n_traps and i < max_iter:
pt_x = np.random.uniform(low=-radius, high=radius, size=1)
pt_y = np.random.uniform(low=-radius, high=radius, size=1)
pt = np.stack([pt_x, pt_y], axis=1)
if not self._pts and pt_x**2 + pt_y**2 <= radius**2:
self._pts.append(pt)
continue
if len(self._pts) == 0:
continue
dist = cdist(np.concatenate(self._pts), pt)
if pt_x**2 + pt_y**2 <= radius**2 and np.all(
dist > self._min_spacing
):
self._pts.append(pt)
i += 1
if len(self._pts) < n_traps:
raise ValueError(
"Could not compute random traps in max iterations"
)
slug = (
f"RandomLayout({self._n_traps} traps, "
f"min spacing {self._min_spacing}µm)"
)
super().__init__(trap_coordinates=np.concatenate(self._pts), slug=slug)
21 changes: 3 additions & 18 deletions tests/test_register_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
from pulser.register import Register, Register3D
from pulser.register.register_layout import RegisterLayout
from pulser.register.special_layouts import (
RandomLayout,
RectangularLatticeLayout,
SquareLatticeLayout,
TriangularLatticeLayout,
TriangularLatticeLayoutRectShape,
)


Expand Down Expand Up @@ -195,15 +193,12 @@ def test_square_lattice_layout():

def test_rectangular_lattice_layout():
rectangle = RectangularLatticeLayout(9, 7, 2, 4)
assert (
str(rectangle)
== "RectangularLatticeLayout(9x7, 2.0x4.0µm)"
)
assert rectangle.square_register(3) == Register.rectangle_rect_lattice(
assert str(rectangle) == "RectangularLatticeLayout(9x7, 2.0x4.0µm)"
assert rectangle.square_register(3) == Register.rectangular_lattice(
3, 3, col_spacing=2, row_spacing=4, prefix="q"
)
# An even number of atoms on the side won't align the center with an atom
assert rectangle.square_register(4) != Register.rectangle_rect_lattice(
assert rectangle.square_register(4) != Register.rectangular_lattice(
4, 4, col_spacing=2, row_spacing=4, prefix="q"
)
with pytest.raises(ValueError, match="'8x8' array doesn't fit"):
Expand Down Expand Up @@ -243,16 +238,6 @@ def test_triangular_lattice_layout():
)


def test_triangular_lattice_layout_rect_shape():
tri = TriangularLatticeLayoutRectShape(10, 4, 5)
assert str(tri) == "TriangularLatticeLayoutRectshape(4x10, 5.0µm)"


def test_random_layout():
rand = RandomLayout(50, max_iter=10000, min_spacing=4)
assert str(rand) == "RandomLayout(50 traps, min spacing 4.0µm)"


def test_mappable_register_creation():
tri = TriangularLatticeLayout(50, 5)
with pytest.raises(ValueError, match="greater than the number of traps"):
Expand Down

0 comments on commit 8ef21d6

Please sign in to comment.