51
51
import abc
52
52
from enum import Enum
53
53
from functools import cached_property
54
- from typing import Any , Iterable , List , Literal , Optional , Sequence , TYPE_CHECKING , Union
54
+ from typing import Any , Iterable , List , Optional , Sequence , Union
55
55
56
56
import attrs
57
+ import galois
57
58
import numpy as np
58
59
from fxpmath import Fxp
59
60
from numpy .typing import NDArray
60
61
61
62
from qualtran .symbolics import bit_length , is_symbolic , SymbolicInt
62
63
63
- if TYPE_CHECKING :
64
- import galois
65
-
66
64
67
65
class QDType (metaclass = abc .ABCMeta ):
68
66
"""This defines the abstract interface for quantum data types."""
@@ -870,6 +868,14 @@ def uint_to_montgomery(self, x: int) -> int:
870
868
return (x * pow (2 , int (self .bitsize ), int (self .modulus ))) % self .modulus
871
869
872
870
871
+ def _poly_converter (p ) -> Union [galois .Poly , None ]:
872
+ if p is None :
873
+ return None
874
+ if isinstance (p , galois .Poly ):
875
+ return p
876
+ return galois .Poly .Degrees (p )
877
+
878
+
873
879
@attrs .frozen
874
880
class QGF (QDType ):
875
881
r"""Galois Field type to represent elements of a finite field.
@@ -896,9 +902,7 @@ class QGF(QDType):
896
902
The characteristic must be prime.
897
903
degree: The degree $m$ of the field $GF(p^{m})$. The degree must be a positive integer.
898
904
irreducible_poly: Optional galois.Poly instance that defines the field arithmetic.
899
- This parameter is passed to `galois.GF(..., irreducible_poly=irreducible_poly)`.
900
- element_repr: The string representation of the galois elements.
901
- This parameter is passed to `galois.GF(..., repr=field_repr)`.
905
+ This parameter is passed to `galois.GF(..., irreducible_poly=irreducible_poly, verify=False)`.
902
906
903
907
References
904
908
[Finite Field](https://en.wikipedia.org/wiki/Finite_field)
@@ -910,8 +914,7 @@ class QGF(QDType):
910
914
911
915
characteristic : SymbolicInt
912
916
degree : SymbolicInt
913
- irreducible_poly : Optional ['galois.Poly' ] = attrs .field ()
914
- element_repr : Literal ["int" , "poly" , "power" ] = attrs .field (default = 'int' )
917
+ irreducible_poly : Optional ['galois.Poly' ] = attrs .field (converter = _poly_converter )
915
918
916
919
@irreducible_poly .default
917
920
def _irreducible_poly_default (self ):
@@ -957,12 +960,13 @@ def gf_type(self):
957
960
int (self .characteristic ),
958
961
int (self .degree ),
959
962
irreducible_poly = poly ,
960
- repr = self .element_repr ,
963
+ verify = False ,
964
+ repr = 'poly' ,
961
965
compile = 'python-calculate' ,
962
966
)
963
967
964
968
def to_bits (self , x ) -> List [int ]:
965
- """Yields individual bits corresponding to binary representation of x"""
969
+ """Returns individual bits corresponding to binary representation of x"""
966
970
self .assert_valid_classical_val (x )
967
971
return self ._quint_equivalent .to_bits (int (x ))
968
972
0 commit comments