5151import abc
5252from enum import Enum
5353from 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
5555
5656import attrs
57+ import galois
5758import numpy as np
5859from fxpmath import Fxp
5960from numpy .typing import NDArray
6061
6162from qualtran .symbolics import bit_length , is_symbolic , SymbolicInt
6263
63- if TYPE_CHECKING :
64- import galois
65-
6664
6765class QDType (metaclass = abc .ABCMeta ):
6866 """This defines the abstract interface for quantum data types."""
@@ -870,6 +868,14 @@ def uint_to_montgomery(self, x: int) -> int:
870868 return (x * pow (2 , int (self .bitsize ), int (self .modulus ))) % self .modulus
871869
872870
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+
873879@attrs .frozen
874880class QGF (QDType ):
875881 r"""Galois Field type to represent elements of a finite field.
@@ -896,9 +902,7 @@ class QGF(QDType):
896902 The characteristic must be prime.
897903 degree: The degree $m$ of the field $GF(p^{m})$. The degree must be a positive integer.
898904 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)`.
902906
903907 References
904908 [Finite Field](https://en.wikipedia.org/wiki/Finite_field)
@@ -910,8 +914,7 @@ class QGF(QDType):
910914
911915 characteristic : SymbolicInt
912916 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 )
915918
916919 @irreducible_poly .default
917920 def _irreducible_poly_default (self ):
@@ -957,12 +960,13 @@ def gf_type(self):
957960 int (self .characteristic ),
958961 int (self .degree ),
959962 irreducible_poly = poly ,
960- repr = self .element_repr ,
963+ verify = False ,
964+ repr = 'poly' ,
961965 compile = 'python-calculate' ,
962966 )
963967
964968 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"""
966970 self .assert_valid_classical_val (x )
967971 return self ._quint_equivalent .to_bits (int (x ))
968972
0 commit comments