Skip to content

Commit

Permalink
Merge branch 'main' into montgomery
Browse files Browse the repository at this point in the history
  • Loading branch information
NoureldinYosri authored Feb 5, 2025
2 parents 0444490 + 676b02a commit ff1e2b2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 17 additions & 4 deletions qualtran/_infra/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import abc
from enum import Enum
from functools import cached_property
from typing import Any, Iterable, List, Optional, Sequence, TYPE_CHECKING, Union
from typing import Any, Iterable, List, Literal, Optional, Sequence, TYPE_CHECKING, Union

import attrs
import numpy as np
Expand Down Expand Up @@ -908,8 +908,19 @@ class QGF(QDType):

characteristic: SymbolicInt
degree: SymbolicInt
irreducible_poly: Optional['galois.Poly'] = None
element_repr: str = 'int'
irreducible_poly: Optional['galois.Poly'] = attrs.field()
element_repr: Literal["int", "poly", "power"] = attrs.field(default='int')

@irreducible_poly.default
def _irreducible_poly_default(self):
if is_symbolic(self.characteristic, self.degree):
return None

from galois import GF

return GF( # type: ignore[call-overload]
int(self.characteristic), int(self.degree), compile='python-calculate'
).irreducible_poly

@cached_property
def order(self) -> SymbolicInt:
Expand Down Expand Up @@ -938,10 +949,12 @@ def _quint_equivalent(self) -> QUInt:
def gf_type(self):
from galois import GF

poly = self.irreducible_poly if self.degree > 1 else None

return GF( # type: ignore[call-overload]
int(self.characteristic),
int(self.degree),
irreducible_poly=self.irreducible_poly,
irreducible_poly=poly,
repr=self.element_repr,
compile='python-calculate',
)
Expand Down
8 changes: 8 additions & 0 deletions qualtran/_infra/data_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,11 @@ def test_montgomery_bit_conversion(bitsize):
dtype = QMontgomeryUInt(bitsize)
for v in range(1 << bitsize):
assert v == dtype.from_bits(dtype.to_bits(v))


def test_qgf_with_default_poly_is_compatible():
qgf_one = QGF(2, 4)

qgf_two = QGF(2, 4, irreducible_poly=qgf_one.gf_type.irreducible_poly)

assert qgf_one == qgf_two

0 comments on commit ff1e2b2

Please sign in to comment.