Skip to content

Commit

Permalink
add tests for adjoint
Browse files Browse the repository at this point in the history
  • Loading branch information
anurudhp committed Oct 30, 2024
1 parent 863fef1 commit 1147578
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions qualtran/bloqs/mcmt/specialized_ctrl_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@
)
from qualtran.bloqs.mcmt import And
from qualtran.bloqs.mcmt.specialized_ctrl import (
AdjointWithSpecializedCtrl,
get_ctrl_system_1bit_cv,
get_ctrl_system_1bit_cv_from_bloqs,
SpecializeOnCtrlBit,
)
from qualtran.resource_counting import CostKey, GateCounts, get_cost_value, QECGatesCost


def _keep_and(b):
# TODO remove this after https://github.com/quantumlib/Qualtran/issues/1346 is resolved.
return isinstance(b, And)


@attrs.frozen
class AtomWithSpecializedControl(Bloq):
cv: Optional[int] = None
Expand Down Expand Up @@ -78,6 +85,9 @@ def my_static_costs(self, cost_key: 'CostKey'):

return NotImplemented

def adjoint(self) -> 'AdjointWithSpecializedCtrl':
return AdjointWithSpecializedCtrl(self, specialize_on_ctrl=SpecializeOnCtrlBit.BOTH)


def ON(n: int = 1) -> CtrlSpec:
return CtrlSpec(cvs=[1] * n)
Expand Down Expand Up @@ -133,6 +143,9 @@ def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlled
ctrl_reg_name='ctrl',
)

def adjoint(self) -> 'AdjointWithSpecializedCtrl':
return AdjointWithSpecializedCtrl(self, specialize_on_ctrl=SpecializeOnCtrlBit.ONE)


@attrs.frozen
class CTestAtom(Bloq):
Expand All @@ -147,14 +160,13 @@ def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlled
self, ctrl_spec, current_ctrl_bit=1, bloq_with_ctrl=self, ctrl_reg_name='ctrl'
)

def adjoint(self) -> 'AdjointWithSpecializedCtrl':
return AdjointWithSpecializedCtrl(self, specialize_on_ctrl=SpecializeOnCtrlBit.ONE)


def test_bloq_with_controlled_bloq():
assert TestAtom('g').controlled() == CTestAtom('g')

def _keep_and(b):
# TODO remove this after https://github.com/quantumlib/Qualtran/issues/1346 is resolved.
return isinstance(b, And)

ctrl_bloq = CTestAtom('g').controlled()
_, sigma = ctrl_bloq.call_graph(keep=_keep_and)
assert sigma == {And(): 1, CTestAtom('g'): 1, And().adjoint(): 1}
Expand All @@ -168,6 +180,22 @@ def _keep_and(b):
assert sigma == {And(0, 0): 1, CTestAtom('nn'): 1, And(0, 0).adjoint(): 1}


def test_ctrl_adjoint():
assert TestAtom('a').adjoint().controlled() == CTestAtom('a').adjoint()

_, sigma = TestAtom('g').adjoint().controlled(CtrlSpec(cvs=[1, 1])).call_graph(keep=_keep_and)
assert sigma == {And(): 1, And().adjoint(): 1, CTestAtom('g').adjoint(): 1}

_, sigma = CTestAtom('c').adjoint().controlled().call_graph(keep=_keep_and)
assert sigma == {And(): 1, And().adjoint(): 1, CTestAtom('c').adjoint(): 1}

for cv in [0, 1]:
assert (
AtomWithSpecializedControl().adjoint().controlled(CtrlSpec(cvs=(cv,)))
== AtomWithSpecializedControl(cv=cv).adjoint()
)


@attrs.frozen
class TestBloqWithDecompose(Bloq):
ctrl_reg_name: str
Expand Down

0 comments on commit 1147578

Please sign in to comment.