diff --git a/qualtran/_infra/data_types.py b/qualtran/_infra/data_types.py index 8c541bfcc..bc3e80e28 100644 --- a/qualtran/_infra/data_types.py +++ b/qualtran/_infra/data_types.py @@ -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 @@ -906,8 +906,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: @@ -936,10 +947,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', ) diff --git a/qualtran/_infra/data_types_test.py b/qualtran/_infra/data_types_test.py index ff08b7df6..0c889cdc8 100644 --- a/qualtran/_infra/data_types_test.py +++ b/qualtran/_infra/data_types_test.py @@ -540,3 +540,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