Skip to content

Commit c40a116

Browse files
verultrht
authored andcommitted
SerializableGateSet deprecation (quantumlib#5573)
* Deprecate global SerializableGateSets * Deprecate `GOOGLE_GATESETS`. This is not exposed in cirq_google/__init__.py; it is only used in `engine_program.py` for selecting the deserializer using the Program.language.gate_set proto field. Removing other gatesets from consideration here is premature since global gatesets have not been removed. This code should move to CircuitSerializer completely before 1.0. * Deprecate the `SerializableGateSet` class. A new class wrapper is introduced to bypass the behavior that deprecation warnings thrown while loading global gatesets at the module level fail unit tests. @dstrain115 @MichaelBroughton
1 parent b1119e0 commit c40a116

16 files changed

+1563
-1056
lines changed

cirq-google/cirq_google/__init__.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,50 @@
163163
_compat.deprecate_attributes(
164164
__name__,
165165
{
166-
'Bristlecone': ('v0.15', 'Bristlecone will no longer be supported.'),
167-
'Foxtail': ('v0.15', 'Foxtail will no longer be supported.'),
166+
'XMON': (
167+
'v0.16',
168+
'SerializableGateSet will no longer be supported.'
169+
' In cirq_google.GridDevice, the new representation of Google devices, the gateset of '
170+
' a device is represented as a cirq.Gateset and is available as'
171+
' GridDevice.metadata.gateset.'
172+
' Engine methods no longer require gate sets to be passed in.'
173+
' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.',
174+
),
175+
'FSIM_GATESET': (
176+
'v0.16',
177+
'SerializableGateSet will no longer be supported.'
178+
' In cirq_google.GridDevice, the new representation of Google devices, the gateset of '
179+
' a device is represented as a cirq.Gateset and is available as'
180+
' GridDevice.metadata.gateset.'
181+
' Engine methods no longer require gate sets to be passed in.'
182+
' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.',
183+
),
184+
'SQRT_ISWAP_GATESET': (
185+
'v0.16',
186+
'SerializableGateSet will no longer be supported.'
187+
' In cirq_google.GridDevice, the new representation of Google devices, the gateset of '
188+
' a device is represented as a cirq.Gateset and is available as'
189+
' GridDevice.metadata.gateset.'
190+
' Engine methods no longer require gate sets to be passed in.'
191+
' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.',
192+
),
193+
'SYC_GATESET': (
194+
'v0.16',
195+
'SerializableGateSet will no longer be supported.'
196+
' In cirq_google.GridDevice, the new representation of Google devices, the gateset of '
197+
' a device is represented as a cirq.Gateset and is available as'
198+
' GridDevice.metadata.gateset.'
199+
' Engine methods no longer require gate sets to be passed in.'
200+
' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.',
201+
),
202+
'NAMED_GATESETS': (
203+
'v0.16',
204+
'SerializableGateSet will no longer be supported.'
205+
' In cirq_google.GridDevice, the new representation of Google devices, the gateset of '
206+
' a device is represented as a cirq.Gateset and is available as'
207+
' GridDevice.metadata.gateset.'
208+
' Engine methods no longer require gate sets to be passed in.'
209+
' In addition, circuit serialization is replaced by cirq_google.CircuitSerializer.',
210+
),
168211
},
169212
)

cirq-google/cirq_google/calibration/phased_fsim_test.py

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -221,54 +221,55 @@ def test_floquet_to_calibration_layer_with_measure_qubits():
221221

222222

223223
def test_xeb_to_calibration_layer():
224-
q_00, q_01, q_02, q_03 = [cirq.GridQubit(0, index) for index in range(4)]
225-
gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0)
226-
request = XEBPhasedFSimCalibrationRequest(
227-
gate=gate,
228-
pairs=((q_00, q_01), (q_02, q_03)),
229-
options=XEBPhasedFSimCalibrationOptions(
230-
n_library_circuits=22,
231-
fsim_options=XEBPhasedFSimCharacterizationOptions(
232-
characterize_theta=True,
233-
characterize_zeta=True,
234-
characterize_chi=False,
235-
characterize_gamma=False,
236-
characterize_phi=True,
224+
with cirq.testing.assert_deprecated('SerializableGateSet', deadline='v0.16', count=None):
225+
q_00, q_01, q_02, q_03 = [cirq.GridQubit(0, index) for index in range(4)]
226+
gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0)
227+
request = XEBPhasedFSimCalibrationRequest(
228+
gate=gate,
229+
pairs=((q_00, q_01), (q_02, q_03)),
230+
options=XEBPhasedFSimCalibrationOptions(
231+
n_library_circuits=22,
232+
fsim_options=XEBPhasedFSimCharacterizationOptions(
233+
characterize_theta=True,
234+
characterize_zeta=True,
235+
characterize_chi=False,
236+
characterize_gamma=False,
237+
characterize_phi=True,
238+
),
237239
),
238-
),
239-
)
240-
layer = request.to_calibration_layer()
241-
assert layer == cirq_google.CalibrationLayer(
242-
calibration_type='xeb_phased_fsim_characterization',
243-
program=cirq.Circuit([gate.on(q_00, q_01), gate.on(q_02, q_03)]),
244-
args={
245-
'n_library_circuits': 22,
246-
'n_combinations': 10,
247-
'cycle_depths': '5_25_50_100_200_300',
248-
'fatol': 5e-3,
249-
'xatol': 5e-3,
250-
'characterize_theta': True,
251-
'characterize_zeta': True,
252-
'characterize_chi': False,
253-
'characterize_gamma': False,
254-
'characterize_phi': True,
255-
},
256-
)
240+
)
241+
layer = request.to_calibration_layer()
242+
assert layer == cirq_google.CalibrationLayer(
243+
calibration_type='xeb_phased_fsim_characterization',
244+
program=cirq.Circuit([gate.on(q_00, q_01), gate.on(q_02, q_03)]),
245+
args={
246+
'n_library_circuits': 22,
247+
'n_combinations': 10,
248+
'cycle_depths': '5_25_50_100_200_300',
249+
'fatol': 5e-3,
250+
'xatol': 5e-3,
251+
'characterize_theta': True,
252+
'characterize_zeta': True,
253+
'characterize_chi': False,
254+
'characterize_gamma': False,
255+
'characterize_phi': True,
256+
},
257+
)
257258

