Skip to content

Commit

Permalink
fix ctrl sys for CZ and CY: target register names of uncontrolled…
Browse files Browse the repository at this point in the history
… and controlled versions do not match
  • Loading branch information
anurudhp committed Oct 29, 2024
1 parent 5ea723a commit 6248dbe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
15 changes: 13 additions & 2 deletions qualtran/bloqs/basic_gates/y_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
ConnectionT,
CtrlSpec,
DecomposeTypeError,
QBit,
Register,
Signature,
)
Expand Down Expand Up @@ -73,10 +74,15 @@ def my_tensors(
]

def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
from qualtran.bloqs.bookkeeping import AutoPartition
from qualtran.bloqs.mcmt.specialized_ctrl import get_ctrl_system_1bit_cv_from_bloqs

cy_wrapped = AutoPartition(
CYGate(), [(Register('ctrl', QBit()), ['ctrl']), (Register('q', QBit()), ['target'])]
)

return get_ctrl_system_1bit_cv_from_bloqs(
self, ctrl_spec, current_ctrl_bit=None, bloq_with_ctrl=CYGate(), ctrl_reg_name='ctrl'
self, ctrl_spec, current_ctrl_bit=None, bloq_with_ctrl=cy_wrapped, ctrl_reg_name='ctrl'
)

def as_cirq_op(
Expand Down Expand Up @@ -164,10 +170,15 @@ def wire_symbol(
raise ValueError(f"Unknown register {reg}.")

def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
from qualtran.bloqs.bookkeeping import AutoPartition
from qualtran.bloqs.mcmt.specialized_ctrl import get_ctrl_system_1bit_cv_from_bloqs

cy_wrapped = AutoPartition(
self, [(Register('ctrl', QBit()), ['ctrl']), (Register('q', QBit()), ['target'])]
)

return get_ctrl_system_1bit_cv_from_bloqs(
self, ctrl_spec, current_ctrl_bit=1, bloq_with_ctrl=self, ctrl_reg_name='ctrl'
self, ctrl_spec, current_ctrl_bit=1, bloq_with_ctrl=cy_wrapped, ctrl_reg_name='ctrl'
)


Expand Down
15 changes: 13 additions & 2 deletions qualtran/bloqs/basic_gates/z_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,16 @@ def my_tensors(
]

def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
from qualtran.bloqs.bookkeeping import AutoPartition
from qualtran.bloqs.mcmt.specialized_ctrl import get_ctrl_system_1bit_cv_from_bloqs

cz = CZ()
cz_wrapped = AutoPartition(
cz, [(Register('ctrl', QBit()), ['q1']), (Register('q', QBit()), ['q2'])]
)

return get_ctrl_system_1bit_cv_from_bloqs(
self, ctrl_spec, current_ctrl_bit=None, bloq_with_ctrl=CZ(), ctrl_reg_name='q1'
self, ctrl_spec, current_ctrl_bit=None, bloq_with_ctrl=cz_wrapped, ctrl_reg_name='ctrl'
)

def as_cirq_op(
Expand Down Expand Up @@ -332,10 +338,15 @@ def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -
raise ValueError(f'Unknown wire symbol register name: {reg.name}')

def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
from qualtran.bloqs.bookkeeping import AutoPartition
from qualtran.bloqs.mcmt.specialized_ctrl import get_ctrl_system_1bit_cv_from_bloqs

cz_wrapped = AutoPartition(
self, [(Register('ctrl', QBit()), ['q1']), (Register('q', QBit()), ['q2'])]
)

return get_ctrl_system_1bit_cv_from_bloqs(
self, ctrl_spec, current_ctrl_bit=1, bloq_with_ctrl=self, ctrl_reg_name='q1'
self, ctrl_spec, current_ctrl_bit=1, bloq_with_ctrl=cz_wrapped, ctrl_reg_name='ctrl'
)


Expand Down
8 changes: 7 additions & 1 deletion qualtran/bloqs/mcmt/specialized_ctrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import numpy as np

from qualtran import Bloq, QBit, Register, Signature
from qualtran.bloqs.bookkeeping import AutoPartition

if TYPE_CHECKING:
from qualtran import AddControlledT, BloqBuilder, CtrlSpec, SoquetT
Expand Down Expand Up @@ -180,7 +181,12 @@ def _adder(

return [ctrl0], [ctrl1, *out_soqs]

return ctrl_bloq, _adder
def _unwrap(b):
if isinstance(b, AutoPartition):
return _unwrap(b.bloq)
return b

return _unwrap(ctrl_bloq), _adder


def get_ctrl_system_1bit_cv(
Expand Down

0 comments on commit 6248dbe

Please sign in to comment.