Skip to content

Commit f668c7a

Browse files
authored
Add Add.controlled() -> CAdd (#1576)
cadd
1 parent 458705c commit f668c7a

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

qualtran/bloqs/arithmetic/addition.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
)
4545
from qualtran.bloqs.basic_gates import CNOT
4646
from qualtran.bloqs.mcmt.and_bloq import And
47+
from qualtran.bloqs.mcmt.specialized_ctrl import get_ctrl_system_1bit_cv
4748
from qualtran.cirq_interop import decompose_from_cirq_style_method
4849
from qualtran.drawing import directional_text_box, Text
4950
from qualtran.resource_counting.generalizers import ignore_split_join
@@ -129,7 +130,6 @@ def _circuit_diagram_info_(self, _) -> cirq.CircuitDiagramInfo:
129130
return cirq.CircuitDiagramInfo(wire_symbols=wire_symbols)
130131

131132
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
132-
133133
if reg is None:
134134
return Text("")
135135
if reg.name == 'a':
@@ -203,6 +203,19 @@ def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
203203
n_cnot = (n - 2) * 6 + 3
204204
return {And(): n - 1, And().adjoint(): n - 1, CNOT(): n_cnot}
205205

206+
def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
207+
from qualtran.bloqs.arithmetic import CAdd
208+
209+
return get_ctrl_system_1bit_cv(
210+
bloq=self,
211+
ctrl_spec=ctrl_spec,
212+
current_ctrl_bit=None,
213+
get_ctrl_bloq_and_ctrl_reg_name=lambda cv: (
214+
CAdd(a_dtype=self.a_dtype, b_dtype=self.b_dtype, cv=cv),
215+
"ctrl",
216+
),
217+
)
218+
206219

207220
@bloq_example(generalizer=ignore_split_join)
208221
def _add_symb() -> Add:

qualtran/bloqs/arithmetic/addition_test.py

+15
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,18 @@ def test_outofplaceadder_classical_action(bitsize):
407407
cb = b.decompose_bloq()
408408
for x, y in itertools.product(range(2**bitsize), repeat=2):
409409
assert b.call_classically(a=x, b=y, c=x + y) == cb.call_classically(a=x, b=y, c=x + y)
410+
411+
412+
def test_controlled_add_from_add():
413+
add = Add(QUInt(32))
414+
cadd = add.controlled(CtrlSpec(cvs=0))
415+
416+
ctrl, a, b = cadd.call_classically(ctrl=1, a=5, b=10)
417+
assert ctrl == 1
418+
assert a == 5
419+
assert b == 10
420+
421+
ctrl, a, b = cadd.call_classically(ctrl=0, a=5, b=10)
422+
assert ctrl == 0
423+
assert a == 5
424+
assert b == 15

qualtran/bloqs/arithmetic/controlled_add_or_subtract_test.py

-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def build_composite_bloq(
9292
return {'ctrl': ctrl, 'a': a, 'b': b}
9393

9494

95-
@pytest.mark.slow
9695
@pytest.mark.parametrize("dtype", [QUInt(3), QUInt(4)])
9796
def test_tensor(dtype):
9897
bloq = ControlledAddOrSubtract(dtype, dtype)

0 commit comments

Comments
 (0)