Skip to content

Commit 1d9ecc7

Browse files
Update gf2.MultiplyPolyByConstantMod to support QGF dtype (#1533)
- rename `MultiplyPolyByConstantMod` to `GF2MultiplyByConstantMod` - change signature to use `QGF` instead of an array of qubits - update `QGF` to include an optional field for the galois field
1 parent 89e916e commit 1d9ecc7

File tree

5 files changed

+166
-89
lines changed

5 files changed

+166
-89
lines changed

qualtran/_infra/data_types.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
import abc
5252
from enum import Enum
5353
from functools import cached_property
54-
from typing import Any, Iterable, List, Sequence, Union
54+
from typing import Any, Iterable, List, Optional, Sequence, TYPE_CHECKING, Union
5555

5656
import attrs
5757
import numpy as np
@@ -60,6 +60,9 @@
6060

6161
from qualtran.symbolics import bit_length, is_symbolic, SymbolicInt
6262

63+
if TYPE_CHECKING:
64+
import galois
65+
6366

6467
class QDType(metaclass=abc.ABCMeta):
6568
"""This defines the abstract interface for quantum data types."""
@@ -888,6 +891,10 @@ class QGF(QDType):
888891
characteristic: The characteristic $p$ of the field $GF(p^m)$.
889892
The characteristic must be prime.
890893
degree: The degree $m$ of the field $GF(p^{m})$. The degree must be a positive integer.
894+
irreducible_poly: Optional galois.Poly instance that defines the field arithmetic.
895+
This parameter is passed to `galois.GF(..., irreducible_poly=irreducible_poly)`.
896+
element_repr: The string representation of the galois elements.
897+
This parameter is passed to `galois.GF(..., repr=field_repr)`.
891898
892899
References
893900
[Finite Field](https://en.wikipedia.org/wiki/Finite_field)
@@ -899,6 +906,8 @@ class QGF(QDType):
899906

900907
characteristic: SymbolicInt
901908
degree: SymbolicInt
909+
irreducible_poly: Optional['galois.Poly'] = None
910+
element_repr: str = 'int'
902911

903912
@cached_property
904913
def order(self) -> SymbolicInt:
@@ -927,7 +936,13 @@ def _quint_equivalent(self) -> QUInt:
927936
def gf_type(self):
928937
from galois import GF
929938

930-
return GF(int(self.characteristic), int(self.degree), compile='python-calculate')
939+
return GF( # type: ignore[call-overload]
940+
int(self.characteristic),
941+
int(self.degree),
942+
irreducible_poly=self.irreducible_poly,
943+
repr=self.element_repr,
944+
compile='python-calculate',
945+
)
931946

932947
def to_bits(self, x) -> List[int]:
933948
"""Yields individual bits corresponding to binary representation of x"""

qualtran/bloqs/gf_arithmetic/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
from qualtran.bloqs.gf_arithmetic.gf2_inverse import GF2Inverse
1818
from qualtran.bloqs.gf_arithmetic.gf2_multiplication import (
1919
GF2Multiplication,
20-
MultiplyPolyByConstantMod,
20+
GF2MultiplyByConstantMod,
2121
)
2222
from qualtran.bloqs.gf_arithmetic.gf2_square import GF2Square

qualtran/bloqs/gf_arithmetic/gf2_multiplication.ipynb

+52-35
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "f2e9ace3",
5+
"id": "bd16181d",
66
"metadata": {
77
"cq.autogen": "title_cell"
88
},
@@ -13,7 +13,7 @@
1313
{
1414
"cell_type": "code",
1515
"execution_count": null,
16-
"id": "84523966",
16+
"id": "c6216556",
1717
"metadata": {
1818
"cq.autogen": "top_imports"
1919
},
@@ -30,7 +30,7 @@
3030
},
3131
{
3232
"cell_type": "markdown",
33-
"id": "d77b58bc",
33+
"id": "667e99e4",
3434
"metadata": {
3535
"cq.autogen": "GF2Multiplication.bloq_doc.md"
3636
},
@@ -72,7 +72,7 @@
7272
{
7373
"cell_type": "code",
7474
"execution_count": null,
75-
"id": "40013f0c",
75+
"id": "e7be0a06",
7676
"metadata": {
7777
"cq.autogen": "GF2Multiplication.bloq_doc.py"
7878
},
@@ -83,7 +83,7 @@
8383
},
8484
{
8585
"cell_type": "markdown",
86-
"id": "c5bcc7f0",
86+
"id": "eda29d2c",
8787
"metadata": {
8888
"cq.autogen": "GF2Multiplication.example_instances.md"
8989
},
@@ -94,7 +94,7 @@
9494
{
9595
"cell_type": "code",
9696
"execution_count": null,
97-
"id": "75114297",
97+
"id": "19a69ab5",
9898
"metadata": {
9999
"cq.autogen": "GF2Multiplication.gf16_multiplication"
100100
},
@@ -106,7 +106,7 @@
106106
{
107107
"cell_type": "code",
108108
"execution_count": null,
109-
"id": "7bdc2588",
109+
"id": "a5d9f3a7",
110110
"metadata": {
111111
"cq.autogen": "GF2Multiplication.gf2_multiplication_symbolic"
112112
},
@@ -120,7 +120,7 @@
120120
},
121121
{
122122
"cell_type": "markdown",
123-
"id": "9866335a",
123+
"id": "22e6b63a",
124124
"metadata": {
125125
"cq.autogen": "GF2Multiplication.graphical_signature.md"
126126
},
@@ -131,7 +131,7 @@
131131
{
132132
"cell_type": "code",
133133
"execution_count": null,
134-
"id": "f09a13f8",
134+
"id": "345d09a7",
135135
"metadata": {
136136
"cq.autogen": "GF2Multiplication.graphical_signature.py"
137137
},
@@ -144,7 +144,7 @@
144144
},
145145
{
146146
"cell_type": "markdown",
147-
"id": "b26814e0",
147+
"id": "94ab395f",
148148
"metadata": {
149149
"cq.autogen": "GF2Multiplication.call_graph.md"
150150
},
@@ -155,7 +155,7 @@
155155
{
156156
"cell_type": "code",
157157
"execution_count": null,
158-
"id": "7219fea5",
158+
"id": "7701f35c",
159159
"metadata": {
160160
"cq.autogen": "GF2Multiplication.call_graph.py"
161161
},
@@ -169,17 +169,17 @@
169169
},
170170
{
171171
"cell_type": "markdown",
172-
"id": "a9729be2",
172+
"id": "410bdd3b",
173173
"metadata": {
174-
"cq.autogen": "MultiplyPolyByConstantMod.bloq_doc.md"
174+
"cq.autogen": "GF2MultiplyByConstantMod.bloq_doc.md"
175175
},
176176
"source": [
177-
"## `MultiplyPolyByConstantMod`\n",
178-
"Multiply a polynomial by $f(x)$ modulu $m(x)$. Both $f(x)$ and $m(x)$ are constants.\n",
177+
"## `GF2MultiplyByConstantMod`\n",
178+
"Multiply by constant $f(x)$ modulu $m(x)$. Both $f(x)$ and $m(x)$ are constants.\n",
179179
"\n",
180180
"#### Parameters\n",
181-
" - `f_x`: The polynomial to mulitply with, given either a galois.Poly or as a sequence degrees.\n",
182-
" - `m_x`: The modulus polynomial, given either a galois.Poly or as a sequence degrees. \n",
181+
" - `const`: The multiplication constant which is an element of the given field.\n",
182+
" - `galois_field`: The galois field that defines the arithmetics. \n",
183183
"\n",
184184
"#### Registers\n",
185185
" - `g`: The polynomial coefficients (in GF(2)). \n",
@@ -191,20 +191,20 @@
191191
{
192192
"cell_type": "code",
193193
"execution_count": null,
194-
"id": "a25c2e49",
194+
"id": "fcca1ecd",
195195
"metadata": {
196-
"cq.autogen": "MultiplyPolyByConstantMod.bloq_doc.py"
196+
"cq.autogen": "GF2MultiplyByConstantMod.bloq_doc.py"
197197
},
198198
"outputs": [],
199199
"source": [
200-
"from qualtran.bloqs.gf_arithmetic import MultiplyPolyByConstantMod"
200+
"from qualtran.bloqs.gf_arithmetic import GF2MultiplyByConstantMod"
201201
]
202202
},
203203
{
204204
"cell_type": "markdown",
205-
"id": "3690dcdb",
205+
"id": "f17b748f",
206206
"metadata": {
207-
"cq.autogen": "MultiplyPolyByConstantMod.example_instances.md"
207+
"cq.autogen": "GF2MultiplyByConstantMod.example_instances.md"
208208
},
209209
"source": [
210210
"### Example Instances"
@@ -213,22 +213,39 @@
213213
{
214214
"cell_type": "code",
215215
"execution_count": null,
216-
"id": "dffa638f",
216+
"id": "b34f2d09",
217217
"metadata": {
218-
"cq.autogen": "MultiplyPolyByConstantMod.gf2_multiply_by_constant_modulu"
218+
"cq.autogen": "GF2MultiplyByConstantMod.gf2_multiply_by_constant_modulu"
219+
},
220+
"outputs": [],
221+
"source": [
222+
"import galois\n",
223+
"\n",
224+
"mx = galois.Poly.Degrees([0, 1, 3]) # x^3 + x + 1\n",
225+
"gf = galois.GF(2, 3, irreducible_poly=mx)\n",
226+
"const = gf(5) # x^2 + 1\n",
227+
"gf2_multiply_by_constant_modulu = GF2MultiplyByConstantMod(const, gf)"
228+
]
229+
},
230+
{
231+
"cell_type": "code",
232+
"execution_count": null,
233+
"id": "b8488436",
234+
"metadata": {
235+
"cq.autogen": "GF2MultiplyByConstantMod.gf2_poly_multiply_by_constant_modulu"
219236
},
220237
"outputs": [],
221238
"source": [
222239
"fx = [2, 0] # x^2 + 1\n",
223240
"mx = [0, 1, 3] # x^3 + x + 1\n",
224-
"gf2_multiply_by_constant_modulu = MultiplyPolyByConstantMod(fx, mx)"
241+
"gf2_poly_multiply_by_constant_modulu = GF2MultiplyByConstantMod.from_polynomials(fx, mx)"
225242
]
226243
},
227244
{
228245
"cell_type": "markdown",
229-
"id": "e140ad12",
246+
"id": "0836d017",
230247
"metadata": {
231-
"cq.autogen": "MultiplyPolyByConstantMod.graphical_signature.md"
248+
"cq.autogen": "GF2MultiplyByConstantMod.graphical_signature.md"
232249
},
233250
"source": [
234251
"#### Graphical Signature"
@@ -237,22 +254,22 @@
237254
{
238255
"cell_type": "code",
239256
"execution_count": null,
240-
"id": "501a0781",
257+
"id": "46c85d5d",
241258
"metadata": {
242-
"cq.autogen": "MultiplyPolyByConstantMod.graphical_signature.py"
259+
"cq.autogen": "GF2MultiplyByConstantMod.graphical_signature.py"
243260
},
244261
"outputs": [],
245262
"source": [
246263
"from qualtran.drawing import show_bloqs\n",
247-
"show_bloqs([gf2_multiply_by_constant_modulu],\n",
248-
" ['`gf2_multiply_by_constant_modulu`'])"
264+
"show_bloqs([gf2_multiply_by_constant_modulu, gf2_poly_multiply_by_constant_modulu],\n",
265+
" ['`gf2_multiply_by_constant_modulu`', '`gf2_poly_multiply_by_constant_modulu`'])"
249266
]
250267
},
251268
{
252269
"cell_type": "markdown",
253-
"id": "292b96b9",
270+
"id": "fea3aa3d",
254271
"metadata": {
255-
"cq.autogen": "MultiplyPolyByConstantMod.call_graph.md"
272+
"cq.autogen": "GF2MultiplyByConstantMod.call_graph.md"
256273
},
257274
"source": [
258275
"### Call Graph"
@@ -261,9 +278,9 @@
261278
{
262279
"cell_type": "code",
263280
"execution_count": null,
264-
"id": "fad6337a",
281+
"id": "a73ee748",
265282
"metadata": {
266-
"cq.autogen": "MultiplyPolyByConstantMod.call_graph.py"
283+
"cq.autogen": "GF2MultiplyByConstantMod.call_graph.py"
267284
},
268285
"outputs": [],
269286
"source": [

0 commit comments

Comments
 (0)