Skip to content

Commit c34afb6

Browse files
maffoorht
authored andcommitted
Return self in Operation.with_tags if adding no tags (quantumlib#4301)
Review: @viathor
1 parent 8c1ccc8 commit c34afb6

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

cirq-core/cirq/devices/noise_model.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,7 @@ def is_virtual_moment(self, moment: 'cirq.Moment') -> bool:
8888
"""
8989
if not moment.operations:
9090
return False
91-
return all(
92-
[
93-
isinstance(op, ops.TaggedOperation) and ops.VirtualTag() in op.tags
94-
for op in moment.operations
95-
]
96-
)
91+
return all(ops.VirtualTag() in op.tags for op in moment)
9792

9893
def _noisy_moments_impl_moment(
9994
self, moments: 'Iterable[cirq.Moment]', system_qubits: Sequence['cirq.Qid']

cirq-core/cirq/ops/raw_types.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ def untagged(self) -> 'cirq.Operation':
425425
"""Returns the underlying operation without any tags."""
426426
return self
427427

428-
def with_tags(self, *new_tags: Hashable) -> 'cirq.TaggedOperation':
428+
def with_tags(self, *new_tags: Hashable) -> 'cirq.Operation':
429429
"""Creates a new TaggedOperation, with this op and the specified tags.
430430
431431
This method can be used to attach meta-data to specific operations
@@ -443,6 +443,8 @@ def with_tags(self, *new_tags: Hashable) -> 'cirq.TaggedOperation':
443443
Args:
444444
new_tags: The tags to wrap this operation in.
445445
"""
446+
if not new_tags:
447+
return self
446448
return TaggedOperation(self, *new_tags)
447449

448450
def transform_qubits(
@@ -608,6 +610,8 @@ def with_tags(self, *new_tags: Hashable) -> 'cirq.TaggedOperation':
608610
that has the tags of this operation combined with the new_tags
609611
specified as the parameter.
610612
"""
613+
if not new_tags:
614+
return self
611615
return TaggedOperation(self.sub_operation, *self._tags, *new_tags)
612616

613617
def __str__(self) -> str:

cirq-core/cirq/ops/raw_types_test.py

+8
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,14 @@ def test_tagged_operation():
434434
assert not cirq.is_measurement(op)
435435

436436

437+
def test_with_tags_returns_same_instance_if_possible():
438+
untagged = cirq.X(cirq.GridQubit(1, 1))
439+
assert untagged.with_tags() is untagged
440+
441+
tagged = untagged.with_tags('foo')
442+
assert tagged.with_tags() is tagged
443+
444+
437445
def test_tagged_measurement():
438446
assert not cirq.is_measurement(cirq.GlobalPhaseOperation(coefficient=-1.0).with_tags('tag0'))
439447

0 commit comments

Comments
 (0)