Skip to content

Commit 19d5e50

Browse files
committed
Fix tests, mypy and pylint
1 parent 54d8d0b commit 19d5e50

8 files changed

Lines changed: 121 additions & 81 deletions

File tree

dev_tools/qualtran_dev_tools/notebook_specs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,17 +611,17 @@
611611
# ----- Polynomials defined over Galois Fields (GF) Arithmetic --------
612612
# --------------------------------------------------------------------------
613613
NotebookSpecV2(
614-
title='GF Polynomials Split and Join',
614+
title='Polynomials over GF($p^m$) - Split and Join',
615615
module=qualtran.bloqs.gf_poly_arithmetic.gf_poly_split_and_join,
616616
bloq_specs=[
617617
qualtran.bloqs.gf_poly_arithmetic.gf_poly_split_and_join._GF_POLY_SPLIT_DOC,
618618
qualtran.bloqs.gf_poly_arithmetic.gf_poly_split_and_join._GF_POLY_JOIN_DOC,
619619
],
620620
),
621621
NotebookSpecV2(
622-
title='GF($2^m$) Polynomials Add Constant',
622+
title='Polynomials over GF($2^m$) - Add Constant',
623623
module=qualtran.bloqs.gf_poly_arithmetic.gf2_poly_add_k,
624-
bloq_specs=[qualtran.bloqs.gf_poly_arithmetic.gf2_poly_add_k._GF2_ADD_K_DOC],
624+
bloq_specs=[qualtran.bloqs.gf_poly_arithmetic.gf2_poly_add_k._GF2_POLY_ADD_K_DOC],
625625
),
626626
]
627627

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
from qualtran.bloqs.gf_poly_arithmetic.gf2_poly_add_k import GF2PolyAddK
17+
from qualtran.bloqs.gf_poly_arithmetic.gf_poly_split_and_join import GFPolyJoin, GFPolySplit

