Skip to content

Commit e0f7432

Browse files
authoredMar 18, 2022
Deprecate cirq_google.ConvertToXmonGates and replace with cirq.CZTargetGateset + cirq.optimize_for_target_gateset (#5096)
Part of #5028
1 parent a7df579 commit e0f7432

File tree

5 files changed

+28
-44
lines changed

5 files changed

+28
-44
lines changed
 

‎cirq-google/cirq_google/devices/xmon_device.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import cirq
1818
from cirq import _compat
19-
from cirq_google.optimizers import convert_to_xmon_gates
2019

2120
if TYPE_CHECKING:
2221
import cirq
@@ -52,6 +51,7 @@ def __init__(
5251
cirq.XPowGate,
5352
cirq.YPowGate,
5453
cirq.PhasedXPowGate,
54+
cirq.PhasedXZGate,
5555
cirq.MeasurementGate,
5656
cirq.ZPowGate,
5757
),
@@ -72,10 +72,17 @@ def qubit_set(self) -> FrozenSet[cirq.GridQubit]:
7272

7373
@_compat.deprecated(
7474
deadline='v0.15',
75-
fix='XmonDevice.decompose_operation is deperecated. Please use ConvertToXmonGates().',
75+
fix='XmonDevice.decompose_operation is deprecated. '
76+
'Please use cirq.optimize_for_target_gateset() and cirq.CZTargetGateset.',
7677
)
7778
def decompose_operation(self, operation: cirq.Operation) -> cirq.OP_TREE:
78-
return convert_to_xmon_gates.ConvertToXmonGates().convert(operation)
79+
if operation.gate is not None and self.is_supported_gate(operation.gate):
80+
return operation
81+
return [
82+
cirq.optimize_for_target_gateset(
83+
cirq.Circuit(operation), gateset=cirq.CZTargetGateset(allow_partial_czs=True)
84+
).all_operations()
85+
]
7986

8087
def neighbors_of(self, qubit: cirq.GridQubit):
8188
"""Returns the qubits that the given qubit can interact with."""
@@ -109,6 +116,7 @@ def is_supported_gate(cls, gate: cirq.Gate):
109116
cirq.XPowGate,
110117
cirq.YPowGate,
111118
cirq.PhasedXPowGate,
119+
cirq.PhasedXZGate,
112120
cirq.MeasurementGate,
113121
cirq.ZPowGate,
114122
),

‎cirq-google/cirq_google/devices/xmon_device_test.py

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def test_device_metadata():
5252
cirq.XPowGate,
5353
cirq.YPowGate,
5454
cirq.PhasedXPowGate,
55+
cirq.PhasedXZGate,
5556
cirq.MeasurementGate,
5657
cirq.ZPowGate,
5758
)

‎cirq-google/cirq_google/optimizers/convert_to_xmon_gates.py

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
import cirq
1717

1818

