Skip to content

Commit 8a78e10

Browse files
authored
Make gate_sets actually optional (#4850)
- mypy and pycharm need a default value in the abstract interface in order to be truly optional. - Also, add a copy of the inherited arguments in SimulatedLocalProcessor.
1 parent 745ee1b commit 8a78e10

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

cirq-google/cirq_google/engine/abstract_processor.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ def run_calibration(
237237
"""
238238

239239
@abc.abstractmethod
240-
def get_sampler(self, gate_set: Optional['serializer.Serializer']) -> cirq.Sampler:
240+
def get_sampler(
241+
self,
242+
gate_set: Optional['serializer.Serializer'] = None,
243+
) -> cirq.Sampler:
241244
"""Returns a sampler backed by the processor.
242245
243246
Args:
@@ -279,7 +282,10 @@ def get_device_specification(self) -> Optional[v2.device_pb2.DeviceSpecification
279282
"""
280283

281284
@abc.abstractmethod
282-
def get_device(self, gate_sets: Iterable['serializer.Serializer']) -> cirq.Device:
285+
def get_device(
286+
self,
287+
gate_sets: Iterable['serializer.Serializer'] = (),
288+
) -> cirq.Device:
283289
"""Returns a `Device` created from the processor's device specification.
284290
285291
This method queries the processor to retrieve the device specification,

cirq-google/cirq_google/engine/engine_processor.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
engine_sampler,
2929
)
3030
from cirq_google.serialization import circuit_serializer, serializable_gate_set, serializer
31+
from cirq_google.serialization import gate_sets as gs
3132

3233
if TYPE_CHECKING:
3334
import cirq_google.engine.engine as engine_base
@@ -95,13 +96,14 @@ def engine(self) -> 'engine_base.Engine':
9596
return engine_base.Engine(self.project_id, context=self.context)
9697

9798
def get_sampler(
98-
self, gate_set: Optional[serializer.Serializer]
99+
self,
100+
gate_set: Optional[serializer.Serializer] = None,
99101
) -> engine_sampler.QuantumEngineSampler:
100102
"""Returns a sampler backed by the engine.
101103
102104
Args:
103105
gate_set: A `Serializer` that determines how to serialize circuits
104-
when requesting samples.
106+
when requesting samples. If not specified, uses proto v2.5 serialization.
105107
106108
Returns:
107109
A `cirq.Sampler` instance (specifically a `engine_sampler.QuantumEngineSampler`
@@ -335,7 +337,10 @@ def get_device_specification(self) -> Optional[v2.device_pb2.DeviceSpecification
335337
else:
336338
return None
337339

338-
def get_device(self, gate_sets: Iterable[serializer.Serializer]) -> cirq.Device:
340+
def get_device(
341+
self,
342+
gate_sets: Iterable[serializer.Serializer] = (),
343+
) -> cirq.Device:
339344
"""Returns a `Device` created from the processor's device specification.
340345
341346
This method queries the processor to retrieve the device specification,
@@ -345,6 +350,12 @@ def get_device(self, gate_sets: Iterable[serializer.Serializer]) -> cirq.Device:
345350
spec = self.get_device_specification()
346351
if not spec:
347352
raise ValueError('Processor does not have a device specification')
353+
if not gate_sets:
354+
# Default is to use all named gatesets in the device spec
355+
gate_sets = []
356+
for valid_gate_set in spec.valid_gate_sets:
357+
if valid_gate_set.name in gs.NAMED_GATESETS:
358+
gate_sets.append(gs.NAMED_GATESETS[valid_gate_set.name])
348359
if not all(isinstance(gs, serializable_gate_set.SerializableGateSet) for gs in gate_sets):
349360
raise ValueError('All gate_sets must be SerializableGateSet currently.')
350361
return serializable_device.SerializableDevice.from_proto(

cirq-google/cirq_google/engine/engine_processor_test.py

+19
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from google.protobuf.timestamp_pb2 import Timestamp
2525
import cirq
2626
import cirq_google as cg
27+
import cirq_google.devices.known_devices as known_devices
2728
from cirq_google.api import v2
2829
from cirq_google.engine.engine import EngineContext
2930
from cirq_google.engine.client.quantum_v1alpha1 import enums as qenums
@@ -334,6 +335,24 @@ def test_get_device():
334335
processor.get_device(gate_sets=[cg.serialization.circuit_serializer.CIRCUIT_SERIALIZER])
335336

336337

338+
def test_default_gate_sets():
339+
# Sycamore should have valid gate sets with default
340+
processor = cg.EngineProcessor(
341+
'a',
342+
'p',
343+
EngineContext(),
344+
_processor=qtypes.QuantumProcessor(device_spec=_to_any(known_devices.SYCAMORE_PROTO)),
345+
)
346+
device = processor.get_device()
347+
device.validate_operation(cirq.X(cirq.GridQubit(5, 4)))
348+
# Test that a device with no standard gatesets doesn't blow up
349+
processor = cg.EngineProcessor(
350+
'a', 'p', EngineContext(), _processor=qtypes.QuantumProcessor(device_spec=_DEVICE_SPEC)
351+
)
352+
device = processor.get_device()
353+
assert device.qubits == [cirq.GridQubit(0, 0), cirq.GridQubit(1, 1)]
354+
355+
337356
def test_get_missing_device():
338357
processor = cg.EngineProcessor('a', 'p', EngineContext(), _processor=qtypes.QuantumProcessor())
339358
with pytest.raises(ValueError, match='device specification'):

cirq-google/cirq_google/engine/simulated_local_processor.py

+9
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ class SimulatedLocalProcessor(AbstractLocalProcessor):
7777
based on the given serializer.
7878
calibrations: A dictionary of calibration metrics keyed by epoch seconds
7979
that can be returned by the processor.
80+
processor_id: Unique string id of the processor.
81+
engine: The parent `AbstractEngine` object, if available.
82+
expected_down_time: Optional datetime of the next expected downtime.
83+
For informational purpose only.
84+
expected_recovery_time: Optional datetime when the processor is
85+
expected to be available again. For informational purpose only.
86+
schedule: List of time slots that the scheduling/reservation should
87+
use. All time slots must be non-overlapping.
88+
project_name: A project_name for resource naming.
8089
"""
8190

8291
def __init__(

0 commit comments

Comments
 (0)