258-
# Serialize to proto
259-
calibration = v2.calibration_pb2.FocusedCalibration()
260-
new_layer = calibration.layers.add()
261-
new_layer.calibration_type = layer.calibration_type
262-
for arg in layer.args:
263-
arg_to_proto(layer.args[arg], out=new_layer.args[arg])
264-
cirq_google.SQRT_ISWAP_GATESET.serialize(layer.program, msg=new_layer.layer)
265-
with open(os.path.dirname(__file__) + '/test_data/xeb_calibration_layer.textproto') as f:
266-
desired_textproto = f.read()
267-
268-
layer_str = str(new_layer)
269-
# Fix precision issues
270-
layer_str = re.sub(r'0.004999\d+', '0.005', layer_str)
271-
assert layer_str == desired_textproto
259+
# Serialize to proto
260+
calibration = v2.calibration_pb2.FocusedCalibration()
261+
new_layer = calibration.layers.add()
262+
new_layer.calibration_type = layer.calibration_type
263+
for arg in layer.args:
264+
arg_to_proto(layer.args[arg], out=new_layer.args[arg])
265+
cirq_google.SQRT_ISWAP_GATESET.serialize(layer.program, msg=new_layer.layer)
266+
with open(os.path.dirname(__file__) + '/test_data/xeb_calibration_layer.textproto') as f:
267+
desired_textproto = f.read()
268+
269+
layer_str = str(new_layer)
270+
# Fix precision issues
271+
layer_str = re.sub(r'0.004999\d+', '0.005', layer_str)
272+
assert layer_str == desired_textproto
272273

273274

274275
def test_from_moment():

