Skip to content

Commit ed6170e

Browse files
Create a bloq for galois multiplication (#1557)
This is the final PR in the series #1554, #1533, and #1516. These PRs implemented the GF($2^n$) multiplication construction from https://arxiv.org/abs/1910.02849v2 which has a toffoli complexity of $n^{\log_2{3}}$
1 parent ab9d2ea commit ed6170e

File tree

6 files changed

+776
-122
lines changed

6 files changed

+776
-122
lines changed

dev_tools/qualtran_dev_tools/notebook_specs.py

+2
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,8 @@
578578
qualtran.bloqs.gf_arithmetic.gf2_multiplication._MULTIPLY_BY_CONSTANT_MOD_DOC,
579579
qualtran.bloqs.gf_arithmetic.gf2_multiplication._MULTIPLY_POLY_BY_ONE_PLUS_XK_DOC,
580580
qualtran.bloqs.gf_arithmetic.gf2_multiplication._BINARY_POLYNOMIAL_MULTIPLICATION_DOC,
581+
qualtran.bloqs.gf_arithmetic.gf2_multiplication._GF2_SHIFT_RIGHT_MOD_DOC,
582+
qualtran.bloqs.gf_arithmetic.gf2_multiplication._GF2_MUL_DOC,
581583
],
582584
),
583585
NotebookSpecV2(

qualtran/_infra/data_types.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,16 @@
5151
import abc
5252
from enum import Enum
5353
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
5555

5656
import attrs
57+
import galois
5758
import numpy as np
5859
from fxpmath import Fxp
5960
from numpy.typing import NDArray
6061

6162
from qualtran.symbolics import bit_length, is_symbolic, SymbolicInt
6263

63-
if TYPE_CHECKING:
64-
import galois
65-
6664

6765
class 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
874880
class 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

qualtran/bloqs/gf_arithmetic/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
from qualtran.bloqs.gf_arithmetic.gf2_inverse import GF2Inverse
1818
from qualtran.bloqs.gf_arithmetic.gf2_multiplication import (
1919
BinaryPolynomialMultiplication,
20+
GF2MulK,
2021
GF2Multiplication,
21-
GF2MultiplyByConstantMod,
22+
GF2MulViaKaratsuba,
23+
GF2ShiftRight,
2224
MultiplyPolyByOnePlusXk,
2325
)
2426
from qualtran.bloqs.gf_arithmetic.gf2_square import GF2Square

0 commit comments

Comments
 (0)