cirq-core 1.6.0.dev20250424231143__py3-none-any.whl → 1.6.0.dev20250425004112__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of cirq-core might be problematic. Click here for more details.

Files changed (57) hide show
  1. cirq/_version.py +1 -1
  2. cirq/_version_test.py +1 -1
  3. cirq/ops/dense_pauli_string.py +28 -28
  4. cirq/ops/diagonal_gate.py +12 -14
  5. cirq/ops/eigen_gate.py +8 -5
  6. cirq/ops/fourier_transform.py +8 -12
  7. cirq/ops/fsim_gate.py +38 -37
  8. cirq/ops/gate_operation.py +25 -27
  9. cirq/ops/gateset.py +7 -5
  10. cirq/ops/global_phase_op.py +11 -11
  11. cirq/ops/greedy_qubit_manager.py +9 -7
  12. cirq/ops/identity.py +8 -6
  13. cirq/ops/kraus_channel.py +7 -4
  14. cirq/ops/linear_combinations.py +36 -34
  15. cirq/ops/matrix_gates.py +10 -10
  16. cirq/ops/measure_util.py +6 -4
  17. cirq/ops/measurement_gate.py +12 -12
  18. cirq/ops/mixed_unitary_channel.py +7 -4
  19. cirq/ops/named_qubit.py +8 -5
  20. cirq/ops/op_tree.py +4 -2
  21. cirq/ops/parallel_gate.py +11 -13
  22. cirq/ops/parity_gates.py +15 -17
  23. cirq/ops/pauli_gates.py +20 -17
  24. cirq/ops/pauli_interaction_gate.py +10 -14
  25. cirq/ops/pauli_measurement_gate.py +20 -20
  26. cirq/ops/pauli_string.py +76 -79
  27. cirq/ops/pauli_string_phasor.py +30 -30
  28. cirq/ops/pauli_string_raw_types.py +6 -4
  29. cirq/ops/pauli_sum_exponential.py +11 -11
  30. cirq/ops/permutation_gate.py +5 -3
  31. cirq/ops/phased_iswap_gate.py +8 -8
  32. cirq/ops/phased_x_gate.py +8 -9
  33. cirq/ops/phased_x_z_gate.py +15 -13
  34. cirq/ops/qid_util.py +6 -4
  35. cirq/ops/qubit_manager.py +9 -7
  36. cirq/ops/random_gate_channel.py +9 -7
  37. cirq/ops/raw_types.py +70 -72
  38. cirq/ops/raw_types_test.py +10 -6
  39. cirq/ops/state_preparation_channel.py +4 -4
  40. cirq/ops/swap_gates.py +7 -9
  41. cirq/ops/three_qubit_gates.py +22 -28
  42. cirq/ops/two_qubit_diagonal_gate.py +8 -8
  43. cirq/ops/uniform_superposition_gate.py +3 -1
  44. cirq/ops/wait_gate.py +12 -9
  45. cirq/protocols/act_on_protocol.py +6 -4
  46. cirq/protocols/act_on_protocol_test.py +8 -5
  47. cirq/protocols/apply_unitary_protocol.py +10 -8
  48. cirq/protocols/circuit_diagram_info_protocol.py +10 -8
  49. cirq/protocols/control_key_protocol.py +5 -3
  50. cirq/protocols/decompose_protocol.py +25 -22
  51. cirq/protocols/decompose_protocol_test.py +5 -2
  52. cirq/protocols/inverse_protocol.py +10 -8
  53. {cirq_core-1.6.0.dev20250424231143.dist-info → cirq_core-1.6.0.dev20250425004112.dist-info}/METADATA +1 -1
  54. {cirq_core-1.6.0.dev20250424231143.dist-info → cirq_core-1.6.0.dev20250425004112.dist-info}/RECORD +57 -57
  55. {cirq_core-1.6.0.dev20250424231143.dist-info → cirq_core-1.6.0.dev20250425004112.dist-info}/WHEEL +0 -0
  56. {cirq_core-1.6.0.dev20250424231143.dist-info → cirq_core-1.6.0.dev20250425004112.dist-info}/licenses/LICENSE +0 -0
  57. {cirq_core-1.6.0.dev20250424231143.dist-info → cirq_core-1.6.0.dev20250425004112.dist-info}/top_level.txt +0 -0
@@ -12,6 +12,8 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ from __future__ import annotations
16
+
15
17
  import numbers
16
18
  from typing import AbstractSet, Any, cast, Dict, Optional, SupportsFloat, Tuple, TYPE_CHECKING
17
19
 
@@ -29,7 +31,7 @@ if TYPE_CHECKING:
29
31
  class RandomGateChannel(raw_types.Gate):
30
32
  """Applies a sub gate with some probability."""
31
33
 
