Skip to content

Commit d8a93b4

Browse files
authored
Move two/three qubit decompositions from optimizers/ to transformers/analytical_decompositions (quantumlib#4809)
Moves the following files from `cirq/optimizers/` to `cirq/transformers/analytical_decompositions/` using `deprecated_submodule`: - two_qubit_decompositions.py --> two_qubit_to_cz.py - two_qubit_to_fsim.py - two_qubit_to_sqrt_iswap.py - three_qubit_decomposition.py Part of quantumlib#4722
1 parent 9b9af09 commit d8a93b4

16 files changed

+175
-39
lines changed

cirq/__init__.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@
329329
AlignLeft,
330330
AlignRight,
331331
ConvertToCzAndSingleGates,
332-
decompose_two_qubit_interaction_into_four_fsim_gates,
333332
DropEmptyMoments,
334333
DropNegligible,
335334
EjectPhasedPaulis,
@@ -342,10 +341,6 @@
342341
MergeSingleQubitGates,
343342
stratified_circuit,
344343
SynchronizeTerminalMeasurements,
345-
two_qubit_matrix_to_operations,
346-
two_qubit_matrix_to_diagonal_and_operations,
347-
two_qubit_matrix_to_sqrt_iswap_operations,
348-
three_qubit_matrix_to_operations,
349344
)
350345

351346
from cirq.transformers import (
@@ -354,6 +349,7 @@
354349
decompose_cphase_into_two_fsim,
355350
decompose_multi_controlled_x,
356351
decompose_multi_controlled_rotation,
352+
decompose_two_qubit_interaction_into_four_fsim_gates,
357353
is_negligible_turn,
358354
map_moments,
359355
map_operations,
@@ -367,6 +363,10 @@
367363
single_qubit_matrix_to_phased_x_z,
368364
single_qubit_matrix_to_phxz,
369365
single_qubit_op_to_framed_phase_form,
366+
three_qubit_matrix_to_operations,
367+
two_qubit_matrix_to_diagonal_and_operations,
368+
two_qubit_matrix_to_operations,
369+
two_qubit_matrix_to_sqrt_iswap_operations,
370370
unroll_circuit_op,
371371
unroll_circuit_op_greedy_earliest,
372372
unroll_circuit_op_greedy_frontier,

cirq/neutral_atoms/convert_to_neutral_atom_gates.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
PointOptimizer,
2020
)
2121
from cirq.neutral_atoms import neutral_atom_devices
22-
from cirq import optimizers, transformers
22+
from cirq import transformers
2323

