|
| 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 | + |
| 19 | +from qualtran import ( |
| 20 | + Bloq, |
| 21 | + bloq_example, |
| 22 | + BloqDocSpec, |
| 23 | + DecomposeTypeError, |
| 24 | + QGFPoly, |
| 25 | + Register, |
| 26 | + Signature, |
| 27 | +) |
| 28 | +from qualtran.bloqs.gf_arithmetic import GF2Addition |
| 29 | +from qualtran.bloqs.gf_poly_arithmetic.gf_poly_split_and_join import GFPolyJoin, GFPolySplit |
| 30 | +from qualtran.symbolics import is_symbolic |
| 31 | + |
| 32 | +if TYPE_CHECKING: |
| 33 | + from qualtran import BloqBuilder, Soquet |
| 34 | + from qualtran.resource_counting import BloqCountDictT, BloqCountT, SympySymbolAllocator |
| 35 | + from qualtran.simulation.classical_sim import ClassicalValT |
| 36 | + |
| 37 | + |
| 38 | +@attrs.frozen |
| 39 | +class GF2PolyAdd(Bloq): |
| 40 | + r"""In place quantum-quantum addition of two polynomials defined over GF($2^m$). |
| 41 | +
|
| 42 | + The bloq implements in place addition of quantum registers $|f(x)\rangle$ and $|g(x)\rangle$ |
| 43 | + storing coefficients of two degree-n polynomials defined over GF($2^m$). |
| 44 | + Addition in GF($2^m$) simply reduces to a component wise XOR, which can be implemented via |
| 45 | + CNOT gates. |
| 46 | +
|
| 47 | + $$ |
| 48 | + |f(x)\rangle |g(x)\rangle \rightarrow |f(x)\rangle |f(x) + g(x)\rangle |
| 49 | + $$ |
| 50 | +
|
| 51 | + Args: |
| 52 | + qgf_poly: An instance of `QGFPoly` type that defines the data type for quantum |
| 53 | + register $|f(x)\rangle$ storing coefficients of a degree-n polynomial defined |
| 54 | + over GF($2^m$). |
| 55 | +
|
| 56 | + Registers: |
| 57 | + f_x: THRU register that stores coefficients of first polynomial defined over $GF(2^m)$. |
| 58 | + g_x: THRU register that stores coefficients of second polynomial defined over $GF(2^m)$. |
| 59 | + """ |
| 60 | + |
| 61 | + qgf_poly: QGFPoly |
| 62 | + |
| 63 | + @cached_property |
| 64 | + def signature(self) -> 'Signature': |
| 65 | + return Signature( |
| 66 | + [Register('f_x', dtype=self.qgf_poly), Register('g_x', dtype=self.qgf_poly)] |
| 67 | + ) |
| 68 | + |
| 69 | + def is_symbolic(self): |
| 70 | + return is_symbolic(self.qgf_poly.degree) |
| 71 | + |
| 72 | + def build_composite_bloq( |
| 73 | + self, bb: 'BloqBuilder', *, f_x: 'Soquet', g_x: 'Soquet' |
| 74 | + ) -> Dict[str, 'Soquet']: |
| 75 | + if self.is_symbolic(): |
| 76 | + raise DecomposeTypeError(f"Cannot decompose symbolic {self}") |
| 77 | + f_x = bb.add(GFPolySplit(self.qgf_poly), reg=f_x) |
| 78 | + g_x = bb.add(GFPolySplit(self.qgf_poly), reg=g_x) |
| 79 | + for i in range(self.qgf_poly.degree + 1): |
| 80 | + f_x[i], g_x[i] = bb.add(GF2Addition(self.qgf_poly.qgf.bitsize), x=f_x[i], y=g_x[i]) |
| 81 | + |
| 82 | + f_x = bb.add(GFPolyJoin(self.qgf_poly), reg=f_x) |
| 83 | + g_x = bb.add(GFPolyJoin(self.qgf_poly), reg=g_x) |
| 84 | + return {'f_x': f_x, 'g_x': g_x} |
| 85 | + |
| 86 | + def build_call_graph( |
| 87 | + self, ssa: 'SympySymbolAllocator' |
| 88 | + ) -> Union['BloqCountDictT', Set['BloqCountT']]: |
| 89 | + return {GF2Addition(self.qgf_poly.qgf.bitsize): self.qgf_poly.degree + 1} |
| 90 | + |
| 91 | + def on_classical_vals(self, *, f_x, g_x) -> Dict[str, 'ClassicalValT']: |
| 92 | + return {'f_x': f_x, 'g_x': f_x + g_x} |
| 93 | + |
| 94 | + |
| 95 | +@bloq_example |
| 96 | +def _gf2_poly_4_8_add() -> GF2PolyAdd: |
| 97 | + from qualtran import QGF, QGFPoly |
| 98 | + |
| 99 | + qgf_poly = QGFPoly(4, QGF(2, 3)) |
| 100 | + gf2_poly_4_8_add = GF2PolyAdd(qgf_poly) |
| 101 | + return gf2_poly_4_8_add |
| 102 | + |
| 103 | + |
| 104 | +@bloq_example |
| 105 | +def _gf2_poly_add_symbolic() -> GF2PolyAdd: |
| 106 | + import sympy |
| 107 | + |
| 108 | + from qualtran import QGF, QGFPoly |
| 109 | + |
| 110 | + n, m = sympy.symbols('n, m', positive=True, integers=True) |
| 111 | + qgf_poly = QGFPoly(n, QGF(2, m)) |
| 112 | + gf2_poly_add_symbolic = GF2PolyAdd(qgf_poly) |
| 113 | + return gf2_poly_add_symbolic |
| 114 | + |
| 115 | + |
| 116 | +_GF2_POLY_ADD_DOC = BloqDocSpec( |
| 117 | + bloq_cls=GF2PolyAdd, examples=(_gf2_poly_4_8_add, _gf2_poly_add_symbolic) |
| 118 | +) |
0 commit comments