|
| 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 | +from functools import cached_property |
| 15 | +from typing import Dict, Set, TYPE_CHECKING, Union |
| 16 | + |
| 17 | +import attrs |
| 18 | +import galois |
| 19 | + |
| 20 | +from qualtran import ( |
| 21 | + Bloq, |
| 22 | + bloq_example, |
| 23 | + BloqDocSpec, |
| 24 | + DecomposeTypeError, |
| 25 | + QGFPoly, |
| 26 | + Register, |
| 27 | + Signature, |
| 28 | +) |
| 29 | +from qualtran.bloqs.gf_arithmetic import GF2AddK |
| 30 | +from qualtran.bloqs.gf_poly_arithmetic.gf_poly_split_and_join import GFPolyJoin, GFPolySplit |
| 31 | +from qualtran.symbolics import is_symbolic |
| 32 | + |
| 33 | +if TYPE_CHECKING: |
| 34 | + from qualtran import BloqBuilder, Soquet |
| 35 | + from qualtran.resource_counting import BloqCountDictT, BloqCountT, SympySymbolAllocator |
| 36 | + from qualtran.simulation.classical_sim import ClassicalValT |
| 37 | + |
| 38 | + |
| 39 | +@attrs.frozen |
| 40 | +class GF2PolyAddK(Bloq): |
| 41 | + r"""In place addition of a constant polynomial defined over GF($2^m$). |
| 42 | +
|
| 43 | + The bloq implements in place addition of a classical constant polynomial $g(x)$ and |
| 44 | + a quantum register $|f(x)\rangle$ storing coefficients of a degree-n polynomial defined |
| 45 | + over GF($2^m$). Addition in GF($2^m$) simply reduces to a component wise XOR, which can |
| 46 | + be implemented via X gates. |
| 47 | +
|
| 48 | + $$ |
| 49 | + |f(x)\rangle \rightarrow |f(x) + g(x)\rangle |
| 50 | + $$ |
| 51 | +
|
| 52 | + Args: |
| 53 | + qgf_poly: An instance of `QGFPoly` type that defines the data type for quantum |
| 54 | + register $|f(x)\rangle$ storing coefficients of a degree-n polynomial defined |
| 55 | + over GF($2^m$). |
| 56 | + g_x: An instance of `galois.Poly` that specifies that constant polynomial g(x) |
| 57 | + defined over GF($2^m$) that should be added to the input register f(x). |
| 58 | +
|
| 59 | + Registers: |
| 60 | + f_x: Input THRU register that stores coefficients of polynomial defined over $GF(2^m)$. |
| 61 | + """ |
| 62 | + |
| 63 | + qgf_poly: QGFPoly |
| 64 | + g_x: galois.Poly = attrs.field() |
| 65 | + |
| 66 | + @cached_property |
| 67 | + def signature(self) -> 'Signature': |
| 68 | + return Signature([Register('f_x', dtype=self.qgf_poly)]) |
| 69 | + |
| 70 | + @g_x.validator |
| 71 | + def _validate_g_x(self, attribute, value): |
| 72 | + if not is_symbolic(self.qgf_poly.degree): |
| 73 | + if value.degree > self.qgf_poly.degree: |
| 74 | + raise ValueError(f"Degree of constant polynomial must be <= {self.qgf_poly.degree}") |
| 75 | + if not is_symbolic(self.qgf_poly.degree, self.qgf_poly.qgf): |
| 76 | + if not value.field is self.qgf_poly.qgf.gf_type: |
| 77 | + raise ValueError( |
| 78 | + f"Constant polynomial must be defined over galois field {self.qgf_poly.qgf.gf_type}" |
| 79 | + ) |
| 80 | + |
| 81 | + def is_symbolic(self): |
| 82 | + return is_symbolic(self.qgf_poly.degree) |
| 83 | + |
| 84 | + def build_composite_bloq(self, bb: 'BloqBuilder', *, f_x: 'Soquet') -> Dict[str, 'Soquet']: |
| 85 | + if self.is_symbolic(): |
| 86 | + raise DecomposeTypeError(f"Cannot decompose symbolic {self}") |
| 87 | + f_x = bb.add(GFPolySplit(self.qgf_poly), reg=f_x) |
| 88 | + g_x = self.qgf_poly.to_gf_coefficients(self.g_x) |
| 89 | + for i in range(self.qgf_poly.degree + 1): |
| 90 | + f_x[i] = bb.add(GF2AddK(self.qgf_poly.qgf.bitsize, int(g_x[i])), x=f_x[i]) |
| 91 | + |
| 92 | + f_x = bb.add(GFPolyJoin(self.qgf_poly), reg=f_x) |
| 93 | + return {'f_x': f_x} |
| 94 | + |
| 95 | + def build_call_graph( |
| 96 | + self, ssa: 'SympySymbolAllocator' |
| 97 | + ) -> Union['BloqCountDictT', Set['BloqCountT']]: |
| 98 | + if self.is_symbolic(): |
| 99 | + k = ssa.new_symbol('g_x') |
| 100 | + return {GF2AddK(self.qgf_poly.qgf.bitsize, k): self.qgf_poly.degree + 1} |
| 101 | + return super().build_call_graph(ssa) |
| 102 | + |
| 103 | + def on_classical_vals(self, *, f_x) -> Dict[str, 'ClassicalValT']: |
| 104 | + return {'f_x': f_x + self.g_x} |
| 105 | + |
| 106 | + |
| 107 | +@bloq_example |
| 108 | +def _gf2_poly_4_8_add_k() -> GF2PolyAddK: |
| 109 | + from galois import Poly |
| 110 | + |
| 111 | + from qualtran import QGF, QGFPoly |
| 112 | + |
| 113 | + qgf_poly = QGFPoly(4, QGF(2, 3)) |
| 114 | + g_x = Poly(qgf_poly.qgf.gf_type([1, 2, 3, 4, 5])) |
| 115 | + gf2_poly_4_8_add_k = GF2PolyAddK(qgf_poly, g_x) |
| 116 | + return gf2_poly_4_8_add_k |
| 117 | + |
| 118 | + |
| 119 | +@bloq_example |
| 120 | +def _gf2_poly_add_k_symbolic() -> GF2PolyAddK: |
| 121 | + import sympy |
| 122 | + from galois import Poly |
| 123 | + |
| 124 | + from qualtran import QGF, QGFPoly |
| 125 | + |
| 126 | + n, m = sympy.symbols('n, m', positive=True, integers=True) |
| 127 | + qgf_poly = QGFPoly(n, QGF(2, m)) |
| 128 | + gf2_poly_add_k_symbolic = GF2PolyAddK(qgf_poly, Poly([0, 0, 0, 0])) |
| 129 | + return gf2_poly_add_k_symbolic |
| 130 | + |
| 131 | + |
| 132 | +_GF2_POLY_ADD_K_DOC = BloqDocSpec( |
| 133 | + bloq_cls=GF2PolyAddK, examples=(_gf2_poly_4_8_add_k, _gf2_poly_add_k_symbolic) |
| 134 | +) |
0 commit comments