Skip to content

create_device_from_processor_id #5303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cirq-google/cirq_google/engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions cirq-google/cirq_google/engine/virtual_engine_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ 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,
gate_sets: Optional[Iterable[serializable_gate_set.SerializableGateSet]] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend omitting this arg and always set the gateset to FSIM_GATESET. The new Device implementation (cirq_google.GridDevice) will not have gatesets as a parameter to from_proto.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

) -> serializable_device.SerializableDevice:
"""Generates a `cirq_google.SerializableDevice` for a given processor ID.

Args:
processor_id: name of the processor to simulate.
gate_sets: Iterable of serializers to use in the processor. Defaults
to the FSIM_GATESET.

Raises:
ValueError: if processor_id is not a supported QCS processor.
"""
if gate_sets is None:
gate_sets = [FSIM_GATESET]

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, gate_sets)


def create_noiseless_virtual_processor_from_template(
processor_id: str,
template_name: str,
Expand Down
8 changes: 8 additions & 0 deletions cirq-google/cirq_google/engine/virtual_engine_factory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down