@@ -622,27 +622,33 @@ def classical_controls(self) -> FrozenSet['cirq.Condition']:
622
622
623
623
def with_classical_controls (
624
624
self , * conditions : Union [str , 'cirq.MeasurementKey' , 'cirq.Condition' , sympy .Expr ]
625
- ) -> 'cirq.ClassicallyControlledOperation ' :
625
+ ) -> 'cirq.Operation ' :
626
626
"""Returns a classically controlled version of this operation.
627
627
628
628
An operation that is classically controlled is executed iff all
629
629
conditions evaluate to True. Currently the only condition type is a
630
630
measurement key. A measurement key evaluates to True iff any qubit in
631
631
the corresponding measurement operation evaluated to a non-zero value.
632
632
633
- The classical control will hide any tags on the existing operation,
634
- since tags are considered a local attribute.
633
+ If no conditions are specified, returns self.
634
+
635
+ The classical control will remove any tags on the existing operation,
636
+ since tags are fragile, and we always opt to get rid of the tags when
637
+ the underlying operation is changed.
635
638
636
639
Args:
637
640
*conditions: A list of measurement keys, strings that can be parsed
638
641
into measurement keys, or sympy expressions where the free
639
642
symbols are measurement key strings.
640
643
641
644
Returns:
642
- A `ClassicallyControlledOperation` wrapping the operation.
645
+ A `ClassicallyControlledOperation` wrapping the operation. If no conditions
646
+ are specified, returns self.
643
647
"""
644
648
from cirq .ops .classically_controlled_operation import ClassicallyControlledOperation
645
649
650
+ if not conditions :
651
+ return self
646
652
return ClassicallyControlledOperation (self , conditions )
647
653
648
654
def without_classical_controls (self ) -> 'cirq.Operation' :
@@ -655,10 +661,10 @@ def without_classical_controls(self) -> 'cirq.Operation':
655
661
If there are no classical controls on the operation, it will return
656
662
`self`.
657
663
658
- Since tags are considered local , this will also remove any tags from
659
- the operation (unless there are no classical controls on it). If a
660
- `TaggedOperation` is under all the classical control layers, that
661
- `TaggedOperation` will be returned from this function.
664
+ Since tags are fragile , this will also remove any tags from the operation,
665
+ when called on `TaggedOperation` (unless there are no classical controls on it).
666
+ If a `TaggedOperation` is under all the classical control layers,
667
+ that `TaggedOperation` will be returned from this function.
662
668
663
669
Returns:
664
670
The operation with all classical controls removed.
@@ -712,6 +718,8 @@ def controlled_by(
712
718
* control_qubits : 'cirq.Qid' ,
713
719
control_values : Optional [Sequence [Union [int , Collection [int ]]]] = None ,
714
720
) -> 'cirq.Operation' :
721
+ if len (control_qubits ) == 0 :
722
+ return self
715
723
return self .sub_operation .controlled_by (* control_qubits , control_values = control_values )
716
724
717
725
@property
@@ -864,6 +872,13 @@ def without_classical_controls(self) -> 'cirq.Operation':
864
872
new_sub_operation = self .sub_operation .without_classical_controls ()
865
873
return self if new_sub_operation is self .sub_operation else new_sub_operation
866
874
875
+ def with_classical_controls (
876
+ self , * conditions : Union [str , 'cirq.MeasurementKey' , 'cirq.Condition' , sympy .Expr ]
877
+ ) -> 'cirq.Operation' :
878
+ if not conditions :
879
+ return self
880
+ return self .sub_operation .with_classical_controls (* conditions )
881
+
867
882
def _control_keys_ (self ) -> AbstractSet ['cirq.MeasurementKey' ]:
868
883
return protocols .control_keys (self .sub_operation )
869
884
0 commit comments