19+
@cirq._compat.deprecated_class(
20+
deadline='v1.0',
21+
fix='Use cirq.optimize_for_target_gateset and cirq.CZTargetGateset instead.',
22+
)
1923
class ConvertToXmonGates(cirq.PointOptimizer):
2024
"""Attempts to convert strange gates into XmonGates.
2125

‎cirq-google/cirq_google/optimizers/convert_to_xmon_gates_test.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ class NonNativeGate(cirq.SingleQubitGate):
4040
def test_avoids_infinite_cycle_when_matrix_available():
4141
q = cirq.GridQubit(0, 0)
4242
c = cirq.Circuit(OtherX().on(q), OtherOtherX().on(q))
43-
cirq_google.ConvertToXmonGates().optimize_circuit(c)
43+
with cirq.testing.assert_deprecated("Use cirq.optimize_for_target_gateset", deadline='v1.0'):
44+
cirq_google.ConvertToXmonGates().optimize_circuit(c)
4445
cirq.testing.assert_has_diagram(c, '(0, 0): ───PhX(1)───PhX(1)───')
46+
4547
cirq.protocols.decompose(c)
4648

4749

@@ -52,7 +54,10 @@ def test_avoids_infinite_cycle_when_matrix_available():
5254
def test_bad_operation():
5355
c = cirq.Circuit(NonNativeGate().on(q[0]))
5456
with pytest.raises(TypeError):
55-
cirq_google.ConvertToXmonGates().optimize_circuit(c)
57+
with cirq.testing.assert_deprecated(
58+
"Use cirq.optimize_for_target_gateset", deadline='v1.0'
59+
):
60+
cirq_google.ConvertToXmonGates().optimize_circuit(c)
5661

5762

5863
@pytest.mark.parametrize(
@@ -68,4 +73,5 @@ def test_bad_operation():
6873
)
6974
def test_supported_operation(op, is_valid):
7075
c = cirq.Circuit(op)
71-
assert (cirq_google.ConvertToXmonGates().optimization_at(c, 0, op) is not None) == is_valid
76+
with cirq.testing.assert_deprecated("Use cirq.optimize_for_target_gateset", deadline='v1.0'):
77+
assert (cirq_google.ConvertToXmonGates().optimization_at(c, 0, op) is not None) == is_valid

‎cirq-google/cirq_google/optimizers/optimize_for_sycamore.py

+3-38
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,18 @@
1313
# limitations under the License.
1414
"""A combination of several optimizations targeting XmonDevice."""
1515
from functools import lru_cache
16-
from typing import Callable, cast, List, Optional, TYPE_CHECKING
16+
from typing import Callable, cast, Optional, TYPE_CHECKING
1717

1818
import numpy as np
1919

2020
import cirq
2121
from cirq_google import ops as cg_ops
22-
from cirq_google.optimizers import (
23-
convert_to_xmon_gates,
24-
)
2522
from cirq_google.transformers.target_gatesets import sycamore_gateset
2623

2724
if TYPE_CHECKING:
2825
import cirq_google
2926

3027

31-
def _get_xmon_optimizers(
32-
tolerance: float, tabulation: Optional[cirq.TwoQubitGateTabulation]
33-
) -> List[Callable[[cirq.Circuit], None]]:
34-
if tabulation is not None:
35-
# coverage: ignore
36-
raise ValueError("Gate tabulation not supported for xmon")
37-
38-
return [
39-
convert_to_xmon_gates.ConvertToXmonGates().optimize_circuit,
40-
]
41-
42-
43-
def _get_xmon_optimizers_part_cz(
44-
tolerance: float, tabulation: Optional[cirq.TwoQubitGateTabulation]
45-
) -> List[Callable[[cirq.Circuit], None]]:
46-
if tabulation is not None:
47-
# coverage: ignore
48-
raise ValueError("Gate tabulation not supported for xmon")
49-
return [
50-
convert_to_xmon_gates.ConvertToXmonGates().optimize_circuit,
51-
]
52-
53-
54-
_OPTIMIZER_TYPES = {
55-
'xmon': _get_xmon_optimizers,
56-
'xmon_partial_cz': _get_xmon_optimizers_part_cz,
57-
}
58-
5928
_TARGET_GATESETS = {
6029
'sqrt_iswap': lambda atol, _: cirq.SqrtIswapTargetGateset(atol=atol),
6130
'sycamore': lambda atol, tabulation: sycamore_gateset.SycamoreTargetGateset(
@@ -122,10 +91,10 @@ def optimized_for_sycamore(
12291
ValueError: If the `optimizer_type` is not a supported type.
12392
"""
12493
copy = circuit.copy()
125-
if optimizer_type not in _OPTIMIZER_TYPES and optimizer_type not in _TARGET_GATESETS:
94+
if optimizer_type not in _TARGET_GATESETS:
12695
raise ValueError(
12796
f'{optimizer_type} is not an allowed type. Allowed '
128-
f'types are: {_OPTIMIZER_TYPES.keys()}'
97+
f'types are: {_TARGET_GATESETS.keys()}'
12998
)
13099

131100
tabulation: Optional[cirq.TwoQubitGateTabulation] = None
@@ -137,10 +106,6 @@ def optimized_for_sycamore(
137106
circuit,
138107
gateset=_TARGET_GATESETS[optimizer_type](tolerance, tabulation),
139108
)
140-
if optimizer_type in _OPTIMIZER_TYPES:
141-
opts = _OPTIMIZER_TYPES[optimizer_type](tolerance=tolerance, tabulation=tabulation)
142-
for optimizer in opts:
143-
optimizer(copy)
144109
copy = cirq.merge_single_qubit_gates_to_phxz(copy, atol=tolerance)
145110
copy = cirq.eject_phased_paulis(copy, atol=tolerance)
146111
copy = cirq.eject_z(copy, atol=tolerance)

0 commit comments

Comments
 (0)
Please sign in to comment.