|
| 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 cirq |
| 16 | + |
| 17 | +NO_COMPILE_TAG = "no_compile_tag" |
| 18 | + |
| 19 | + |
| 20 | +def assert_optimizes(before, after, measure_only_moment=True, with_context=False): |
| 21 | + transformed_circuit = ( |
| 22 | + cirq.synchronize_terminal_measurements(before, after_other_operations=measure_only_moment) |
| 23 | + if not with_context |
| 24 | + else cirq.synchronize_terminal_measurements( |
| 25 | + before, |
| 26 | + context=cirq.TransformerContext(ignore_tags=(NO_COMPILE_TAG,)), |
| 27 | + after_other_operations=measure_only_moment, |
| 28 | + ) |
| 29 | + ) |
| 30 | + cirq.testing.assert_same_circuits(transformed_circuit, after) |
| 31 | + |
| 32 | + |
| 33 | +def test_no_move(): |
| 34 | + q1 = cirq.NamedQubit('q1') |
| 35 | + before = cirq.Circuit([cirq.Moment([cirq.H(q1)])]) |
| 36 | + after = before |
| 37 | + assert_optimizes(before=before, after=after) |
| 38 | + |
| 39 | + |
| 40 | +def test_simple_align(): |
| 41 | + q1 = cirq.NamedQubit('q1') |
| 42 | + q2 = cirq.NamedQubit('q2') |
| 43 | + before = cirq.Circuit( |
| 44 | + [ |
| 45 | + cirq.Moment([cirq.H(q1), cirq.H(q2)]), |
| 46 | + cirq.Moment([cirq.measure(q1).with_tags(NO_COMPILE_TAG), cirq.Z(q2)]), |
| 47 | + cirq.Moment([cirq.measure(q2)]), |
| 48 | + ] |
| 49 | + ) |
| 50 | + after = cirq.Circuit( |
| 51 | + [ |
| 52 | + cirq.Moment([cirq.H(q1), cirq.H(q2)]), |
| 53 | + cirq.Moment([cirq.Z(q2)]), |
| 54 | + cirq.Moment([cirq.measure(q1).with_tags(NO_COMPILE_TAG), cirq.measure(q2)]), |
| 55 | + ] |
| 56 | + ) |
| 57 | + assert_optimizes(before=before, after=after) |
| 58 | + assert_optimizes(before=before, after=before, with_context=True) |
| 59 | + |
| 60 | + |
| 61 | +def test_simple_partial_align(): |
| 62 | + q1 = cirq.NamedQubit('q1') |
| 63 | + q2 = cirq.NamedQubit('q2') |
| 64 | + before = cirq.Circuit( |
| 65 | + [ |
| 66 | + cirq.Moment([cirq.measure(q1), cirq.Z(q2)]), |
| 67 | + cirq.Moment([cirq.Z(q1), cirq.measure(q2).with_tags(NO_COMPILE_TAG)]), |
| 68 | + ] |
| 69 | + ) |
| 70 | + after = cirq.Circuit( |
| 71 | + [ |
| 72 | + cirq.Moment([cirq.measure(q1), cirq.Z(q2)]), |
| 73 | + cirq.Moment([cirq.Z(q1)]), |
| 74 | + cirq.Moment([cirq.measure(q2).with_tags(NO_COMPILE_TAG)]), |
| 75 | + ] |
| 76 | + ) |
| 77 | + assert_optimizes(before=before, after=after) |
| 78 | + assert_optimizes(before=before, after=before, with_context=True) |
| 79 | + |
| 80 | + |
| 81 | +def test_slide_forward_one(): |
| 82 | + q1 = cirq.NamedQubit('q1') |
| 83 | + q2 = cirq.NamedQubit('q2') |
| 84 | + q3 = cirq.NamedQubit('q3') |
| 85 | + before = cirq.Circuit( |
| 86 | + [ |
| 87 | + cirq.Moment([cirq.H(q1), cirq.measure(q2).with_tags(NO_COMPILE_TAG), cirq.measure(q3)]), |
| 88 | + ] |
| 89 | + ) |
| 90 | + after = cirq.Circuit( |
| 91 | + [ |
| 92 | + cirq.Moment([cirq.H(q1)]), |
| 93 | + cirq.Moment([cirq.measure(q2).with_tags(NO_COMPILE_TAG), cirq.measure(q3)]), |
| 94 | + ] |
| 95 | + ) |
| 96 | + after_no_compile = cirq.Circuit( |
| 97 | + [ |
| 98 | + cirq.Moment([cirq.H(q1), cirq.measure(q2).with_tags(NO_COMPILE_TAG)]), |
| 99 | + cirq.Moment([cirq.measure(q3)]), |
| 100 | + ] |
| 101 | + ) |
| 102 | + assert_optimizes(before=before, after=after) |
| 103 | + assert_optimizes(before=before, after=after_no_compile, with_context=True) |
| 104 | + |
| 105 | + |
| 106 | +def test_no_slide_forward_one(): |
| 107 | + q1 = cirq.NamedQubit('q1') |
| 108 | + q2 = cirq.NamedQubit('q2') |
| 109 | + q3 = cirq.NamedQubit('q3') |
| 110 | + before = cirq.Circuit( |
| 111 | + [ |
| 112 | + cirq.Moment([cirq.H(q1), cirq.measure(q2), cirq.measure(q3)]), |
| 113 | + ] |
| 114 | + ) |
| 115 | + after = cirq.Circuit( |
| 116 | + [ |
| 117 | + cirq.Moment([cirq.H(q1), cirq.measure(q2), cirq.measure(q3)]), |
| 118 | + ] |
| 119 | + ) |
| 120 | + assert_optimizes(before=before, after=after, measure_only_moment=False) |
| 121 | + |
| 122 | + |
| 123 | +def test_blocked_shift_one(): |
| 124 | + q1 = cirq.NamedQubit('q1') |
| 125 | + q2 = cirq.NamedQubit('q2') |
| 126 | + before = cirq.Circuit( |
| 127 | + [ |
| 128 | + cirq.Moment([cirq.H(q1), cirq.H(q2)]), |
| 129 | + cirq.Moment([cirq.measure(q1), cirq.Z(q2)]), |
| 130 | + cirq.Moment([cirq.H(q1), cirq.measure(q2).with_tags(NO_COMPILE_TAG)]), |
| 131 | + ] |
| 132 | + ) |
| 133 | + after = cirq.Circuit( |
| 134 | + [ |
| 135 | + cirq.Moment([cirq.H(q1), cirq.H(q2)]), |
| 136 | + cirq.Moment([cirq.measure(q1), cirq.Z(q2)]), |
| 137 | + cirq.Moment([cirq.H(q1)]), |
| 138 | + cirq.Moment([cirq.measure(q2).with_tags(NO_COMPILE_TAG)]), |
| 139 | + ] |
| 140 | + ) |
| 141 | + assert_optimizes(before=before, after=after) |
| 142 | + assert_optimizes(before=before, after=before, with_context=True) |
| 143 | + |
| 144 | + |
| 145 | +def test_complex_move(): |
| 146 | + q1 = cirq.NamedQubit('q1') |
| 147 | + q2 = cirq.NamedQubit('q2') |
| 148 | + q3 = cirq.NamedQubit('q3') |
| 149 | + before = cirq.Circuit( |
| 150 | + [ |
| 151 | + cirq.Moment([cirq.H(q1), cirq.H(q2)]), |
| 152 | + cirq.Moment([cirq.measure(q1), cirq.Z(q2)]), |
| 153 | + cirq.Moment([cirq.H(q1), cirq.measure(q2).with_tags(NO_COMPILE_TAG)]), |
| 154 | + cirq.Moment([cirq.H(q3)]), |
| 155 | + cirq.Moment([cirq.X(q1), cirq.measure(q3).with_tags(NO_COMPILE_TAG)]), |
| 156 | + ] |
| 157 | + ) |
| 158 | + after = cirq.Circuit( |
| 159 | + [ |
| 160 | + cirq.Moment([cirq.H(q1), cirq.H(q2)]), |
| 161 | + cirq.Moment([cirq.measure(q1), cirq.Z(q2)]), |
| 162 | + cirq.Moment([cirq.H(q1)]), |
| 163 | + cirq.Moment([cirq.H(q3)]), |
| 164 | + cirq.Moment([cirq.X(q1)]), |
| 165 | + cirq.Moment( |
| 166 | + [ |
| 167 | + cirq.measure(q2).with_tags(NO_COMPILE_TAG), |
| 168 | + cirq.measure(q3).with_tags(NO_COMPILE_TAG), |
| 169 | + ] |
| 170 | + ), |
| 171 | + ] |
| 172 | + ) |
| 173 | + assert_optimizes(before=before, after=after) |
| 174 | + assert_optimizes(before=before, after=before, with_context=True) |
| 175 | + |
| 176 | + |
| 177 | +def test_complex_move_no_slide(): |
| 178 | + q1 = cirq.NamedQubit('q1') |
| 179 | + q2 = cirq.NamedQubit('q2') |
| 180 | + q3 = cirq.NamedQubit('q3') |
| 181 | + before = cirq.Circuit( |
| 182 | + [ |
| 183 | + cirq.Moment([cirq.H(q1), cirq.H(q2)]), |
| 184 | + cirq.Moment([cirq.measure(q1), cirq.Z(q2)]), |
| 185 | + cirq.Moment([cirq.H(q1), cirq.measure(q2).with_tags(NO_COMPILE_TAG)]), |
| 186 | + cirq.Moment([cirq.H(q3)]), |
| 187 | + cirq.Moment([cirq.X(q1), cirq.measure(q3)]), |
| 188 | + ] |
| 189 | + ) |
| 190 | + after = cirq.Circuit( |
| 191 | + [ |
| 192 | + cirq.Moment(cirq.H(q1), cirq.H(q2)), |
| 193 | + cirq.Moment(cirq.measure(q1), cirq.Z(q2)), |
| 194 | + cirq.Moment(cirq.H(q1)), |
| 195 | + cirq.Moment(cirq.H(q3)), |
| 196 | + cirq.Moment(cirq.X(q1), cirq.measure(q2).with_tags(NO_COMPILE_TAG), cirq.measure(q3)), |
| 197 | + ] |
| 198 | + ) |
| 199 | + assert_optimizes(before=before, after=after, measure_only_moment=False) |
| 200 | + assert_optimizes(before=before, after=before, measure_only_moment=False, with_context=True) |
| 201 | + |
| 202 | + |
| 203 | +def test_multi_qubit(): |
| 204 | + q0, q1 = cirq.LineQubit.range(2) |
| 205 | + circuit = cirq.Circuit(cirq.measure(q0, q1, key='m'), cirq.H(q1)) |
| 206 | + assert_optimizes(before=circuit, after=circuit) |
| 207 | + |
| 208 | + |
| 209 | +def test_classically_controlled_op(): |
| 210 | + q0, q1 = cirq.LineQubit.range(2) |
| 211 | + circuit = cirq.Circuit( |
| 212 | + cirq.H(q0), cirq.measure(q0, key='m'), cirq.X(q1).with_classical_controls('m') |
| 213 | + ) |
| 214 | + assert_optimizes(before=circuit, after=circuit) |
0 commit comments