Skip to content

Commit 5ee7dd4

Browse files
committed
Split new code into a separate file
1 parent c0a6a9f commit 5ee7dd4

File tree

3 files changed

+79
-50
lines changed

3 files changed

+79
-50
lines changed

cirq-core/cirq/optimizers/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858

5959
from cirq.optimizers.merge_interactions import (
6060
MergeInteractions,
61+
)
62+
63+
from cirq.optimizers.merge_interactions_to_sqrt_iswap import (
6164
MergeInteractionsToSqrtIswap,
6265
)
6366

cirq-core/cirq/optimizers/merge_interactions.py

+1-50
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import numpy as np
2121

2222
from cirq import circuits, ops, protocols
23-
from cirq.optimizers import two_qubit_decompositions, two_qubit_to_sqrt_iswap
23+
from cirq.optimizers import two_qubit_decompositions
2424

2525
if TYPE_CHECKING:
2626
import cirq
@@ -237,52 +237,3 @@ def _two_qubit_matrix_to_operations(
237237
return two_qubit_decompositions.two_qubit_matrix_to_operations(
238238
q0, q1, mat, self.allow_partial_czs, self.tolerance, False
239239
)
240-
241-
242-
class MergeInteractionsToSqrtIswap(MergeInteractionsAbc):
243-
"""Combines series of adjacent one and two-qubit gates operating on a pair
244-
of qubits and replaces each series with the minimum number of SQRT_ISWAP
245-
gates."""
246-
247-
def __init__(
248-
self,
249-
tolerance: float = 1e-8,
250-
require_three_sqrt_iswap: bool = False,
251-
use_sqrt_iswap_inv: bool = False,
252-
post_clean_up: Callable[[Sequence[ops.Operation]], ops.OP_TREE] = lambda op_list: op_list,
253-
) -> None:
254-
super().__init__(tolerance=tolerance, post_clean_up=post_clean_up)
255-
self.require_three_sqrt_iswap = require_three_sqrt_iswap
256-
self.use_sqrt_iswap_inv = use_sqrt_iswap_inv
257-
258-
def _may_keep_old_op(self, old_op: 'cirq.Operation') -> bool:
259-
"""Returns True if the old two-qubit operation may be left unchanged
260-
without decomposition."""
261-
return isinstance(old_op.gate, ops.ISwapPowGate) and old_op.gate.exponent == 0.5
262-
263-
def _two_qubit_matrix_to_operations(
264-
self,
265-
q0: 'cirq.Qid',
266-
q1: 'cirq.Qid',
267-
mat: np.ndarray,
268-
) -> Sequence['cirq.Operation']:
269-
"""Decomposes the merged two-qubit gate unitary into the minimum number
270-
of SQRT_ISWAP gates.
271-
272-
Args:
273-
q0: The first qubit being operated on.
274-
q1: The other qubit being operated on.
275-
mat: Defines the operation to apply to the pair of qubits.
276-
277-
Returns:
278-
A list of operations implementing the matrix.
279-
"""
280-
return two_qubit_to_sqrt_iswap.two_qubit_matrix_to_sqrt_iswap_operations(
281-
q0,
282-
q1,
283-
mat,
284-
required_sqrt_iswap_count=3 if self.require_three_sqrt_iswap else None,
285-
use_sqrt_iswap_inv=self.use_sqrt_iswap_inv,
286-
atol=self.tolerance,
287-
check_preconditions=False,
288-
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)