@@ -33,14 +33,15 @@ class _BaseGridQid(ops.Qid):
33
33
_row : int
34
34
_col : int
35
35
_dimension : int
36
+ _comp_key : Optional [Tuple [int , int ]] = None
36
37
_hash : Optional [int ] = None
37
38
38
39
def __hash__ (self ) -> int :
39
40
if self ._hash is None :
40
41
self ._hash = hash ((self ._row , self ._col , self ._dimension ))
41
42
return self ._hash
42
43
43
- def __eq__ (self , other ):
44
+ def __eq__ (self , other ) -> bool :
44
45
# Explicitly implemented for performance (vs delegating to Qid).
45
46
if isinstance (other , _BaseGridQid ):
46
47
return self is other or (
@@ -50,7 +51,7 @@ def __eq__(self, other):
50
51
)
51
52
return NotImplemented
52
53
53
- def __ne__ (self , other ):
54
+ def __ne__ (self , other ) -> bool :
54
55
# Explicitly implemented for performance (vs delegating to Qid).
55
56
if isinstance (other , _BaseGridQid ):
56
57
return self is not other and (
@@ -60,8 +61,38 @@ def __ne__(self, other):
60
61
)
61
62
return NotImplemented
62
63
64
+ def __lt__ (self , other ) -> bool :
65
+ # Explicitly implemented for performance (vs delegating to Qid).
66
+ if isinstance (other , _BaseGridQid ):
67
+ k0 , k1 = self ._comparison_key (), other ._comparison_key ()
68
+ return k0 < k1 or (k0 == k1 and self ._dimension < other ._dimension )
69
+ return super ().__lt__ (other )
70
+
71
+ def __le__ (self , other ) -> bool :
72
+ # Explicitly implemented for performance (vs delegating to Qid).
73
+ if isinstance (other , _BaseGridQid ):
74
+ k0 , k1 = self ._comparison_key (), other ._comparison_key ()
75
+ return k0 < k1 or (k0 == k1 and self ._dimension <= other ._dimension )
76
+ return super ().__le__ (other )
77
+
78
+ def __ge__ (self , other ) -> bool :
79
+ # Explicitly implemented for performance (vs delegating to Qid).
80
+ if isinstance (other , _BaseGridQid ):
81
+ k0 , k1 = self ._comparison_key (), other ._comparison_key ()
82
+ return k0 > k1 or (k0 == k1 and self ._dimension >= other ._dimension )
83
+ return super ().__ge__ (other )
84
+
85
+ def __gt__ (self , other ) -> bool :
86
+ # Explicitly implemented for performance (vs delegating to Qid).
87
+ if isinstance (other , _BaseGridQid ):
88
+ k0 , k1 = self ._comparison_key (), other ._comparison_key ()
89
+ return k0 > k1 or (k0 == k1 and self ._dimension > other ._dimension )
90
+ return super ().__gt__ (other )
91
+
63
92
def _comparison_key (self ):
64
- return self ._row , self ._col
93
+ if self ._comp_key is None :
94
+ self ._comp_key = self ._row , self ._col
95
+ return self ._comp_key
65
96
66
97
@property
67
98
def row (self ) -> int :
@@ -359,11 +390,6 @@ def __getnewargs__(self):
359
390
def _with_row_col (self , row : int , col : int ) -> 'GridQubit' :
360
391
return GridQubit (row , col )
361
392
362
- def _cmp_tuple (self ):
363
- cls = GridQid if type (self ) is GridQubit else type (self )
364
- # Must be same as Qid._cmp_tuple but with cls in place of type(self).
365
- return (cls .__name__ , repr (cls ), self ._comparison_key (), self .dimension )
366
-
367
393
@staticmethod
368
394
def square (diameter : int , top : int = 0 , left : int = 0 ) -> List ['GridQubit' ]:
369
395
"""Returns a square of GridQubits.
0 commit comments