cirq-google/cirq_google/devices/grid_device_test.py

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -418,78 +418,82 @@ def test_to_proto_invalid_input(error_match, qubits, qubit_pairs, gateset, gate_
418418

419419

420420
def test_to_proto_backward_compatibility():
421-
device_info, _ = _create_device_spec_with_horizontal_couplings()
422-
423-
# The set of gates in gate_durations are consistent with what's generated in
424-
# _create_device_spec_with_horizontal_couplings()
425-
base_duration = cirq.Duration(picos=1_000)
426-
gate_durations = {
427-
cirq.GateFamily(cirq_google.SYC): base_duration * 0,
428-
cirq.GateFamily(cirq.SQRT_ISWAP): base_duration * 1,
429-
cirq.GateFamily(cirq.SQRT_ISWAP_INV): base_duration * 2,
430-
cirq.GateFamily(cirq.CZ): base_duration * 3,
431-
cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate): base_duration * 4,
432-
cirq.GateFamily(
433-
cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]
434-
): base_duration
435-
* 5,
436-
cirq.GateFamily(
437-
cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]
438-
): base_duration
439-
* 6,
440-
cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse): base_duration * 7,
441-
cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate): base_duration * 8,
442-
cirq.GateFamily(cirq.ops.wait_gate.WaitGate): base_duration * 9,
443-
}
444-
445-
# Serialize the old way
446-
spec = known_devices.create_device_proto_for_qubits(
447-
device_info.grid_qubits,
448-
device_info.qubit_pairs,
449-
[cirq_google.FSIM_GATESET],
450-
known_devices._SYCAMORE_DURATIONS_PICOS,
451-
)
452-
453-
# Serialize the new way
454-
grid_device.create_device_specification_proto(
455-
qubits=device_info.grid_qubits,
456-
pairs=device_info.qubit_pairs,
457-
gateset=cirq.Gateset(*gate_durations.keys()),
458-
gate_durations=gate_durations,
459-
out=spec,
460-
)
461-
462-
with cirq.testing.assert_deprecated('Use cirq_google.GridDevice', deadline='v0.16', count=None):
463-
# Deserialize both ways
464-
serializable_dev = cirq_google.SerializableDevice.from_proto(
465-
spec, [cirq_google.FSIM_GATESET]
466-
)
467-
grid_dev = cirq_google.GridDevice.from_proto(spec)
468-
469-
assert serializable_dev.metadata.qubit_set == grid_dev.metadata.qubit_set
470-
assert serializable_dev.metadata.qubit_pairs == grid_dev.metadata.qubit_pairs
471-
472-
assert serializable_dev.metadata.gateset == cirq.Gateset(
473-
cirq.FSimGate,
474-
cirq.ISwapPowGate,
475-
cirq.CZPowGate,
476-
cirq.PhasedXPowGate,
477-
cirq.XPowGate,
478-
cirq.YPowGate,
479-
cirq.ZPowGate,
480-
cirq.PhasedXZGate,
481-
cirq.MeasurementGate,
482-
cirq.WaitGate,
483-
cirq.GlobalPhaseGate,
421+
with cirq.testing.assert_deprecated('SerializableGateSet', deadline='v0.16', count=None):
422+
device_info, _ = _create_device_spec_with_horizontal_couplings()
423+
424+
# The set of gates in gate_durations are consistent with what's generated in
425+
# _create_device_spec_with_horizontal_couplings()
426+
base_duration = cirq.Duration(picos=1_000)
427+
gate_durations = {
428+
cirq.GateFamily(cirq_google.SYC): base_duration * 0,
429+
cirq.GateFamily(cirq.SQRT_ISWAP): base_duration * 1,
430+
cirq.GateFamily(cirq.SQRT_ISWAP_INV): base_duration * 2,
431+
cirq.GateFamily(cirq.CZ): base_duration * 3,
432+
cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate): base_duration * 4,
433+
cirq.GateFamily(
434+
cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]
435+
): base_duration
436+
* 5,
437+
cirq.GateFamily(
438+
cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]
439+
): base_duration
440+
* 6,
441+
cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse): base_duration
442+
* 7,
443+
cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate): base_duration * 8,
444+
cirq.GateFamily(cirq.ops.wait_gate.WaitGate): base_duration * 9,
445+
}
446+
447+
# Serialize the old way
448+
spec = known_devices.create_device_proto_for_qubits(
449+
device_info.grid_qubits,
450+
device_info.qubit_pairs,
451+
[cirq_google.FSIM_GATESET],
452+
known_devices._SYCAMORE_DURATIONS_PICOS,
484453
)
485454

