Skip to content

Commit

Permalink
Merge pull request #63 from Infleqtion/group-name
Browse files Browse the repository at this point in the history
Add `Group.name` property
  • Loading branch information
perlinm authored Apr 1, 2024
2 parents 8ec0b0d + c88d39b commit e03a1ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
32 changes: 27 additions & 5 deletions qldpc/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,26 @@ class Group:
_group: comb.PermutationGroup
_field: type[galois.FieldArray]
_lift: Lift
_name: str | None

def __init__(
self, *generators: comb.Permutation, field: int | None = None, lift: Lift | None = None
self,
*generators: comb.Permutation,
field: int | None = None,
lift: Lift | None = None,
name: str | None = None,
) -> None:
self._init_from_group(comb.PermutationGroup(*generators), field, lift)
self._init_from_group(comb.PermutationGroup(*generators), field, lift, name)

def _init_from_group(
self,
group: comb.PermutationGroup | Group,
field: int | None = None,
lift: Lift | None = None,
name: str | None = None,
) -> None:
"""Initialize from an existing group."""
self._name = name
if isinstance(group, comb.PermutationGroup):
self._group = group
self._field = galois.GF(field or DEFAULT_FIELD_ORDER)
Expand All @@ -152,6 +159,15 @@ def _init_from_group(
self._group = group._group
self._field = group._field
self._lift = lift if lift is not None else group._lift
self._name = self._name or group._name # explicitly provided name overrides group name

@property
def name(self) -> str:
"""A name for this group, which is not required to uniquely identify the group."""
return self._name or f"{type(self).__name__} of order {self.order}"

def __str__(self) -> str:
return self.name

def to_sympy(self) -> comb.PermutationGroup:
"""The underlying SymPy permutation group of this Group."""
Expand Down Expand Up @@ -395,7 +411,8 @@ def from_name(cls, name: str) -> Group:
"""Named group in the GAP computer algebra system."""
standardized_name = name.strip().replace(" ", "") # remove whitespace
generators = [GroupMember(gen) for gen in named_groups.get_generators(standardized_name)]
return Group(*generators)
group = Group(*generators, name=standardized_name)
return group


################################################################################
Expand Down Expand Up @@ -641,6 +658,7 @@ def __init__(self, field: int | None = None) -> None:
super().__init__(
field=field,
lift=lambda _: np.array(1, ndmin=2, dtype=int),
name=TrivialGroup.__name__,
)

def random(self, *, seed: int | None = None) -> GroupMember:
Expand Down Expand Up @@ -692,7 +710,9 @@ def __init__(self, *orders: int, product_lift: bool = False) -> None:
group = Group.product(*groups)
else:
group = comb.named_groups.AbelianGroup(*orders)
super()._init_from_group(group)
order_text = ",".join(map(str, orders))
name = f"AbelianGroup({order_text})"
super()._init_from_group(group, name=name)


class DihedralGroup(Group):
Expand Down Expand Up @@ -750,7 +770,7 @@ def lift(member: int) -> npt.NDArray[np.int_]:
return sign * np.block(blocks).T % 3

group = Group.from_table(table, field=3, integer_lift=lift)
super()._init_from_group(group)
super()._init_from_group(group, name=QuaternionGroup.__name__)


class SmallGroup(Group):
Expand Down Expand Up @@ -789,6 +809,7 @@ class SpecialLinearGroup(Group):
_dimension: int

def __init__(self, dimension: int, field: int | None = None, linear_rep: bool = True) -> None:
self._name = f"SL({dimension},{field})"
self._dimension = dimension
self._field = galois.GF(field or DEFAULT_FIELD_ORDER)

Expand Down Expand Up @@ -873,6 +894,7 @@ class ProjectiveSpecialLinearGroup(Group):
_dimension: int

def __init__(self, dimension: int, field: int | None = None) -> None:
self._name = f"PSL({dimension},{field})"
self._dimension = dimension
self._field = galois.GF(field or DEFAULT_FIELD_ORDER)
group: Group
Expand Down
1 change: 1 addition & 0 deletions qldpc/abstract_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_trivial_group() -> None:
assert group.random() == group.identity
assert np.array_equal(group.lift(group.identity), np.array(1, ndmin=2))
assert group == abstract.Group.from_generating_mats()
assert str(group) == "TrivialGroup"


def test_lift() -> None:
Expand Down

0 comments on commit e03a1ac

Please sign in to comment.