32
- def __init__(self, *, sub_gate: 'cirq.Gate', probability: 'cirq.TParamVal'):
34
+ def __init__(self, *, sub_gate: cirq.Gate, probability: cirq.TParamVal):
33
35
  if (
34
36
  isinstance(probability, numbers.Number)
35
37
  and not 0 <= float(cast(SupportsFloat, probability)) <= 1
@@ -45,11 +47,11 @@ class RandomGateChannel(raw_types.Gate):
45
47
  self._sub_gate = self.sub_gate.sub_gate
46
48
 
47
49
  @property
48
- def sub_gate(self) -> 'cirq.Gate':
50
+ def sub_gate(self) -> cirq.Gate:
49
51
  return self._sub_gate
50
52
 
51
53
  @property
52
- def probability(self) -> 'cirq.TParamVal':
54
+ def probability(self) -> cirq.TParamVal:
53
55
  return self._probability
54
56
 
55
57
  def _qid_shape_(self) -> Tuple[int, ...]:
@@ -78,8 +80,8 @@ class RandomGateChannel(raw_types.Gate):
78
80
  )
79
81
 
80
82
  def _resolve_parameters_(
81
- self, resolver: 'cirq.ParamResolver', recursive: bool
82
- ) -> 'RandomGateChannel':
83
+ self, resolver: cirq.ParamResolver, recursive: bool
84
+ ) -> RandomGateChannel:
83
85
  return RandomGateChannel(
84
86
  sub_gate=protocols.resolve_parameters(self.sub_gate, resolver, recursive),
85
87
  probability=protocols.resolve_parameters(self.probability, resolver, recursive),
@@ -129,8 +131,8 @@ class RandomGateChannel(raw_types.Gate):
129
131
  return cls(sub_gate=sub_gate, probability=probability)
130
132
 
131
133
  def _circuit_diagram_info_(
132
- self, args: 'cirq.CircuitDiagramInfoArgs'
133
- ) -> Optional['cirq.CircuitDiagramInfo']:
134
+ self, args: cirq.CircuitDiagramInfoArgs
135
+ ) -> Optional[cirq.CircuitDiagramInfo]:
134
136
  result = protocols.circuit_diagram_info(self.sub_gate, args, None)
135
137
  if result is None:
136
138
  return None
cirq/ops/raw_types.py CHANGED
@@ -14,6 +14,8 @@
14
14
 
15
15
  """Basic types defining qubits, gates, and operations."""
16
16
 
17
+ from __future__ import annotations
18
+
17
19
  import abc
18
20
  import functools
19
21
  from types import NotImplementedType
@@ -43,7 +45,6 @@ from typing_extensions import Self
43
45
  from cirq import protocols, value
44
46
  from cirq._compat import __cirq_debug__, _method_cache_name, cached_method
45
47
  from cirq._import import LazyLoader
46
- from cirq.ops import control_values as cv
47
48
 
48
49
  # Lazy imports to break circular dependencies.
49
50
  ops = LazyLoader("ops", globals(), "cirq.ops")
@@ -52,6 +53,7 @@ line_qubit = LazyLoader("line_qubit", globals(), "cirq.devices.line_qubit")
52
53
 
53
54
  if TYPE_CHECKING:
54
55
  import cirq
56
+ from cirq.ops import control_values as cv
55
57
 
56
58
 
57
59
  class Qid(metaclass=abc.ABCMeta):
@@ -96,7 +98,7 @@ class Qid(metaclass=abc.ABCMeta):
96
98
  f'Wrong qid dimension. Expected a positive integer but got {dimension}.'
97
99
  )
98
100
 
99
- def with_dimension(self, dimension: int) -> 'Qid':
101
+ def with_dimension(self, dimension: int) -> Qid:
100
102
  """Returns a new qid with a different dimension.
101
103
 
102
104
  Child classes can override. Wraps the qubit object by default.
@@ -154,9 +156,7 @@ class Qid(metaclass=abc.ABCMeta):
154
156
  return NotImplemented
155
157
  return self._cmp_tuple() >= other._cmp_tuple()
156
158
 
157
- def _circuit_diagram_info_(
158
- self, args: 'cirq.CircuitDiagramInfoArgs'
159
- ) -> 'cirq.CircuitDiagramInfo':
159
+ def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
160
160
  """Circuit symbol for qids defaults to the string representation."""
161
161
  return protocols.CircuitDiagramInfo(wire_symbols=(str(self),))
162
162
 
@@ -210,7 +210,7 @@ class Gate(metaclass=value.ABCMetaImplementAnyOneOf):
210
210
  multiplying them by scalars.
211
211
  """
212
212
 
213
- def validate_args(self, qubits: Sequence['cirq.Qid']) -> None:
213
+ def validate_args(self, qubits: Sequence[cirq.Qid]) -> None:
214
214
  """Checks if this gate can be applied to the given qubits.
215
215
 
216
216
  By default checks that:
@@ -230,7 +230,7 @@ class Gate(metaclass=value.ABCMetaImplementAnyOneOf):
230
230
  if __cirq_debug__.get():
231
231
  _validate_qid_shape(self, qubits)
232
232
 
233
- def on(self, *qubits: Qid) -> 'Operation':
233
+ def on(self, *qubits: Qid) -> Operation:
234
234
  """Returns an application of this gate to the given qubits.
235
235
 
236
236
  Args:
@@ -241,7 +241,7 @@ class Gate(metaclass=value.ABCMetaImplementAnyOneOf):
241
241
  """
242
242
  return ops.gate_operation.GateOperation(self, qubits)
243
243
 
244
- def on_each(self, *targets: Union[Qid, Iterable[Any]]) -> List['cirq.Operation']:
244
+ def on_each(self, *targets: Union[Qid, Iterable[Any]]) -> List[cirq.Operation]:
245
245
  """Returns a list of operations applying the gate to all targets.
246
246
 
247
247
  Args:
@@ -259,7 +259,7 @@ class Gate(metaclass=value.ABCMetaImplementAnyOneOf):
259
259
  If the gate qubit number is incompatible.
260
260
  TypeError: If a single target is supplied and it is not iterable.
261
261
  """
262
- operations: List['cirq.Operation'] = []
262
+ operations: List[cirq.Operation] = []
263
263
  if self._num_qubits_() > 1:
264
264
  iterator: Iterable = targets
265
265
  if len(targets) == 1:
@@ -306,8 +306,8 @@ class Gate(metaclass=value.ABCMetaImplementAnyOneOf):
306
306
  return operations
307
307
 
308
308
  def wrap_in_linear_combination(
309
- self, coefficient: 'cirq.TParamValComplex' = 1
310
- ) -> 'cirq.LinearCombinationOfGates':
309
+ self, coefficient: cirq.TParamValComplex = 1
310
+ ) -> cirq.LinearCombinationOfGates:
311
311
  """Returns a LinearCombinationOfGates with this gate.
312
312
 
313
313
  Args:
@@ -321,29 +321,29 @@ class Gate(metaclass=value.ABCMetaImplementAnyOneOf):
321
321
  return ops.linear_combinations.LinearCombinationOfGates({self: coefficient})
322
322
 
323
323
  def __add__(
324
- self, other: Union['Gate', 'cirq.LinearCombinationOfGates']
325
- ) -> 'cirq.LinearCombinationOfGates':
324
+ self, other: Union[Gate, cirq.LinearCombinationOfGates]
325
+ ) -> cirq.LinearCombinationOfGates:
326
326
  if isinstance(other, Gate):
327
327
  return self.wrap_in_linear_combination() + other.wrap_in_linear_combination()
328
328
  return self.wrap_in_linear_combination() + other
329
329
 
330
330
  def __sub__(
331
- self, other: Union['Gate', 'cirq.LinearCombinationOfGates']
332
- ) -> 'cirq.LinearCombinationOfGates':
331
+ self, other: Union[Gate, cirq.LinearCombinationOfGates]
332
+ ) -> cirq.LinearCombinationOfGates:
333
333
  if isinstance(other, Gate):
334
334
  return self.wrap_in_linear_combination() - other.wrap_in_linear_combination()
335
335
  return self.wrap_in_linear_combination() - other
336
336
 
337
- def __neg__(self) -> 'cirq.LinearCombinationOfGates':
337
+ def __neg__(self) -> cirq.LinearCombinationOfGates:
338
338
  return self.wrap_in_linear_combination(coefficient=-1)
339
339
 
340
- def __mul__(self, other: complex) -> 'cirq.LinearCombinationOfGates':
340
+ def __mul__(self, other: complex) -> cirq.LinearCombinationOfGates:
341
341
  return self.wrap_in_linear_combination(coefficient=other)
342
342
 
343
- def __rmul__(self, other: complex) -> 'cirq.LinearCombinationOfGates':
343
+ def __rmul__(self, other: complex) -> cirq.LinearCombinationOfGates:
344
344
  return self.wrap_in_linear_combination(coefficient=other)
345
345
 
346
- def __truediv__(self, other: complex) -> 'cirq.LinearCombinationOfGates':
346
+ def __truediv__(self, other: complex) -> cirq.LinearCombinationOfGates:
347
347
  return self.wrap_in_linear_combination(coefficient=1 / other)
348
348
 
349
349
  def __pow__(self, power):
@@ -368,7 +368,7 @@ class Gate(metaclass=value.ABCMetaImplementAnyOneOf):
368
368
  def __call__(self, *qubits: Qid, **kwargs):
369
369
  return self.on(*qubits)
370
370
 
371
- def with_probability(self, probability: 'cirq.TParamVal') -> 'cirq.Gate':
371
+ def with_probability(self, probability: cirq.TParamVal) -> cirq.Gate:
372
372
  """Creates a probabilistic channel with this gate.
373
373
 
374
374
  Args:
@@ -390,7 +390,7 @@ class Gate(metaclass=value.ABCMetaImplementAnyOneOf):
390
390
  Union[cv.AbstractControlValues, Sequence[Union[int, Collection[int]]]]
391
391
  ] = None,
392
392
  control_qid_shape: Optional[Tuple[int, ...]] = None,
393
- ) -> 'Gate':
393
+ ) -> Gate:
394
394
  """Returns a controlled version of this gate. If no arguments are
395
395
  specified, defaults to a single qubit control.
396
396
 
@@ -477,7 +477,7 @@ class Gate(metaclass=value.ABCMetaImplementAnyOneOf):
477
477
  return NotImplemented
478
478
 
