Skip to content

Commit

Permalink
Classical support for And().adjoint() (#623)
Browse files Browse the repository at this point in the history
Co-authored-by: Fionn Malone <[email protected]>
  • Loading branch information
mpharrigan and fdmalone authored Feb 2, 2024
1 parent 2c7bbed commit fb1708f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
16 changes: 10 additions & 6 deletions qualtran/bloqs/and_bloq.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import itertools
from functools import cached_property
from typing import Any, Dict, Set, Tuple
from typing import Any, Dict, Optional, Set, Tuple

import attrs
import cirq
Expand Down Expand Up @@ -102,12 +102,16 @@ def pretty_name(self) -> str:
dag = '†' if self.uncompute else ''
return f'And{dag}'

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

target = 1 if tuple(ctrl) == (self.cv1, self.cv2) else 0
return {'ctrl': ctrl, 'target': target}
# Uncompute
assert target == out
return {'ctrl': ctrl}

def add_my_tensors(
self,
Expand Down
3 changes: 3 additions & 0 deletions qualtran/bloqs/and_bloq_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,6 @@ def test_multiand_adjoint():

cbloq = bb.finalize(q0=qs[0], q1=qs[1], q2=qs[2])
qlt_testing.assert_valid_cbloq(cbloq)

ret = cbloq.call_classically(q0=1, q1=1, q2=1)
assert ret == (1, 1, 1)

0 comments on commit fb1708f

Please sign in to comment.