|
| 1 | +# Copyright 2024 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 | +from typing import Sequence, Union |
| 16 | +import cirq |
| 17 | +from cirq import add_dynamical_decoupling |
| 18 | +import pytest |
| 19 | + |
| 20 | + |
| 21 | +def assert_dd( |
| 22 | + input_circuit: cirq.Circuit, |
| 23 | + expected_circuit: cirq.Circuit, |
| 24 | + schema: Union[str, Sequence['cirq.Gate']], |
| 25 | +): |
| 26 | + updated_circuit = add_dynamical_decoupling(input_circuit, schema=schema) |
| 27 | + cirq.testing.assert_same_circuits(updated_circuit, expected_circuit) |
| 28 | + |
| 29 | + |
| 30 | +def test_no_insert_due_to_no_consecutive_moments(): |
| 31 | + a = cirq.NamedQubit('a') |
| 32 | + b = cirq.NamedQubit('b') |
| 33 | + |
| 34 | + # No insertion as there is no room for a dd sequence. |
| 35 | + assert_dd( |
| 36 | + input_circuit=cirq.Circuit( |
| 37 | + cirq.Moment(cirq.H(a)), cirq.Moment(cirq.CNOT(a, b)), cirq.Moment(cirq.H(b)) |
| 38 | + ), |
| 39 | + expected_circuit=cirq.Circuit( |
| 40 | + cirq.Moment(cirq.H(a)), cirq.Moment(cirq.CNOT(a, b)), cirq.Moment(cirq.H(b)) |
| 41 | + ), |
| 42 | + schema='XX_PAIR', |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize( |
| 47 | + 'schema,inserted_gates', |
| 48 | + [ |
| 49 | + ('XX_PAIR', (cirq.X, cirq.X)), |
| 50 | + ('X_XINV', (cirq.X, cirq.X**-1)), |
| 51 | + ('YY_PAIR', (cirq.Y, cirq.Y)), |
| 52 | + ('Y_YINV', (cirq.Y, cirq.Y**-1)), |
| 53 | + ], |
| 54 | +) |
| 55 | +def test_insert_provided_schema(schema: str, inserted_gates: Sequence['cirq.Gate']): |
| 56 | + a = cirq.NamedQubit('a') |
| 57 | + b = cirq.NamedQubit('b') |
| 58 | + c = cirq.NamedQubit('c') |
| 59 | + |
| 60 | + input_circuit = cirq.Circuit( |
| 61 | + cirq.Moment(cirq.H(a)), |
| 62 | + cirq.Moment(cirq.CNOT(a, b)), |
| 63 | + cirq.Moment(cirq.CNOT(b, c)), |
| 64 | + cirq.Moment(cirq.CNOT(b, c)), |
| 65 | + cirq.Moment(cirq.measure_each(a, b, c)), |
| 66 | + ) |
| 67 | + expected_circuit = cirq.Circuit( |
| 68 | + cirq.Moment(cirq.H(a)), |
| 69 | + cirq.Moment(cirq.CNOT(a, b)), |
| 70 | + cirq.Moment(cirq.CNOT(b, c), inserted_gates[0](a)), |
| 71 | + cirq.Moment(cirq.CNOT(b, c), inserted_gates[1](a)), |
| 72 | + cirq.Moment(cirq.measure_each(a, b, c)), |
| 73 | + ) |
| 74 | + |
| 75 | + # Insert one dynamical decoupling sequence in idle moments. |
| 76 | + assert_dd(input_circuit, expected_circuit, schema=schema) |
| 77 | + |
| 78 | + |
| 79 | +def test_insert_by_customized_dd_sequence(): |
| 80 | + a = cirq.NamedQubit('a') |
| 81 | + b = cirq.NamedQubit('b') |
| 82 | + c = cirq.NamedQubit('c') |
| 83 | + |
| 84 | + assert_dd( |
| 85 | + input_circuit=cirq.Circuit( |
| 86 | + cirq.Moment(cirq.H(a)), |
| 87 | + cirq.Moment(cirq.CNOT(a, b)), |
| 88 | + cirq.Moment(cirq.CNOT(b, c)), |
| 89 | + cirq.Moment(cirq.CNOT(b, c)), |
| 90 | + cirq.Moment(cirq.CNOT(b, c)), |
| 91 | + cirq.Moment(cirq.CNOT(b, c)), |
| 92 | + cirq.Moment(cirq.measure_each(a, b, c)), |
| 93 | + ), |
| 94 | + expected_circuit=cirq.Circuit( |
| 95 | + cirq.Moment(cirq.H(a)), |
| 96 | + cirq.Moment(cirq.CNOT(a, b)), |
| 97 | + cirq.Moment(cirq.CNOT(b, c), cirq.X(a)), |
| 98 | + cirq.Moment(cirq.CNOT(b, c), cirq.X(a)), |
| 99 | + cirq.Moment(cirq.CNOT(b, c), cirq.Y(a)), |
| 100 | + cirq.Moment(cirq.CNOT(b, c), cirq.Y(a)), |
| 101 | + cirq.Moment(cirq.measure_each(a, b, c)), |
| 102 | + ), |
| 103 | + schema=[cirq.X, cirq.X, cirq.Y, cirq.Y], |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.parametrize( |
| 108 | + 'schema,error_msg_regex', |
| 109 | + [ |
| 110 | + ('INVALID_SCHEMA', 'Invalid schema name.'), |
| 111 | + ([cirq.X], 'Invalid dynamical decoupling sequence. Expect more than one gates.'), |
| 112 | + ( |
| 113 | + [cirq.X, cirq.H], |
| 114 | + 'Invalid dynamical decoupling sequence. Expect sequence production equals identity' |
| 115 | + ' up to a global phase, got', |
| 116 | + ), |
| 117 | + ], |
| 118 | +) |
| 119 | +def test_invalid_dd_schema(schema: Union[str, Sequence['cirq.Gate']], error_msg_regex): |
| 120 | + a = cirq.NamedQubit('a') |
| 121 | + input_circuit = cirq.Circuit(cirq.H(a)) |
| 122 | + with pytest.raises(ValueError, match=error_msg_regex): |
| 123 | + add_dynamical_decoupling(input_circuit, schema=schema) |
0 commit comments