486-
assert grid_dev.metadata.gateset == device_info.expected_gateset
487-
assert (
488-
tuple(grid_dev.metadata.compilation_target_gatesets)
489-
== device_info.expected_target_gatesets
455+
# Serialize the new way
456+
grid_device.create_device_specification_proto(
457+
qubits=device_info.grid_qubits,
458+
pairs=device_info.qubit_pairs,
459+
gateset=cirq.Gateset(*gate_durations.keys()),
460+
gate_durations=gate_durations,
461+
out=spec,
490462
)
491463

492-
assert grid_dev.metadata.gate_durations == device_info.expected_gate_durations
464+
with cirq.testing.assert_deprecated(
465+
'Use cirq_google.GridDevice', deadline='v0.16', count=None
466+
):
467+
# Deserialize both ways
468+
serializable_dev = cirq_google.SerializableDevice.from_proto(
469+
spec, [cirq_google.FSIM_GATESET]
470+
)
471+
grid_dev = cirq_google.GridDevice.from_proto(spec)
472+
473+
assert serializable_dev.metadata.qubit_set == grid_dev.metadata.qubit_set
474+
assert serializable_dev.metadata.qubit_pairs == grid_dev.metadata.qubit_pairs
475+
476+
assert serializable_dev.metadata.gateset == cirq.Gateset(
477+
cirq.FSimGate,
478+
cirq.ISwapPowGate,
479+
cirq.CZPowGate,
480+
cirq.PhasedXPowGate,
481+
cirq.XPowGate,
482+
cirq.YPowGate,
483+
cirq.ZPowGate,
484+
cirq.PhasedXZGate,
485+
cirq.MeasurementGate,
486+
cirq.WaitGate,
487+
cirq.GlobalPhaseGate,
488+
)
489+
490+
assert grid_dev.metadata.gateset == device_info.expected_gateset
491+
assert (
492+
tuple(grid_dev.metadata.compilation_target_gatesets)
493+
== device_info.expected_target_gatesets
494+
)
495+
496+
assert grid_dev.metadata.gate_durations == device_info.expected_gate_durations
493497

494498

495499
def test_to_proto_empty():

cirq-google/cirq_google/devices/known_devices_test.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ def test_create_device_proto_for_irregular_grid():
6161

6262

6363
def test_multiple_gate_sets():
64-
with cirq.testing.assert_deprecated('no longer be available', deadline='v0.16', count=1):
64+
# Deprecations: cirq_google.SerializableGateSets class,
65+
# well-known cirq_google SerializableGateSets (e.g. cirq_google.SYC_GATESET), and
66+
# cirq_google.devices.known_devices.create_device_proto_from_diagram
67+
with cirq.testing.assert_deprecated(
68+
'SerializableGateSet', 'no longer be available', deadline='v0.16', count=3
69+
):
6570
halfPiGateSet = cirq_google.SerializableGateSet(
6671
gate_set_name='half_pi_gateset',
6772
serializers=[*cgc.SINGLE_QUBIT_HALF_PI_SERIALIZERS, cgc.MEASUREMENT_SERIALIZER],
@@ -221,10 +226,15 @@ def test_multiple_gate_sets():
221226

222227

223228
def test_sycamore_circuitop_device():
224-
# Deprecations: cirq_google.SerializableDevice and
229+
# Deprecations: cirq_google.SerializableGateSets class,
230+
# well-known cirq_google SerializableGateSets (e.g. cirq_google.SYC_GATESET), and
225231
# cirq_google.devices.known_devices.create_device_proto_from_diagram
226232
with cirq.testing.assert_deprecated(
227-
'Use cirq_google.GridDevice', 'no longer be available', deadline='v0.16', count=3
233+
'Use cirq_google.GridDevice',
234+
'SerializableGateSet',
235+
'no longer be available',
236+
deadline='v0.16',
237+
count=6,
228238
):
229239
circuitop_gateset = cirq_google.SerializableGateSet(
230240
gate_set_name='circuitop_gateset',
@@ -268,7 +278,11 @@ def test_sycamore_grid_layout():
268278

269279

270280
def test_proto_with_circuitop():
271-
with cirq.testing.assert_deprecated('no longer be available', deadline='v0.16', count=1):
281+
# Deprecations: cirq_google.SerializableGateSets class and
282+
# cirq_google.devices.known_devices.create_device_proto_from_diagram
283+
with cirq.testing.assert_deprecated(
284+
'SerializableGateSet', 'no longer be available', deadline='v0.16', count=2
285+
):
272286
circuitop_gateset = cirq_google.SerializableGateSet(
273287
gate_set_name='circuitop_gateset',
274288
serializers=[cgc.CIRCUIT_OP_SERIALIZER],
@@ -320,10 +334,14 @@ def test_proto_with_circuitop():
320334

321335

322336
def test_proto_with_waitgate():
323-
# Deprecations: cirq_google.SerializableDevice and
337+
# Deprecations: cirq_google.SerializableDevice, cirq_google.SerializableGateSet, and
324338
# cirq_google.devices.known_devices.create_device_proto_from_diagram
325339
with cirq.testing.assert_deprecated(
326-
'Use cirq_google.GridDevice', 'no longer be available', deadline='v0.16', count=3
340+
'Use cirq_google.GridDevice',
341+
'SerializableGateSet',
342+
'no longer be available',
343+
deadline='v0.16',
344+
count=4,
327345
):
328346
wait_gateset = cirq_google.SerializableGateSet(
329347
gate_set_name='wait_gateset',
@@ -387,10 +405,14 @@ def test_proto_with_waitgate():
387405

388406

389407
def test_adding_gates_multiple_times():
390-
# Deprecations: cirq_google.SerializableDevice and
408+
# Deprecations: cirq_google.SerializableDevice, cirq_google.SerializableGateSet, and
391409
# cirq_google.devices.known_devices.create_device_proto_from_diagram
392410
with cirq.testing.assert_deprecated(
393-
'Use cirq_google.GridDevice', 'no longer be available', deadline='v0.16', count=3
411+
'Use cirq_google.GridDevice',
412+
'SerializableGateSet',
413+
'no longer be available',
414+
deadline='v0.16',
415+
count=4,
394416
):
395417
waiting_for_godot = cirq_google.SerializableGateSet(
396418
gate_set_name='wait_gateset',

0 commit comments

Comments
 (0)