Skip to content
2 changes: 2 additions & 0 deletions dev_tools/qualtran_dev_tools/notebook_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@
qualtran.bloqs.gf_arithmetic.gf2_multiplication._MULTIPLY_BY_CONSTANT_MOD_DOC,
qualtran.bloqs.gf_arithmetic.gf2_multiplication._MULTIPLY_POLY_BY_ONE_PLUS_XK_DOC,
qualtran.bloqs.gf_arithmetic.gf2_multiplication._BINARY_POLYNOMIAL_MULTIPLICATION_DOC,
qualtran.bloqs.gf_arithmetic.gf2_multiplication._GF2_SHIFT_RIGHT_MOD_DOC,
qualtran.bloqs.gf_arithmetic.gf2_multiplication._GF2_MUL_DOC,
],
),
NotebookSpecV2(
Expand Down
26 changes: 15 additions & 11 deletions qualtran/_infra/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,16 @@
import abc
from enum import Enum
from functools import cached_property
from typing import Any, Iterable, List, Literal, Optional, Sequence, TYPE_CHECKING, Union
from typing import Any, Iterable, List, Optional, Sequence, Union

import attrs
import galois
import numpy as np
from fxpmath import Fxp
from numpy.typing import NDArray

from qualtran.symbolics import bit_length, is_symbolic, SymbolicInt

if TYPE_CHECKING:
import galois


class QDType(metaclass=abc.ABCMeta):
"""This defines the abstract interface for quantum data types."""
Expand Down Expand Up @@ -870,6 +868,14 @@ def uint_to_montgomery(self, x: int) -> int:
return (x * pow(2, int(self.bitsize), int(self.modulus))) % self.modulus


def _poly_converter(p) -> Union[galois.Poly, None]:
if p is None:
return None
if isinstance(p, galois.Poly):
return p
return galois.Poly.Degrees(p)


@attrs.frozen
class QGF(QDType):
r"""Galois Field type to represent elements of a finite field.
Expand All @@ -896,9 +902,7 @@ class QGF(QDType):
The characteristic must be prime.
degree: The degree $m$ of the field $GF(p^{m})$. The degree must be a positive integer.
irreducible_poly: Optional galois.Poly instance that defines the field arithmetic.
This parameter is passed to `galois.GF(..., irreducible_poly=irreducible_poly)`.
element_repr: The string representation of the galois elements.
This parameter is passed to `galois.GF(..., repr=field_repr)`.
This parameter is passed to `galois.GF(..., irreducible_poly=irreducible_poly, verify=False)`.

References
[Finite Field](https://en.wikipedia.org/wiki/Finite_field)
Expand All @@ -910,8 +914,7 @@ class QGF(QDType):

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

@irreducible_poly.default
def _irreducible_poly_default(self):
Expand Down Expand Up @@ -957,12 +960,13 @@ def gf_type(self):
int(self.characteristic),
int(self.degree),
irreducible_poly=poly,
repr=self.element_repr,
verify=False,
repr='poly',
compile='python-calculate',
)

def to_bits(self, x) -> List[int]:
"""Yields individual bits corresponding to binary representation of x"""
"""Returns individual bits corresponding to binary representation of x"""
self.assert_valid_classical_val(x)
return self._quint_equivalent.to_bits(int(x))

Expand Down
4 changes: 3 additions & 1 deletion qualtran/bloqs/gf_arithmetic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
from qualtran.bloqs.gf_arithmetic.gf2_inverse import GF2Inverse
from qualtran.bloqs.gf_arithmetic.gf2_multiplication import (
BinaryPolynomialMultiplication,
GF2MulK,
GF2Multiplication,
GF2MultiplyByConstantMod,
GF2MulViaKaratsuba,
GF2ShiftRight,
MultiplyPolyByOnePlusXk,
)
from qualtran.bloqs.gf_arithmetic.gf2_square import GF2Square
Loading