From c070e8979ac4d8e920473eae68e59567f86ffcf6 Mon Sep 17 00:00:00 2001 From: K M Lau Date: Mon, 11 Jul 2022 17:38:06 -0700 Subject: [PATCH] remove 2 deprecated create_device_proto_* methods for v0.16 release (#5704) This is the 1st task under "Remove deprecations" section in https://github.com/quantumlib/Cirq/issues/5050 --- .../cirq_google/devices/grid_device_test.py | 88 +--- .../cirq_google/devices/known_devices.py | 142 +----- .../cirq_google/devices/known_devices_test.py | 446 ------------------ .../devices/serializable_device_test.py | 287 ----------- 4 files changed, 2 insertions(+), 961 deletions(-) diff --git a/cirq-google/cirq_google/devices/grid_device_test.py b/cirq-google/cirq_google/devices/grid_device_test.py index 1dc94d8bb19..c6d019f9482 100644 --- a/cirq-google/cirq_google/devices/grid_device_test.py +++ b/cirq-google/cirq_google/devices/grid_device_test.py @@ -22,7 +22,7 @@ import cirq import cirq_google from cirq_google.api import v2 -from cirq_google.devices import grid_device, known_devices +from cirq_google.devices import grid_device GRID_HEIGHT = 5 @@ -417,92 +417,6 @@ def test_to_proto_invalid_input(error_match, qubits, qubit_pairs, gateset, gate_ ) -def test_to_proto_backward_compatibility(): - # Deprecations: cirq_google.SerializableGateSet and - # cirq_google.device.known_devices.create_device_proto_for_qubits() - with cirq.testing.assert_deprecated( - 'SerializableGateSet', - 'create_device_specification_proto()` can be used', - deadline='v0.16', - count=None, - ): - device_info, _ = _create_device_spec_with_horizontal_couplings() - - # The set of gates in gate_durations are consistent with what's generated in - # _create_device_spec_with_horizontal_couplings() - base_duration = cirq.Duration(picos=1_000) - gate_durations = { - cirq.GateFamily(cirq_google.SYC): base_duration * 0, - cirq.GateFamily(cirq.SQRT_ISWAP): base_duration * 1, - cirq.GateFamily(cirq.SQRT_ISWAP_INV): base_duration * 2, - cirq.GateFamily(cirq.CZ): base_duration * 3, - cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate): base_duration * 4, - cirq.GateFamily( - cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()] - ): base_duration - * 5, - cirq.GateFamily( - cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()] - ): base_duration - * 6, - cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse): base_duration - * 7, - cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate): base_duration * 8, - cirq.GateFamily(cirq.ops.wait_gate.WaitGate): base_duration * 9, - } - - # Serialize the old way - spec = known_devices.create_device_proto_for_qubits( - device_info.grid_qubits, - device_info.qubit_pairs, - [cirq_google.FSIM_GATESET], - known_devices._SYCAMORE_DURATIONS_PICOS, - ) - - # Serialize the new way - grid_device.create_device_specification_proto( - qubits=device_info.grid_qubits, - pairs=device_info.qubit_pairs, - gateset=cirq.Gateset(*gate_durations.keys()), - gate_durations=gate_durations, - out=spec, - ) - - with cirq.testing.assert_deprecated( - 'Use cirq_google.GridDevice', deadline='v0.16', count=None - ): - # Deserialize both ways - serializable_dev = cirq_google.SerializableDevice.from_proto( - spec, [cirq_google.FSIM_GATESET] - ) - grid_dev = cirq_google.GridDevice.from_proto(spec) - - assert serializable_dev.metadata.qubit_set == grid_dev.metadata.qubit_set - assert serializable_dev.metadata.qubit_pairs == grid_dev.metadata.qubit_pairs - - assert serializable_dev.metadata.gateset == cirq.Gateset( - cirq.FSimGate, - cirq.ISwapPowGate, - cirq.CZPowGate, - cirq.PhasedXPowGate, - cirq.XPowGate, - cirq.YPowGate, - cirq.ZPowGate, - cirq.PhasedXZGate, - cirq.MeasurementGate, - cirq.WaitGate, - cirq.GlobalPhaseGate, - ) - - assert grid_dev.metadata.gateset == device_info.expected_gateset - assert ( - tuple(grid_dev.metadata.compilation_target_gatesets) - == device_info.expected_target_gatesets - ) - - assert grid_dev.metadata.gate_durations == device_info.expected_gate_durations - - def test_to_proto_empty(): spec = grid_device.create_device_specification_proto( # Qubits are always expected to be set diff --git a/cirq-google/cirq_google/devices/known_devices.py b/cirq-google/cirq_google/devices/known_devices.py index a93d6d7bf5d..372990e3819 100644 --- a/cirq-google/cirq_google/devices/known_devices.py +++ b/cirq-google/cirq_google/devices/known_devices.py @@ -12,16 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Collection, Dict, Optional, Iterable, List, Set, Tuple, cast +from typing import Collection, Dict, Optional, List, Set, Tuple, cast import cirq -from cirq import _compat from cirq_google.api import v2 from cirq_google.api.v2 import device_pb2 from cirq_google.devices import grid_device from cirq_google.experimental.ops import coupler_pulse from cirq_google.ops import physical_z_tag, sycamore_gate -from cirq_google.serialization import op_serializer, serializable_gate_set _2_QUBIT_TARGET_SET = "2_qubit_targets" _MEAS_TARGET_SET = "meas_targets" @@ -55,42 +53,6 @@ def _parse_device(s: str) -> Tuple[List[cirq.GridQubit], Dict[str, Set[cirq.Grid return qubits, measurement_lines -@_compat.deprecated( - deadline='v0.16', - fix='This function will no longer be available.' - ' `cirq_google.grid_device.create_device_specification_proto()` can be used' - ' to generate a DeviceSpecification proto which matches the format expected' - ' by GridDevice.', -) -def create_device_proto_from_diagram( - ascii_grid: str, - gate_sets: Optional[Iterable[serializable_gate_set.SerializableGateSet]] = None, - durations_picos: Optional[Dict[str, int]] = None, - out: Optional[device_pb2.DeviceSpecification] = None, -) -> device_pb2.DeviceSpecification: - """Parse ASCIIart device layout into DeviceSpecification proto. - This function assumes that all pairs of adjacent qubits are valid targets - for two-qubit gates. - Args: - ascii_grid: ASCII version of the grid (see _parse_device for details). - gate_sets: Gate sets that define the translation between gate ids and - cirq Gate objects. - durations_picos: A map from gate ids to gate durations in picoseconds. - out: If given, populate this proto, otherwise create a new proto. - """ - qubits, _ = _parse_device(ascii_grid) - - # Create a list of all adjacent pairs on the grid for two-qubit gates. - qubit_set = frozenset(qubits) - pairs: List[Tuple[cirq.Qid, cirq.Qid]] = [] - for qubit in qubits: - for neighbor in sorted(qubit.neighbors()): - if neighbor > qubit and neighbor in qubit_set: - pairs.append((qubit, neighbor)) - - return create_device_proto_for_qubits(qubits, pairs, gate_sets, durations_picos, out) - - def _create_grid_device_from_diagram( ascii_grid: str, gateset: cirq.Gateset, @@ -124,108 +86,6 @@ def _create_grid_device_from_diagram( return grid_device.GridDevice.from_proto(device_specification) -@_compat.deprecated( - deadline='v0.16', - fix='This function will no longer be available.' - ' `cirq_google.grid_device.create_device_specification_proto()` can be used' - ' to generate a DeviceSpecification proto which matches the format expected' - ' by GridDevice.', -) -def create_device_proto_for_qubits( - qubits: Collection[cirq.Qid], - pairs: Collection[Tuple[cirq.Qid, cirq.Qid]], - gate_sets: Optional[Iterable[serializable_gate_set.SerializableGateSet]] = None, - durations_picos: Optional[Dict[str, int]] = None, - out: Optional[device_pb2.DeviceSpecification] = None, -) -> device_pb2.DeviceSpecification: - """Create device spec for the given qubits and coupled pairs. - - Args: - qubits: Qubits that can perform single-qubit gates. - pairs: Pairs of coupled qubits that can perform two-qubit gates. - gate_sets: Gate sets that define the translation between gate ids and - cirq Gate objects. - durations_picos: A map from gate ids to gate durations in picoseconds. - out: If given, populate this proto, otherwise create a new proto. - """ - if out is None: - out = device_pb2.DeviceSpecification() - - # Create valid qubit list - populate_qubits_in_device_proto(qubits, out) - - # Single qubit gates in this gateset - single_qubit_gates = (cirq.PhasedXPowGate, cirq.PhasedXZGate, cirq.ZPowGate) - - # Set up a target set for measurement (any qubit permutation) - meas_targets = out.valid_targets.add() - meas_targets.name = _MEAS_TARGET_SET - meas_targets.target_ordering = device_pb2.TargetSet.SUBSET_PERMUTATION - - # Set up a target set for 2 qubit gates (specified qubit pairs) - populate_qubit_pairs_in_device_proto(pairs, out) - - # Create gate sets - arg_def = device_pb2.ArgDefinition - for gate_set in gate_sets or []: - gs_proto = out.valid_gate_sets.add() - gs_proto.name = gate_set.name - gate_ids: Set[str] = set() - for internal_type in gate_set.serializers: - for serializer in gate_set.serializers[internal_type]: - gate_id = serializer.serialized_id - if gate_id in gate_ids: - # Only add each type once - continue - - gate_ids.add(gate_id) - gate = gs_proto.valid_gates.add() - gate.id = gate_id - - if not isinstance(serializer, op_serializer._GateOpSerializer): - # This implies that 'serializer' handles non-gate ops, - # such as CircuitOperations. No other properties apply. - continue - - # Choose target set and number of qubits based on gate type. - gate_type = internal_type - - # Note: if it is not a measurement gate and it's type - # is not in the single_qubit_gates tuple, it's assumed to be a two qubit gate. - if gate_type == cirq.MeasurementGate: - gate.valid_targets.append(_MEAS_TARGET_SET) - elif gate_type == cirq.WaitGate: - # TODO: Refactor gate-sets / device to eliminate the need - # to keep checking type here. - # Github issue: - # https://github.com/quantumlib/Cirq/issues/2537 - gate.number_of_qubits = 1 - elif gate_type in single_qubit_gates: - gate.number_of_qubits = 1 - else: - # This must be a two-qubit gate - gate.valid_targets.append(_2_QUBIT_TARGET_SET) - gate.number_of_qubits = 2 - - # Add gate duration - if durations_picos is not None and gate.id in durations_picos: - gate.gate_duration_picos = durations_picos[gate.id] - - # Add argument names and types for each gate. - for arg in serializer.args: - new_arg = gate.valid_args.add() - if arg.serialized_type == str: - new_arg.type = arg_def.STRING - if arg.serialized_type == float: - new_arg.type = arg_def.FLOAT - if arg.serialized_type == List[bool]: - new_arg.type = arg_def.REPEATED_BOOLEAN - new_arg.name = arg.serialized_name - # Note: this does not yet support adding allowed_ranges - - return out - - def populate_qubits_in_device_proto( qubits: Collection[cirq.Qid], out: device_pb2.DeviceSpecification ) -> None: diff --git a/cirq-google/cirq_google/devices/known_devices_test.py b/cirq-google/cirq_google/devices/known_devices_test.py index 228ee93ef19..e17e5ea8ecc 100644 --- a/cirq-google/cirq_google/devices/known_devices_test.py +++ b/cirq-google/cirq_google/devices/known_devices_test.py @@ -17,256 +17,6 @@ import cirq import cirq_google import cirq_google.experimental.ops.coupler_pulse as coupler_pulse -import cirq_google.devices.known_devices as known_devices -import cirq_google.serialization.common_serializers as cgc - - -def test_create_device_proto_for_irregular_grid(): - qubits = cirq.GridQubit.rect(2, 2) - pairs = [ - (cirq.GridQubit(0, 0), cirq.GridQubit(0, 1)), - (cirq.GridQubit(0, 0), cirq.GridQubit(1, 0)), - (cirq.GridQubit(1, 0), cirq.GridQubit(1, 1)), - ] - with cirq.testing.assert_deprecated( - 'create_device_specification_proto()` can be used', deadline='v0.16', count=1 - ): - proto = known_devices.create_device_proto_for_qubits(qubits, pairs) - assert ( - str(proto) - == """\ -valid_qubits: "0_0" -valid_qubits: "0_1" -valid_qubits: "1_0" -valid_qubits: "1_1" -valid_targets { - name: "meas_targets" - target_ordering: SUBSET_PERMUTATION -} -valid_targets { - name: "2_qubit_targets" - target_ordering: SYMMETRIC - targets { - ids: "0_0" - ids: "0_1" - } - targets { - ids: "0_0" - ids: "1_0" - } - targets { - ids: "1_0" - ids: "1_1" - } -} -""" - ) - - -def test_multiple_gate_sets(): - # Deprecations: cirq_google.SerializableGateSets class, - # well-known cirq_google SerializableGateSets (e.g. cirq_google.SYC_GATESET), common serializers - # and cirq_google.devices.known_devices.create_device_proto_from_diagram - with cirq.testing.assert_deprecated( - 'SerializableGateSet', - 'CircuitSerializer', - 'no longer be available', - deadline='v0.16', - count=8, - ): - halfPiGateSet = cirq_google.SerializableGateSet( - gate_set_name='half_pi_gateset', - serializers=[*cgc.SINGLE_QUBIT_HALF_PI_SERIALIZERS, cgc.MEASUREMENT_SERIALIZER], - deserializers=[*cgc.SINGLE_QUBIT_HALF_PI_DESERIALIZERS, cgc.MEASUREMENT_DESERIALIZER], - ) - durations_dict = { - 'xy_pi': 20_000, - 'xy_half_pi': 10_000, - 'xy': 53_000, - 'cz': 11_000, - 'meas': 14_141, - } - test_proto = known_devices.create_device_proto_from_diagram( - "aa\naa", [cirq_google.XMON, halfPiGateSet], durations_dict - ) - assert ( - str(test_proto) - == """\ -valid_gate_sets { - name: "xmon" - valid_gates { - id: "xy" - number_of_qubits: 1 - valid_args { - name: "axis_half_turns" - type: FLOAT - } - valid_args { - name: "half_turns" - type: FLOAT - } - gate_duration_picos: 53000 - } - valid_gates { - id: "z" - number_of_qubits: 1 - valid_args { - name: "half_turns" - type: FLOAT - } - valid_args { - name: "type" - type: STRING - } - } - valid_gates { - id: "xyz" - number_of_qubits: 1 - valid_args { - name: "x_exponent" - type: FLOAT - } - valid_args { - name: "z_exponent" - type: FLOAT - } - valid_args { - name: "axis_phase_exponent" - type: FLOAT - } - } - valid_gates { - id: "cz" - number_of_qubits: 2 - valid_args { - name: "half_turns" - type: FLOAT - } - valid_args { - name: "phase_match" - type: STRING - } - gate_duration_picos: 11000 - valid_targets: "2_qubit_targets" - } - valid_gates { - id: "meas" - valid_args { - name: "key" - type: STRING - } - valid_args { - name: "invert_mask" - type: REPEATED_BOOLEAN - } - gate_duration_picos: 14141 - valid_targets: "meas_targets" - } - valid_gates { - id: "circuit" - } -} -valid_gate_sets { - name: "half_pi_gateset" - valid_gates { - id: "xy_pi" - number_of_qubits: 1 - valid_args { - name: "axis_half_turns" - type: FLOAT - } - gate_duration_picos: 20000 - } - valid_gates { - id: "xy_half_pi" - number_of_qubits: 1 - valid_args { - name: "axis_half_turns" - type: FLOAT - } - gate_duration_picos: 10000 - } - valid_gates { - id: "meas" - valid_args { - name: "key" - type: STRING - } - valid_args { - name: "invert_mask" - type: REPEATED_BOOLEAN - } - gate_duration_picos: 14141 - valid_targets: "meas_targets" - } -} -valid_qubits: "0_0" -valid_qubits: "0_1" -valid_qubits: "1_0" -valid_qubits: "1_1" -valid_targets { - name: "meas_targets" - target_ordering: SUBSET_PERMUTATION -} -valid_targets { - name: "2_qubit_targets" - target_ordering: SYMMETRIC - targets { - ids: "0_0" - ids: "0_1" - } - targets { - ids: "0_0" - ids: "1_0" - } - targets { - ids: "0_1" - ids: "1_1" - } - targets { - ids: "1_0" - ids: "1_1" - } -} -""" - ) - - -def test_sycamore_circuitop_device(): - # Deprecations: cirq_google.SerializableDevice, cirq_google.SerializableGateSets class, - # well-known cirq_google SerializableGateSets (e.g. cirq_google.SYC_GATESET), and - # cirq_google.devices.known_devices.create_device_proto_from_diagram - with cirq.testing.assert_deprecated( - 'Use cirq_google.GridDevice', - 'SerializableGateSet', - 'no longer be available', - deadline='v0.16', - count=7, - ): - circuitop_gateset = cirq_google.SerializableGateSet( - gate_set_name='circuitop_gateset', - serializers=[cgc.CIRCUIT_OP_SERIALIZER], - deserializers=[cgc.CIRCUIT_OP_DESERIALIZER], - ) - gateset_list = [cirq_google.SQRT_ISWAP_GATESET, cirq_google.SYC_GATESET, circuitop_gateset] - circuitop_proto = cirq_google.devices.known_devices.create_device_proto_from_diagram( - known_devices._SYCAMORE23_GRID, gateset_list, known_devices._SYCAMORE_DURATIONS_PICOS - ) - device = cirq_google.SerializableDevice.from_proto( - proto=circuitop_proto, gate_sets=gateset_list - ) - q0 = cirq.GridQubit(5, 3) - q1 = cirq.GridQubit(5, 4) - syc = cirq.FSimGate(theta=np.pi / 2, phi=np.pi / 6)(q0, q1) - sqrt_iswap = cirq.FSimGate(theta=np.pi / 4, phi=0)(q0, q1) - circuit_op = cirq.CircuitOperation(cirq.FrozenCircuit(syc, sqrt_iswap)) - device.validate_operation(syc) - device.validate_operation(sqrt_iswap) - device.validate_operation(circuit_op) - assert device.duration_of(syc) == cirq.Duration(nanos=12) - assert device.duration_of(sqrt_iswap) == cirq.Duration(nanos=32) - # CircuitOperations don't have a set duration. - assert device.duration_of(circuit_op) == cirq.Duration(nanos=0) def test_sycamore_grid_layout(): @@ -284,202 +34,6 @@ def test_sycamore_grid_layout(): cirq_google.Sycamore23.validate_operation(sqrt_iswap) -def test_proto_with_circuitop(): - # Deprecations: cirq_google.SerializableGateSets class and - # cirq_google.devices.known_devices.create_device_proto_from_diagram - with cirq.testing.assert_deprecated( - 'SerializableGateSet', 'no longer be available', deadline='v0.16', count=3 - ): - circuitop_gateset = cirq_google.SerializableGateSet( - gate_set_name='circuitop_gateset', - serializers=[cgc.CIRCUIT_OP_SERIALIZER], - deserializers=[cgc.CIRCUIT_OP_DESERIALIZER], - ) - circuitop_proto = cirq_google.devices.known_devices.create_device_proto_from_diagram( - "aa\naa", [circuitop_gateset] - ) - - assert ( - str(circuitop_proto) - == """\ -valid_gate_sets { - name: "circuitop_gateset" - valid_gates { - id: "circuit" - } -} -valid_qubits: "0_0" -valid_qubits: "0_1" -valid_qubits: "1_0" -valid_qubits: "1_1" -valid_targets { - name: "meas_targets" - target_ordering: SUBSET_PERMUTATION -} -valid_targets { - name: "2_qubit_targets" - target_ordering: SYMMETRIC - targets { - ids: "0_0" - ids: "0_1" - } - targets { - ids: "0_0" - ids: "1_0" - } - targets { - ids: "0_1" - ids: "1_1" - } - targets { - ids: "1_0" - ids: "1_1" - } -} -""" - ) - - -def test_proto_with_waitgate(): - # Deprecations: cirq_google.SerializableDevice, cirq_google.SerializableGateSet, - # common serializers, and - # cirq_google.devices.known_devices.create_device_proto_from_diagram - with cirq.testing.assert_deprecated( - 'Use cirq_google.GridDevice', - 'SerializableGateSet', - 'CircuitSerializer', - 'no longer be available', - deadline='v0.16', - count=7, - ): - wait_gateset = cirq_google.SerializableGateSet( - gate_set_name='wait_gateset', - serializers=[cgc.WAIT_GATE_SERIALIZER], - deserializers=[cgc.WAIT_GATE_DESERIALIZER], - ) - wait_proto = cirq_google.devices.known_devices.create_device_proto_from_diagram( - "aa\naa", [wait_gateset] - ) - wait_device = cirq_google.SerializableDevice.from_proto( - proto=wait_proto, gate_sets=[wait_gateset] - ) - q0 = cirq.GridQubit(1, 1) - wait_op = cirq.wait(q0, nanos=25) - wait_device.validate_operation(wait_op) - - assert ( - str(wait_proto) - == """\ -valid_gate_sets { - name: "wait_gateset" - valid_gates { - id: "wait" - number_of_qubits: 1 - valid_args { - name: "nanos" - type: FLOAT - } - } -} -valid_qubits: "0_0" -valid_qubits: "0_1" -valid_qubits: "1_0" -valid_qubits: "1_1" -valid_targets { - name: "meas_targets" - target_ordering: SUBSET_PERMUTATION -} -valid_targets { - name: "2_qubit_targets" - target_ordering: SYMMETRIC - targets { - ids: "0_0" - ids: "0_1" - } - targets { - ids: "0_0" - ids: "1_0" - } - targets { - ids: "0_1" - ids: "1_1" - } - targets { - ids: "1_0" - ids: "1_1" - } -} -""" - ) - - -def test_adding_gates_multiple_times(): - # Deprecations: cirq_google.SerializableDevice, cirq_google.SerializableGateSet, - # common serializers, and - # cirq_google.devices.known_devices.create_device_proto_from_diagram - with cirq.testing.assert_deprecated( - 'Use cirq_google.GridDevice', - 'SerializableGateSet', - 'CircuitSerializer', - 'no longer be available', - deadline='v0.16', - count=11, - ): - waiting_for_godot = cirq_google.SerializableGateSet( - gate_set_name='wait_gateset', - serializers=[ - cgc.WAIT_GATE_SERIALIZER, - cgc.WAIT_GATE_SERIALIZER, - cgc.WAIT_GATE_SERIALIZER, - ], - deserializers=[ - cgc.WAIT_GATE_DESERIALIZER, - cgc.WAIT_GATE_DESERIALIZER, - cgc.WAIT_GATE_DESERIALIZER, - ], - ) - wait_proto = cirq_google.devices.known_devices.create_device_proto_from_diagram( - "aa", [waiting_for_godot] - ) - wait_device = cirq_google.SerializableDevice.from_proto( - proto=wait_proto, gate_sets=[waiting_for_godot] - ) - q0 = cirq.GridQubit(0, 0) - wait_op = cirq.wait(q0, nanos=25) - wait_device.validate_operation(wait_op) - - assert ( - str(wait_proto) - == """\ -valid_gate_sets { - name: "wait_gateset" - valid_gates { - id: "wait" - number_of_qubits: 1 - valid_args { - name: "nanos" - type: FLOAT - } - } -} -valid_qubits: "0_0" -valid_qubits: "0_1" -valid_targets { - name: "meas_targets" - target_ordering: SUBSET_PERMUTATION -} -valid_targets { - name: "2_qubit_targets" - target_ordering: SYMMETRIC - targets { - ids: "0_0" - ids: "0_1" - } -} -""" - ) - - @pytest.mark.parametrize( 'device, qubit_size, layout_str', [ diff --git a/cirq-google/cirq_google/devices/serializable_device_test.py b/cirq-google/cirq_google/devices/serializable_device_test.py index db2c55bcb4c..26ddc1f0581 100644 --- a/cirq-google/cirq_google/devices/serializable_device_test.py +++ b/cirq-google/cirq_google/devices/serializable_device_test.py @@ -12,18 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import textwrap import unittest.mock as mock import pytest import cirq -import cirq_google import cirq_google as cg import cirq_google.api.v2 as v2 import cirq_google.api.v2.device_pb2 as device_pb2 -import cirq_google.devices.known_devices as cgdk -import cirq_google.serialization.common_serializers as cgc def _just_cz(): @@ -66,38 +62,6 @@ def _just_meas(): ) -def test_str_with_grid_qubits(): - # Deprecations: cirq_google.SerializableDevice, well-known cirq_google SerializableGateSets - # (e.g. cirq_google.SYC_GATESET) and - # cirq_google.devices.known_devices.create_device_proto_for_qubits - with cirq.testing.assert_deprecated( - 'Use cirq_google.GridDevice', - 'SerializableGateSet', - 'create_device_specification_proto()` can be used', - deadline='v0.16', - count=5, - ): - qubits = cirq.GridQubit.rect(2, 3, left=1, top=1) - device_proto = cgdk.create_device_proto_for_qubits( - qubits=qubits, - pairs=[ - (qubits[0], qubits[1]), - (qubits[0], qubits[3]), - (qubits[1], qubits[4]), - (qubits[4], qubits[5]), - ], - gate_sets=[cg.FSIM_GATESET], - ) - device = cg.SerializableDevice.from_proto(device_proto, gate_sets=[cg.FSIM_GATESET]) - assert str(device) == textwrap.dedent( - """\ - q(1, 1)───q(1, 2) q(1, 3) - │ │ - │ │ - q(2, 1) q(2, 2)───q(2, 3)""" - ) - - @pytest.mark.parametrize('cycle,func', [(False, str), (True, repr)]) def test_repr_pretty(cycle, func): device = cg.Sycamore @@ -106,45 +70,6 @@ def test_repr_pretty(cycle, func): printer.text.assert_called_once_with(func(device)) -def test_metadata_correct(): - # Deprecations: cirq_google.SerializableDevice, well-known cirq_google SerializableGateSets - # (e.g. cirq_google.SYC_GATESET) and - # cirq_google.devices.known_devices.create_device_proto_for_qubits - with cirq.testing.assert_deprecated( - 'Use cirq_google.GridDevice', - 'SerializableGateSet', - 'create_device_specification_proto()` can be used', - deadline='v0.16', - count=5, - ): - qubits = cirq.GridQubit.rect(2, 3, left=1, top=1) - pairs = [ - (qubits[0], qubits[1]), - (qubits[0], qubits[3]), - (qubits[1], qubits[4]), - (qubits[4], qubits[5]), - ] - device_proto = cgdk.create_device_proto_for_qubits( - qubits=qubits, pairs=pairs, gate_sets=[cg.FSIM_GATESET] - ) - - device = cg.SerializableDevice.from_proto(device_proto, gate_sets=[cg.FSIM_GATESET]) - assert device.metadata.qubit_pairs == frozenset({frozenset(p) for p in pairs}) - assert device.metadata.gateset == cirq.Gateset( - cirq.FSimGate, - cirq.ISwapPowGate, - cirq.CZPowGate, - cirq.PhasedXPowGate, - cirq.XPowGate, - cirq.YPowGate, - cirq.ZPowGate, - cirq.PhasedXZGate, - cirq.MeasurementGate, - cirq.WaitGate, - cirq.GlobalPhaseGate, - ) - - def test_gate_definition_equality(): def1 = cg.devices.serializable_device._GateDefinition( duration=cirq.Duration(picos=4), @@ -172,83 +97,6 @@ def test_gate_definition_equality(): eq.add_equality_group(cirq.X) -def test_mismatched_proto_serializer(): - # Deprecations: cirq_google.SerializableDevice, well-known cirq_google SerializableGateSets - # (e.g. cirq_google.SYC_GATESET), and - # cirq_google.devices.known_devices.create_device_proto_from_diagram - with cirq.testing.assert_deprecated( - 'Use cirq_google.GridDevice', - 'SerializableGateSet', - 'no longer be available', - deadline='v0.16', - count=6, - ): - augmented_proto = cgdk.create_device_proto_from_diagram( - cgdk._SYCAMORE_GRID, - [cg.SQRT_ISWAP_GATESET, cg.SYC_GATESET], - cgdk._SYCAMORE_DURATIONS_PICOS, - ) - # Remove measurement gate - del augmented_proto.valid_gate_sets[0].valid_gates[3] - - # Should throw value error that measurement gate is serialized - # but not supported by the hardware - with pytest.raises(ValueError): - _ = cg.SerializableDevice.from_proto(proto=augmented_proto, gate_sets=[cg.XMON]) - - -def test_named_qubit(): - # Deprecations: cirq_google.SerializableDevice, well-known cirq_google SerializableGateSets - # (e.g. cirq_google.SYC_GATESET), and - # cirq_google.devices.known_devices.create_device_proto_from_diagram - with cirq.testing.assert_deprecated( - 'Use cirq_google.GridDevice', - 'SerializableGateSet', - 'no longer be available', - deadline='v0.16', - count=7, - ): - augmented_proto = cgdk.create_device_proto_from_diagram( - cgdk._SYCAMORE_GRID, - [cg.SQRT_ISWAP_GATESET, cg.SYC_GATESET], - cgdk._SYCAMORE_DURATIONS_PICOS, - ) - augmented_proto.valid_qubits.extend(["scooby_doo"]) - sycamore = cg.SerializableDevice.from_proto( - proto=augmented_proto, gate_sets=[cg.SYC_GATESET] - ) - sycamore.validate_operation(cirq.X(cirq.NamedQubit("scooby_doo"))) - with pytest.raises(ValueError): - sycamore.validate_operation(cirq.X(cirq.NamedQubit("scrappy_doo"))) - - -def test_duration_of(): - # Deprecations: cirq_google.SerializableDevice, well-known cirq_google SerializableGateSets - # (e.g. cirq_google.SYC_GATESET), and - # cirq_google.devices.known_devices.create_device_proto_from_diagram - with cirq.testing.assert_deprecated( - 'Use cirq_google.GridDevice', - 'SerializableGateSet', - 'no longer be available', - deadline='v0.16', - count=6, - ): - valid_qubit1 = cirq.GridQubit(0, 0) - - sycamore_proto = cgdk.create_device_proto_from_diagram( - cgdk._SYCAMORE_GRID, [cg.SYC_GATESET], cgdk._SYCAMORE_DURATIONS_PICOS - ) - sycamore = cg.SerializableDevice.from_proto( - proto=sycamore_proto, gate_sets=[cg.SYC_GATESET] - ) - - assert sycamore.duration_of(cirq.X(valid_qubit1)) == cirq.Duration(nanos=25) - - # Unsupported op - with pytest.raises(ValueError): - assert sycamore.duration_of(cirq.H(valid_qubit1)) - - def test_asymmetric_gate(): spec = device_pb2.DeviceSpecification() for row in range(5): @@ -394,141 +242,6 @@ def test_mixing_types(): _ = cg.SerializableDevice.from_proto(proto=spec, gate_sets=[meas_gateset]) -def test_multiple_gatesets(): - # Deprecations: cirq_google.SerializableDevice, cirq_google.SerializableGateSet, - # common serializers, and - # cirq_google.devices.known_devices.create_device_proto_from_diagram, - with cirq.testing.assert_deprecated( - 'Use cirq_google.GridDevice', - 'SerializableGateSet', - 'CircuitSerializer', - 'no longer be available', - deadline='v0.16', - count=10, - ): - halfPiGateSet = cirq_google.SerializableGateSet( - gate_set_name='half_pi_gateset', - serializers=cgc.SINGLE_QUBIT_HALF_PI_SERIALIZERS, - deserializers=cgc.SINGLE_QUBIT_HALF_PI_DESERIALIZERS, - ) - allAnglesGateSet = cirq_google.SerializableGateSet( - gate_set_name='all_angles_gateset', - serializers=cgc.SINGLE_QUBIT_SERIALIZERS, - deserializers=cgc.SINGLE_QUBIT_DESERIALIZERS, - ) - durations_dict = {'xy_pi': 20_000, 'xy_half_pi': 20_000, 'xy': 20_000} - spec = cirq_google.devices.known_devices.create_device_proto_from_diagram( - "aa\naa", [allAnglesGateSet, halfPiGateSet], durations_dict - ) - dev = cg.SerializableDevice.from_proto( - proto=spec, gate_sets=[allAnglesGateSet, halfPiGateSet] - ) - q0 = cirq.GridQubit(0, 0) - q1 = cirq.GridQubit(1, 0) - dev.validate_operation(cirq.X(q0)) - dev.validate_operation(cirq.X(q1)) - dev.validate_operation(cirq.XPowGate(exponent=0.1234)(q0)) - dev.validate_operation(cirq.XPowGate(exponent=0.2345)(q1)) - - with pytest.raises(ValueError): - dev.validate_operation(cirq.X(cirq.GridQubit(2, 2))) - - -def test_half_pi_takes_half_duration(): - """This tests that gate sets with two different definitions for the same - gate perform correctly. In this case, we set the XPowGate to be - half the duration of the full exponent and make sure it still works. - """ - # Deprecations: cirq_google.SerializableDevice, cirq_google.SerializableGateSet, - # common serializers, and - # cirq_google.devices.known_devices.create_device_proto_from_diagram, - with cirq.testing.assert_deprecated( - 'Use cirq_google.GridDevice', - 'SerializableGateSet', - 'CircuitSerializer', - 'no longer be available', - deadline='v0.16', - count=7, - ): - half_pi_gs = cirq_google.SerializableGateSet( - gate_set_name='half_pi', - serializers=[*cgc.SINGLE_QUBIT_HALF_PI_SERIALIZERS], - deserializers=[*cgc.SINGLE_QUBIT_HALF_PI_DESERIALIZERS], - ) - durations_dict = {'xy_pi': 20_000, 'xy_half_pi': 10_000} - spec = cirq_google.devices.known_devices.create_device_proto_from_diagram( - "aa\naa", [half_pi_gs], durations_dict - ) - - # The gate set defines two different serializations for PhasedXPowGate - device = cg.SerializableDevice.from_proto(proto=spec, gate_sets=[half_pi_gs]) - q = cirq.GridQubit(0, 0) - pi = cirq.XPowGate(exponent=1.0) - half_pi = cirq.XPowGate(exponent=0.5) - assert device.duration_of(pi(q)) == cirq.Duration(picos=20_000) - assert device.duration_of(half_pi(q)) == cirq.Duration(picos=10_000) - - -def test_multiple_fsim_gatesets(): - """This tests that gate sets with two different definitions for the same - gate perform correctly. In this case, we set the XPowGate to be - half the duration of the full exponent and make sure it still works. - """ - # Deprecations: cirq_google.SerializableDevice, cirq_google.SerializableGateSet, - # common serializers, and - # cirq_google.devices.known_devices.create_device_proto_from_diagram, - with cirq.testing.assert_deprecated( - 'Use cirq_google.GridDevice', - 'SerializableGateSet', - 'CircuitSerializer', - 'no longer be available', - deadline='v0.16', - count=7, - ): - half_pi_gs = cirq_google.SerializableGateSet( - gate_set_name='half_pi', - serializers=[*cgc.SINGLE_QUBIT_HALF_PI_SERIALIZERS], - deserializers=[*cgc.SINGLE_QUBIT_HALF_PI_DESERIALIZERS], - ) - durations_dict = {'xy_pi': 20_000, 'xy_half_pi': 10_000} - spec = cirq_google.devices.known_devices.create_device_proto_from_diagram( - "aa\naa", [half_pi_gs], durations_dict - ) - - # The gate set defines two different serializations for PhasedXPowGate - device = cg.SerializableDevice.from_proto(proto=spec, gate_sets=[half_pi_gs]) - q = cirq.GridQubit(0, 0) - pi = cirq.XPowGate(exponent=1.0) - half_pi = cirq.XPowGate(exponent=0.5) - assert device.duration_of(pi(q)) == cirq.Duration(picos=20_000) - assert device.duration_of(half_pi(q)) == cirq.Duration(picos=10_000) - - -def test_serializable_device_str_grid_qubits(): - # Deprecations: cirq_google.SerializableDevice, well-known cirq_google SerializableGateSets - # (e.g. cirq_google.SYC_GATESET), and - # cirq_google.devices.known_devices.create_device_proto_from_diagram - with cirq.testing.assert_deprecated( - 'Use cirq_google.GridDevice', - 'SerializableGateSet', - 'no longer be available', - deadline='v0.16', - count=6, - ): - spec = cirq_google.devices.known_devices.create_device_proto_from_diagram( - "aa\naa", [cg.SYC_GATESET] - ) - device = cg.SerializableDevice.from_proto(proto=spec, gate_sets=[cg.SYC_GATESET]) - assert ( - str(device) - == """\ -q(0, 0)───q(0, 1) -│ │ -│ │ -q(1, 0)───q(1, 1)""" - ) - - def test_serializable_device_str_named_qubits(): with cirq.testing.assert_deprecated('Use cirq_google.GridDevice', deadline='v0.16', count=1): device = cg.SerializableDevice(