From fb1708f15e6229d54358107e8eee4503d8d30385 Mon Sep 17 00:00:00 2001 From: Matthew Harrigan Date: Fri, 2 Feb 2024 15:14:37 -0800 Subject: [PATCH] Classical support for And().adjoint() (#623) Co-authored-by: Fionn Malone --- qualtran/bloqs/and_bloq.py | 16 ++++++++++------ qualtran/bloqs/and_bloq_test.py | 3 +++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/qualtran/bloqs/and_bloq.py b/qualtran/bloqs/and_bloq.py index c409e9702..7eb1dad1c 100644 --- a/qualtran/bloqs/and_bloq.py +++ b/qualtran/bloqs/and_bloq.py @@ -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 @@ -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, diff --git a/qualtran/bloqs/and_bloq_test.py b/qualtran/bloqs/and_bloq_test.py index 0e0e552d5..04dbefda8 100644 --- a/qualtran/bloqs/and_bloq_test.py +++ b/qualtran/bloqs/and_bloq_test.py @@ -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)