|
| 1 | +# Copyright 2022 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 pytest |
| 16 | +import cirq |
| 17 | + |
| 18 | + |
| 19 | +def test_routed_circuit_with_mapping_simple(): |
| 20 | + q = cirq.LineQubit.range(2) |
| 21 | + circuit = cirq.Circuit([cirq.Moment(cirq.SWAP(q[0], q[1]).with_tags(cirq.RoutingSwapTag()))]) |
| 22 | + expected_diagram = """ |
| 23 | +0: ───q(0)───×[cirq.RoutingSwapTag()]───q(1)─── |
| 24 | + │ │ │ |
| 25 | +1: ───q(1)───×──────────────────────────q(0)───""" |
| 26 | + cirq.testing.assert_has_diagram(cirq.routed_circuit_with_mapping(circuit), expected_diagram) |
| 27 | + |
| 28 | + expected_diagram_with_initial_mapping = """ |
| 29 | +0: ───a───×[cirq.RoutingSwapTag()]───b─── |
| 30 | + │ │ │ |
| 31 | +1: ───b───×──────────────────────────a───""" |
| 32 | + cirq.testing.assert_has_diagram( |
| 33 | + cirq.routed_circuit_with_mapping( |
| 34 | + circuit, {cirq.NamedQubit("a"): q[0], cirq.NamedQubit("b"): q[1]} |
| 35 | + ), |
| 36 | + expected_diagram_with_initial_mapping, |
| 37 | + ) |
| 38 | + |
| 39 | + # if swap is untagged should not affect the mapping |
| 40 | + circuit = cirq.Circuit([cirq.Moment(cirq.SWAP(q[0], q[1]))]) |
| 41 | + expected_diagram = """ |
| 42 | +0: ───q(0)───×─── |
| 43 | + │ │ |
| 44 | +1: ───q(1)───×───""" |
| 45 | + cirq.testing.assert_has_diagram(cirq.routed_circuit_with_mapping(circuit), expected_diagram) |
| 46 | + |
| 47 | + circuit = cirq.Circuit( |
| 48 | + [ |
| 49 | + cirq.Moment(cirq.X(q[0]).with_tags(cirq.RoutingSwapTag())), |
| 50 | + cirq.Moment(cirq.SWAP(q[0], q[1])), |
| 51 | + ] |
| 52 | + ) |
| 53 | + with pytest.raises( |
| 54 | + ValueError, match="Invalid circuit. A non-SWAP gate cannot be tagged a RoutingSwapTag." |
| 55 | + ): |
| 56 | + cirq.routed_circuit_with_mapping(circuit) |
| 57 | + |
| 58 | + |
| 59 | +def test_routed_circuit_with_mapping_multi_swaps(): |
| 60 | + q = cirq.LineQubit.range(6) |
| 61 | + circuit = cirq.Circuit( |
| 62 | + [ |
| 63 | + cirq.Moment(cirq.CNOT(q[3], q[4])), |
| 64 | + cirq.Moment(cirq.CNOT(q[5], q[4]), cirq.CNOT(q[2], q[3])), |
| 65 | + cirq.Moment( |
| 66 | + cirq.CNOT(q[2], q[1]), cirq.SWAP(q[4], q[3]).with_tags(cirq.RoutingSwapTag()) |
| 67 | + ), |
| 68 | + cirq.Moment( |
| 69 | + cirq.SWAP(q[0], q[1]).with_tags(cirq.RoutingSwapTag()), |
| 70 | + cirq.SWAP(q[3], q[2]).with_tags(cirq.RoutingSwapTag()), |
| 71 | + ), |
| 72 | + cirq.Moment(cirq.CNOT(q[2], q[1])), |
| 73 | + cirq.Moment(cirq.CNOT(q[1], q[0])), |
| 74 | + ] |
| 75 | + ) |
| 76 | + expected_diagram = """ |
| 77 | +0: ───q(0)──────────────────────────────────────q(0)───×[cirq.RoutingSwapTag()]───q(1)───────X─── |
| 78 | + │ │ │ │ │ |
| 79 | +1: ───q(1)───────────X──────────────────────────q(1)───×──────────────────────────q(0)───X───@─── |
| 80 | + │ │ │ │ │ |
| 81 | +2: ───q(2)───────@───@──────────────────────────q(2)───×──────────────────────────q(4)───@─────── |
| 82 | + │ │ │ │ │ |
| 83 | +3: ───q(3)───@───X───×──────────────────────────q(4)───×[cirq.RoutingSwapTag()]───q(2)─────────── |
| 84 | + │ │ │ │ │ |
| 85 | +4: ───q(4)───X───X───×[cirq.RoutingSwapTag()]───q(3)──────────────────────────────q(3)─────────── |
| 86 | + │ │ │ │ |
| 87 | +5: ───q(5)───────@──────────────────────────────q(5)──────────────────────────────q(5)─────────── |
| 88 | +""" |
| 89 | + cirq.testing.assert_has_diagram(cirq.routed_circuit_with_mapping(circuit), expected_diagram) |
0 commit comments