|
| 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 typing import Dict |
| 15 | + |
| 16 | +import attrs |
| 17 | + |
| 18 | +from qualtran import Bloq, BloqBuilder, Signature, Soquet, SoquetT |
| 19 | +from qualtran.bloqs.basic_gates import CNOT, TGate |
| 20 | + |
| 21 | + |
| 22 | +@attrs.frozen |
| 23 | +class TestManyAllocOnce(Bloq): |
| 24 | + """Allocate an ancilla once, and re-use it explicitly |
| 25 | +
|
| 26 | + See qualtran.resource.counting._qubit_counts_test:test_many_alloc |
| 27 | + """ |
| 28 | + |
| 29 | + n: int |
| 30 | + |
| 31 | + @property |
| 32 | + def signature(self) -> Signature: |
| 33 | + return Signature.build(x=self.n) |
| 34 | + |
| 35 | + def build_composite_bloq(self, bb: 'BloqBuilder', *, x: 'Soquet') -> dict[str, 'SoquetT']: |
| 36 | + x = bb.split(x) |
| 37 | + anc = bb.allocate() |
| 38 | + for i in range(self.n): |
| 39 | + x[i], anc = bb.add(CNOT(), ctrl=x[i], target=anc) |
| 40 | + anc = bb.add(TGate(), q=anc) |
| 41 | + x[i], anc = bb.add(CNOT(), ctrl=x[i], target=anc) |
| 42 | + bb.free(anc) |
| 43 | + return {'x': bb.join(x)} |
| 44 | + |
| 45 | + |
| 46 | +@attrs.frozen |
| 47 | +class TestManyAllocMany(Bloq): |
| 48 | + """Allocate a new ancilla for each pseudo-subcall. |
| 49 | +
|
| 50 | + See qualtran.resource.counting._qubit_counts_test:test_many_alloc |
| 51 | + """ |
| 52 | + |
| 53 | + n: int |
| 54 | + |
| 55 | + @property |
| 56 | + def signature(self) -> Signature: |
| 57 | + return Signature.build(x=self.n) |
| 58 | + |
| 59 | + def build_composite_bloq(self, bb: 'BloqBuilder', *, x: 'Soquet') -> dict[str, 'SoquetT']: |
| 60 | + x = bb.split(x) |
| 61 | + for i in range(self.n): |
| 62 | + anc = bb.allocate() |
| 63 | + x[i], anc = bb.add(CNOT(), ctrl=x[i], target=anc) |
| 64 | + anc = bb.add(TGate(), q=anc) |
| 65 | + x[i], anc = bb.add(CNOT(), ctrl=x[i], target=anc) |
| 66 | + bb.free(anc) |
| 67 | + return {'x': bb.join(x)} |
| 68 | + |
| 69 | + |
| 70 | +@attrs.frozen |
| 71 | +class _Inner(Bloq): |
| 72 | + """Inner part, used by `TestManyAllocAbstracted`""" |
| 73 | + |
| 74 | + @property |
| 75 | + def signature(self) -> Signature: |
| 76 | + return Signature.build(x=1) |
| 77 | + |
| 78 | + def build_composite_bloq(self, bb: 'BloqBuilder', *, x: 'Soquet') -> Dict[str, 'SoquetT']: |
| 79 | + anc = bb.allocate() |
| 80 | + x, anc = bb.add(CNOT(), ctrl=x, target=anc) |
| 81 | + anc = bb.add(TGate(), q=anc) |
| 82 | + x, anc = bb.add(CNOT(), ctrl=x, target=anc) |
| 83 | + bb.free(anc) |
| 84 | + return {'x': x} |
| 85 | + |
| 86 | + |
| 87 | +@attrs.frozen |
| 88 | +class TestManyAllocAbstracted(Bloq): |
| 89 | + """Factor allocation into subbloq |
| 90 | +
|
| 91 | + See qualtran.resource.counting._qubit_counts_test:test_many_alloc |
| 92 | + """ |
| 93 | + |
| 94 | + n: int |
| 95 | + |
| 96 | + @property |
| 97 | + def signature(self) -> Signature: |
| 98 | + return Signature.build(x=self.n) |
| 99 | + |
| 100 | + def build_composite_bloq(self, bb: 'BloqBuilder', *, x: 'Soquet') -> dict[str, 'SoquetT']: |
| 101 | + x = bb.split(x) |
| 102 | + for i in range(self.n): |
| 103 | + x[i] = bb.add(_Inner(), x=x[i]) |
| 104 | + return {'x': bb.join(x)} |
0 commit comments