Skip to content

Commit c070e89

Browse files
kmlaurht
authored andcommitted
remove 2 deprecated create_device_proto_* methods for v0.16 release (quantumlib#5704)
This is the 1st task under "Remove deprecations" section in quantumlib#5050
1 parent a3d68e2 commit c070e89

File tree

4 files changed

+2
-961
lines changed

4 files changed

+2
-961
lines changed

cirq-google/cirq_google/devices/grid_device_test.py

+1-87
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import cirq
2323
import cirq_google
2424
from cirq_google.api import v2
25-
from cirq_google.devices import grid_device, known_devices
25+
from cirq_google.devices import grid_device
2626

2727

2828
GRID_HEIGHT = 5
@@ -417,92 +417,6 @@ def test_to_proto_invalid_input(error_match, qubits, qubit_pairs, gateset, gate_
417417
)
418418

419419

420-
def test_to_proto_backward_compatibility():
421-
# Deprecations: cirq_google.SerializableGateSet and
422-
# cirq_google.device.known_devices.create_device_proto_for_qubits()
423-
with cirq.testing.assert_deprecated(
424-
'SerializableGateSet',
425-
'create_device_specification_proto()` can be used',
426-
deadline='v0.16',
427-
count=None,
428-
):
429-
device_info, _ = _create_device_spec_with_horizontal_couplings()
430-
431-
# The set of gates in gate_durations are consistent with what's generated in
432-
# _create_device_spec_with_horizontal_couplings()
433-
base_duration = cirq.Duration(picos=1_000)
434-
gate_durations = {
435-
cirq.GateFamily(cirq_google.SYC): base_duration * 0,
436-
cirq.GateFamily(cirq.SQRT_ISWAP): base_duration * 1,
437-
cirq.GateFamily(cirq.SQRT_ISWAP_INV): base_duration * 2,
438-
cirq.GateFamily(cirq.CZ): base_duration * 3,
439-
cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate): base_duration * 4,
440-
cirq.GateFamily(
441-
cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]
442-
): base_duration
443-
* 5,
444-
cirq.GateFamily(
445-
cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]
446-
): base_duration
447-
* 6,
448-
cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse): base_duration
449-
* 7,
450-
cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate): base_duration * 8,
451-
cirq.GateFamily(cirq.ops.wait_gate.WaitGate): base_duration * 9,
452-
}
453-
454-
# Serialize the old way
455-
spec = known_devices.create_device_proto_for_qubits(
456-
device_info.grid_qubits,
457-
device_info.qubit_pairs,
458-
[cirq_google.FSIM_GATESET],
459-
known_devices._SYCAMORE_DURATIONS_PICOS,
460-
)
461-
462-
# Serialize the new way
463-
grid_device.create_device_specification_proto(
464-
qubits=device_info.grid_qubits,
465-
pairs=device_info.qubit_pairs,
466-
gateset=cirq.Gateset(*gate_durations.keys()),
467-
gate_durations=gate_durations,
468-
out=spec,
469-
)
470-
471-
with cirq.testing.assert_deprecated(
472-
'Use cirq_google.GridDevice', deadline='v0.16', count=None
473-
):
474-
# Deserialize both ways
475-
serializable_dev = cirq_google.SerializableDevice.from_proto(
476-
spec, [cirq_google.FSIM_GATESET]
477-
)
478-
grid_dev = cirq_google.GridDevice.from_proto(spec)
479-
480-
assert serializable_dev.metadata.qubit_set == grid_dev.metadata.qubit_set
481-
assert serializable_dev.metadata.qubit_pairs == grid_dev.metadata.qubit_pairs
482-
483-
assert serializable_dev.metadata.gateset == cirq.Gateset(
484-
cirq.FSimGate,
485-
cirq.ISwapPowGate,
486-
cirq.CZPowGate,
487-
cirq.PhasedXPowGate,
488-
cirq.XPowGate,
489-
cirq.YPowGate,
490-
cirq.ZPowGate,
491-
cirq.PhasedXZGate,
492-
cirq.MeasurementGate,
493-
cirq.WaitGate,
494-
cirq.GlobalPhaseGate,
495-
)
496-
497-
assert grid_dev.metadata.gateset == device_info.expected_gateset
498-
assert (
499-
tuple(grid_dev.metadata.compilation_target_gatesets)
500-
== device_info.expected_target_gatesets
501-
)
502-
503-
assert grid_dev.metadata.gate_durations == device_info.expected_gate_durations
504-
505-
506420
def test_to_proto_empty():
507421
spec = grid_device.create_device_specification_proto(
508422
# Qubits are always expected to be set

cirq-google/cirq_google/devices/known_devices.py

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

15-
from typing import Collection, Dict, Optional, Iterable, List, Set, Tuple, cast
15+
from typing import Collection, Dict, Optional, List, Set, Tuple, cast
1616

1717
import cirq
18-
from cirq import _compat
1918
from cirq_google.api import v2
2019
from cirq_google.api.v2 import device_pb2
2120
from cirq_google.devices import grid_device
2221
from cirq_google.experimental.ops import coupler_pulse
2322
from cirq_google.ops import physical_z_tag, sycamore_gate
24-
from cirq_google.serialization import op_serializer, serializable_gate_set
2523

2624
_2_QUBIT_TARGET_SET = "2_qubit_targets"
2725
_MEAS_TARGET_SET = "meas_targets"
@@ -55,42 +53,6 @@ def _parse_device(s: str) -> Tuple[List[cirq.GridQubit], Dict[str, Set[cirq.Grid
5553
return qubits, measurement_lines
5654

5755

58-
@_compat.deprecated(
59-
deadline='v0.16',
60-
fix='This function will no longer be available.'
61-
' `cirq_google.grid_device.create_device_specification_proto()` can be used'
62-
' to generate a DeviceSpecification proto which matches the format expected'
63-
' by GridDevice.',
64-
)
65-
def create_device_proto_from_diagram(
66-
ascii_grid: str,
67-
gate_sets: Optional[Iterable[serializable_gate_set.SerializableGateSet]] = None,
68-
durations_picos: Optional[Dict[str, int]] = None,
69-
out: Optional[device_pb2.DeviceSpecification] = None,
70-
) -> device_pb2.DeviceSpecification:
71-
"""Parse ASCIIart device layout into DeviceSpecification proto.
72-
This function assumes that all pairs of adjacent qubits are valid targets
73-
for two-qubit gates.
74-
Args:
75-
ascii_grid: ASCII version of the grid (see _parse_device for details).
76-
gate_sets: Gate sets that define the translation between gate ids and
77-
cirq Gate objects.
78-
durations_picos: A map from gate ids to gate durations in picoseconds.
79-
out: If given, populate this proto, otherwise create a new proto.
80-
"""
81-
qubits, _ = _parse_device(ascii_grid)
82-
83-
# Create a list of all adjacent pairs on the grid for two-qubit gates.
84-
qubit_set = frozenset(qubits)
85-
pairs: List[Tuple[cirq.Qid, cirq.Qid]] = []
86-
for qubit in qubits:
87-
for neighbor in sorted(qubit.neighbors()):
88-
if neighbor > qubit and neighbor in qubit_set:
89-
pairs.append((qubit, neighbor))
90-
91-
return create_device_proto_for_qubits(qubits, pairs, gate_sets, durations_picos, out)
92-
93-
9456
def _create_grid_device_from_diagram(
9557
ascii_grid: str,
9658
gateset: cirq.Gateset,
@@ -124,108 +86,6 @@ def _create_grid_device_from_diagram(
12486
return grid_device.GridDevice.from_proto(device_specification)
12587

12688

127-
@_compat.deprecated(
128-
deadline='v0.16',
129-
fix='This function will no longer be available.'
130-
' `cirq_google.grid_device.create_device_specification_proto()` can be used'
131-
' to generate a DeviceSpecification proto which matches the format expected'
132-
' by GridDevice.',
133-
)
134-
def create_device_proto_for_qubits(
135-
qubits: Collection[cirq.Qid],
136-
pairs: Collection[Tuple[cirq.Qid, cirq.Qid]],
137-
gate_sets: Optional[Iterable[serializable_gate_set.SerializableGateSet]] = None,
138-
durations_picos: Optional[Dict[str, int]] = None,
139-
out: Optional[device_pb2.DeviceSpecification] = None,
140-
) -> device_pb2.DeviceSpecification:
141-
"""Create device spec for the given qubits and coupled pairs.
142-
143-
Args:
144-
qubits: Qubits that can perform single-qubit gates.
145-
pairs: Pairs of coupled qubits that can perform two-qubit gates.
146-
gate_sets: Gate sets that define the translation between gate ids and
147-
cirq Gate objects.
148-
durations_picos: A map from gate ids to gate durations in picoseconds.
149-
out: If given, populate this proto, otherwise create a new proto.
150-
"""
151-
if out is None:
152-
out = device_pb2.DeviceSpecification()
153-
154-
# Create valid qubit list
155-
populate_qubits_in_device_proto(qubits, out)
156-
157-
# Single qubit gates in this gateset
158-
single_qubit_gates = (cirq.PhasedXPowGate, cirq.PhasedXZGate, cirq.ZPowGate)
159-
160-
# Set up a target set for measurement (any qubit permutation)
161-
meas_targets = out.valid_targets.add()
162-
meas_targets.name = _MEAS_TARGET_SET
163-
meas_targets.target_ordering = device_pb2.TargetSet.SUBSET_PERMUTATION
164-
165-
# Set up a target set for 2 qubit gates (specified qubit pairs)
166-
populate_qubit_pairs_in_device_proto(pairs, out)
167-
168-
# Create gate sets
169-
arg_def = device_pb2.ArgDefinition
170-
for gate_set in gate_sets or []:
171-
gs_proto = out.valid_gate_sets.add()
172-
gs_proto.name = gate_set.name
173-
gate_ids: Set[str] = set()
174-
for internal_type in gate_set.serializers:
175-
for serializer in gate_set.serializers[internal_type]:
176-
gate_id = serializer.serialized_id
177-
if gate_id in gate_ids:
178-
# Only add each type once
179-
continue
180-
181-
gate_ids.add(gate_id)
182-
gate = gs_proto.valid_gates.add()
183-
gate.id = gate_id
184-
185-
if not isinstance(serializer, op_serializer._GateOpSerializer):
186-
# This implies that 'serializer' handles non-gate ops,
187-
# such as CircuitOperations. No other properties apply.
188-
continue
189-
190-
# Choose target set and number of qubits based on gate type.
191-
gate_type = internal_type
192-
193-
# Note: if it is not a measurement gate and it's type
194-
# is not in the single_qubit_gates tuple, it's assumed to be a two qubit gate.
195-
if gate_type == cirq.MeasurementGate:
196-
gate.valid_targets.append(_MEAS_TARGET_SET)
197-
elif gate_type == cirq.WaitGate:
198-
# TODO: Refactor gate-sets / device to eliminate the need
199-
# to keep checking type here.
200-
# Github issue:
201-
# https://github.com/quantumlib/Cirq/issues/2537
202-
gate.number_of_qubits = 1
203-
elif gate_type in single_qubit_gates:
204-
gate.number_of_qubits = 1
205-
else:
206-
# This must be a two-qubit gate
207-
gate.valid_targets.append(_2_QUBIT_TARGET_SET)
208-
gate.number_of_qubits = 2
209-
210-
# Add gate duration
211-
if durations_picos is not None and gate.id in durations_picos:
212-
gate.gate_duration_picos = durations_picos[gate.id]
213-
214-
# Add argument names and types for each gate.
215-
for arg in serializer.args:
216-
new_arg = gate.valid_args.add()
217-
if arg.serialized_type == str:
218-
new_arg.type = arg_def.STRING
219-
if arg.serialized_type == float:
220-
new_arg.type = arg_def.FLOAT
221-
if arg.serialized_type == List[bool]:
222-
new_arg.type = arg_def.REPEATED_BOOLEAN
223-
new_arg.name = arg.serialized_name
224-
# Note: this does not yet support adding allowed_ranges
225-
226-
return out
227-
228-
22989
def populate_qubits_in_device_proto(
23090
qubits: Collection[cirq.Qid], out: device_pb2.DeviceSpecification
23191
) -> None:

0 commit comments

Comments
 (0)