qualtran/bloqs/gf_poly_arithmetic/gf2_poly_add_k.ipynb

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "3d480a1d",
5+
"id": "71e0dbd8",
66
"metadata": {
77
"cq.autogen": "title_cell"
88
},
99
"source": [
10-
"# GF($2^m$) Polynomials Add Constant"
10+
"# Polynomials over GF($2^m$) - Add Constant"
1111
]
1212
},
1313
{
1414
"cell_type": "code",
1515
"execution_count": null,
16-
"id": "218a8310",
16+
"id": "c02448cd",
1717
"metadata": {
1818
"cq.autogen": "top_imports"
1919
},
@@ -30,47 +30,48 @@
3030
},
3131
{
3232
"cell_type": "markdown",
33-
"id": "203cfaf0",
33+
"id": "a5f23a08",
3434
"metadata": {
35-
"cq.autogen": "GF2AddK.bloq_doc.md"
35+
"cq.autogen": "GF2PolyAddK.bloq_doc.md"
3636
},
3737
"source": [
38-
"## `GF2AddK`\n",
39-
"In place addition of a constant $k$ for elements in GF($2^m$).\n",
38+
"## `GF2PolyAddK`\n",
39+
"In place addition of a constant polynomial defined over GF($2^m$).\n",
4040
"\n",
41-
"The bloq implements in place addition of a classical constant $k$ and a quantum register\n",
42-
"$|x\\rangle$ storing elements from GF($2^m$). Addition in GF($2^m$) simply reduces to a component\n",
43-
"wise XOR, which can be implemented via X gates.\n",
41+
"The bloq implements in place addition of a classical constant polynomial $g(x)$ and\n",
42+
"a quantum register $|f(x)\\rangle$ storing coefficients of a degree-n polynomial defined\n",
43+
"over GF($2^m$). Addition in GF($2^m$) simply reduces to a component wise XOR, which can\n",
44+
"be implemented via X gates.\n",
4445
"\n",
4546
"$$\n",
46-
"|x\\rangle \\rightarrow |x + k\\rangle\n",
47+
" |f(x)\\rangle \\rightarrow |f(x) + g(x)\\rangle\n",
4748
"$$\n",
4849
"\n",
4950
"#### Parameters\n",
50-
" - `bitsize`: The degree $m$ of the galois field GF($2^m$). Also corresponds to the number of qubits in the input register x.\n",
51-
" - `k`: Integer representation of constant over GF($2^m$) that should be added to the input register x. \n",
51+
" - `qgf_poly`: An instance of `QGFPoly` type that defines the data type for quantum register $|f(x)\\rangle$ storing coefficients of a degree-n polynomial defined over GF($2^m$).\n",
52+
" - `g_x`: An instance of `galois.Poly` that specifies that constant polynomial g(x) defined over GF($2^m$) that should be added to the input register f(x). \n",
5253
"\n",
5354
"#### Registers\n",
54-
" - `x`: Input THRU register of size $m$ that stores elements from $GF(2^m)$.\n"
55+
" - `f_x`: Input THRU register that stores coefficients of polynomial defined over $GF(2^m)$.\n"
5556
]
5657
},
5758
{
5859
"cell_type": "code",
5960
"execution_count": null,
60-
"id": "d52372c6",
61+
"id": "d40e23be",
6162
"metadata": {
62-
"cq.autogen": "GF2AddK.bloq_doc.py"
63+
"cq.autogen": "GF2PolyAddK.bloq_doc.py"
6364
},
6465
"outputs": [],
6566
"source": [
66-
"from qualtran.bloqs.gf_arithmetic import GF2AddK"
67+
"from qualtran.bloqs.gf_poly_arithmetic import GF2PolyAddK"
6768
]
6869
},
6970
{
7071
"cell_type": "markdown",
71-
"id": "1b73fd35",
72+
"id": "101615d7",
7273
"metadata": {
73-
"cq.autogen": "GF2AddK.example_instances.md"
74+
"cq.autogen": "GF2PolyAddK.example_instances.md"
7475
},
7576
"source": [
7677
"### Example Instances"
@@ -79,13 +80,14 @@
7980
{
8081
"cell_type": "code",
8182
"execution_count": null,
82-
"id": "ab076857",
83+
"id": "2cc9165f",
8384
"metadata": {
84-
"cq.autogen": "GF2AddK.gf2_poly_4_8_add_k"
85+
"cq.autogen": "GF2PolyAddK.gf2_poly_4_8_add_k"
8586
},
8687
"outputs": [],
8788
"source": [
8889
"from galois import Poly\n",
90+
"\n",
8991
"from qualtran import QGF, QGFPoly\n",
9092
"\n",
9193
"qgf_poly = QGFPoly(4, QGF(2, 3))\n",
@@ -96,26 +98,27 @@
9698
{
9799
"cell_type": "code",
98100
"execution_count": null,
99-
"id": "991e90de",
101+
"id": "9081edee",
100102
"metadata": {
101-
"cq.autogen": "GF2AddK.gf2_poly_add_k_symbolic"
103+
"cq.autogen": "GF2PolyAddK.gf2_poly_add_k_symbolic"
102104
},
103105
"outputs": [],
104106
"source": [
105107
"import sympy\n",
106108
"from galois import Poly\n",
109+
"\n",
107110
"from qualtran import QGF, QGFPoly\n",
108111
"\n",
109112
"n, m = sympy.symbols('n, m', positive=True, integers=True)\n",
110113
"qgf_poly = QGFPoly(n, QGF(2, m))\n",
111-
"gf2_poly_symbolic_add_k = GF2PolyAddK(qgf_poly, Poly([0, 0, 0, 0]))"
114+
"gf2_poly_add_k_symbolic = GF2PolyAddK(qgf_poly, Poly([0, 0, 0, 0]))"
112115
]
113116
},
114117
{
115118
"cell_type": "markdown",
116-
"id": "6a3ecb1e",
119+
"id": "11a4d71f",
117120
"metadata": {
118-
"cq.autogen": "GF2AddK.graphical_signature.md"
121+
"cq.autogen": "GF2PolyAddK.graphical_signature.md"
119122
},
120123
"source": [
121124
"#### Graphical Signature"
@@ -124,9 +127,9 @@
124127
{
125128
"cell_type": "code",
126129
"execution_count": null,
127-
"id": "32a100a5",
130+
"id": "66b4a211",
128131
"metadata": {
129-
"cq.autogen": "GF2AddK.graphical_signature.py"
132+
"cq.autogen": "GF2PolyAddK.graphical_signature.py"
130133
},
131134
"outputs": [],
132135
"source": [
@@ -137,9 +140,9 @@
137140
},
138141
{
139142
"cell_type": "markdown",
140-
"id": "3cd8496b",
143+
"id": "5940114d",
141144
"metadata": {
142-
"cq.autogen": "GF2AddK.call_graph.md"
145+
"cq.autogen": "GF2PolyAddK.call_graph.md"
143146
},
144147
"source": [
145148
"### Call Graph"
@@ -148,9 +151,9 @@
148151
{
149152
"cell_type": "code",
150153
"execution_count": null,
151-
"id": "605f81e0",
154+
"id": "dffd364c",
152155
"metadata": {
153-
"cq.autogen": "GF2AddK.call_graph.py"
156+
"cq.autogen": "GF2PolyAddK.call_graph.py"
154157
},
155158
"outputs": [],
156159
"source": [

qualtran/bloqs/gf_poly_arithmetic/gf2_poly_add_k.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
from functools import cached_property
15-
from typing import Dict, TYPE_CHECKING
15+
from typing import Dict, Set, TYPE_CHECKING, Union
1616

1717
import attrs
1818
import galois
@@ -32,7 +32,7 @@
3232

3333
if TYPE_CHECKING:
3434
from qualtran import BloqBuilder, Soquet
35-
from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator
35+
from qualtran.resource_counting import BloqCountDictT, BloqCountT, SympySymbolAllocator
3636
from qualtran.simulation.classical_sim import ClassicalValT
3737

3838

@@ -92,7 +92,9 @@ def build_composite_bloq(self, bb: 'BloqBuilder', *, f_x: 'Soquet') -> Dict[str,
9292
f_x = bb.add(GFPolyJoin(self.qgf_poly), reg=f_x)
9393
return {'f_x': f_x}
9494

95-
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
95+
def build_call_graph(
96+
self, ssa: 'SympySymbolAllocator'
97+
) -> Union['BloqCountDictT', Set['BloqCountT']]:
9698
if self.is_symbolic():
9799
k = ssa.new_symbol('g_x')
98100
return {GF2AddK(self.qgf_poly.qgf.bitsize, k): self.qgf_poly.degree + 1}
@@ -123,10 +125,10 @@ def _gf2_poly_add_k_symbolic() -> GF2PolyAddK:
123125

124126
n, m = sympy.symbols('n, m', positive=True, integers=True)
125127
qgf_poly = QGFPoly(n, QGF(2, m))
126-
gf2_poly_symbolic_add_k = GF2PolyAddK(qgf_poly, Poly([0, 0, 0, 0]))
127-
return gf2_poly_symbolic_add_k
128+
gf2_poly_add_k_symbolic = GF2PolyAddK(qgf_poly, Poly([0, 0, 0, 0]))
129+
return gf2_poly_add_k_symbolic
128130

129131

130-
_GF2_ADD_K_DOC = BloqDocSpec(
131-
bloq_cls=GF2AddK, examples=(_gf2_poly_4_8_add_k, _gf2_poly_add_k_symbolic)
132+
_GF2_POLY_ADD_K_DOC = BloqDocSpec(
133+
bloq_cls=GF2PolyAddK, examples=(_gf2_poly_4_8_add_k, _gf2_poly_add_k_symbolic)
132134
)

qualtran/bloqs/gf_poly_arithmetic/gf2_poly_add_k_test.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import numpy as np
1415
from galois import Poly
1516

1617
from qualtran.bloqs.gf_poly_arithmetic.gf2_poly_add_k import (
@@ -31,12 +32,14 @@ def test_gf2_poly_symbolic_add_k(bloq_autotester):
3132
def test_gf2_poly_add_k_classical_sim():
3233
bloq = _gf2_poly_4_8_add_k.make()
3334
f_x = Poly(bloq.qgf_poly.qgf.gf_type([0, 1, 2, 3, 4]))
34-
assert bloq.call_classically(f_x=f_x)[0] == f_x + bloq.g_x
35-
36-
f_x_range = [
37-
Poly(bloq.qgf_poly.qgf.gf_type([0, 0, 0, 0, 0])),
38-
Poly(bloq.qgf_poly.qgf.gf_type([7, 7, 7, 7, 7])),
39-
Poly(bloq.qgf_poly.qgf.gf_type([0, 0, 3, 5, 7])),
40-
Poly(bloq.qgf_poly.qgf.gf_type([2, 3, 5, 0, 0])),
41-
]
35+
assert bloq.call_classically(f_x=f_x)[0] == f_x + bloq.g_x # type: ignore[arg-type]
36+
37+
f_x_range = np.asarray(
38+
[
39+
Poly(bloq.qgf_poly.qgf.gf_type([0, 0, 0, 0, 0])),
40+
Poly(bloq.qgf_poly.qgf.gf_type([7, 7, 7, 7, 7])),
41+
Poly(bloq.qgf_poly.qgf.gf_type([0, 0, 3, 5, 7])),
42+
Poly(bloq.qgf_poly.qgf.gf_type([2, 3, 5, 0, 0])),
43+
]
44+
)
4245
assert_consistent_classical_action(bloq, f_x=f_x_range)

0 commit comments

Comments
 (0)