-
Notifications
You must be signed in to change notification settings - Fork 103
Bloq namespacing and display (__str__) updates. #1825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,7 @@ | |
| """ | ||
|
|
||
| from functools import cached_property | ||
| from typing import Dict, Iterable, Optional, Sequence, Tuple, TYPE_CHECKING, Union | ||
| from typing import Dict, Iterable, Optional, Sequence, Tuple, Type, TYPE_CHECKING, Union | ||
|
|
||
| import attrs | ||
| import cirq | ||
|
|
@@ -163,7 +163,7 @@ def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) - | |
| return TextBox(f'Z^{self.exponent}') | ||
|
|
||
| def __str__(self): | ||
| return f'Z**{self.exponent}' | ||
| return f'Z(pow={self.exponent})' | ||
|
|
||
|
|
||
| @bloq_example | ||
|
|
@@ -231,7 +231,7 @@ def adjoint(self) -> 'CZPowGate': | |
| return attrs.evolve(self, exponent=-self.exponent) | ||
|
|
||
| def __str__(self): | ||
| return f'CZ**{self.exponent}' | ||
| return f'CZ(pow={self.exponent})' | ||
|
|
||
|
|
||
| @bloq_example | ||
|
|
@@ -306,7 +306,7 @@ def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) - | |
| return TextBox(f'X^{self.exponent}') | ||
|
|
||
| def __str__(self): | ||
| return f'X**{self.exponent}' | ||
| return f'X(pow={self.exponent})' | ||
|
|
||
|
|
||
| @bloq_example | ||
|
|
@@ -381,7 +381,7 @@ def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) - | |
| return TextBox(f'Y^{self.exponent}') | ||
|
|
||
| def __str__(self): | ||
| return f'Y**{self.exponent}' | ||
| return f'Y(pow={self.exponent})' | ||
|
|
||
|
|
||
| @bloq_example | ||
|
|
@@ -487,6 +487,29 @@ def _rz() -> Rz: | |
|
|
||
| _RZ_DOC = BloqDocSpec(bloq_cls=Rz, examples=[_rz], call_graph_example=None) | ||
|
|
||
| _PowClsT = Union[Type[XPowGate], Type[YPowGate], Type[ZPowGate]] | ||
|
|
||
|
|
||
| def _controlled_rp_circuit( | ||
| bb: 'BloqBuilder', | ||
| /, | ||
| *, | ||
| single_q_pow_cls: _PowClsT, | ||
| angle: SymbolicFloat, | ||
| eps: SymbolicFloat, | ||
| ctrl: 'Soquet', | ||
| q: 'Soquet', | ||
| ) -> Dict[str, 'SoquetT']: | ||
| from qualtran.bloqs.basic_gates import CNOT | ||
|
|
||
| t = angle / np.pi | ||
| q = bb.add(single_q_pow_cls(t / 2, eps=eps / 2), q=q) | ||
| ctrl, q = bb.add(CNOT(), ctrl=ctrl, target=q) | ||
| q = bb.add(single_q_pow_cls(-t / 2, eps=eps / 2), q=q) | ||
| ctrl, q = bb.add(CNOT(), ctrl=ctrl, target=q) | ||
|
|
||
| return {'ctrl': ctrl, 'q': q} | ||
|
|
||
|
|
||
| @frozen | ||
| class CRz(Bloq): | ||
|
|
@@ -532,15 +555,9 @@ def signature(self) -> 'Signature': | |
| def build_composite_bloq( | ||
| self, bb: 'BloqBuilder', ctrl: 'Soquet', q: 'Soquet' | ||
| ) -> Dict[str, 'SoquetT']: | ||
| from qualtran.bloqs.basic_gates import CNOT | ||
|
|
||
| t = self.angle / np.pi | ||
| q = bb.add(ZPowGate(t / 2, eps=self.eps / 2), q=q) | ||
| ctrl, q = bb.add(CNOT(), ctrl=ctrl, target=q) | ||
| q = bb.add(ZPowGate(-t / 2, eps=self.eps / 2), q=q) | ||
| ctrl, q = bb.add(CNOT(), ctrl=ctrl, target=q) | ||
|
|
||
| return {'ctrl': ctrl, 'q': q} | ||
| return _controlled_rp_circuit( | ||
| bb, single_q_pow_cls=ZPowGate, angle=self.angle, eps=self.eps, ctrl=ctrl, q=q | ||
| ) | ||
|
|
||
| def __str__(self): | ||
| return f'CRz({self.angle})' | ||
|
|
@@ -677,6 +694,20 @@ def as_pl_op(self, wires: 'Wires') -> 'Operation': | |
| def adjoint(self) -> 'Ry': | ||
| return attrs.evolve(self, angle=-self.angle) | ||
|
|
||
| def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']: | ||
| if ctrl_spec != CtrlSpec(): | ||
| return super().get_ctrl_system(ctrl_spec) | ||
|
|
||
| from qualtran.bloqs.mcmt.specialized_ctrl import get_ctrl_system_1bit_cv_from_bloqs | ||
|
|
||
| return get_ctrl_system_1bit_cv_from_bloqs( | ||
| bloq=self, | ||
| ctrl_spec=ctrl_spec, | ||
| current_ctrl_bit=None, | ||
| bloq_with_ctrl=CRy(self.angle, eps=self.eps), | ||
| ctrl_reg_name='ctrl', | ||
| ) | ||
|
Comment on lines
+697
to
+709
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a great addition! For consistency with You could add the following @frozen
class CRx(Bloq):
r"""A controlled Rx rotation."""
angle: SymbolicFloat
eps: SymbolicFloat = 1e-11
@cached_property
def signature(self) -> 'Signature':
return Signature.build(ctrl=1, q=1)
def build_composite_bloq(
self, bb: 'BloqBuilder', ctrl: 'Soquet', q: 'Soquet'
) -> Dict[str, 'SoquetT']:
return _controlled_rp_circuit(
bb, single_q_pow_cls=XPowGate, angle=self.angle, eps=self.eps, ctrl=ctrl, q=q
)
def __str__(self):
return f'CRx({self.angle})'And then add this method to the def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
if ctrl_spec != CtrlSpec():
return super().get_ctrl_system(ctrl_spec)
from qualtran.bloqs.mcmt.specialized_ctrl import get_ctrl_system_1bit_cv_from_bloqs
return get_ctrl_system_1bit_cv_from_bloqs(
bloq=self,
ctrl_spec=ctrl_spec,
current_ctrl_bit=None,
bloq_with_ctrl=CRx(self.angle, eps=self.eps),
ctrl_reg_name='ctrl',
)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can do this in a follow up |
||
|
|
||
| def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol': | ||
| if reg is None: | ||
| return Text('') | ||
|
|
@@ -696,3 +727,36 @@ def _rx() -> Rx: | |
| def _ry() -> Ry: | ||
| ry = Ry(angle=np.pi / 4.0) | ||
| return ry | ||
|
|
||
|
|
||
| @frozen | ||
| class CRy(Bloq): | ||
| r"""A controlled Ry rotation. | ||
|
|
||
| Args: | ||
| angle: The rotation angle in radians. | ||
| eps: The precision of the rotation. This parameter is for bookkeeping and does | ||
| not affect e.g. the tensor representation of this gate. When synthesizing | ||
| a rotation from a discrete gate set, you must fix a precision `eps`. | ||
|
|
||
| Registers: | ||
| ctrl: Whether the rotation is active. | ||
| q: The qubit on which we optionally perform the rotation. | ||
| """ | ||
|
|
||
| angle: SymbolicFloat | ||
| eps: SymbolicFloat = 1e-11 | ||
|
|
||
| @cached_property | ||
| def signature(self) -> 'Signature': | ||
| return Signature.build(ctrl=1, q=1) | ||
|
|
||
| def build_composite_bloq( | ||
| self, bb: 'BloqBuilder', ctrl: 'Soquet', q: 'Soquet' | ||
| ) -> Dict[str, 'SoquetT']: | ||
| return _controlled_rp_circuit( | ||
| bb, single_q_pow_cls=YPowGate, angle=self.angle, eps=self.eps, ctrl=ctrl, q=q | ||
| ) | ||
|
|
||
| def __str__(self): | ||
| return f'CRy({self.angle})' | ||
Uh oh!
There was an error while loading. Please reload this page.