Skip to content

Disallow passing types as tags #6947

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

Merged
merged 5 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cirq-core/cirq/ops/raw_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,8 @@ class TaggedOperation(Operation):
def __init__(self, sub_operation: 'cirq.Operation', *tags: Hashable):
self._sub_operation = sub_operation
self._tags = tuple(tags)
if any(isinstance(tag, type) for tag in tags):
raise ValueError('Tags cannot be types. Did you forget to instantiate the tag type?')

@property
def sub_operation(self) -> 'cirq.Operation':
Expand Down
6 changes: 6 additions & 0 deletions cirq-core/cirq/ops/raw_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,12 @@ def test_tagged_operation():
assert op.with_qubits(q2).qubits == (q2,)
assert not cirq.is_measurement(op)

# Tags can't be types
# This is to prevent typos of cirq.X(q1).with_tags(TagType)
# when you meant cirq.X(q1).with_tags(TagType())
with pytest.raises(ValueError, match="cannot be types"):
_ = cirq.X(q1).with_tags(cirq.Circuit)


def test_with_tags_returns_same_instance_if_possible():
untagged = cirq.X(cirq.GridQubit(1, 1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,6 @@ class NoiseModelFromGoogleNoiseProperties(devices.NoiseModelFromNoiseProperties)
"""A noise model defined from noise properties of a Google device."""

def is_virtual(self, op: cirq.Operation) -> bool:
return isinstance(op.gate, cirq.ZPowGate) and cirq_google.PhysicalZTag not in op.tags
return isinstance(op.gate, cirq.ZPowGate) and cirq_google.PhysicalZTag() not in op.tags

# noisy_moments is implemented by the superclass.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def test_with_params_opid_with_gate():
@pytest.mark.parametrize(
'op',
[
(cirq.Z(cirq.LineQubit(0)) ** 0.3).with_tags(cirq_google.PhysicalZTag),
(cirq.Z(cirq.LineQubit(0)) ** 0.3).with_tags(cirq_google.PhysicalZTag()),
cirq.PhasedXZGate(x_exponent=0.8, z_exponent=0.2, axis_phase_exponent=0.1).on(
cirq.LineQubit(0)
),
Expand Down