diff --git a/cirq-google/cirq_google/engine/__init__.py b/cirq-google/cirq_google/engine/__init__.py index 04771f8b96e..bb50e452b78 100644 --- a/cirq-google/cirq_google/engine/__init__.py +++ b/cirq-google/cirq_google/engine/__init__.py @@ -71,6 +71,7 @@ from cirq_google.engine.validating_sampler import ValidatingSampler from cirq_google.engine.virtual_engine_factory import ( + create_device_from_processor_id, create_noiseless_virtual_engine_from_device, create_noiseless_virtual_engine_from_proto, create_noiseless_virtual_engine_from_templates, diff --git a/cirq-google/cirq_google/engine/virtual_engine_factory.py b/cirq-google/cirq_google/engine/virtual_engine_factory.py index 4dd7cff1170..03448e411fc 100644 --- a/cirq-google/cirq_google/engine/virtual_engine_factory.py +++ b/cirq-google/cirq_google/engine/virtual_engine_factory.py @@ -206,6 +206,22 @@ def _create_device_spec_from_template(template_name: str) -> v2.device_pb2.Devic return device_spec +def create_device_from_processor_id(processor_id: str) -> serializable_device.SerializableDevice: + """Generates a `cirq_google.SerializableDevice` for a given processor ID. + + Args: + processor_id: name of the processor to simulate. + + Raises: + ValueError: if processor_id is not a supported QCS processor. + """ + template_name = MOST_RECENT_TEMPLATES.get(processor_id, None) + if template_name is None: + raise ValueError(f"Got processor_id={processor_id}, but no such processor is defined.") + device_specification = _create_device_spec_from_template(template_name) + return serializable_device.SerializableDevice.from_proto(device_specification, [FSIM_GATESET]) + + def create_noiseless_virtual_processor_from_template( processor_id: str, template_name: str, diff --git a/cirq-google/cirq_google/engine/virtual_engine_factory_test.py b/cirq-google/cirq_google/engine/virtual_engine_factory_test.py index bea085a407c..05bbe62856c 100644 --- a/cirq-google/cirq_google/engine/virtual_engine_factory_test.py +++ b/cirq-google/cirq_google/engine/virtual_engine_factory_test.py @@ -38,6 +38,14 @@ def _test_processor(processor: cg.engine.abstract_processor.AbstractProcessor): _ = processor.run(circuit, repetitions=100) +def test_create_device_from_processor_id(): + device = factory.create_device_from_processor_id('rainbow') + assert device is not None + + with pytest.raises(ValueError, match='no such processor is defined'): + _ = factory.create_device_from_processor_id('bad_processor') + + def test_create_from_device(): engine = factory.create_noiseless_virtual_engine_from_device('sycamore', cg.Sycamore) _test_processor(engine.get_processor('sycamore'))