Skip to content

Commit

Permalink
Make param ctrl_spec of get_ctrl_system mandatory (#1209)
Browse files Browse the repository at this point in the history
* make param `ctrl_spec` of `get_ctrl_system` mandatory

* invert if branch in `CNOT.get_ctrl_system`
  • Loading branch information
anurudhp authored Jul 30, 2024
1 parent 18e62ca commit 2a78ca1
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 57 deletions.
14 changes: 7 additions & 7 deletions qualtran/_infra/bloq.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,7 @@ def bloq_counts(

return dict(get_bloq_callee_counts(self, generalizer=generalizer))

def get_ctrl_system(
self, ctrl_spec: Optional['CtrlSpec'] = None
) -> Tuple['Bloq', 'AddControlledT']:
def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
"""Get a controlled version of this bloq and a function to wire it up correctly.
Users should likely call `Bloq.controlled(...)` which uses this method behind-the-scenes.
Expand Down Expand Up @@ -400,10 +398,7 @@ def _my_add_controlled(
add_controlled: A function with the signature documented above that the system
can use to automatically wire up the new control registers.
"""
from qualtran import Controlled, CtrlSpec

if ctrl_spec is None:
ctrl_spec = CtrlSpec()
from qualtran import Controlled

return Controlled.make_ctrl_system(self, ctrl_spec=ctrl_spec)

Expand All @@ -423,6 +418,11 @@ def controlled(self, ctrl_spec: Optional['CtrlSpec'] = None) -> 'Bloq':
Returns:
A controlled version of the bloq.
"""
from qualtran import CtrlSpec

if ctrl_spec is None:
ctrl_spec = CtrlSpec()

controlled_bloq, _ = self.get_ctrl_system(ctrl_spec=ctrl_spec)
return controlled_bloq

Expand Down
7 changes: 1 addition & 6 deletions qualtran/_infra/single_qubit_controlled.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ def get_single_qubit_controlled_bloq(
"""Override this to provide a custom controlled bloq"""
return attrs.evolve(self, control_val=control_val) # type: ignore[misc]

def get_ctrl_system(
self, ctrl_spec: Optional['CtrlSpec'] = None
) -> Tuple['Bloq', 'AddControlledT']:
if ctrl_spec is None:
ctrl_spec = CtrlSpec()

def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
if self.control_val is None and ctrl_spec.shapes in [((),), ((1,),)]:
control_val = int(ctrl_spec.cvs[0].item())
cbloq = self.get_single_qubit_controlled_bloq(control_val)
Expand Down
7 changes: 1 addition & 6 deletions qualtran/bloqs/arithmetic/addition.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,7 @@ def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']:

return {loading_cost, (Add(QUInt(self.bitsize)), 1)}

def get_ctrl_system(
self, ctrl_spec: Optional['CtrlSpec'] = None
) -> Tuple['Bloq', 'AddControlledT']:
if ctrl_spec is None:
ctrl_spec = CtrlSpec()

def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
if self.cvs:
# We're already controlled, use default fallback
return super().get_ctrl_system(ctrl_spec)
Expand Down
28 changes: 13 additions & 15 deletions qualtran/bloqs/basic_gates/cnot.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,24 @@ def my_tensors(
def on_classical_vals(self, ctrl: int, target: int) -> Dict[str, 'ClassicalValT']:
return {'ctrl': ctrl, 'target': (ctrl + target) % 2}

def get_ctrl_system(
self, ctrl_spec: Optional['CtrlSpec'] = None
) -> Tuple['Bloq', 'AddControlledT']:
def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
from qualtran.bloqs.basic_gates.toffoli import Toffoli

if ctrl_spec is None or ctrl_spec == CtrlSpec():
bloq = Toffoli()
if ctrl_spec != CtrlSpec():
return super().get_ctrl_system(ctrl_spec=ctrl_spec)

def add_controlled(
bb: 'BloqBuilder', ctrl_soqs: Sequence['SoquetT'], in_soqs: Dict[str, 'SoquetT']
) -> Tuple[Iterable['SoquetT'], Iterable['SoquetT']]:
(new_ctrl,) = ctrl_soqs
(new_ctrl, existing_ctrl), target = bb.add(
bloq, ctrl=np.array([new_ctrl, in_soqs['ctrl']]), target=in_soqs['target']
)
return (new_ctrl,), (existing_ctrl, target)
bloq = Toffoli()

return bloq, add_controlled
def add_controlled(
bb: 'BloqBuilder', ctrl_soqs: Sequence['SoquetT'], in_soqs: Dict[str, 'SoquetT']
) -> Tuple[Iterable['SoquetT'], Iterable['SoquetT']]:
(new_ctrl,) = ctrl_soqs
(new_ctrl, existing_ctrl), target = bb.add(
bloq, ctrl=np.array([new_ctrl, in_soqs['ctrl']]), target=in_soqs['target']
)
return (new_ctrl,), (existing_ctrl, target)

return super().get_ctrl_system(ctrl_spec=ctrl_spec)
return bloq, add_controlled

def as_cirq_op(
self, qubit_manager: 'cirq.QubitManager', ctrl: 'CirqQuregT', target: 'CirqQuregT' # type: ignore[type-var]
Expand Down
9 changes: 3 additions & 6 deletions qualtran/bloqs/basic_gates/global_phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from functools import cached_property
from typing import Dict, Iterable, List, Optional, Sequence, Tuple, TYPE_CHECKING
from typing import Dict, Iterable, List, Sequence, Tuple, TYPE_CHECKING

import attrs
import cirq
Expand Down Expand Up @@ -86,12 +86,9 @@ def my_tensors(

return [qtn.Tensor(data=self.coefficient, inds=[], tags=[str(self)])]

def get_ctrl_system(
self, ctrl_spec: Optional['CtrlSpec'] = None
) -> Tuple['Bloq', 'AddControlledT']:

def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
# Delegate to superclass logic for more than one control.
if not (ctrl_spec is None or ctrl_spec == CtrlSpec() or ctrl_spec == CtrlSpec(cvs=0)):
if not (ctrl_spec == CtrlSpec() or ctrl_spec == CtrlSpec(cvs=0)):
return super().get_ctrl_system(ctrl_spec=ctrl_spec)

# Otherwise, it's a ZPowGate
Expand Down
6 changes: 2 additions & 4 deletions qualtran/bloqs/basic_gates/hadamard.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ def my_tensors(
)
]

def get_ctrl_system(
self, ctrl_spec: Optional['CtrlSpec'] = None
) -> Tuple['Bloq', 'AddControlledT']:
if not (ctrl_spec is None or ctrl_spec == CtrlSpec()):
def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
if ctrl_spec != CtrlSpec():
return super().get_ctrl_system(ctrl_spec=ctrl_spec)

bloq = CHadamard()
Expand Down
6 changes: 2 additions & 4 deletions qualtran/bloqs/basic_gates/x_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,10 @@ def my_tensors(
)
]

def get_ctrl_system(
self, ctrl_spec: Optional['CtrlSpec'] = None
) -> Tuple['Bloq', 'AddControlledT']:
def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
from qualtran.bloqs.basic_gates import CNOT, Toffoli

if ctrl_spec is None or ctrl_spec == CtrlSpec():
if ctrl_spec == CtrlSpec():
bloq: 'Bloq' = CNOT()
elif ctrl_spec == CtrlSpec(cvs=(1, 1)):
bloq = Toffoli()
Expand Down
6 changes: 2 additions & 4 deletions qualtran/bloqs/basic_gates/y_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,8 @@ def my_tensors(
)
]

def get_ctrl_system(
self, ctrl_spec: Optional['CtrlSpec'] = None
) -> Tuple['Bloq', 'AddControlledT']:
if not (ctrl_spec is None or ctrl_spec == CtrlSpec()):
def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
if ctrl_spec != CtrlSpec():
return super().get_ctrl_system(ctrl_spec=ctrl_spec)

bloq = CYGate()
Expand Down
7 changes: 2 additions & 5 deletions qualtran/bloqs/basic_gates/z_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,8 @@ def my_tensors(
)
]

def get_ctrl_system(
self, ctrl_spec: Optional['CtrlSpec'] = None
) -> Tuple['Bloq', 'AddControlledT']:

if not (ctrl_spec is None or ctrl_spec == CtrlSpec()):
def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
if ctrl_spec != CtrlSpec():
# Delegate to the general superclass behavior
return super().get_ctrl_system(ctrl_spec=ctrl_spec)

Expand Down

0 comments on commit 2a78ca1

Please sign in to comment.