Skip to content

Commit be837b3

Browse files
committed
Disallow tags_to_ignore in tag_transformers
1 parent c3e3f41 commit be837b3

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

cirq-core/cirq/transformers/tag_transformers.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def index_tags(
2626
circuit: 'cirq.AbstractCircuit',
2727
*,
2828
context: Optional['cirq.TransformerContext'] = None,
29-
target_tags: Optional[set[Hashable]] = None,
29+
target_tags: set[Hashable],
3030
) -> 'cirq.Circuit':
3131
"""Indexes tags in target_tags as tag_0, tag_1, ... per tag.
3232
@@ -38,7 +38,10 @@ def index_tags(
3838
Returns:
3939
Copy of the transformed input circuit.
4040
"""
41-
target_tags = target_tags or set()
41+
if context and context.tags_to_ignore:
42+
raise ValueError("index_tags doesn't support tags_to_ignore, use function args instead.")
43+
if not target_tags:
44+
return circuit
4245
tag_iter_by_tags = {tag: itertools.count(start=0, step=1) for tag in target_tags}
4346

4447
def _map_func(op: 'cirq.Operation', _) -> 'cirq.OP_TREE':
@@ -51,10 +54,7 @@ def _map_func(op: 'cirq.Operation', _) -> 'cirq.OP_TREE':
5154
return op.untagged.with_tags(*tag_set)
5255

5356
return transformer_primitives.map_operations(
54-
circuit,
55-
_map_func,
56-
deep=context.deep if context else False,
57-
tags_to_ignore=context.tags_to_ignore if context else [],
57+
circuit, _map_func, deep=context.deep if context else False
5858
).unfreeze(copy=False)
5959

6060

@@ -68,8 +68,6 @@ def remove_tags(
6868
) -> 'cirq.Circuit':
6969
"""Removes tags from the operations based on the input args.
7070
71-
Note: context.tags_to_ignore has higher priority than target_tags and remove_if.
72-
7371
Args:
7472
circuit: Input circuit to apply the transformations on. The input circuit is not mutated.
7573
context: `cirq.TransformerContext` storing common configurable options for transformers.
@@ -80,6 +78,8 @@ def remove_tags(
8078
Returns:
8179
Copy of the transformed input circuit.
8280
"""
81+
if context and context.tags_to_ignore:
82+
raise ValueError("remove_tags doesn't support tags_to_ignore, use function args instead.")
8383
target_tags = target_tags or set()
8484

8585
def _map_func(op: 'cirq.Operation', _) -> 'cirq.OP_TREE':
@@ -91,8 +91,5 @@ def _map_func(op: 'cirq.Operation', _) -> 'cirq.OP_TREE':
9191
return op.untagged.with_tags(*remaing_tags)
9292

9393
return transformer_primitives.map_operations(
94-
circuit,
95-
_map_func,
96-
deep=context.deep if context else False,
97-
tags_to_ignore=context.tags_to_ignore if context else [],
94+
circuit, _map_func, deep=context.deep if context else False
9895
).unfreeze(copy=False)

cirq-core/cirq/transformers/tag_transformers_test.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import pytest
16+
1517
import cirq
1618

1719

@@ -38,6 +40,18 @@ def test_index_tags():
3840
)
3941

4042

43+
def test_index_tags_empty_target_tags():
44+
q0, q1 = cirq.LineQubit.range(2)
45+
input_circuit = cirq.Circuit(
46+
cirq.X(q0).with_tags("tag1", "tag2"),
47+
cirq.Y(q1).with_tags("tag1"),
48+
cirq.CZ(q0, q1).with_tags("tag2"),
49+
)
50+
check_same_circuit_with_same_tag_sets(
51+
cirq.index_tags(input_circuit, target_tags={}), input_circuit
52+
)
53+
54+
4155
def test_remove_tags():
4256
q0, q1 = cirq.LineQubit.range(2)
4357
input_circuit = cirq.Circuit(
@@ -67,21 +81,21 @@ def test_remove_tags_via_remove_if():
6781
)
6882

6983

84+
def test_index_tags_with_tags_to_ignore():
85+
with pytest.raises(
86+
ValueError, match="index_tags doesn't support tags_to_ignore, use function args instead."
87+
):
88+
cirq.index_tags(
89+
circuit=cirq.Circuit(),
90+
target_tags={"tag0"},
91+
context=cirq.TransformerContext(tags_to_ignore=["tag0"]),
92+
)
93+
94+
7095
def test_remove_tags_with_tags_to_ignore():
71-
q0, q1 = cirq.LineQubit.range(2)
72-
input_circuit = cirq.Circuit(
73-
cirq.X(q0).with_tags("tag1", "tag0"),
74-
cirq.Y(q1).with_tags("not_tag1"),
75-
cirq.CZ(q0, q1).with_tags("tag2"),
76-
)
77-
expected_circuit = cirq.Circuit(
78-
cirq.X(q0).with_tags("tag1", "tag0"), cirq.Y(q1).with_tags("not_tag1"), cirq.CZ(q0, q1)
79-
)
80-
check_same_circuit_with_same_tag_sets(
96+
with pytest.raises(
97+
ValueError, match="remove_tags doesn't support tags_to_ignore, use function args instead."
98+
):
8199
cirq.remove_tags(
82-
input_circuit,
83-
remove_if=lambda tag: tag.startswith("tag"),
84-
context=cirq.TransformerContext(tags_to_ignore=["tag0"]),
85-
),
86-
expected_circuit,
87-
)
100+
circuit=cirq.Circuit(), context=cirq.TransformerContext(tags_to_ignore=["tag0"])
101+
)

0 commit comments

Comments
 (0)