Skip to content

Commit fb1708f

Browse files
mpharriganfdmalone
andauthored
Classical support for And().adjoint() (#623)
Co-authored-by: Fionn Malone <[email protected]>
1 parent 2c7bbed commit fb1708f

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

qualtran/bloqs/and_bloq.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import itertools
2626
from functools import cached_property
27-
from typing import Any, Dict, Set, Tuple
27+
from typing import Any, Dict, Optional, Set, Tuple
2828

2929
import attrs
3030
import cirq
@@ -102,12 +102,16 @@ def pretty_name(self) -> str:
102102
dag = '†' if self.uncompute else ''
103103
return f'And{dag}'
104104

105-
def on_classical_vals(self, ctrl: NDArray[np.uint8]) -> Dict[str, NDArray[np.uint8]]:
106-
if self.uncompute:
107-
raise NotImplementedError("Come back later.")
105+
def on_classical_vals(
106+
self, *, ctrl: NDArray[np.uint8], target: Optional[int] = None
107+
) -> Dict[str, NDArray[np.uint8]]:
108+
out = 1 if tuple(ctrl) == (self.cv1, self.cv2) else 0
109+
if not self.uncompute:
110+
return {'ctrl': ctrl, 'target': out}
108111

109-
target = 1 if tuple(ctrl) == (self.cv1, self.cv2) else 0
110-
return {'ctrl': ctrl, 'target': target}
112+
# Uncompute
113+
assert target == out
114+
return {'ctrl': ctrl}
111115

112116
def add_my_tensors(
113117
self,

qualtran/bloqs/and_bloq_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,6 @@ def test_multiand_adjoint():
223223

224224
cbloq = bb.finalize(q0=qs[0], q1=qs[1], q2=qs[2])
225225
qlt_testing.assert_valid_cbloq(cbloq)
226+
227+
ret = cbloq.call_classically(q0=1, q1=1, q2=1)
228+
assert ret == (1, 1, 1)

0 commit comments

Comments
 (0)