Skip to content

Commit

Permalink
Merge pull request #128 from Infleqtion/protograph-fixes
Browse files Browse the repository at this point in the history
Protograph bugfixes
  • Loading branch information
perlinm authored Aug 14, 2024
2 parents 16e9e5e + 76b7d01 commit 01ffee4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
16 changes: 12 additions & 4 deletions qldpc/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,15 +580,16 @@ def __new__(
for value in protograph.ravel():
if not isinstance(value, Element):
raise ValueError(
"Requirement failed: all entries of a protograph must be Element-valued"
"Requirement failed: all entries of a protograph must be Element-valued\n"
"Try building a protograph with Protograph.build(...)"
)
else:
if not (group is None or group == value.group):
raise ValueError("Inconsistent base groups provided for protograph")
raise ValueError("Inconsistent base groups provided for a protograph")
group = value.group

if group is None:
raise ValueError("Cannot determine underlying group for a protograh")
raise ValueError("Cannot determine the underlying group for a protograh")
protograph._group = group

return protograph
Expand All @@ -601,7 +602,7 @@ def group(self) -> Group:
@property
def field(self) -> type[galois.FieldArray]:
"""Base field of this protograph."""
return self.group.field
return self._group.field

def lift(self) -> galois.FieldArray:
"""Block matrix obtained by lifting each entry of the protograph."""
Expand Down Expand Up @@ -646,6 +647,13 @@ def elevate(value: Element | GroupMember | int) -> Element:
vals = [elevate(value) for value in array.ravel()]
return Protograph(np.array(vals).reshape(array.shape), group)

def __matmul__(self, other: object) -> Protograph:
if isinstance(other, Protograph) and not self._group == other._group:
raise ValueError("Cannot multiply protographs with different base groups")
protograph = super().__matmul__(other)
protograph._group = self._group
return protograph


################################################################################
# "simple" named groups
Expand Down
6 changes: 5 additions & 1 deletion qldpc/abstract_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,19 @@ def test_protograph() -> None:
assert protograph.group == abstract.TrivialGroup()
assert protograph.field == abstract.TrivialGroup().field
assert np.array_equal(protograph.lift(), matrix)
assert np.array_equal((protograph @ protograph).lift(), matrix @ matrix % 2)

# fail to construct a valid protograph
with pytest.raises(ValueError, match="must be Element-valued"):
abstract.Protograph([[0]])
with pytest.raises(ValueError, match="Inconsistent base groups"):
groups = [abstract.TrivialGroup(), abstract.CyclicGroup(1)]
abstract.Protograph([[abstract.Element(group) for group in groups]])
with pytest.raises(ValueError, match="Cannot determine underlying group"):
with pytest.raises(ValueError, match="Cannot determine the underlying group"):
abstract.Protograph([])
with pytest.raises(ValueError, match="different base groups"):
new_protograph = abstract.Protograph.build(abstract.CyclicGroup(1), [[1]])
protograph @ new_protograph


def test_transpose() -> None:
Expand Down

0 comments on commit 01ffee4

Please sign in to comment.