479
479
  def _commutes_on_qids_(
480
- self, qids: 'Sequence[cirq.Qid]', other: Any, *, atol: float = 1e-8
480
+ self, qids: Sequence[cirq.Qid], other: Any, *, atol: float = 1e-8
481
481
  ) -> Union[bool, NotImplementedType, None]:
482
482
  return NotImplemented
483
483
 
@@ -492,11 +492,11 @@ class Gate(metaclass=value.ABCMetaImplementAnyOneOf):
492
492
  qs = line_qubit.LineQid.for_qid_shape(protocols.qid_shape(self))
493
493
  return protocols.commutes(self(*qs), other(*qs))
494
494
 
495
- def _mul_with_qubits(self, qubits: Tuple['cirq.Qid', ...], other):
495
+ def _mul_with_qubits(self, qubits: Tuple[cirq.Qid, ...], other):
496
496
  """cirq.GateOperation.__mul__ delegates to this method."""
497
497
  return NotImplemented
498
498
 
499
- def _rmul_with_qubits(self, qubits: Tuple['cirq.Qid', ...], other):
499
+ def _rmul_with_qubits(self, qubits: Tuple[cirq.Qid, ...], other):
500
500
  """cirq.GateOperation.__rmul__ delegates to this method."""
501
501
  return NotImplemented
502
502
 
@@ -512,12 +512,12 @@ class Operation(metaclass=abc.ABCMeta):
512
512
  """
513
513
 
514
514
  @property
515
- def gate(self) -> Optional['cirq.Gate']:
515
+ def gate(self) -> Optional[cirq.Gate]:
516
516
  return None
517
517
 
518
518
  @property
519
519
  @abc.abstractmethod
520
- def qubits(self) -> Tuple['cirq.Qid', ...]:
520
+ def qubits(self) -> Tuple[cirq.Qid, ...]:
521
521
  raise NotImplementedError()
522
522
 
523
523
  def _num_qubits_(self) -> int:
@@ -532,7 +532,7 @@ class Operation(metaclass=abc.ABCMeta):
532
532
  return protocols.qid_shape(self.qubits)
533
533
 
534
534
  @abc.abstractmethod
535
- def with_qubits(self, *new_qubits: 'cirq.Qid') -> Self:
535
+ def with_qubits(self, *new_qubits: cirq.Qid) -> Self:
536
536
  """Returns the same operation, but applied to different qubits.
537
537
 
538
538
  Args:
@@ -547,11 +547,11 @@ class Operation(metaclass=abc.ABCMeta):
547
547
  return ()
548
548
 
549
549
  @property
550
- def untagged(self) -> 'cirq.Operation':
550
+ def untagged(self) -> cirq.Operation:
551
551
  """Returns the underlying operation without any tags."""
552
552
  return self
553
553
 
554
- def with_tags(self, *new_tags: Hashable) -> 'cirq.Operation':
554
+ def with_tags(self, *new_tags: Hashable) -> cirq.Operation:
555
555
  """Creates a new TaggedOperation, with this op and the specified tags.
556
556
 
557
557
  This method can be used to attach meta-data to specific operations
@@ -577,7 +577,7 @@ class Operation(metaclass=abc.ABCMeta):
577
577
  return TaggedOperation(self, *new_tags)
578
578
 
579
579
  def transform_qubits(
580
- self, qubit_map: Union[Dict['cirq.Qid', 'cirq.Qid'], Callable[['cirq.Qid'], 'cirq.Qid']]
580
+ self, qubit_map: Union[Dict[cirq.Qid, cirq.Qid], Callable[[cirq.Qid], cirq.Qid]]
581
581
  ) -> Self:
582
582
  """Returns the same operation, but with different qubits.
583
583
 
@@ -622,11 +622,11 @@ class Operation(metaclass=abc.ABCMeta):
622
622
 
623
623
  def controlled_by(
624
624
  self,
625
- *control_qubits: 'cirq.Qid',
625
+ *control_qubits: cirq.Qid,
626
626
  control_values: Optional[
627
627
  Union[cv.AbstractControlValues, Sequence[Union[int, Collection[int]]]]
628
628
  ] = None,
629
- ) -> 'cirq.Operation':
629
+ ) -> cirq.Operation:
630
630
  """Returns a controlled version of this operation. If no control_qubits
631
631
  are specified, returns self.
632
632
 
@@ -644,7 +644,7 @@ class Operation(metaclass=abc.ABCMeta):
644
644
  return self
645
645
  return ops.controlled_operation.ControlledOperation(control_qubits, self, control_values)
646
646
 
647
- def with_probability(self, probability: 'cirq.TParamVal') -> 'cirq.Operation':
647
+ def with_probability(self, probability: cirq.TParamVal) -> cirq.Operation:
648
648
  """Creates a probabilistic channel with this operation.
649
649
 
650
650
  Args:
@@ -667,7 +667,7 @@ class Operation(metaclass=abc.ABCMeta):
667
667
  *self.qubits
668
668
  )
669
669
 
