|
13 | 13 | # limitations under the License. |
14 | 14 | import itertools |
15 | 15 | from functools import cached_property |
16 | | -from typing import Dict, List, Optional, Tuple, TYPE_CHECKING, Union |
| 16 | +from typing import cast, Dict, Iterable, List, Optional, Sequence, Tuple, TYPE_CHECKING, Union |
17 | 17 |
|
18 | 18 | import numpy as np |
19 | 19 | from attrs import frozen |
|
25 | 25 | BloqDocSpec, |
26 | 26 | CompositeBloq, |
27 | 27 | Connection, |
| 28 | + CtrlSpec, |
28 | 29 | DecomposeTypeError, |
29 | 30 | QBit, |
30 | 31 | Register, |
|
35 | 36 | import cirq |
36 | 37 | import quimb.tensor as qtn |
37 | 38 |
|
| 39 | + from qualtran import AddControlledT, BloqBuilder, SoquetT |
38 | 40 | from qualtran.cirq_interop import CirqQuregT |
39 | 41 | from qualtran.drawing import WireSymbol |
40 | 42 | from qualtran.simulation.classical_sim import ClassicalValT |
@@ -127,6 +129,29 @@ def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) - |
127 | 129 | return ModPlus() |
128 | 130 | raise ValueError(f'Unknown wire symbol register name: {reg.name}') |
129 | 131 |
|
| 132 | + def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']: |
| 133 | + from qualtran.bloqs.basic_gates import CNOT |
| 134 | + from qualtran.bloqs.mcmt import ControlledViaAnd |
| 135 | + |
| 136 | + if ctrl_spec != CtrlSpec(): |
| 137 | + return super().get_ctrl_system(ctrl_spec) |
| 138 | + |
| 139 | + cc_cnot = ControlledViaAnd(CNOT(), CtrlSpec(cvs=[1, 1])) |
| 140 | + |
| 141 | + def add_controlled( |
| 142 | + bb: 'BloqBuilder', ctrl_soqs: Sequence['SoquetT'], in_soqs: dict[str, 'SoquetT'] |
| 143 | + ) -> tuple[Iterable['SoquetT'], Iterable['SoquetT']]: |
| 144 | + (new_ctrl,) = ctrl_soqs |
| 145 | + ctrl0, ctrl1 = cast(NDArray, in_soqs.pop('ctrl')) |
| 146 | + |
| 147 | + (new_ctrl, ctrl0), ctrl1, target = bb.add( |
| 148 | + cc_cnot, ctrl2=np.array([new_ctrl, ctrl0]), ctrl=ctrl1, target=in_soqs.pop('target') |
| 149 | + ) |
| 150 | + |
| 151 | + return [new_ctrl], [ctrl0, ctrl1, target] |
| 152 | + |
| 153 | + return cc_cnot, add_controlled |
| 154 | + |
130 | 155 |
|
131 | 156 | @bloq_example |
132 | 157 | def _toffoli() -> Toffoli: |
|
0 commit comments