diff --git a/cirq-google/cirq_google/__init__.py b/cirq-google/cirq_google/__init__.py index 13b4633fa29..33f2bf0a106 100644 --- a/cirq-google/cirq_google/__init__.py +++ b/cirq-google/cirq_google/__init__.py @@ -160,53 +160,25 @@ _register_resolver(_class_resolver_dictionary) + +_SERIALIZABLE_GATESET_DEPRECATION_MESSAGE = ( + 'SerializableGateSet and associated classes (GateOpSerializer, GateOpDeserializer,' + ' SerializingArgs, DeserializingArgs) will no longer be supported.' + ' In cirq_google.GridDevice, the new representation of Google devices, the gateset of a device' + ' is represented as a cirq.Gateset and is available as' + ' GridDevice.metadata.gateset.' + ' Engine methods no longer require gate sets to be passed in.' + ' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.' +) + + _compat.deprecate_attributes( __name__, { - 'XMON': ( - 'v0.16', - 'SerializableGateSet will no longer be supported.' - ' In cirq_google.GridDevice, the new representation of Google devices, the gateset of ' - ' a device is represented as a cirq.Gateset and is available as' - ' GridDevice.metadata.gateset.' - ' Engine methods no longer require gate sets to be passed in.' - ' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.', - ), - 'FSIM_GATESET': ( - 'v0.16', - 'SerializableGateSet will no longer be supported.' - ' In cirq_google.GridDevice, the new representation of Google devices, the gateset of ' - ' a device is represented as a cirq.Gateset and is available as' - ' GridDevice.metadata.gateset.' - ' Engine methods no longer require gate sets to be passed in.' - ' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.', - ), - 'SQRT_ISWAP_GATESET': ( - 'v0.16', - 'SerializableGateSet will no longer be supported.' - ' In cirq_google.GridDevice, the new representation of Google devices, the gateset of ' - ' a device is represented as a cirq.Gateset and is available as' - ' GridDevice.metadata.gateset.' - ' Engine methods no longer require gate sets to be passed in.' - ' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.', - ), - 'SYC_GATESET': ( - 'v0.16', - 'SerializableGateSet will no longer be supported.' - ' In cirq_google.GridDevice, the new representation of Google devices, the gateset of ' - ' a device is represented as a cirq.Gateset and is available as' - ' GridDevice.metadata.gateset.' - ' Engine methods no longer require gate sets to be passed in.' - ' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.', - ), - 'NAMED_GATESETS': ( - 'v0.16', - 'SerializableGateSet will no longer be supported.' - ' In cirq_google.GridDevice, the new representation of Google devices, the gateset of ' - ' a device is represented as a cirq.Gateset and is available as' - ' GridDevice.metadata.gateset.' - ' Engine methods no longer require gate sets to be passed in.' - ' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.', - ), + 'XMON': ('v0.16', _SERIALIZABLE_GATESET_DEPRECATION_MESSAGE), + 'FSIM_GATESET': ('v0.16', _SERIALIZABLE_GATESET_DEPRECATION_MESSAGE), + 'SQRT_ISWAP_GATESET': ('v0.16', _SERIALIZABLE_GATESET_DEPRECATION_MESSAGE), + 'SYC_GATESET': ('v0.16', _SERIALIZABLE_GATESET_DEPRECATION_MESSAGE), + 'NAMED_GATESETS': ('v0.16', _SERIALIZABLE_GATESET_DEPRECATION_MESSAGE), }, ) diff --git a/cirq-google/cirq_google/devices/known_devices.py b/cirq-google/cirq_google/devices/known_devices.py index 2816dcd2e8c..a93d6d7bf5d 100644 --- a/cirq-google/cirq_google/devices/known_devices.py +++ b/cirq-google/cirq_google/devices/known_devices.py @@ -182,7 +182,7 @@ def create_device_proto_for_qubits( gate = gs_proto.valid_gates.add() gate.id = gate_id - if not isinstance(serializer, op_serializer.GateOpSerializer): + if not isinstance(serializer, op_serializer._GateOpSerializer): # This implies that 'serializer' handles non-gate ops, # such as CircuitOperations. No other properties apply. continue diff --git a/cirq-google/cirq_google/devices/serializable_device_test.py b/cirq-google/cirq_google/devices/serializable_device_test.py index bdaf61d1231..e6ec898f0a1 100644 --- a/cirq-google/cirq_google/devices/serializable_device_test.py +++ b/cirq-google/cirq_google/devices/serializable_device_test.py @@ -27,7 +27,11 @@ def _just_cz(): - with cirq.testing.assert_deprecated('SerializableGateSet', deadline='v0.16', count=None): + # Deprecations: cirq_google.SerializableGateSet, cirq_google.GateOpSerializer, and + # cirq_google.GateOpDeserializer + with cirq.testing.assert_deprecated( + 'SerializableGateSet', 'CircuitSerializer', deadline='v0.16', count=None + ): return cg.SerializableGateSet( gate_set_name='cz_gate_set', serializers=[ @@ -42,7 +46,11 @@ def _just_cz(): def _just_meas(): - with cirq.testing.assert_deprecated('SerializableGateSet', deadline='v0.16', count=None): + # Deprecations: cirq_google.SerializableGateSet, cirq_google.GateOpSerializer, and + # cirq_google.GateOpDeserializer + with cirq.testing.assert_deprecated( + 'SerializableGateSet', 'CircuitSerializer', deadline='v0.16', count=None + ): return cg.SerializableGateSet( gate_set_name='meas_gate_set', serializers=[ diff --git a/cirq-google/cirq_google/serialization/common_serializers.py b/cirq-google/cirq_google/serialization/common_serializers.py index cfc7ac6f3a3..e3f8fb57835 100644 --- a/cirq-google/cirq_google/serialization/common_serializers.py +++ b/cirq-google/cirq_google/serialization/common_serializers.py @@ -75,70 +75,109 @@ def _convert_physical_z(op: cirq.Operation, proto: v2.program_pb2.Operation): # Single qubit serializers for arbitrary rotations # SINGLE_QUBIT_SERIALIZERS = [ - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.PhasedXPowGate, serialized_gate_id='xy', args=[ - op_serializer.SerializingArg( - serialized_name='axis_half_turns', serialized_type=float, op_getter='phase_exponent' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='axis_half_turns', + serialized_type=float, + op_getter='phase_exponent', + ), ), - op_serializer.SerializingArg( - serialized_name='half_turns', serialized_type=float, op_getter='exponent' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='half_turns', serialized_type=float, op_getter='exponent' + ), ), ], ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.XPowGate, serialized_gate_id='xy', args=[ - op_serializer.SerializingArg( - serialized_name='axis_half_turns', serialized_type=float, op_getter=lambda op: 0.0 + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='axis_half_turns', + serialized_type=float, + op_getter=lambda op: 0.0, + ), ), - op_serializer.SerializingArg( - serialized_name='half_turns', serialized_type=float, op_getter='exponent' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='half_turns', serialized_type=float, op_getter='exponent' + ), ), ], ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.YPowGate, serialized_gate_id='xy', args=[ - op_serializer.SerializingArg( - serialized_name='axis_half_turns', serialized_type=float, op_getter=lambda op: 0.5 + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='axis_half_turns', + serialized_type=float, + op_getter=lambda op: 0.5, + ), ), - op_serializer.SerializingArg( - serialized_name='half_turns', serialized_type=float, op_getter='exponent' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='half_turns', serialized_type=float, op_getter='exponent' + ), ), ], ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.ZPowGate, serialized_gate_id='z', args=[ - op_serializer.SerializingArg( - serialized_name='half_turns', serialized_type=float, op_getter='exponent' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='half_turns', serialized_type=float, op_getter='exponent' + ), ), - op_serializer.SerializingArg( - serialized_name='type', - serialized_type=str, - op_getter=lambda op: PHYSICAL_Z if PhysicalZTag() in op.tags else VIRTUAL_Z, + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='type', + serialized_type=str, + op_getter=lambda op: PHYSICAL_Z if PhysicalZTag() in op.tags else VIRTUAL_Z, + ), ), ], ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.PhasedXZGate, serialized_gate_id='xyz', args=[ - op_serializer.SerializingArg( - serialized_name='x_exponent', serialized_type=float, op_getter='x_exponent' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='x_exponent', serialized_type=float, op_getter='x_exponent' + ), ), - op_serializer.SerializingArg( - serialized_name='z_exponent', serialized_type=float, op_getter='z_exponent' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='z_exponent', serialized_type=float, op_getter='z_exponent' + ), ), - op_serializer.SerializingArg( - serialized_name='axis_phase_exponent', - serialized_type=float, - op_getter='axis_phase_exponent', + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='axis_phase_exponent', + serialized_type=float, + op_getter='axis_phase_exponent', + ), ), ], ), @@ -148,44 +187,62 @@ def _convert_physical_z(op: cirq.Operation, proto: v2.program_pb2.Operation): # Single qubit deserializers for arbitrary rotations # SINGLE_QUBIT_DESERIALIZERS = [ - op_deserializer.GateOpDeserializer( + op_deserializer._GateOpDeserializer( serialized_gate_id='xy', gate_constructor=cirq.PhasedXPowGate, args=[ - op_deserializer.DeserializingArg( - serialized_name='axis_half_turns', - constructor_arg_name='phase_exponent', - default=0.0, + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='axis_half_turns', + constructor_arg_name='phase_exponent', + default=0.0, + ), ), - op_deserializer.DeserializingArg( - serialized_name='half_turns', constructor_arg_name='exponent', default=1.0 + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='half_turns', constructor_arg_name='exponent', default=1.0 + ), ), ], ), - op_deserializer.GateOpDeserializer( + op_deserializer._GateOpDeserializer( serialized_gate_id='z', gate_constructor=cirq.ZPowGate, args=[ - op_deserializer.DeserializingArg( - serialized_name='half_turns', constructor_arg_name='exponent', default=1.0 + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='half_turns', constructor_arg_name='exponent', default=1.0 + ), ) ], op_wrapper=lambda op, proto: _convert_physical_z(op, proto), ), - op_deserializer.GateOpDeserializer( + op_deserializer._GateOpDeserializer( serialized_gate_id='xyz', gate_constructor=cirq.PhasedXZGate, args=[ - op_deserializer.DeserializingArg( - serialized_name='x_exponent', constructor_arg_name='x_exponent', default=0.0 + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='x_exponent', constructor_arg_name='x_exponent', default=0.0 + ), ), - op_deserializer.DeserializingArg( - serialized_name='z_exponent', constructor_arg_name='z_exponent', default=0.0 + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='z_exponent', constructor_arg_name='z_exponent', default=0.0 + ), ), - op_deserializer.DeserializingArg( - serialized_name='axis_phase_exponent', - constructor_arg_name='axis_phase_exponent', - default=0.0, + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='axis_phase_exponent', + constructor_arg_name='axis_phase_exponent', + default=0.0, + ), ), ], ), @@ -195,27 +252,39 @@ def _convert_physical_z(op: cirq.Operation, proto: v2.program_pb2.Operation): # # Measurement Serializer and Deserializer # -MEASUREMENT_SERIALIZER = op_serializer.GateOpSerializer( +MEASUREMENT_SERIALIZER = op_serializer._GateOpSerializer( gate_type=cirq.MeasurementGate, serialized_gate_id='meas', args=[ - op_serializer.SerializingArg( - serialized_name='key', serialized_type=str, op_getter=cirq.measurement_key_name + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='key', serialized_type=str, op_getter=cirq.measurement_key_name + ), ), - op_serializer.SerializingArg( - serialized_name='invert_mask', serialized_type=List[bool], op_getter='invert_mask' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='invert_mask', serialized_type=List[bool], op_getter='invert_mask' + ), ), ], ) -MEASUREMENT_DESERIALIZER = op_deserializer.GateOpDeserializer( +MEASUREMENT_DESERIALIZER = op_deserializer._GateOpDeserializer( serialized_gate_id='meas', gate_constructor=cirq.MeasurementGate, args=[ - op_deserializer.DeserializingArg(serialized_name='key', constructor_arg_name='key'), - op_deserializer.DeserializingArg( - serialized_name='invert_mask', - constructor_arg_name='invert_mask', - value_func=lambda x: tuple(cast(list, x)), + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg(serialized_name='key', constructor_arg_name='key'), + ), + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='invert_mask', + constructor_arg_name='invert_mask', + value_func=lambda x: tuple(cast(list, x)), + ), ), ], num_qubits_param='num_qubits', @@ -226,72 +295,94 @@ def _convert_physical_z(op: cirq.Operation, proto: v2.program_pb2.Operation): # Serializers for single qubit rotations confined to half-pi increments # SINGLE_QUBIT_HALF_PI_SERIALIZERS = [ - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.PhasedXPowGate, serialized_gate_id='xy_pi', args=[ - op_serializer.SerializingArg( - serialized_name='axis_half_turns', serialized_type=float, op_getter='phase_exponent' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='axis_half_turns', + serialized_type=float, + op_getter='phase_exponent', + ), ) ], can_serialize_predicate=lambda op: _near_mod_2( cast(cirq.PhasedXPowGate, op.gate).exponent, 1 ), ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.XPowGate, serialized_gate_id='xy_pi', args=[ - op_serializer.SerializingArg( - serialized_name='axis_half_turns', - serialized_type=float, - op_getter=lambda op: (cast(cirq.XPowGate, op.gate).exponent - 1) / 2, + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='axis_half_turns', + serialized_type=float, + op_getter=lambda op: (cast(cirq.XPowGate, op.gate).exponent - 1) / 2, + ), ) ], can_serialize_predicate=lambda op: _near_mod_2(cast(cirq.XPowGate, op.gate).exponent, 1), ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.YPowGate, serialized_gate_id='xy_pi', args=[ - op_serializer.SerializingArg( - serialized_name='axis_half_turns', - serialized_type=float, - op_getter=lambda op: cast(cirq.YPowGate, op.gate).exponent / 2, + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='axis_half_turns', + serialized_type=float, + op_getter=lambda op: cast(cirq.YPowGate, op.gate).exponent / 2, + ), ) ], can_serialize_predicate=lambda op: _near_mod_2(cast(cirq.YPowGate, op.gate).exponent, 1), ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.XPowGate, serialized_gate_id='xy_half_pi', args=[ - op_serializer.SerializingArg( - serialized_name='axis_half_turns', - serialized_type=float, - op_getter=lambda op: cast(cirq.XPowGate, op.gate).exponent - 0.5, + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='axis_half_turns', + serialized_type=float, + op_getter=lambda op: cast(cirq.XPowGate, op.gate).exponent - 0.5, + ), ) ], can_serialize_predicate=lambda op: _near_mod_2(cast(cirq.XPowGate, op.gate).exponent, 0.5), ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.YPowGate, serialized_gate_id='xy_half_pi', args=[ - op_serializer.SerializingArg( - serialized_name='axis_half_turns', - serialized_type=float, - op_getter=lambda op: cast(cirq.YPowGate, op.gate).exponent, + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='axis_half_turns', + serialized_type=float, + op_getter=lambda op: cast(cirq.YPowGate, op.gate).exponent, + ), ) ], can_serialize_predicate=lambda op: _near_mod_2(cast(cirq.YPowGate, op.gate).exponent, 0.5), ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.PhasedXPowGate, serialized_gate_id='xy_half_pi', args=[ - op_serializer.SerializingArg( - serialized_name='axis_half_turns', serialized_type=float, op_getter='phase_exponent' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='axis_half_turns', + serialized_type=float, + op_getter='phase_exponent', + ), ) ], can_serialize_predicate=lambda op: _near_mod_2( @@ -304,31 +395,43 @@ def _convert_physical_z(op: cirq.Operation, proto: v2.program_pb2.Operation): # Deserializers for single qubit rotations confined to half-pi increments # SINGLE_QUBIT_HALF_PI_DESERIALIZERS = [ - op_deserializer.GateOpDeserializer( + op_deserializer._GateOpDeserializer( serialized_gate_id='xy_pi', gate_constructor=cirq.PhasedXPowGate, args=[ - op_deserializer.DeserializingArg( - serialized_name='axis_half_turns', constructor_arg_name='phase_exponent' + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='axis_half_turns', constructor_arg_name='phase_exponent' + ), ), - op_deserializer.DeserializingArg( - serialized_name='axis_half_turns', - constructor_arg_name='exponent', - value_func=lambda _: 1, + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='axis_half_turns', + constructor_arg_name='exponent', + value_func=lambda _: 1, + ), ), ], ), - op_deserializer.GateOpDeserializer( + op_deserializer._GateOpDeserializer( serialized_gate_id='xy_half_pi', gate_constructor=cirq.PhasedXPowGate, args=[ - op_deserializer.DeserializingArg( - serialized_name='axis_half_turns', constructor_arg_name='phase_exponent' + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='axis_half_turns', constructor_arg_name='phase_exponent' + ), ), - op_deserializer.DeserializingArg( - serialized_name='axis_half_turns', - constructor_arg_name='exponent', - value_func=lambda _: 0.5, + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='axis_half_turns', + constructor_arg_name='exponent', + value_func=lambda _: 0.5, + ), ), ], ), @@ -340,11 +443,14 @@ def _convert_physical_z(op: cirq.Operation, proto: v2.program_pb2.Operation): # ############################################# -_phase_match_arg = op_serializer.SerializingArg( - serialized_name='phase_match', - serialized_type=str, - op_getter=lambda op: PHASE_MATCH_PHYS_Z if PhysicalZTag() in op.tags else None, - required=False, +_phase_match_arg = cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='phase_match', + serialized_type=str, + op_getter=lambda op: PHASE_MATCH_PHYS_Z if PhysicalZTag() in op.tags else None, + required=False, + ), ) @@ -360,12 +466,15 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation): # # Only CZ -CZ_SERIALIZER = op_serializer.GateOpSerializer( +CZ_SERIALIZER = op_serializer._GateOpSerializer( gate_type=cirq.CZPowGate, serialized_gate_id='cz', args=[ - op_serializer.SerializingArg( - serialized_name='half_turns', serialized_type=float, op_getter='exponent' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='half_turns', serialized_type=float, op_getter='exponent' + ), ), _phase_match_arg, ], @@ -373,23 +482,29 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation): ) # CZ to any power -CZ_POW_SERIALIZER = op_serializer.GateOpSerializer( +CZ_POW_SERIALIZER = op_serializer._GateOpSerializer( gate_type=cirq.CZPowGate, serialized_gate_id='cz', args=[ - op_serializer.SerializingArg( - serialized_name='half_turns', serialized_type=float, op_getter='exponent' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='half_turns', serialized_type=float, op_getter='exponent' + ), ), _phase_match_arg, ], ) -CZ_POW_DESERIALIZER = op_deserializer.GateOpDeserializer( +CZ_POW_DESERIALIZER = op_deserializer._GateOpDeserializer( serialized_gate_id='cz', gate_constructor=cirq.CZPowGate, args=[ - op_deserializer.DeserializingArg( - serialized_name='half_turns', constructor_arg_name='exponent', default=1.0 + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='half_turns', constructor_arg_name='exponent', default=1.0 + ), ) ], op_wrapper=lambda op, proto: _add_phase_match(op, proto), @@ -398,7 +513,7 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation): # # Sycamore Gate Serializer and deserializer # -SYC_SERIALIZER = op_serializer.GateOpSerializer( +SYC_SERIALIZER = op_serializer._GateOpSerializer( gate_type=cirq.FSimGate, serialized_gate_id='syc', args=[_phase_match_arg], @@ -408,7 +523,7 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation): ), ) -SYC_DESERIALIZER = op_deserializer.GateOpDeserializer( +SYC_DESERIALIZER = op_deserializer._GateOpDeserializer( serialized_gate_id='syc', gate_constructor=lambda: cirq.FSimGate(theta=np.pi / 2, phi=np.pi / 6), args=[], @@ -420,7 +535,7 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation): # (e.g. ISWAP ** 0.5) # SQRT_ISWAP_SERIALIZERS = [ - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.FSimGate, serialized_gate_id='fsim_pi_4', args=[_phase_match_arg], @@ -429,7 +544,7 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation): and _near_mod_2pi(cast(cirq.FSimGate, op.gate).phi, 0) ), ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.ISwapPowGate, serialized_gate_id='fsim_pi_4', args=[_phase_match_arg], @@ -437,7 +552,7 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation): lambda op: _near_mod_n(cast(cirq.ISwapPowGate, op.gate).exponent, -0.5, 4) ), ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.FSimGate, serialized_gate_id='inv_fsim_pi_4', args=[_phase_match_arg], @@ -446,7 +561,7 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation): and _near_mod_2pi(cast(cirq.FSimGate, op.gate).phi, 0) ), ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.ISwapPowGate, serialized_gate_id='inv_fsim_pi_4', args=[_phase_match_arg], @@ -457,13 +572,13 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation): ] SQRT_ISWAP_DESERIALIZERS = [ - op_deserializer.GateOpDeserializer( + op_deserializer._GateOpDeserializer( serialized_gate_id='fsim_pi_4', gate_constructor=lambda: cirq.FSimGate(theta=np.pi / 4, phi=0), args=[], op_wrapper=lambda op, proto: _add_phase_match(op, proto), ), - op_deserializer.GateOpDeserializer( + op_deserializer._GateOpDeserializer( serialized_gate_id='inv_fsim_pi_4', gate_constructor=lambda: cirq.FSimGate(theta=-np.pi / 4, phi=0), args=[], @@ -496,46 +611,64 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation): allow_symbols=True, ) LIMITED_FSIM_SERIALIZERS = [ - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.FSimGate, serialized_gate_id='fsim', args=[ - op_serializer.SerializingArg( - serialized_name='theta', serialized_type=float, op_getter='theta' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='theta', serialized_type=float, op_getter='theta' + ), ), - op_serializer.SerializingArg( - serialized_name='phi', serialized_type=float, op_getter='phi' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='phi', serialized_type=float, op_getter='phi' + ), ), _phase_match_arg, ], can_serialize_predicate=(lambda op: op in _LIMITED_FSIM_GATE_FAMILY), ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.ISwapPowGate, serialized_gate_id='fsim', args=[ - op_serializer.SerializingArg( - serialized_name='theta', - serialized_type=float, - # Note that ISWAP ** 0.5 is Fsim(-pi/4,0) - op_getter=(lambda op: cast(cirq.ISwapPowGate, op.gate).exponent * -np.pi / 2), + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='theta', + serialized_type=float, + # Note that ISWAP ** 0.5 is Fsim(-pi/4,0) + op_getter=(lambda op: cast(cirq.ISwapPowGate, op.gate).exponent * -np.pi / 2), + ), ), - op_serializer.SerializingArg( - serialized_name='phi', serialized_type=float, op_getter=lambda e: 0 + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='phi', serialized_type=float, op_getter=lambda e: 0 + ), ), _phase_match_arg, ], can_serialize_predicate=(lambda op: op in _LIMITED_ISWAP_GATE_FAMILY), ), - op_serializer.GateOpSerializer( + op_serializer._GateOpSerializer( gate_type=cirq.CZPowGate, serialized_gate_id='fsim', args=[ - op_serializer.SerializingArg( - serialized_name='theta', serialized_type=float, op_getter=lambda e: 0 + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='theta', serialized_type=float, op_getter=lambda e: 0 + ), ), - op_serializer.SerializingArg( - serialized_name='phi', serialized_type=float, op_getter=lambda e: np.pi + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='phi', serialized_type=float, op_getter=lambda e: np.pi + ), ), _phase_match_arg, ], @@ -544,15 +677,21 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation): ] -LIMITED_FSIM_DESERIALIZER = op_deserializer.GateOpDeserializer( +LIMITED_FSIM_DESERIALIZER = op_deserializer._GateOpDeserializer( serialized_gate_id='fsim', gate_constructor=cirq.FSimGate, args=[ - op_deserializer.DeserializingArg( - serialized_name='theta', constructor_arg_name='theta', default=0.0 + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='theta', constructor_arg_name='theta', default=0.0 + ), ), - op_deserializer.DeserializingArg( - serialized_name='phi', constructor_arg_name='phi', default=0.0 + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='phi', constructor_arg_name='phi', default=0.0 + ), ), ], op_wrapper=lambda op, proto: _add_phase_match(op, proto), @@ -568,56 +707,80 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation): # Coupler Pulse serializer and deserializer # -COUPLER_PULSE_SERIALIZER = op_serializer.GateOpSerializer( +COUPLER_PULSE_SERIALIZER = op_serializer._GateOpSerializer( gate_type=CouplerPulse, serialized_gate_id='coupler_pulse', args=[ - op_serializer.SerializingArg( - serialized_name='coupling_mhz', serialized_type=float, op_getter='coupling_mhz' + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='coupling_mhz', serialized_type=float, op_getter='coupling_mhz' + ), ), - op_serializer.SerializingArg( - serialized_name='hold_time_ns', - serialized_type=float, - op_getter=lambda op: cast(CouplerPulse, op.gate).hold_time.total_nanos(), + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='hold_time_ns', + serialized_type=float, + op_getter=lambda op: cast(CouplerPulse, op.gate).hold_time.total_nanos(), + ), ), - op_serializer.SerializingArg( - serialized_name='rise_time_ns', - serialized_type=float, - op_getter=lambda op: cast(CouplerPulse, op.gate).rise_time.total_nanos(), + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='rise_time_ns', + serialized_type=float, + op_getter=lambda op: cast(CouplerPulse, op.gate).rise_time.total_nanos(), + ), ), - op_serializer.SerializingArg( - serialized_name='padding_time_ns', - serialized_type=float, - op_getter=lambda op: cast(CouplerPulse, op.gate).padding_time.total_nanos(), + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='padding_time_ns', + serialized_type=float, + op_getter=lambda op: cast(CouplerPulse, op.gate).padding_time.total_nanos(), + ), ), ], ) -COUPLER_PULSE_DESERIALIZER = op_deserializer.GateOpDeserializer( +COUPLER_PULSE_DESERIALIZER = op_deserializer._GateOpDeserializer( serialized_gate_id='coupler_pulse', gate_constructor=CouplerPulse, args=[ - op_deserializer.DeserializingArg( - serialized_name='coupling_mhz', constructor_arg_name='coupling_mhz' + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='coupling_mhz', constructor_arg_name='coupling_mhz' + ), ), - op_deserializer.DeserializingArg( - serialized_name='hold_time_ns', - constructor_arg_name='hold_time', - value_func=lambda nanos: cirq.Duration( - nanos=cast(Union[int, float, sympy.Expr], nanos) + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='hold_time_ns', + constructor_arg_name='hold_time', + value_func=lambda nanos: cirq.Duration( + nanos=cast(Union[int, float, sympy.Expr], nanos) + ), ), ), - op_deserializer.DeserializingArg( - serialized_name='rise_time_ns', - constructor_arg_name='rise_time', - value_func=lambda nanos: cirq.Duration( - nanos=cast(Union[int, float, sympy.Expr], nanos) + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='rise_time_ns', + constructor_arg_name='rise_time', + value_func=lambda nanos: cirq.Duration( + nanos=cast(Union[int, float, sympy.Expr], nanos) + ), ), ), - op_deserializer.DeserializingArg( - serialized_name='padding_time_ns', - constructor_arg_name='padding_time', - value_func=lambda nanos: cirq.Duration( - nanos=cast(Union[int, float, sympy.Expr], nanos) + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='padding_time_ns', + constructor_arg_name='padding_time', + value_func=lambda nanos: cirq.Duration( + nanos=cast(Union[int, float, sympy.Expr], nanos) + ), ), ), ], @@ -626,26 +789,32 @@ def _add_phase_match(op: cirq.Operation, proto: v2.program_pb2.Operation): # # WaitGate serializer and deserializer # -WAIT_GATE_SERIALIZER = op_serializer.GateOpSerializer( +WAIT_GATE_SERIALIZER = op_serializer._GateOpSerializer( gate_type=cirq.WaitGate, serialized_gate_id='wait', args=[ - op_serializer.SerializingArg( - serialized_name='nanos', - serialized_type=float, - op_getter=lambda op: cast(cirq.WaitGate, op.gate).duration.total_nanos(), + cast( + op_serializer.SerializingArg, + op_serializer._SerializingArg( + serialized_name='nanos', + serialized_type=float, + op_getter=lambda op: cast(cirq.WaitGate, op.gate).duration.total_nanos(), + ), ) ], ) -WAIT_GATE_DESERIALIZER = op_deserializer.GateOpDeserializer( +WAIT_GATE_DESERIALIZER = op_deserializer._GateOpDeserializer( serialized_gate_id='wait', gate_constructor=cirq.WaitGate, args=[ - op_deserializer.DeserializingArg( - serialized_name='nanos', - constructor_arg_name='duration', - value_func=lambda nanos: cirq.Duration( - nanos=cast(Union[int, float, sympy.Expr], nanos) + cast( + op_deserializer.DeserializingArg, + op_deserializer._DeserializingArg( + serialized_name='nanos', + constructor_arg_name='duration', + value_func=lambda nanos: cirq.Duration( + nanos=cast(Union[int, float, sympy.Expr], nanos) + ), ), ) ], diff --git a/cirq-google/cirq_google/serialization/gate_sets.py b/cirq-google/cirq_google/serialization/gate_sets.py index f1c55c0a1c3..14b89a1bb38 100644 --- a/cirq-google/cirq_google/serialization/gate_sets.py +++ b/cirq-google/cirq_google/serialization/gate_sets.py @@ -168,26 +168,21 @@ document(GOOGLE_GATESETS, """All Google gatesets""") +_SERIALIZABLE_GATESET_DEPRECATION_MESSAGE = ( + 'SerializableGateSet and associated classes (GateOpSerializer, GateOpDeserializer,' + ' SerializingArgs, DeserializingArgs) will no longer be supported.' + ' In cirq_google.GridDevice, the new representation of Google devices, the gateset of a device' + ' is represented as a cirq.Gateset and is available as' + ' GridDevice.metadata.gateset.' + ' Engine methods no longer require gate sets to be passed in.' + ' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.' +) + + _compat.deprecate_attributes( __name__, { - 'EXPERIMENTAL_PULSE_GATESET': ( - 'v0.16', - 'SerializableGateSet will no longer be supported.' - ' In cirq_google.GridDevice, the new representation of Google devices, the gateset of ' - ' a device is represented as a cirq.Gateset and is available as' - ' GridDevice.metadata.gateset.' - ' Engine methods no longer require gate sets to be passed in.' - ' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.', - ), - 'GOOGLE_GATESETS': ( - 'v0.16', - 'SerializableGateSet will no longer be supported.' - ' In cirq_google.GridDevice, the new representation of Google devices, the gateset of ' - ' a device is represented as a cirq.Gateset and is available as' - ' GridDevice.metadata.gateset.' - ' Engine methods no longer require gate sets to be passed in.' - ' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.', - ), + 'EXPERIMENTAL_PULSE_GATESET': ('v0.16', _SERIALIZABLE_GATESET_DEPRECATION_MESSAGE), + 'GOOGLE_GATESETS': ('v0.16', _SERIALIZABLE_GATESET_DEPRECATION_MESSAGE), }, ) diff --git a/cirq-google/cirq_google/serialization/op_deserializer.py b/cirq-google/cirq_google/serialization/op_deserializer.py index 3768acff13c..0623b2442f6 100644 --- a/cirq-google/cirq_google/serialization/op_deserializer.py +++ b/cirq-google/cirq_google/serialization/op_deserializer.py @@ -66,7 +66,27 @@ def from_proto( @dataclass(frozen=True) -class DeserializingArg: +class _DeserializingArg: + """HACK: non-deprecated version of DeserializingArg. + + This is used by global gatesets to bypass the behavior that deprecation warnings thrown + during module loading fail unit tests. + """ + + serialized_name: str + constructor_arg_name: str + value_func: Optional[Callable[[arg_func_langs.ARG_LIKE], Any]] = None + required: bool = True + default: Any = None + + +@cirq._compat.deprecated_class( + deadline='v0.16', + fix='Will no longer be used because GateOpDeserializer is deprecated.' + ' CircuitSerializer will be the only supported circuit serializer going forward.', +) +@dataclass(frozen=True) +class DeserializingArg(_DeserializingArg): """Specification of the arguments to deserialize an argument to a gate. Args: @@ -84,18 +104,12 @@ class DeserializingArg: arg. If set, required is ignored. """ - serialized_name: str - constructor_arg_name: str - value_func: Optional[Callable[[arg_func_langs.ARG_LIKE], Any]] = None - required: bool = True - default: Any = None +class _GateOpDeserializer(OpDeserializer): + """HACK: non-deprecated version of GateOpDeserializer. -class GateOpDeserializer(OpDeserializer): - """Describes how to deserialize a proto to a given Gate type. - - Attributes: - serialized_gate_id: The id used when serializing the gate. + This is used by global gatesets to bypass the behavior that deprecation warnings thrown + during module loading fail unit tests. """ def __init__( @@ -109,25 +123,6 @@ def __init__( ] = lambda x, y: x, deserialize_tokens: Optional[bool] = True, ): - """Constructs a deserializer. - - Args: - serialized_gate_id: The serialized id of the gate that is being - deserialized. - gate_constructor: A function that produces the deserialized gate - given arguments from args. - args: A list of the arguments to be read from the serialized - gate and the information required to use this to construct - the gate using the gate_constructor above. - num_qubits_param: Some gate constructors require that the number - of qubits be passed to their constructor. This is the name - of the parameter in the constructor for this value. If None, - no number of qubits is passed to the constructor. - op_wrapper: An optional Callable to modify the resulting - GateOperation, for instance, to add tags - deserialize_tokens: Whether to convert tokens to - CalibrationTags. Defaults to True. - """ self._serialized_gate_id = serialized_gate_id self._gate_constructor = gate_constructor self._args = args @@ -147,23 +142,6 @@ def from_proto( constants: List[v2.program_pb2.Constant] = None, deserialized_constants: List[Any] = None, # unused ) -> cirq.Operation: - """Turns a cirq_google.api.v2.Operation proto into a GateOperation. - - Args: - proto: The proto object to be deserialized. - arg_function_language: The `arg_function_language` field from - `Program.Language`. - constants: The list of Constant protos referenced by constant - table indices in `proto`. - deserialized_constants: Unused in this method. - - Returns: - The deserialized GateOperation represented by `proto`. - - Raises: - ValueError: If the proto references a missing constants table, or a required arg is - missing. - """ qubits = [v2.qubit_from_proto_id(q.id) for q in proto.qubits] args = self._args_from_proto(proto, arg_function_language=arg_function_language) if self._num_qubits_param is not None: @@ -215,6 +193,90 @@ def _args_from_proto( return return_args +@cirq._compat.deprecated_class( + deadline='v0.16', + fix='Will no longer be supported.' + ' CircuitSerializer will be the only supported circuit serializer going forward.', +) +class GateOpDeserializer(_GateOpDeserializer): + """Describes how to deserialize a proto to a given Gate type. + + Attributes: + serialized_gate_id: The id used when serializing the gate. + """ + + def __init__( + self, + serialized_gate_id: str, + gate_constructor: Callable, + args: Sequence[DeserializingArg], + num_qubits_param: Optional[str] = None, + op_wrapper: Callable[ + [cirq.Operation, v2.program_pb2.Operation], cirq.Operation + ] = lambda x, y: x, + deserialize_tokens: Optional[bool] = True, + ): + """Constructs a deserializer. + + Args: + serialized_gate_id: The serialized id of the gate that is being + deserialized. + gate_constructor: A function that produces the deserialized gate + given arguments from args. + args: A list of the arguments to be read from the serialized + gate and the information required to use this to construct + the gate using the gate_constructor above. + num_qubits_param: Some gate constructors require that the number + of qubits be passed to their constructor. This is the name + of the parameter in the constructor for this value. If None, + no number of qubits is passed to the constructor. + op_wrapper: An optional Callable to modify the resulting + GateOperation, for instance, to add tags + deserialize_tokens: Whether to convert tokens to + CalibrationTags. Defaults to True. + """ + super().__init__( + serialized_gate_id, + gate_constructor, + args, + num_qubits_param, + op_wrapper, + deserialize_tokens, + ) + + def from_proto( + self, + proto: v2.program_pb2.Operation, + *, + arg_function_language: str = '', + constants: List[v2.program_pb2.Constant] = None, + deserialized_constants: List[Any] = None, # unused + ) -> cirq.Operation: + """Turns a cirq_google.api.v2.Operation proto into a GateOperation. + + Args: + proto: The proto object to be deserialized. + arg_function_language: The `arg_function_language` field from + `Program.Language`. + constants: The list of Constant protos referenced by constant + table indices in `proto`. + deserialized_constants: Unused in this method. + + Returns: + The deserialized GateOperation represented by `proto`. + + Raises: + ValueError: If the proto references a missing constants table, or a required arg is + missing. + """ + return super().from_proto( + proto, + arg_function_language=arg_function_language, + constants=constants, + deserialized_constants=deserialized_constants, + ) + + class CircuitOpDeserializer(OpDeserializer): """Describes how to serialize CircuitOperations.""" diff --git a/cirq-google/cirq_google/serialization/op_deserializer_test.py b/cirq-google/cirq_google/serialization/op_deserializer_test.py index b9ca8206c15..36ed39be650 100644 --- a/cirq-google/cirq_google/serialization/op_deserializer_test.py +++ b/cirq-google/cirq_google/serialization/op_deserializer_test.py @@ -43,11 +43,33 @@ def _value_equality_values_(self): def base_deserializer(): - return cg.GateOpDeserializer( - serialized_gate_id='my_gate', - gate_constructor=GateWithAttribute, - args=[cg.DeserializingArg(serialized_name='my_val', constructor_arg_name='val')], - ) + # Deprecated: cirq_google.GateOpDeserializer and cirq_google.DeserializingArg. + with cirq.testing.assert_deprecated('CircuitSerializer', deadline='v0.16', count=2): + return cg.GateOpDeserializer( + serialized_gate_id='my_gate', + gate_constructor=GateWithAttribute, + args=[cg.DeserializingArg(serialized_name='my_val', constructor_arg_name='val')], + ) + + +def _create_gate_op_deserializer(*, serialized_gate_id, gate_constructor, args): + with cirq.testing.assert_deprecated('CircuitSerializer', deadline='v0.16', count=1): + return cg.GateOpDeserializer( + serialized_gate_id=serialized_gate_id, gate_constructor=gate_constructor, args=args + ) + + +def _create_deserializing_arg( + *, serialized_name, constructor_arg_name, value_func=None, required=True, default=None +): + with cirq.testing.assert_deprecated('CircuitSerializer', deadline='v0.16', count=1): + return cg.DeserializingArg( + serialized_name=serialized_name, + constructor_arg_name=constructor_arg_name, + value_func=value_func, + required=required, + default=default, + ) TEST_CASES = [ @@ -148,11 +170,11 @@ def test_from_proto_function_argument_not_set(): def test_from_proto_value_func(): - deserializer = cg.GateOpDeserializer( + deserializer = _create_gate_op_deserializer( serialized_gate_id='my_gate', gate_constructor=GateWithAttribute, args=[ - cg.DeserializingArg( + _create_deserializing_arg( serialized_name='my_val', constructor_arg_name='val', value_func=lambda x: x + 1 ) ], @@ -170,12 +192,12 @@ def test_from_proto_value_func(): def test_from_proto_not_required_ok(): - deserializer = cg.GateOpDeserializer( + deserializer = _create_gate_op_deserializer( serialized_gate_id='my_gate', gate_constructor=GateWithAttribute, args=[ - cg.DeserializingArg(serialized_name='my_val', constructor_arg_name='val'), - cg.DeserializingArg( + _create_deserializing_arg(serialized_name='my_val', constructor_arg_name='val'), + _create_deserializing_arg( serialized_name='not_req', constructor_arg_name='not_req', required=False ), ], @@ -193,12 +215,12 @@ def test_from_proto_not_required_ok(): def test_from_proto_missing_required_arg(): - deserializer = cg.GateOpDeserializer( + deserializer = _create_gate_op_deserializer( serialized_gate_id='my_gate', gate_constructor=GateWithAttribute, args=[ - cg.DeserializingArg(serialized_name='my_val', constructor_arg_name='val'), - cg.DeserializingArg( + _create_deserializing_arg(serialized_name='my_val', constructor_arg_name='val'), + _create_deserializing_arg( serialized_name='not_req', constructor_arg_name='not_req', required=False ), ], @@ -215,12 +237,12 @@ def test_from_proto_missing_required_arg(): def test_from_proto_required_arg_not_assigned(): - deserializer = cg.GateOpDeserializer( + deserializer = _create_gate_op_deserializer( serialized_gate_id='my_gate', gate_constructor=GateWithAttribute, args=[ - cg.DeserializingArg(serialized_name='my_val', constructor_arg_name='val'), - cg.DeserializingArg( + _create_deserializing_arg(serialized_name='my_val', constructor_arg_name='val'), + _create_deserializing_arg( serialized_name='not_req', constructor_arg_name='not_req', required=False ), ], @@ -233,12 +255,14 @@ def test_from_proto_required_arg_not_assigned(): def test_defaults(): - deserializer = cg.GateOpDeserializer( + deserializer = _create_gate_op_deserializer( serialized_gate_id='my_gate', gate_constructor=GateWithAttribute, args=[ - cg.DeserializingArg(serialized_name='my_val', constructor_arg_name='val', default=1.0), - cg.DeserializingArg( + _create_deserializing_arg( + serialized_name='my_val', constructor_arg_name='val', default=1.0 + ), + _create_deserializing_arg( serialized_name='not_req', constructor_arg_name='not_req', default='hello', diff --git a/cirq-google/cirq_google/serialization/op_serializer.py b/cirq-google/cirq_google/serialization/op_serializer.py index 76e80fecc5c..bf838f4434b 100644 --- a/cirq-google/cirq_google/serialization/op_serializer.py +++ b/cirq-google/cirq_google/serialization/op_serializer.py @@ -99,7 +99,27 @@ def can_serialize_operation(self, op: cirq.Operation) -> bool: @dataclass(frozen=True) -class SerializingArg: +class _SerializingArg: + """HACK: non-deprecated version of SerializingArg. + + This is used by global gatesets to bypass the behavior that deprecation warnings thrown + during module loading fail unit tests. + """ + + serialized_name: str + serialized_type: Type[ARG_LIKE] + op_getter: Union[str, Callable[[cirq.Operation], Optional[ARG_LIKE]]] + required: bool = True + default: Any = None + + +@cirq._compat.deprecated_class( + deadline='v0.16', + fix='Will no longer be used because GateOpSerializer is deprecated.' + ' CircuitSerializer will be the only supported circuit serializer going forward.', +) +@dataclass(frozen=True) +class SerializingArg(_SerializingArg): """Specification of the arguments for a Gate and its serialization. Args: @@ -116,21 +136,12 @@ class SerializingArg: Note that the DeserializingArg must also have this as default. """ - serialized_name: str - serialized_type: Type[ARG_LIKE] - op_getter: Union[str, Callable[[cirq.Operation], Optional[ARG_LIKE]]] - required: bool = True - default: Any = None - -class GateOpSerializer(OpSerializer): - """Describes how to serialize a GateOperation for a given Gate type. +class _GateOpSerializer(OpSerializer): + """HACK: non-deprecated version of GateOpSerializer. - Attributes: - gate_type: The type of the gate that can be serialized. - serialized_gate_id: The id used when serializing the gate. - serialize_tokens: Whether to convert CalibrationTags into tokens - on the Operation proto. Defaults to True. + This is used by global gatesets to bypass the behavior that deprecation warnings thrown + during module loading fail unit tests. """ def __init__( @@ -142,22 +153,6 @@ def __init__( can_serialize_predicate: Callable[[cirq.Operation], bool] = lambda x: True, serialize_tokens: Optional[bool] = True, ): - """Construct the serializer. - - Args: - gate_type: The type of the gate that is being serialized. - serialized_gate_id: The string id of the gate when serialized. - args: A list of specification of the arguments to the gate when - serializing, including how to get this information from the - gate of the given gate type. - can_serialize_predicate: Sometimes an Operation can only be - serialized for particular parameters. This predicate will be - checked before attempting to serialize the Operation. If the - predicate is False, serialization will result in a None value. - Default value is a lambda that always returns True. - serialize_tokens: Whether to convert calibration tags into tokens - on the Operation proto. - """ self._gate_type = gate_type self._serialized_gate_id = serialized_gate_id self._args = args @@ -181,12 +176,6 @@ def can_serialize_predicate(self): return self._can_serialize_predicate def can_serialize_operation(self, op: cirq.Operation) -> bool: - """Whether the given operation can be serialized by this serializer. - - This checks that the gate is a subclass of the gate type for this - serializer, and that the gate returns true for - `can_serializer_predicate` called on the gate. - """ supported_gate_type = self._gate_type in type(op.gate).mro() return supported_gate_type and super().can_serialize_operation(op) @@ -199,11 +188,6 @@ def to_proto( constants: List[v2.program_pb2.Constant] = None, raw_constants: Dict[Any, int] = None, ) -> Optional[v2.program_pb2.Operation]: - """Returns the cirq_google.api.v2.Operation message as a proto dict. - - Note that this function may modify the constant list if it adds - tokens to the circuit's constant table. - """ gate = op.gate if not isinstance(gate, self.internal_type): @@ -289,6 +273,86 @@ def _check_type(self, value: ARG_LIKE, arg: SerializingArg) -> None: ) +@cirq._compat.deprecated_class( + deadline='v0.16', + fix='Will no longer be supported.' + ' CircuitSerializer will be the only supported circuit serializer going forward.', +) +class GateOpSerializer(_GateOpSerializer): + """Describes how to serialize a GateOperation for a given Gate type. + + Attributes: + gate_type: The type of the gate that can be serialized. + serialized_gate_id: The id used when serializing the gate. + serialize_tokens: Whether to convert CalibrationTags into tokens + on the Operation proto. Defaults to True. + """ + + def __init__( + self, + *, + gate_type: Type[Gate], + serialized_gate_id: str, + args: List[SerializingArg], + can_serialize_predicate: Callable[[cirq.Operation], bool] = lambda x: True, + serialize_tokens: Optional[bool] = True, + ): + """Construct the serializer. + + Args: + gate_type: The type of the gate that is being serialized. + serialized_gate_id: The string id of the gate when serialized. + args: A list of specification of the arguments to the gate when + serializing, including how to get this information from the + gate of the given gate type. + can_serialize_predicate: Sometimes an Operation can only be + serialized for particular parameters. This predicate will be + checked before attempting to serialize the Operation. If the + predicate is False, serialization will result in a None value. + Default value is a lambda that always returns True. + serialize_tokens: Whether to convert calibration tags into tokens + on the Operation proto. + """ + super().__init__( + gate_type=gate_type, + serialized_gate_id=serialized_gate_id, + args=args, + can_serialize_predicate=can_serialize_predicate, + serialize_tokens=serialize_tokens, + ) + + def can_serialize_operation(self, op: cirq.Operation) -> bool: + """Whether the given operation can be serialized by this serializer. + + This checks that the gate is a subclass of the gate type for this + serializer, and that the gate returns true for + `can_serializer_predicate` called on the gate. + """ + return super().can_serialize_operation(op) + + def to_proto( + self, + op: cirq.Operation, + msg: Optional[v2.program_pb2.Operation] = None, + *, + arg_function_language: Optional[str] = '', + constants: List[v2.program_pb2.Constant] = None, + raw_constants: Dict[Any, int] = None, + ) -> Optional[v2.program_pb2.Operation]: + """Returns the cirq_google.api.v2.Operation message as a proto dict. + + Note that this function may modify the constant list if it adds + tokens to the circuit's constant table. + """ + return super().to_proto( + op, + msg, + arg_function_language=arg_function_language, + constants=constants, + raw_constants=raw_constants, + ) + + class CircuitOpSerializer(OpSerializer): """Describes how to serialize CircuitOperations.""" diff --git a/cirq-google/cirq_google/serialization/op_serializer_test.py b/cirq-google/cirq_google/serialization/op_serializer_test.py index 845fb1f701c..1f3b0bc2f06 100644 --- a/cirq-google/cirq_google/serialization/op_serializer_test.py +++ b/cirq-google/cirq_google/serialization/op_serializer_test.py @@ -29,6 +29,37 @@ DEFAULT_TOKEN = 'test_tag' +def _create_gate_op_serializer( + *, + gate_type, + serialized_gate_id, + args, + can_serialize_predicate=lambda x: True, + serialize_tokens=True, +): + with cirq.testing.assert_deprecated('CircuitSerializer', deadline='v0.16', count=1): + return cg.GateOpSerializer( + gate_type=gate_type, + serialized_gate_id=serialized_gate_id, + args=args, + can_serialize_predicate=can_serialize_predicate, + serialize_tokens=serialize_tokens, + ) + + +def _create_serializing_arg( + *, serialized_name, serialized_type, op_getter, required=True, default=None +): + with cirq.testing.assert_deprecated('CircuitSerializer', deadline='v0.16', count=1): + return cg.SerializingArg( + serialized_name=serialized_name, + serialized_type=serialized_type, + op_getter=op_getter, + required=required, + default=default, + ) + + def op_proto(json: Dict) -> v2.program_pb2.Operation: op = v2.program_pb2.Operation() json_format.ParseDict(json, op) @@ -105,11 +136,13 @@ def get_val(op): @pytest.mark.parametrize(('val_type', 'val', 'arg_value'), TEST_CASES) def test_to_proto_attribute(val_type, val, arg_value): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithAttribute, serialized_gate_id='my_gate', args=[ - cg.SerializingArg(serialized_name='my_val', serialized_type=val_type, op_getter='val') + _create_serializing_arg( + serialized_name='my_val', serialized_type=val_type, op_getter='val' + ) ], ) q = cirq.GridQubit(1, 2) @@ -122,11 +155,13 @@ def test_to_proto_attribute(val_type, val, arg_value): @pytest.mark.parametrize(('val_type', 'val', 'arg_value'), TEST_CASES) def test_to_proto_property(val_type, val, arg_value): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithProperty, serialized_gate_id='my_gate', args=[ - cg.SerializingArg(serialized_name='my_val', serialized_type=val_type, op_getter='val') + _create_serializing_arg( + serialized_name='my_val', serialized_type=val_type, op_getter='val' + ) ], ) q = cirq.GridQubit(1, 2) @@ -139,11 +174,13 @@ def test_to_proto_property(val_type, val, arg_value): @pytest.mark.parametrize(('val_type', 'val', 'arg_value'), TEST_CASES) def test_to_proto_callable(val_type, val, arg_value): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithMethod, serialized_gate_id='my_gate', args=[ - cg.SerializingArg(serialized_name='my_val', serialized_type=val_type, op_getter=get_val) + _create_serializing_arg( + serialized_name='my_val', serialized_type=val_type, op_getter=get_val + ) ], ) q = cirq.GridQubit(1, 2) @@ -155,10 +192,14 @@ def test_to_proto_callable(val_type, val, arg_value): def test_to_proto_gate_predicate(): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithAttribute, serialized_gate_id='my_gate', - args=[cg.SerializingArg(serialized_name='my_val', serialized_type=float, op_getter='val')], + args=[ + _create_serializing_arg( + serialized_name='my_val', serialized_type=float, op_getter='val' + ) + ], can_serialize_predicate=lambda x: x.gate.val == 1, ) q = cirq.GridQubit(1, 2) @@ -169,10 +210,14 @@ def test_to_proto_gate_predicate(): def test_to_proto_gate_mismatch(): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithProperty, serialized_gate_id='my_gate', - args=[cg.SerializingArg(serialized_name='my_val', serialized_type=float, op_getter='val')], + args=[ + _create_serializing_arg( + serialized_name='my_val', serialized_type=float, op_getter='val' + ) + ], ) q = cirq.GridQubit(1, 2) with pytest.raises(ValueError, match='GateWithAttribute.*GateWithProperty'): @@ -180,10 +225,14 @@ def test_to_proto_gate_mismatch(): def test_to_proto_unsupported_type(): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithProperty, serialized_gate_id='my_gate', - args=[cg.SerializingArg(serialized_name='my_val', serialized_type=bytes, op_getter='val')], + args=[ + _create_serializing_arg( + serialized_name='my_val', serialized_type=bytes, op_getter='val' + ) + ], ) q = cirq.GridQubit(1, 2) with pytest.raises(ValueError, match='bytes'): @@ -191,10 +240,14 @@ def test_to_proto_unsupported_type(): def test_to_proto_named_qubit_supported(): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithProperty, serialized_gate_id='my_gate', - args=[cg.SerializingArg(serialized_name='my_val', serialized_type=float, op_getter='val')], + args=[ + _create_serializing_arg( + serialized_name='my_val', serialized_type=float, op_getter='val' + ) + ], ) q = cirq.NamedQubit('a') arg_value = 1.0 @@ -211,10 +264,14 @@ def test_to_proto_named_qubit_supported(): def test_to_proto_line_qubit_supported(): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithProperty, serialized_gate_id='my_gate', - args=[cg.SerializingArg(serialized_name='my_val', serialized_type=float, op_getter='val')], + args=[ + _create_serializing_arg( + serialized_name='my_val', serialized_type=float, op_getter='val' + ) + ], ) q = cirq.LineQubit('10') arg_value = 1.0 @@ -231,11 +288,11 @@ def test_to_proto_line_qubit_supported(): def test_to_proto_required_but_not_present(): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithProperty, serialized_gate_id='my_gate', args=[ - cg.SerializingArg( + _create_serializing_arg( serialized_name='my_val', serialized_type=float, op_getter=lambda x: None ) ], @@ -246,10 +303,14 @@ def test_to_proto_required_but_not_present(): def test_to_proto_no_getattr(): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithProperty, serialized_gate_id='my_gate', - args=[cg.SerializingArg(serialized_name='my_val', serialized_type=float, op_getter='nope')], + args=[ + _create_serializing_arg( + serialized_name='my_val', serialized_type=float, op_getter='nope' + ) + ], ) q = cirq.GridQubit(1, 2) with pytest.raises(ValueError, match='does not have'): @@ -257,12 +318,14 @@ def test_to_proto_no_getattr(): def test_to_proto_not_required_ok(): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithProperty, serialized_gate_id='my_gate', args=[ - cg.SerializingArg(serialized_name='my_val', serialized_type=float, op_getter='val'), - cg.SerializingArg( + _create_serializing_arg( + serialized_name='my_val', serialized_type=float, op_getter='val' + ), + _create_serializing_arg( serialized_name='not_req', serialized_type=float, op_getter='not_req', @@ -294,11 +357,13 @@ def test_to_proto_not_required_ok(): ), ) def test_to_proto_type_mismatch(val_type, val): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithProperty, serialized_gate_id='my_gate', args=[ - cg.SerializingArg(serialized_name='my_val', serialized_type=val_type, op_getter='val') + _create_serializing_arg( + serialized_name='my_val', serialized_type=val_type, op_getter='val' + ) ], ) q = cirq.GridQubit(1, 2) @@ -307,10 +372,14 @@ def test_to_proto_type_mismatch(val_type, val): def test_can_serialize_operation_subclass(): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithAttribute, serialized_gate_id='my_gate', - args=[cg.SerializingArg(serialized_name='my_val', serialized_type=float, op_getter='val')], + args=[ + _create_serializing_arg( + serialized_name='my_val', serialized_type=float, op_getter='val' + ) + ], can_serialize_predicate=lambda x: x.gate.val == 1, ) q = cirq.GridQubit(1, 1) @@ -319,11 +388,11 @@ def test_can_serialize_operation_subclass(): def test_defaults_not_serialized(): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithAttribute, serialized_gate_id='my_gate', args=[ - cg.SerializingArg( + _create_serializing_arg( serialized_name='my_val', serialized_type=float, default=1.0, op_getter='val' ) ], @@ -342,10 +411,14 @@ def test_defaults_not_serialized(): def test_token_serialization(): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithAttribute, serialized_gate_id='my_gate', - args=[cg.SerializingArg(serialized_name='my_val', serialized_type=float, op_getter='val')], + args=[ + _create_serializing_arg( + serialized_name='my_val', serialized_type=float, op_getter='val' + ) + ], ) q = cirq.GridQubit(1, 2) tag = cg.CalibrationTag('my_token') @@ -372,10 +445,14 @@ def test_token_serialization(): (([], 0, ONE_CONSTANT), (ONE_CONSTANT, 0, ONE_CONSTANT), (TWO_CONSTANTS, 1, TWO_CONSTANTS)), ) def test_token_serialization_with_constant_reference(constants, expected_index, expected_constants): - serializer = cg.GateOpSerializer( + serializer = _create_gate_op_serializer( gate_type=GateWithAttribute, serialized_gate_id='my_gate', - args=[cg.SerializingArg(serialized_name='my_val', serialized_type=float, op_getter='val')], + args=[ + _create_serializing_arg( + serialized_name='my_val', serialized_type=float, op_getter='val' + ) + ], ) # Make a local copy since we are modifying the array in-place. constants = copy.copy(constants) diff --git a/cirq-google/cirq_google/serialization/serializable_gate_set_test.py b/cirq-google/cirq_google/serialization/serializable_gate_set_test.py index 03e0fab6fd5..dd1eea4f6e0 100644 --- a/cirq-google/cirq_google/serialization/serializable_gate_set_test.py +++ b/cirq-google/cirq_google/serialization/serializable_gate_set_test.py @@ -21,44 +21,67 @@ import cirq_google as cg from cirq_google.api import v2 -X_SERIALIZER = cg.GateOpSerializer( - gate_type=cirq.XPowGate, - serialized_gate_id='x_pow', - args=[ - cg.SerializingArg(serialized_name='half_turns', serialized_type=float, op_getter='exponent') - ], -) - -X_DESERIALIZER = cg.GateOpDeserializer( - serialized_gate_id='x_pow', - gate_constructor=cirq.XPowGate, - args=[cg.DeserializingArg(serialized_name='half_turns', constructor_arg_name='exponent')], -) - -Y_SERIALIZER = cg.GateOpSerializer( - gate_type=cirq.YPowGate, - serialized_gate_id='y_pow', - args=[ - cg.SerializingArg(serialized_name='half_turns', serialized_type=float, op_getter='exponent') - ], -) - -Y_DESERIALIZER = cg.GateOpDeserializer( - serialized_gate_id='y_pow', - gate_constructor=cirq.YPowGate, - args=[cg.DeserializingArg(serialized_name='half_turns', constructor_arg_name='exponent')], -) + +def _x_serializer(): + with cirq.testing.assert_deprecated('CircuitSerializer', deadline='v0.16', count=None): + return cg.GateOpSerializer( + gate_type=cirq.XPowGate, + serialized_gate_id='x_pow', + args=[ + cg.SerializingArg( + serialized_name='half_turns', serialized_type=float, op_getter='exponent' + ) + ], + ) + + +def _x_deserializer(): + with cirq.testing.assert_deprecated('CircuitSerializer', deadline='v0.16', count=None): + return cg.GateOpDeserializer( + serialized_gate_id='x_pow', + gate_constructor=cirq.XPowGate, + args=[ + cg.DeserializingArg(serialized_name='half_turns', constructor_arg_name='exponent') + ], + ) + + +def _y_serializer(): + with cirq.testing.assert_deprecated('CircuitSerializer', deadline='v0.16', count=None): + return cg.GateOpSerializer( + gate_type=cirq.YPowGate, + serialized_gate_id='y_pow', + args=[ + cg.SerializingArg( + serialized_name='half_turns', serialized_type=float, op_getter='exponent' + ) + ], + ) + + +def _y_deserializer(): + with cirq.testing.assert_deprecated('CircuitSerializer', deadline='v0.16', count=None): + return cg.GateOpDeserializer( + serialized_gate_id='y_pow', + gate_constructor=cirq.YPowGate, + args=[ + cg.DeserializingArg(serialized_name='half_turns', constructor_arg_name='exponent') + ], + ) + CIRCUIT_OP_SERIALIZER = cg.CircuitOpSerializer() CIRCUIT_OP_DESERIALIZER = cg.CircuitOpDeserializer() def _my_gate_set(): + x_serializer = _x_serializer() + x_deserializer = _x_deserializer() with cirq.testing.assert_deprecated('SerializableGateSet', deadline='v0.16', count=1): return cg.SerializableGateSet( gate_set_name='my_gate_set', - serializers=[X_SERIALIZER, CIRCUIT_OP_SERIALIZER], - deserializers=[X_DESERIALIZER, CIRCUIT_OP_DESERIALIZER], + serializers=[x_serializer, CIRCUIT_OP_SERIALIZER], + deserializers=[x_deserializer, CIRCUIT_OP_DESERIALIZER], ) @@ -118,7 +141,12 @@ def test_is_supported_circuit_operation(): def test_is_supported_operation_can_serialize_predicate(): - with cirq.testing.assert_deprecated('SerializableGateSet', deadline='v0.16', count=1): + x_deserializer = _x_deserializer() + # Deprecations: cirq_google.SerializableGateSet, cirq_google.GateOpSerializer, and + # cirq_google.SerializingArg + with cirq.testing.assert_deprecated( + 'SerializableGateSet', 'CircuitSerializer', deadline='v0.16', count=3 + ): q = cirq.GridQubit(1, 2) serializer = cg.GateOpSerializer( gate_type=cirq.XPowGate, @@ -131,7 +159,7 @@ def test_is_supported_operation_can_serialize_predicate(): can_serialize_predicate=lambda x: x.gate.exponent == 1.0, ) gate_set = cg.SerializableGateSet( - gate_set_name='my_gate_set', serializers=[serializer], deserializers=[X_DESERIALIZER] + gate_set_name='my_gate_set', serializers=[serializer], deserializers=[x_deserializer] ) assert gate_set.is_supported_operation(cirq.XPowGate()(q)) assert not gate_set.is_supported_operation(cirq.XPowGate()(q) ** 0.5) @@ -149,11 +177,11 @@ def test_serialize_deserialize_circuit(): moments=[ v2.program_pb2.Moment( operations=[ - X_SERIALIZER.to_proto(cirq.X(q0)), - X_SERIALIZER.to_proto(cirq.X(q1)), + _x_serializer().to_proto(cirq.X(q0)), + _x_serializer().to_proto(cirq.X(q1)), ] ), - v2.program_pb2.Moment(operations=[X_SERIALIZER.to_proto(cirq.X(q0))]), + v2.program_pb2.Moment(operations=[_x_serializer().to_proto(cirq.X(q0))]), ], ), ) @@ -187,7 +215,7 @@ def test_serialize_deserialize_circuit_with_tokens(): scheduling_strategy=v2.program_pb2.Circuit.MOMENT_BY_MOMENT, moments=[ v2.program_pb2.Moment(operations=[op1, op2]), - v2.program_pb2.Moment(operations=[X_SERIALIZER.to_proto(cirq.X(q0))]), + v2.program_pb2.Moment(operations=[_x_serializer().to_proto(cirq.X(q0))]), ], ), constants=[ @@ -237,7 +265,7 @@ def test_serialize_deserialize_circuit_with_subcircuit(): moments=[ v2.program_pb2.Moment(operations=[op1], circuit_operations=[c_op1]), v2.program_pb2.Moment( - operations=[X_SERIALIZER.to_proto(cirq.X(q0))], circuit_operations=[c_op2] + operations=[_x_serializer().to_proto(cirq.X(q0))], circuit_operations=[c_op2] ), ], ), @@ -246,7 +274,9 @@ def test_serialize_deserialize_circuit_with_subcircuit(): v2.program_pb2.Constant( circuit_value=v2.program_pb2.Circuit( scheduling_strategy=v2.program_pb2.Circuit.MOMENT_BY_MOMENT, - moments=[v2.program_pb2.Moment(operations=[X_SERIALIZER.to_proto(cirq.X(q0))])], + moments=[ + v2.program_pb2.Moment(operations=[_x_serializer().to_proto(cirq.X(q0))]) + ], ) ), ], @@ -441,11 +471,13 @@ def test_serialize_circuit_op_errors(): with pytest.raises(ValueError, match='CircuitOp serialization requires a constants list'): _my_gate_set().serialize_op(op, raw_constants=raw_constants) + x_serializer = _x_serializer() + x_deserializer = _x_deserializer() with cirq.testing.assert_deprecated('SerializableGateSet', deadline='v0.16', count=1): NO_CIRCUIT_OP_GATE_SET = cg.SerializableGateSet( gate_set_name='no_circuit_op_gateset', - serializers=[X_SERIALIZER], - deserializers=[X_DESERIALIZER], + serializers=[x_serializer], + deserializers=[x_deserializer], ) with pytest.raises(ValueError, match='Cannot serialize CircuitOperation'): NO_CIRCUIT_OP_GATE_SET.serialize_op( @@ -454,7 +486,13 @@ def test_serialize_circuit_op_errors(): def test_deserialize_circuit_op_errors(): - with cirq.testing.assert_deprecated('SerializableGateSet', deadline='v0.16', count=2): + x_serializer = _x_serializer() + x_deserializer = _x_deserializer() + + # Deprecations: cirq_google.SerializableGateSet, cirq_google.GateOpSerializer + with cirq.testing.assert_deprecated( + 'SerializableGateSet', 'CircuitSerializer', deadline='v0.16', count=3 + ): constants = [default_circuit_proto()] deserialized_constants = [default_circuit()] @@ -464,8 +502,8 @@ def test_deserialize_circuit_op_errors(): NO_CIRCUIT_OP_GATE_SET = cg.SerializableGateSet( gate_set_name='no_circuit_op_gateset', - serializers=[X_SERIALIZER], - deserializers=[X_DESERIALIZER], + serializers=[x_serializer], + deserializers=[x_deserializer], ) with pytest.raises(ValueError, match='Unsupported deserialized of a CircuitOperation'): NO_CIRCUIT_OP_GATE_SET.deserialize_op( @@ -508,7 +546,11 @@ def test_serialize_deserialize_circuit_op(): def test_multiple_serializers(): - with cirq.testing.assert_deprecated('SerializableGateSet', deadline='v0.16', count=1): + # Deprecations: cirq_google.SerializableGateSet, cirq_google.GateOpSerializer, and + # cirq_google.SerializingArg + with cirq.testing.assert_deprecated( + 'SerializableGateSet', 'CircuitSerializer', deadline='v0.16', count=5 + ): serializer1 = cg.GateOpSerializer( gate_type=cirq.XPowGate, serialized_gate_id='x_pow', @@ -538,13 +580,17 @@ def test_multiple_serializers(): def test_gateset_with_added_types(): + x_serializer = _x_serializer() + x_deserializer = _x_deserializer() + y_serializer = _y_serializer() + y_deserializer = _y_deserializer() with cirq.testing.assert_deprecated('SerializableGateSet', deadline='v0.16', count=2): q = cirq.GridQubit(1, 1) x_gateset = cg.SerializableGateSet( - gate_set_name='x', serializers=[X_SERIALIZER], deserializers=[X_DESERIALIZER] + gate_set_name='x', serializers=[x_serializer], deserializers=[x_deserializer] ) xy_gateset = x_gateset.with_added_types( - gate_set_name='xy', serializers=[Y_SERIALIZER], deserializers=[Y_DESERIALIZER] + gate_set_name='xy', serializers=[y_serializer], deserializers=[y_deserializer] ) assert x_gateset.name == 'x' assert x_gateset.is_supported_operation(cirq.X(q)) @@ -568,14 +614,16 @@ def test_gateset_with_added_types(): def test_gateset_with_added_types_again(): + x_serializer = _x_serializer() + x_deserializer = _x_deserializer() """Verify that adding a serializer twice doesn't mess anything up.""" with cirq.testing.assert_deprecated('SerializableGateSet', deadline='v0.16', count=2): q = cirq.GridQubit(2, 2) x_gateset = cg.SerializableGateSet( - gate_set_name='x', serializers=[X_SERIALIZER], deserializers=[X_DESERIALIZER] + gate_set_name='x', serializers=[x_serializer], deserializers=[x_deserializer] ) xx_gateset = x_gateset.with_added_types( - gate_set_name='xx', serializers=[X_SERIALIZER], deserializers=[X_DESERIALIZER] + gate_set_name='xx', serializers=[x_serializer], deserializers=[x_deserializer] ) assert xx_gateset.name == 'xx'