1313# limitations under the License.
1414
1515from functools import cached_property
16- from typing import Dict , Iterable , Iterator , List , Sequence , Set , Tuple , TYPE_CHECKING , Union
16+ from typing import (
17+ Dict ,
18+ Iterable ,
19+ Iterator ,
20+ List ,
21+ Optional ,
22+ Sequence ,
23+ Set ,
24+ Tuple ,
25+ TYPE_CHECKING ,
26+ Union ,
27+ )
1728
1829import attrs
1930import cirq
4253from qualtran .cirq_interop .bit_tools import iter_bits
4354from qualtran .cirq_interop .t_complexity_protocol import t_complexity , TComplexity
4455from qualtran .drawing import WireSymbol
45- from qualtran .drawing .musical_score import TextBox
56+ from qualtran .drawing .musical_score import Text , TextBox
4657
4758if TYPE_CHECKING :
4859 from qualtran import BloqBuilder
@@ -62,8 +73,12 @@ class LessThanConstant(GateWithRegisters, cirq.ArithmeticGate): # type: ignore[
6273 def signature (self ) -> Signature :
6374 return Signature .build_from_dtypes (x = QUInt (self .bitsize ), target = QBit ())
6475
65- def short_name (self ) -> str :
66- return f'x<{ self .less_than_val } '
76+ def wire_symbol (
77+ self , reg : Optional ['Register' ], idx : Tuple [int , ...] = tuple ()
78+ ) -> 'WireSymbol' :
79+ if reg is None :
80+ return Text (f'x<{ self .less_than_val } ' )
81+ return super ().wire_symbol (reg , idx )
6782
6883 def registers (self ) -> Sequence [Union [int , Sequence [int ]]]:
6984 return [2 ] * self .bitsize , self .less_than_val , [2 ]
@@ -428,8 +443,12 @@ def apply(self, *register_vals: int) -> Union[int, int, Iterable[int]]:
428443 x_val , y_val , target_val = register_vals
429444 return x_val , y_val , target_val ^ (x_val <= y_val )
430445
431- def short_name (self ) -> str :
432- return 'x <= y'
446+ def wire_symbol (
447+ self , reg : Optional ['Register' ], idx : Tuple [int , ...] = tuple ()
448+ ) -> 'WireSymbol' :
449+ if reg is None :
450+ return Text ('x <= y' )
451+ return super ().wire_symbol (reg , idx )
433452
434453 def on_classical_vals (self , * , x : int , y : int , target : int ) -> Dict [str , 'ClassicalValT' ]:
435454 return {'x' : x , 'y' : y , 'target' : target ^ (x <= y )}
@@ -599,16 +618,15 @@ def signature(self):
599618 a = QUInt (self .a_bitsize ), b = QUInt (self .b_bitsize ), target = QBit ()
600619 )
601620
602- def short_name (self ) -> str :
603- return "a>b"
604-
605621 def _t_complexity_ (self ) -> 'TComplexity' :
606622 # TODO Determine precise clifford count and/or ignore.
607623 # See: https://github.com/quantumlib/Qualtran/issues/219
608624 # See: https://github.com/quantumlib/Qualtran/issues/217
609625 return t_complexity (LessThanEqual (self .a_bitsize , self .b_bitsize ))
610626
611- def wire_symbol (self , reg : Register , idx : Tuple [int , ...] = tuple ()) -> WireSymbol :
627+ def wire_symbol (self , reg : Optional [Register ], idx : Tuple [int , ...] = tuple ()) -> WireSymbol :
628+ if reg is None :
629+ return Text ("a>b" )
612630 if reg .name == 'a' :
613631 return TextBox ("In(a)" )
614632 if reg .name == 'b' :
@@ -799,8 +817,12 @@ def build_composite_bloq(
799817 # Return the output registers.
800818 return {'a' : a , 'b' : b , 'target' : target }
801819
802- def short_name (self ) -> str :
803- return "a > b"
820+ def wire_symbol (
821+ self , reg : Optional ['Register' ], idx : Tuple [int , ...] = tuple ()
822+ ) -> 'WireSymbol' :
823+ if reg is None :
824+ return Text ('a > b' )
825+ return super ().wire_symbol (reg , idx )
804826
805827
806828@frozen
@@ -836,10 +858,9 @@ def _t_complexity_(self) -> TComplexity:
836858 # See: https://github.com/quantumlib/Qualtran/issues/217
837859 return t_complexity (LessThanConstant (self .bitsize , less_than_val = self .val ))
838860
839- def short_name (self ) -> str :
840- return f"x > { self .val } "
841-
842- def wire_symbol (self , reg : Register , idx : Tuple [int , ...] = tuple ()) -> WireSymbol :
861+ def wire_symbol (self , reg : Optional [Register ], idx : Tuple [int , ...] = tuple ()) -> WireSymbol :
862+ if reg is None :
863+ return Text (f"x > { self .val } " )
843864 if reg .name == 'x' :
844865 return TextBox ("In(x)" )
845866 elif reg .name == 'target' :
@@ -889,10 +910,9 @@ def signature(self) -> Signature:
889910 def _t_complexity_ (self ) -> 'TComplexity' :
890911 return TComplexity (t = 4 * (self .bitsize - 1 ))
891912
892- def short_name (self ) -> str :
893- return f"x == { self .val } "
894-
895- def wire_symbol (self , reg : Register , idx : Tuple [int , ...] = tuple ()) -> WireSymbol :
913+ def wire_symbol (self , reg : Optional [Register ], idx : Tuple [int , ...] = tuple ()) -> WireSymbol :
914+ if reg is None :
915+ return Text (f"x == { self .val } " )
896916 if reg .name == 'x' :
897917 return TextBox ("In(x)" )
898918 elif reg .name == 'target' :
0 commit comments