52
52
53
53
import cirq ._version
54
54
from cirq import _compat , devices , ops , protocols , qis
55
+ from cirq ._doc import document
55
56
from cirq .circuits ._bucket_priority_queue import BucketPriorityQueue
56
57
from cirq .circuits .circuit_operation import CircuitOperation
57
58
from cirq .circuits .insert_strategy import InsertStrategy
65
66
if TYPE_CHECKING :
66
67
import cirq
67
68
68
- T_DESIRED_GATE_TYPE = TypeVar ('T_DESIRED_GATE_TYPE' , bound = 'ops.Gate' )
69
+
70
+ _TGate = TypeVar ('_TGate' , bound = 'cirq.Gate' )
71
+
69
72
CIRCUIT_TYPE = TypeVar ('CIRCUIT_TYPE' , bound = 'AbstractCircuit' )
70
- INT_TYPE = Union [int , np .integer ]
73
+ document (
74
+ CIRCUIT_TYPE ,
75
+ """Type variable for an AbstractCircuit.
76
+
77
+ This can be used when writing generic functions that operate on circuits.
78
+ For example, suppose we define the following function:
79
+
80
+ def foo(circuit: CIRCUIT_TYPE) -> CIRCUIT_TYPE:
81
+ ...
82
+
83
+ This lets the typechecker know that this function takes any kind of circuit
84
+ and returns the same type, that is, if passed a `cirq.Circuit` it will return
85
+ `cirq.Circuit`, and similarly if passed `cirq.FrozenCircuit` it will return
86
+ `cirq.FrozenCircuit`. This is particularly useful for things like the
87
+ transformer API, since it can preserve more type information than if we typed
88
+ the function as taking and returning `cirq.AbstractCircuit`.
89
+ """ ,
90
+ )
91
+
92
+ _INT_TYPE = Union [int , np .integer ]
71
93
72
94
_DEVICE_DEP_MESSAGE = 'Attaching devices to circuits will no longer be supported.'
73
95
@@ -752,8 +774,8 @@ def findall_operations(
752
774
yield index , op
753
775
754
776
def findall_operations_with_gate_type (
755
- self , gate_type : Type [T_DESIRED_GATE_TYPE ]
756
- ) -> Iterable [Tuple [int , 'cirq.GateOperation' , T_DESIRED_GATE_TYPE ]]:
777
+ self , gate_type : Type [_TGate ]
778
+ ) -> Iterable [Tuple [int , 'cirq.GateOperation' , _TGate ]]:
757
779
"""Find the locations of all gate operations of a given type.
758
780
759
781
Args:
@@ -767,7 +789,7 @@ def findall_operations_with_gate_type(
767
789
result = self .findall_operations (lambda operation : isinstance (operation .gate , gate_type ))
768
790
for index , op in result :
769
791
gate_op = cast (ops .GateOperation , op )
770
- yield index , gate_op , cast (T_DESIRED_GATE_TYPE , gate_op .gate )
792
+ yield index , gate_op , cast (_TGate , gate_op .gate )
771
793
772
794
def has_measurements (self ):
773
795
return protocols .is_measurement (self )
@@ -1818,20 +1840,20 @@ def __radd__(self, other):
1818
1840
# Needed for numpy to handle multiplication by np.int64 correctly.
1819
1841
__array_priority__ = 10000
1820
1842
1821
- def __imul__ (self , repetitions : INT_TYPE ):
1843
+ def __imul__ (self , repetitions : _INT_TYPE ):
1822
1844
if not isinstance (repetitions , (int , np .integer )):
1823
1845
return NotImplemented
1824
1846
self ._moments *= int (repetitions )
1825
1847
return self
1826
1848
1827
- def __mul__ (self , repetitions : INT_TYPE ):
1849
+ def __mul__ (self , repetitions : _INT_TYPE ):
1828
1850
if not isinstance (repetitions , (int , np .integer )):
1829
1851
return NotImplemented
1830
1852
if self ._device == cirq .UNCONSTRAINED_DEVICE :
1831
1853
return Circuit (self ._moments * int (repetitions ))
1832
1854
return Circuit (self ._moments * int (repetitions ), device = self ._device )
1833
1855
1834
- def __rmul__ (self , repetitions : INT_TYPE ):
1856
+ def __rmul__ (self , repetitions : _INT_TYPE ):
1835
1857
if not isinstance (repetitions , (int , np .integer )):
1836
1858
return NotImplemented
1837
1859
return self * int (repetitions )
@@ -2750,27 +2772,26 @@ def _list_repr_with_indented_item_lines(items: Sequence[Any]) -> str:
2750
2772
return f'[\n { indented } \n ]'
2751
2773
2752
2774
2753
- TIn = TypeVar ('TIn ' )
2754
- TOut = TypeVar ('TOut ' )
2755
- TKey = TypeVar ('TKey ' )
2775
+ _TIn = TypeVar ('_TIn ' )
2776
+ _TOut = TypeVar ('_TOut ' )
2777
+ _TKey = TypeVar ('_TKey ' )
2756
2778
2757
2779
2758
2780
@overload
2759
2781
def _group_until_different (
2760
- items : Iterable [TIn ],
2761
- key : Callable [[TIn ], TKey ],
2762
- ) -> Iterable [Tuple [TKey , List [TIn ]]]:
2782
+ items : Iterable [_TIn ], key : Callable [[_TIn ], _TKey ]
2783
+ ) -> Iterable [Tuple [_TKey , List [_TIn ]]]:
2763
2784
pass
2764
2785
2765
2786
2766
2787
@overload
2767
2788
def _group_until_different (
2768
- items : Iterable [TIn ], key : Callable [[TIn ], TKey ], val : Callable [[TIn ], TOut ]
2769
- ) -> Iterable [Tuple [TKey , List [TOut ]]]:
2789
+ items : Iterable [_TIn ], key : Callable [[_TIn ], _TKey ], val : Callable [[_TIn ], _TOut ]
2790
+ ) -> Iterable [Tuple [_TKey , List [_TOut ]]]:
2770
2791
pass
2771
2792
2772
2793
2773
- def _group_until_different (items : Iterable [TIn ], key : Callable [[TIn ], TKey ], val = lambda e : e ):
2794
+ def _group_until_different (items : Iterable [_TIn ], key : Callable [[_TIn ], _TKey ], val = lambda e : e ):
2774
2795
"""Groups runs of items that are identical according to a keying function.
2775
2796
2776
2797
Args:
0 commit comments