670
- def validate_args(self, qubits: Sequence['cirq.Qid']):
670
+ def validate_args(self, qubits: Sequence[cirq.Qid]):
671
671
  """Raises an exception if the `qubits` don't match this operation's qid
672
672
  shape.
673
673
 
@@ -691,13 +691,13 @@ class Operation(metaclass=abc.ABCMeta):
691
691
  return _operations_commutes_impl([self], [other], atol=atol)
692
692
 
693
693
  @property
694
- def classical_controls(self) -> FrozenSet['cirq.Condition']:
694
+ def classical_controls(self) -> FrozenSet[cirq.Condition]:
695
695
  """The classical controls gating this operation."""
696
696
  return frozenset()
697
697
 
698
698
  def with_classical_controls(
699
- self, *conditions: Union[str, 'cirq.MeasurementKey', 'cirq.Condition', sympy.Expr]
700
- ) -> 'cirq.Operation':
699
+ self, *conditions: Union[str, cirq.MeasurementKey, cirq.Condition, sympy.Expr]
700
+ ) -> cirq.Operation:
701
701
  """Returns a classically controlled version of this operation.
702
702
 
703
703
  An operation that is classically controlled is executed iff all
@@ -728,7 +728,7 @@ class Operation(metaclass=abc.ABCMeta):
728
728
  return self
729
729
  return ClassicallyControlledOperation(self, conditions)
730
730
 
731
- def without_classical_controls(self) -> 'cirq.Operation':
731
+ def without_classical_controls(self) -> cirq.Operation:
732
732
  """Removes all classical controls from the operation.
733
733
 
734
734
  This function removes all classical controls gating the operation. It
@@ -766,25 +766,25 @@ class TaggedOperation(Operation):
766
766
  See `Operation.with_tags()` for more information on intended usage.
767
767
  """
768
768
 
769
- def __init__(self, sub_operation: 'cirq.Operation', *tags: Hashable):
769
+ def __init__(self, sub_operation: cirq.Operation, *tags: Hashable):
770
770
  self._sub_operation = sub_operation
771
771
  self._tags = tuple(tags)
772
772
  if any(isinstance(tag, type) for tag in tags):
773
773
  raise ValueError('Tags cannot be types. Did you forget to instantiate the tag type?')
774
774
 
775
775
  @property
776
- def sub_operation(self) -> 'cirq.Operation':
776
+ def sub_operation(self) -> cirq.Operation:
777
777
  return self._sub_operation
778
778
 
779
779
  @property
780
- def qubits(self) -> Tuple['cirq.Qid', ...]:
780
+ def qubits(self) -> Tuple[cirq.Qid, ...]:
781
781
  return self.sub_operation.qubits
782
782
 
783
783
  @property
784
- def gate(self) -> Optional['cirq.Gate']:
784
+ def gate(self) -> Optional[cirq.Gate]:
785
785
  return self.sub_operation.gate
786
786
 
787
- def with_qubits(self, *new_qubits: 'cirq.Qid'):
787
+ def with_qubits(self, *new_qubits: cirq.Qid):
788
788
  return TaggedOperation(self.sub_operation.with_qubits(*new_qubits), *self._tags)
789
789
 
790
790
  def _with_measurement_key_mapping_(self, key_map: Mapping[str, str]):
@@ -795,11 +795,11 @@ class TaggedOperation(Operation):
795
795
 
796
796
  def controlled_by(
797
797
  self,
798
- *control_qubits: 'cirq.Qid',
798
+ *control_qubits: cirq.Qid,
799
799
  control_values: Optional[
800
800
  Union[cv.AbstractControlValues, Sequence[Union[int, Collection[int]]]]
801
801
  ] = None,
802
- ) -> 'cirq.Operation':
802
+ ) -> cirq.Operation:
803
803
  if len(control_qubits) == 0:
804
804
  return self
805
805
  return self.sub_operation.controlled_by(*control_qubits, control_values=control_values)
@@ -810,11 +810,11 @@ class TaggedOperation(Operation):
810
810
  return self._tags
811
811
 
812
812
  @property
813
- def untagged(self) -> 'cirq.Operation':
813
+ def untagged(self) -> cirq.Operation:
814
814
  """Returns the underlying operation without any tags."""
815
815
  return self.sub_operation
816
816
 
