Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return self in Operation.with_tags if adding no tags #4301

Merged
merged 2 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 1 addition & 6 deletions cirq-core/cirq/devices/noise_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,7 @@ def is_virtual_moment(self, moment: 'cirq.Moment') -> bool:
"""
if not moment.operations:
return False
return all(
[
isinstance(op, ops.TaggedOperation) and ops.VirtualTag() in op.tags
for op in moment.operations
]
)
return all(ops.VirtualTag() in op.tags for op in moment)

def _noisy_moments_impl_moment(
self, moments: 'Iterable[cirq.Moment]', system_qubits: Sequence['cirq.Qid']
Expand Down
6 changes: 5 additions & 1 deletion cirq-core/cirq/ops/raw_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def untagged(self) -> 'cirq.Operation':
"""Returns the underlying operation without any tags."""
return self

def with_tags(self, *new_tags: Hashable) -> 'cirq.TaggedOperation':
def with_tags(self, *new_tags: Hashable) -> 'cirq.Operation':
"""Creates a new TaggedOperation, with this op and the specified tags.

This method can be used to attach meta-data to specific operations
Expand All @@ -443,6 +443,8 @@ def with_tags(self, *new_tags: Hashable) -> 'cirq.TaggedOperation':
Args:
new_tags: The tags to wrap this operation in.
"""
if not new_tags:
return self
return TaggedOperation(self, *new_tags)

def transform_qubits(
Expand Down Expand Up @@ -608,6 +610,8 @@ def with_tags(self, *new_tags: Hashable) -> 'cirq.TaggedOperation':
that has the tags of this operation combined with the new_tags
specified as the parameter.
"""
if not new_tags:
return self
return TaggedOperation(self.sub_operation, *self._tags, *new_tags)

def __str__(self) -> str:
Expand Down
8 changes: 8 additions & 0 deletions cirq-core/cirq/ops/raw_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ def test_tagged_operation():
assert not cirq.is_measurement(op)


def test_with_tags_returns_same_instance_if_possible():
untagged = cirq.X(cirq.GridQubit(1, 1))
assert untagged.with_tags() is untagged

tagged = untagged.with_tags('foo')
assert tagged.with_tags() is tagged


def test_tagged_measurement():
assert not cirq.is_measurement(cirq.GlobalPhaseOperation(coefficient=-1.0).with_tags('tag0'))

Expand Down