|
| 1 | +# Copyright 2025 The Cirq Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import cirq |
| 16 | + |
| 17 | + |
| 18 | +def check_same_circuit_with_same_tag_sets(circuit1, circuit2): |
| 19 | + for op1, op2 in zip(circuit1.all_operations(), circuit2.all_operations()): |
| 20 | + assert set(op1.tags) == set(op2.tags) |
| 21 | + assert op1.untagged == op2.untagged |
| 22 | + |
| 23 | + |
| 24 | +def test_index_tags(): |
| 25 | + q0, q1 = cirq.LineQubit.range(2) |
| 26 | + input_circuit = cirq.Circuit( |
| 27 | + cirq.X(q0).with_tags("tag1", "tag2"), |
| 28 | + cirq.Y(q1).with_tags("tag1"), |
| 29 | + cirq.CZ(q0, q1).with_tags("tag2"), |
| 30 | + ) |
| 31 | + expected_circuit = cirq.Circuit( |
| 32 | + cirq.X(q0).with_tags("tag1_0", "tag2_0"), |
| 33 | + cirq.Y(q1).with_tags("tag1_1"), |
| 34 | + cirq.CZ(q0, q1).with_tags("tag2_1"), |
| 35 | + ) |
| 36 | + check_same_circuit_with_same_tag_sets( |
| 37 | + cirq.index_tags(input_circuit, target_tags={"tag1", "tag2"}), expected_circuit |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def test_remove_tags(): |
| 42 | + q0, q1 = cirq.LineQubit.range(2) |
| 43 | + input_circuit = cirq.Circuit( |
| 44 | + cirq.X(q0).with_tags("tag1", "tag2"), |
| 45 | + cirq.Y(q1).with_tags("tag1"), |
| 46 | + cirq.CZ(q0, q1).with_tags("tag2"), |
| 47 | + ) |
| 48 | + expected_circuit = cirq.Circuit( |
| 49 | + cirq.X(q0).with_tags("tag2"), cirq.Y(q1), cirq.CZ(q0, q1).with_tags("tag2") |
| 50 | + ) |
| 51 | + check_same_circuit_with_same_tag_sets( |
| 52 | + cirq.remove_tags(input_circuit, target_tags={"tag1"}), expected_circuit |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +def test_remove_tags_via_remove_if(): |
| 57 | + q0, q1 = cirq.LineQubit.range(2) |
| 58 | + input_circuit = cirq.Circuit( |
| 59 | + cirq.X(q0).with_tags("tag1", "tag2"), |
| 60 | + cirq.Y(q1).with_tags("not_tag1"), |
| 61 | + cirq.CZ(q0, q1).with_tags("tag2"), |
| 62 | + ) |
| 63 | + expected_circuit = cirq.Circuit(cirq.X(q0), cirq.Y(q1).with_tags("not_tag1"), cirq.CZ(q0, q1)) |
| 64 | + check_same_circuit_with_same_tag_sets( |
| 65 | + cirq.remove_tags(input_circuit, remove_if=lambda tag: tag.startswith("tag")), |
| 66 | + expected_circuit, |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +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( |
| 81 | + 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 | + ) |
0 commit comments