817
- def with_tags(self, *new_tags: Hashable) -> 'cirq.TaggedOperation':
817
+ def with_tags(self, *new_tags: Hashable) -> cirq.TaggedOperation:
818
818
  """Creates a new TaggedOperation with combined tags.
819
819
 
820
820
  Overloads Operation.with_tags to create a new TaggedOperation
@@ -845,12 +845,12 @@ class TaggedOperation(Operation):
845
845
  def _json_dict_(self) -> Dict[str, Any]:
846
846
  return protocols.obj_to_dict_helper(self, ['sub_operation', 'tags'])
847
847
 
848
- def _decompose_(self) -> 'cirq.OP_TREE':
848
+ def _decompose_(self) -> cirq.OP_TREE:
849
849
  return self._decompose_with_context_()
850
850
 
851
851
  def _decompose_with_context_(
852
- self, context: Optional['cirq.DecompositionContext'] = None
853
- ) -> 'cirq.OP_TREE':
852
+ self, context: Optional[cirq.DecompositionContext] = None
853
+ ) -> cirq.OP_TREE:
854
854
  return protocols.decompose_once(
855
855
  self.sub_operation, default=None, flatten=False, context=context
856
856
  )
@@ -859,7 +859,7 @@ class TaggedOperation(Operation):
859
859
  return protocols.pauli_expansion(self.sub_operation)
860
860
 
861
861
  def _apply_unitary_(
862
- self, args: 'protocols.ApplyUnitaryArgs'
862
+ self, args: protocols.ApplyUnitaryArgs
863
863
  ) -> Union[np.ndarray, None, NotImplementedType]:
864
864
  return protocols.apply_unitary(self.sub_operation, args, default=None)
865
865
 
@@ -894,7 +894,7 @@ class TaggedOperation(Operation):
894
894
  return protocols.measurement_key_names(self.sub_operation)
895
895
 
896
896
  @cached_method
897
- def _measurement_key_objs_(self) -> FrozenSet['cirq.MeasurementKey']:
897
+ def _measurement_key_objs_(self) -> FrozenSet[cirq.MeasurementKey]:
898
898
  return protocols.measurement_key_objs(self.sub_operation)
899
899
 
900
900
  @cached_method
@@ -910,7 +910,7 @@ class TaggedOperation(Operation):
910
910
  protocols.is_parameterized(tag) for tag in self.tags
911
911
  )
912
912
 
913
- def _act_on_(self, sim_state: 'cirq.SimulationStateBase') -> bool:
913
+ def _act_on_(self, sim_state: cirq.SimulationStateBase) -> bool:
914
914
  sub = getattr(self.sub_operation, "_act_on_", None)
915
915
  if sub is not None:
916
916
  return sub(sim_state)
@@ -922,17 +922,15 @@ class TaggedOperation(Operation):
922
922
  return protocols.parameter_names(self.sub_operation) | tag_params
923
923
 
924
924
  def _resolve_parameters_(
925
- self, resolver: 'cirq.ParamResolver', recursive: bool
926
- ) -> 'TaggedOperation':
925
+ self, resolver: cirq.ParamResolver, recursive: bool
926
+ ) -> TaggedOperation:
927
927
  resolved_op = protocols.resolve_parameters(self.sub_operation, resolver, recursive)
928
928
  resolved_tags = (
929
929
  protocols.resolve_parameters(tag, resolver, recursive) for tag in self._tags
930
930
  )
931
931
  return TaggedOperation(resolved_op, *resolved_tags)
932
932
 
933
- def _circuit_diagram_info_(
934
- self, args: 'cirq.CircuitDiagramInfoArgs'
935
- ) -> 'cirq.CircuitDiagramInfo':
933
+ def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
936
934
  sub_op_info = protocols.circuit_diagram_info(self.sub_operation, args, NotImplemented)
937
935
  # Add tag to wire symbol if it exists.
938
936
  if sub_op_info is not NotImplemented and args.include_tags and sub_op_info.wire_symbols:
@@ -945,12 +943,12 @@ class TaggedOperation(Operation):
945
943
  def _trace_distance_bound_(self) -> float:
946
944
  return protocols.trace_distance_bound(self.sub_operation)
947
945
 
948
- def _phase_by_(self, phase_turns: float, qubit_index: int) -> 'cirq.Operation':
946
+ def _phase_by_(self, phase_turns: float, qubit_index: int) -> cirq.Operation:
949
947
  return protocols.phase_by(
950
948
  self.sub_operation, phase_turns, qubit_index, default=NotImplemented
951
949
  )
952
950
 
953
- def __pow__(self, exponent: Any) -> 'cirq.Operation':
951
+ def __pow__(self, exponent: Any) -> cirq.Operation:
954
952
  return self.sub_operation**exponent
955
953
 
956
954
  def __mul__(self, other: Any) -> Any:
@@ -959,7 +957,7 @@ class TaggedOperation(Operation):
959
957
  def __rmul__(self, other: Any) -> Any:
960
958
  return other * self.sub_operation
961
959
 
962
- def _qasm_(self, args: 'protocols.QasmArgs') -> Optional[str]:
960
+ def _qasm_(self, args: protocols.QasmArgs) -> Optional[str]:
963
961
  return protocols.qasm(self.sub_operation, args=args, default=None)
964
962
 
965
963
  def _equal_up_to_global_phase_(
@@ -968,21 +966,21 @@ class TaggedOperation(Operation):
968
966
  return protocols.equal_up_to_global_phase(self.sub_operation, other, atol=atol)
969
967
 
970
968
  @property
971
- def classical_controls(self) -> FrozenSet['cirq.Condition']:
969
+ def classical_controls(self) -> FrozenSet[cirq.Condition]:
972
970
  return self.sub_operation.classical_controls
973
971
 
974
- def without_classical_controls(self) -> 'cirq.Operation':
972
+ def without_classical_controls(self) -> cirq.Operation:
975
973
  new_sub_operation = self.sub_operation.without_classical_controls()
976
974
  return self if new_sub_operation is self.sub_operation else new_sub_operation
977
975
 
978
976
  def with_classical_controls(
979
- self, *conditions: Union[str, 'cirq.MeasurementKey', 'cirq.Condition', sympy.Expr]
980
- ) -> 'cirq.Operation':
977
+ self, *conditions: Union[str, cirq.MeasurementKey, cirq.Condition, sympy.Expr]
978
+ ) -> cirq.Operation:
981
979
  if not conditions:
982
980
  return self
983
981
  return self.sub_operation.with_classical_controls(*conditions)
984
982
 
985
- def _control_keys_(self) -> FrozenSet['cirq.MeasurementKey']:
983
+ def _control_keys_(self) -> FrozenSet[cirq.MeasurementKey]:
986
984
  return protocols.control_keys(self.sub_operation)
987
985
 
988
986
 
@@ -1007,8 +1005,8 @@ class _InverseCompositeGate(Gate):
1007
1005
  return self._decompose_with_context_(qubits)
1008
1006
 
1009
1007
  def _decompose_with_context_(
1010
- self, qubits: Sequence['cirq.Qid'], context: Optional['cirq.DecompositionContext'] = None
1011
- ) -> 'cirq.OP_TREE':
1008
+ self, qubits: Sequence[cirq.Qid], context: Optional[cirq.DecompositionContext] = None
1009
+ ) -> cirq.OP_TREE:
1012
1010
  return protocols.inverse(
1013
1011
  protocols.decompose_once_with_qubits(self._original, qubits, context=context)
1014
1012
  )
@@ -1031,8 +1029,8 @@ class _InverseCompositeGate(Gate):
1031
1029
  return protocols.parameter_names(self._original)
1032
1030
 
1033
1031
  def _resolve_parameters_(
1034
- self, resolver: 'cirq.ParamResolver', recursive: bool
1035
- ) -> '_InverseCompositeGate':
1032
+ self, resolver: cirq.ParamResolver, recursive: bool
1033
+ ) -> _InverseCompositeGate:
1036
1034
  return _InverseCompositeGate(
1037
1035
  protocols.resolve_parameters(self._original, resolver, recursive)
1038
1036
  )
@@ -1040,7 +1038,7 @@ class _InverseCompositeGate(Gate):
1040
1038
  def _value_equality_values_(self):
1041
1039
  return self._original
1042
1040
 
1043
- def _circuit_diagram_info_(self, args: 'cirq.CircuitDiagramInfoArgs'):
1041
+ def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs):
1044
1042
  sub_info = protocols.circuit_diagram_info(self._original, args, default=NotImplemented)
1045
1043
  if sub_info is NotImplemented:
1046
1044
  return NotImplemented
@@ -1057,7 +1055,7 @@ class _InverseCompositeGate(Gate):
1057
1055
  return {'original': self._original}
1058
1056
 
1059
1057
 
1060
- def _validate_qid_shape(val: Any, qubits: Sequence['cirq.Qid']) -> None:
1058
+ def _validate_qid_shape(val: Any, qubits: Sequence[cirq.Qid]) -> None:
1061
1059
  """Helper function to validate qubits for gates and operations.