2424
if TYPE_CHECKING:
2525
import cirq
@@ -60,7 +60,7 @@ def _convert_one(self, op: ops.Operation) -> ops.OP_TREE:
6060
gates = transformers.single_qubit_matrix_to_phased_x_z(mat)
6161
return [g.on(op.qubits[0]) for g in gates]
6262
if mat is not None and len(op.qubits) == 2:
63-
return optimizers.two_qubit_matrix_to_operations(
63+
return transformers.two_qubit_matrix_to_operations(
6464
op.qubits[0], op.qubits[1], mat, allow_partial_czs=False, clean_operations=True
6565
)
6666

cirq/optimizers/__init__.py

+35-18
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,11 @@
6363
from cirq.optimizers.stratify import (
6464
stratified_circuit,
6565
)
66+
6667
from cirq.optimizers.synchronize_terminal_measurements import (
6768
SynchronizeTerminalMeasurements,
6869
)
6970

70-
from cirq.optimizers.three_qubit_decomposition import (
71-
three_qubit_matrix_to_operations,
72-
)
73-
74-
from cirq.optimizers.two_qubit_decompositions import (
75-
two_qubit_matrix_to_operations,
76-
two_qubit_matrix_to_diagonal_and_operations,
77-
)
78-
79-
80-
from cirq.optimizers.two_qubit_to_sqrt_iswap import (
81-
two_qubit_matrix_to_sqrt_iswap_operations,
82-
)
83-
84-
from cirq.optimizers.two_qubit_to_fsim import (
85-
decompose_two_qubit_interaction_into_four_fsim_gates,
86-
)
87-
8871
from cirq import _compat
8972

9073
_compat.deprecated_submodule(
@@ -118,3 +101,37 @@
118101
deadline="v0.16",
119102
create_attribute=True,
120103
)
104+
105+
_compat.deprecated_submodule(
106+
new_module_name="cirq.transformers.analytical_decompositions.three_qubit_decomposition",
107+
old_parent="cirq.optimizers",
108+
old_child="three_qubit_decomposition",
109+
deadline="v0.16",
110+
create_attribute=True,
111+
)
112+
113+
_compat.deprecated_submodule(
114+
new_module_name="cirq.transformers.analytical_decompositions.two_qubit_to_cz",
115+
old_parent="cirq.optimizers",
116+
old_child="two_qubit_decompositions",
117+
deadline="v0.16",
118+
create_attribute=True,
119+
)
120+
121+
122+
_compat.deprecated_submodule(
123+
new_module_name="cirq.transformers.analytical_decompositions.two_qubit_to_fsim",
124+
old_parent="cirq.optimizers",
125+
old_child="two_qubit_to_fsim",
126+
deadline="v0.16",
127+
create_attribute=True,
128+
)
129+
130+
131+
_compat.deprecated_submodule(
132+
new_module_name="cirq.transformers.analytical_decompositions.two_qubit_to_sqrt_iswap",
133+
old_parent="cirq.optimizers",
134+
old_child="two_qubit_to_sqrt_iswap",
135+
deadline="v0.16",
136+
create_attribute=True,
137+
)

cirq/optimizers/convert_to_cz_and_single_gates.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from typing import Optional
1616

1717
from cirq import circuits, ops, protocols
18-
from cirq.optimizers import two_qubit_decompositions
18+
from cirq.transformers.analytical_decompositions import two_qubit_to_cz
1919

2020

2121
class ConvertToCzAndSingleGates(circuits.PointOptimizer):
@@ -55,7 +55,7 @@ def _decompose_two_qubit_unitaries(self, op: ops.Operation) -> ops.OP_TREE:
5555
if len(op.qubits) == 2:
5656
mat = protocols.unitary(op, None)
5757
if mat is not None:
58-
return two_qubit_decompositions.two_qubit_matrix_to_operations(
58+
return two_qubit_to_cz.two_qubit_matrix_to_operations(
5959
op.qubits[0], op.qubits[1], mat, allow_partial_czs=self.allow_partial_czs
6060
)
6161
return NotImplemented

cirq/optimizers/merge_interactions.py

+2-2
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
23+
from cirq.transformers.analytical_decompositions import two_qubit_to_cz
2424

2525
if TYPE_CHECKING:
2626
import cirq
@@ -257,6 +257,6 @@ def _two_qubit_matrix_to_operations(
257257
Returns:
258258
A list of operations implementing the matrix.
259259
"""
260-
return two_qubit_decompositions.two_qubit_matrix_to_operations(
260+
return two_qubit_to_cz.two_qubit_matrix_to_operations(
261261
q0, q1, mat, self.allow_partial_czs, self.tolerance, False
262262
)

cirq/optimizers/merge_interactions_to_sqrt_iswap.py

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

2222
from cirq import ops
23-
from cirq.optimizers import two_qubit_to_sqrt_iswap, merge_interactions
23+
from cirq.optimizers import merge_interactions
24+
from cirq.transformers.analytical_decompositions import two_qubit_to_sqrt_iswap
2425

2526
if TYPE_CHECKING:
2627
import cirq

cirq/transformers/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
decompose_clifford_tableau_to_operations,
2121
decompose_multi_controlled_x,
2222
decompose_multi_controlled_rotation,
23+
decompose_two_qubit_interaction_into_four_fsim_gates,
2324
is_negligible_turn,
2425
prepare_two_qubit_state_using_cz,
2526
prepare_two_qubit_state_using_sqrt_iswap,
@@ -28,6 +29,10 @@
2829
single_qubit_matrix_to_phased_x_z,
2930
single_qubit_matrix_to_phxz,
3031
single_qubit_op_to_framed_phase_form,
32+
three_qubit_matrix_to_operations,
33+
two_qubit_matrix_to_diagonal_and_operations,
34+
two_qubit_matrix_to_operations,
35+
two_qubit_matrix_to_sqrt_iswap_operations,
3136
)
3237

3338
from cirq.transformers.transformer_primitives import (

cirq/transformers/analytical_decompositions/__init__.py

+17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@
3737
single_qubit_op_to_framed_phase_form,
3838
)
3939

40+
from cirq.transformers.analytical_decompositions.three_qubit_decomposition import (
41+
three_qubit_matrix_to_operations,
42+
)
43+
44+
from cirq.transformers.analytical_decompositions.two_qubit_to_cz import (
45+
two_qubit_matrix_to_operations,
46+
two_qubit_matrix_to_diagonal_and_operations,
47+
)
48+
49+
from cirq.transformers.analytical_decompositions.two_qubit_to_fsim import (
50+
decompose_two_qubit_interaction_into_four_fsim_gates,
51+
)
52+
53+
from cirq.transformers.analytical_decompositions.two_qubit_to_sqrt_iswap import (
54+
two_qubit_matrix_to_sqrt_iswap_operations,
55+
)
56+
4057
from cirq.transformers.analytical_decompositions.two_qubit_state_preparation import (
4158
prepare_two_qubit_state_using_cz,
4259
prepare_two_qubit_state_using_sqrt_iswap,

cirq/optimizers/three_qubit_decomposition.py renamed to cirq/transformers/analytical_decompositions/three_qubit_decomposition.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
"""Utility methods for decomposing three-qubit unitaries."""
16+
1517
from typing import Union, Tuple, Sequence, List, Optional
1618

1719
import numpy as np
1820

1921
import cirq
2022
from cirq import ops
21-
from cirq import optimizers as opt
23+
from cirq import transformers as opt
2224

2325

2426
def three_qubit_matrix_to_operations(

cirq/optimizers/three_qubit_decomposition_test.py renamed to cirq/transformers/analytical_decompositions/three_qubit_decomposition_test.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,23 @@
2121
from scipy.linalg import block_diag
2222

2323
import cirq
24-
from cirq.optimizers.three_qubit_decomposition import (
24+
from cirq.transformers.analytical_decompositions.three_qubit_decomposition import (
2525
_multiplexed_angles,
2626
_cs_to_ops,
2727
_middle_multiplexor_to_ops,
2828
_two_qubit_multiplexor_to_ops,
2929
)
3030

31+
ALLOW_DEPRECATION_IN_TEST = 'ALLOW_DEPRECATION_IN_TEST'
32+
33+
34+
def test_deprecated_submodule():
35+
with cirq.testing.assert_deprecated(
36+
"Use cirq.transformers.analytical_decompositions.three_qubit_decomposition instead",
37+
deadline="v0.16",
38+
):
39+
_ = cirq.optimizers.three_qubit_decomposition.three_qubit_matrix_to_operations
40+
3141

3242
def _skip_if_scipy(*, version_is_greater_than_1_5_0: bool) -> Callable[[Callable], Callable]:
3343
def decorator(func):

cirq/optimizers/two_qubit_decompositions.py renamed to cirq/transformers/analytical_decompositions/two_qubit_to_cz.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Utility methods related to optimizing quantum circuits."""
15+
"""Utility methods for decomposing two-qubit unitaries into CZ gates."""
1616

1717
from typing import Iterable, List, Sequence, Tuple, Optional, cast, TYPE_CHECKING
1818

cirq/optimizers/two_qubit_decompositions_test.py renamed to cirq/transformers/analytical_decompositions/two_qubit_to_cz_test.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@
2020

2121
import cirq
2222
from cirq import value
23-
from cirq.optimizers.two_qubit_decompositions import (
23+
from cirq.transformers.analytical_decompositions.two_qubit_to_cz import (
2424
_parity_interaction,
2525
_is_trivial_angle,
2626
two_qubit_matrix_to_diagonal_and_operations,
2727
)
2828
from cirq.testing import random_two_qubit_circuit_with_czs
2929

30+
ALLOW_DEPRECATION_IN_TEST = 'ALLOW_DEPRECATION_IN_TEST'
31+
32+
33+
def test_deprecated_submodule():
34+
with cirq.testing.assert_deprecated(
35+
"Use cirq.transformers.analytical_decompositions.two_qubit_to_cz instead", deadline="v0.16"
36+
):
37+
_ = cirq.optimizers.two_qubit_decompositions.two_qubit_matrix_to_operations
38+
3039

3140
@pytest.mark.parametrize(
3241
'rad,expected',

cirq/optimizers/two_qubit_to_fsim.py renamed to cirq/transformers/analytical_decompositions/two_qubit_to_fsim.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
# pylint: disable=wrong-or-nonexistent-copyright-notice
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+
"""Utility methods for decomposing two-qubit unitaries into FSim gates."""
16+
217
from typing import (
318
Sequence,
419
Union,

cirq/optimizers/two_qubit_to_fsim_test.py renamed to cirq/transformers/analytical_decompositions/two_qubit_to_fsim_test.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
# pylint: disable=wrong-or-nonexistent-copyright-notice
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+
215
import itertools
316
import random
417
from typing import Any
@@ -7,13 +20,24 @@
720
import pytest
821

922
import cirq
10-
from cirq.optimizers.two_qubit_to_fsim import (
23+
from cirq.transformers.analytical_decompositions.two_qubit_to_fsim import (
1124
_decompose_two_qubit_interaction_into_two_b_gates,
1225
_decompose_xx_yy_into_two_fsims_ignoring_single_qubit_ops,
1326
_sticky_0_to_1,
1427
_B,
1528
)
1629

30+
ALLOW_DEPRECATION_IN_TEST = 'ALLOW_DEPRECATION_IN_TEST'
31+
32+
33+
def test_deprecated_submodule():
34+
with cirq.testing.assert_deprecated(
35+
"Use cirq.transformers.analytical_decompositions.two_qubit_to_fsim instead",
36+
deadline="v0.16",
37+
):
38+
_ = cirq.optimizers.two_qubit_to_fsim.decompose_two_qubit_interaction_into_four_fsim_gates
39+
40+
1741
UNITARY_OBJS = [
1842
cirq.IdentityGate(2),
1943
cirq.XX ** 0.25,

cirq/optimizers/two_qubit_to_sqrt_iswap.py renamed to cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
# pylint: disable=wrong-or-nonexistent-copyright-notice
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+
215
"""Utility methods for decomposing two-qubit unitaries into sqrt-iSWAP gates.
316
417
References:

0 commit comments

Comments
 (0)