|
| 1 | +# Copyright 2018 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 | +"""An optimization pass that combines adjacent series of gates on two qubits and |
| 16 | +outputs a circuit with SQRT_ISWAP or SQRT_ISWAP_INV gates.""" |
| 17 | + |
| 18 | +from typing import Callable, Sequence, TYPE_CHECKING |
| 19 | + |
| 20 | +import numpy as np |
| 21 | + |
| 22 | +from cirq import ops |
| 23 | +from cirq.optimizers import two_qubit_to_sqrt_iswap, merge_interactions |
| 24 | + |
| 25 | +if TYPE_CHECKING: |
| 26 | + import cirq |
| 27 | + |
| 28 | + |
| 29 | +class MergeInteractionsToSqrtIswap(merge_interactions.MergeInteractionsAbc): |
| 30 | + """Combines series of adjacent one and two-qubit gates operating on a pair |
| 31 | + of qubits and replaces each series with the minimum number of SQRT_ISWAP |
| 32 | + gates.""" |
| 33 | + |
| 34 | + def __init__( |
| 35 | + self, |
| 36 | + tolerance: float = 1e-8, |
| 37 | + require_three_sqrt_iswap: bool = False, |
| 38 | + use_sqrt_iswap_inv: bool = False, |
| 39 | + post_clean_up: Callable[[Sequence[ops.Operation]], ops.OP_TREE] = lambda op_list: op_list, |
| 40 | + ) -> None: |
| 41 | + super().__init__(tolerance=tolerance, post_clean_up=post_clean_up) |
| 42 | + self.require_three_sqrt_iswap = require_three_sqrt_iswap |
| 43 | + self.use_sqrt_iswap_inv = use_sqrt_iswap_inv |
| 44 | + |
| 45 | + def _may_keep_old_op(self, old_op: 'cirq.Operation') -> bool: |
| 46 | + """Returns True if the old two-qubit operation may be left unchanged |
| 47 | + without decomposition.""" |
| 48 | + return isinstance(old_op.gate, ops.ISwapPowGate) and old_op.gate.exponent == 0.5 |
| 49 | + |
| 50 | + def _two_qubit_matrix_to_operations( |
| 51 | + self, |
| 52 | + q0: 'cirq.Qid', |
| 53 | + q1: 'cirq.Qid', |
| 54 | + mat: np.ndarray, |
| 55 | + ) -> Sequence['cirq.Operation']: |
| 56 | + """Decomposes the merged two-qubit gate unitary into the minimum number |
| 57 | + of SQRT_ISWAP gates. |
| 58 | +
|
| 59 | + Args: |
| 60 | + q0: The first qubit being operated on. |
| 61 | + q1: The other qubit being operated on. |
| 62 | + mat: Defines the operation to apply to the pair of qubits. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + A list of operations implementing the matrix. |
| 66 | + """ |
| 67 | + return two_qubit_to_sqrt_iswap.two_qubit_matrix_to_sqrt_iswap_operations( |
| 68 | + q0, |
| 69 | + q1, |
| 70 | + mat, |
| 71 | + required_sqrt_iswap_count=3 if self.require_three_sqrt_iswap else None, |
| 72 | + use_sqrt_iswap_inv=self.use_sqrt_iswap_inv, |
| 73 | + atol=self.tolerance, |
| 74 | + check_preconditions=False, |
| 75 | + ) |
0 commit comments