|
| 1 | +# Copyright 2023 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 | +"""This module has a selection of minimally-implemented modular arithmetic primitives. |
| 15 | +
|
| 16 | +These bloqs serve as the callees in the call graphs of the algorithms found |
| 17 | +in `qualtran.bloqs.factoring` and `qualtran.bloqs.mod_arithmetic`. They are place-holders, |
| 18 | +so we don't have undefined symbols and can still merge the high-level algorithms. These shims |
| 19 | +will be fleshed out and moved to their final organizational location soon (written: 2024-05-06). |
| 20 | +""" |
| 21 | +from functools import cached_property |
| 22 | +from typing import Set |
| 23 | + |
| 24 | +from attrs import frozen |
| 25 | + |
| 26 | +from qualtran import Bloq, QBit, QUInt, Register, Signature |
| 27 | +from qualtran.bloqs.basic_gates import Toffoli |
| 28 | +from qualtran.resource_counting import BloqCountT, SympySymbolAllocator |
| 29 | + |
| 30 | + |
| 31 | +@frozen |
| 32 | +class MultiCToffoli(Bloq): |
| 33 | + n: int |
| 34 | + |
| 35 | + @cached_property |
| 36 | + def signature(self) -> 'Signature': |
| 37 | + return Signature([Register('ctrl', QBit(), shape=(self.n,)), Register('target', QBit())]) |
| 38 | + |
| 39 | + def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']: |
| 40 | + return {(Toffoli(), self.n - 2)} |
| 41 | + |
| 42 | + |
| 43 | +@frozen |
| 44 | +class AddK(Bloq): |
| 45 | + n: int |
| 46 | + k: int |
| 47 | + |
| 48 | + @cached_property |
| 49 | + def signature(self) -> 'Signature': |
| 50 | + return Signature([Register('x', QUInt(self.n))]) |
| 51 | + |
| 52 | + |
| 53 | +@frozen |
| 54 | +class Sub(Bloq): |
| 55 | + n: int |
| 56 | + |
| 57 | + @cached_property |
| 58 | + def signature(self) -> 'Signature': |
| 59 | + return Signature([Register('x', QUInt(self.n)), Register('y', QUInt(self.n))]) |
| 60 | + |
| 61 | + |
| 62 | +@frozen |
| 63 | +class Lt(Bloq): |
| 64 | + n: int |
| 65 | + signed: bool = False |
| 66 | + |
| 67 | + @cached_property |
| 68 | + def signature(self) -> 'Signature': |
| 69 | + return Signature( |
| 70 | + [Register('x', QUInt(self.n)), Register('y', QUInt(self.n)), Register('out', QBit())] |
| 71 | + ) |
| 72 | + |
| 73 | + def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']: |
| 74 | + # litinski |
| 75 | + return {(Toffoli(), self.n)} |
| 76 | + |
| 77 | + |
| 78 | +@frozen |
| 79 | +class CHalf(Bloq): |
| 80 | + n: int |
| 81 | + |
| 82 | + @cached_property |
| 83 | + def signature(self) -> 'Signature': |
| 84 | + return Signature([Register('ctrl', QBit()), Register('x', QUInt(self.n))]) |
| 85 | + |
| 86 | + |
| 87 | +@frozen |
| 88 | +class Negate(Bloq): |
| 89 | + n: int |
| 90 | + |
| 91 | + @cached_property |
| 92 | + def signature(self) -> 'Signature': |
| 93 | + return Signature([Register('x', QUInt(self.n))]) |
0 commit comments