1062
1060
 
1063
1061
  Raises:
@@ -12,6 +12,8 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ from __future__ import annotations
16
+
15
17
  from typing import AbstractSet, Any, Iterator
16
18
 
17
19
  import numpy as np
@@ -580,8 +582,8 @@ def test_circuit_diagram_tagged_global_phase():
580
582
  # Operation with no qubits and returns diagram info with no wire symbols
581
583
  class NoWireSymbols(cirq.GlobalPhaseGate):
582
584
  def _circuit_diagram_info_(
583
- self, args: 'cirq.CircuitDiagramInfoArgs'
584
- ) -> 'cirq.CircuitDiagramInfo':
585
+ self, args: cirq.CircuitDiagramInfoArgs
586
+ ) -> cirq.CircuitDiagramInfo:
585
587
  return expected
586
588
 
587
589
  no_wire_symbol_op = NoWireSymbols(coefficient=-1.0)().with_tags('tag0')
@@ -737,8 +739,8 @@ class ParameterizableTag:
737
739
  return cirq.parameter_names(self.value)
738
740
 
739
741
  def _resolve_parameters_(
740
- self, resolver: 'cirq.ParamResolver', recursive: bool
741
- ) -> 'ParameterizableTag':
742
+ self, resolver: cirq.ParamResolver, recursive: bool
743
+ ) -> ParameterizableTag:
742
744
  return ParameterizableTag(cirq.resolve_parameters(self.value, resolver, recursive))
743
745
 
744
746
 
@@ -760,7 +762,7 @@ def test_tagged_operation_resolves_parameterized_tags(resolve_fn):
760
762
  def test_inverse_composite_standards():
761
763
  @cirq.value_equality
762
764
  class Gate(cirq.Gate):
763
- def __init__(self, param: 'cirq.TParamVal'):
765
+ def __init__(self, param: cirq.TParamVal):
764
766
  self._param = param
765
767
 
766
768
  def _decompose_(self, qubits):
@@ -781,7 +783,9 @@ def test_inverse_composite_standards():
781
783
  def _is_parameterized_(self) -> bool:
782
784
  return cirq.is_parameterized(self._param)
783
785
 
784
- def _resolve_parameters_(self, resolver: 'cirq.ParamResolver', recursive: bool) -> 'Gate':
786
+ def _resolve_parameters_(
787
+ self, resolver: cirq.ParamResolver, recursive: bool
788
+ ) -> Gate: # pylint: disable=undefined-variable
785
789
  return Gate(cirq.resolve_parameters(self._param, resolver, recursive))
786
790
 
787
791
  def __repr__(self):
@@ -14,6 +14,8 @@
14
14
 
15
15
  """Quantum gates to prepare a given target state."""
16
16
 
17
+ from __future__ import annotations
18
+
17
19
  from typing import Any, Dict, Iterable, Tuple, TYPE_CHECKING
18
20
 
19
21
  import numpy as np
@@ -64,7 +66,7 @@ class StatePreparationChannel(raw_types.Gate):
64
66
  @classmethod
65
67
  def _from_json_dict_(
66
68
  cls, target_state: np.ndarray, name: str, **kwargs
67
- ) -> 'StatePreparationChannel':
69
+ ) -> StatePreparationChannel:
68
70
  """Recreates the channel object from it's serialized form
69
71
 
70
72
  Args:
@@ -80,9 +82,7 @@ class StatePreparationChannel(raw_types.Gate):
80
82
  def _qid_shape_(self) -> Tuple[int, ...]:
81
83
  return self._qid_shape
82
84
 
83
- def _circuit_diagram_info_(
84
- self, _args: 'cirq.CircuitDiagramInfoArgs'
85
- ) -> 'cirq.CircuitDiagramInfo':
85
+ def _circuit_diagram_info_(self, _args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
86
86
  """Returns the information required to draw out the circuit diagram for this channel."""
87
87
  symbols = (
88
88
  [self._name]
cirq/ops/swap_gates.py CHANGED
@@ -25,6 +25,8 @@ raised to a power (i.e. SQRT_ISWAP_INV=cirq.ISWAP**-0.5). See the definition in
25
25
  EigenGate.
26
26
  """
27
27
 
28
+ from __future__ import annotations
29
+
28
30
  from typing import cast, List, Optional, Tuple, TYPE_CHECKING
29
31
 
30
32
  import numpy as np
@@ -105,7 +107,7 @@ class SwapPowGate(gate_features.InterchangeableQubitsGate, eigen_gate.EigenGate)
105
107
  return None
106
108
  return self.exponent % 1 == 0
107
109
 
108
- def _apply_unitary_(self, args: 'protocols.ApplyUnitaryArgs') -> Optional[np.ndarray]:
110
+ def _apply_unitary_(self, args: protocols.ApplyUnitaryArgs) -> Optional[np.ndarray]:
109
111
  if self._exponent != 1:
110
112
  return NotImplemented
111
113
 
@@ -134,9 +136,7 @@ class SwapPowGate(gate_features.InterchangeableQubitsGate, eigen_gate.EigenGate)
134
136
  }
135
137
  )
136
138
 
137
- def _circuit_diagram_info_(
138
- self, args: 'cirq.CircuitDiagramInfoArgs'
139
- ) -> 'cirq.CircuitDiagramInfo':
139
+ def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
140
140
  if not args.use_unicode_characters:
141
141
  return protocols.CircuitDiagramInfo(
142
142
  wire_symbols=('Swap', 'Swap'), exponent=self._diagram_exponent(args)
@@ -145,7 +145,7 @@ class SwapPowGate(gate_features.InterchangeableQubitsGate, eigen_gate.EigenGate)
145
145
  wire_symbols=('×', '×'), exponent=self._diagram_exponent(args)
146
146
  )
147
147
 
148
- def _qasm_(self, args: 'cirq.QasmArgs', qubits: Tuple['cirq.Qid', ...]) -> Optional[str]:
148
+ def _qasm_(self, args: cirq.QasmArgs, qubits: Tuple[cirq.Qid, ...]) -> Optional[str]:
149
149
  if self._exponent != 1:
150
150
  return None # Don't have an equivalent gate in QASM
151
151
  args.validate_version('2.0', '3.0')
@@ -240,7 +240,7 @@ class ISwapPowGate(gate_features.InterchangeableQubitsGate, eigen_gate.EigenGate
240
240
  yield common_gates.H(a)
241
241
  yield common_gates.CNOT(a, b)
242
242
 
243
- def _apply_unitary_(self, args: 'protocols.ApplyUnitaryArgs') -> Optional[np.ndarray]:
243
+ def _apply_unitary_(self, args: protocols.ApplyUnitaryArgs) -> Optional[np.ndarray]:
244
244
  if self._exponent != 1:
245
245
  return NotImplemented
246
246
 
@@ -271,9 +271,7 @@ class ISwapPowGate(gate_features.InterchangeableQubitsGate, eigen_gate.EigenGate
271
271
  }
272
272
  )
273
273
 
274
- def _circuit_diagram_info_(
275
- self, args: 'cirq.CircuitDiagramInfoArgs'
276
- ) -> 'cirq.CircuitDiagramInfo':
274
+ def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
277
275
  return protocols.CircuitDiagramInfo(
278
276
  wire_symbols=('iSwap', 'iSwap'), exponent=self._diagram_exponent(args)
279
277
  )