Skip to content

Commit

Permalink
Move virtual engine functions to GridDevice and deprecate gate_sets p…
Browse files Browse the repository at this point in the history
…arameter
  • Loading branch information
verult committed Jun 19, 2022
1 parent f973824 commit 2e9ef33
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cirq-google/cirq_google/engine/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def deprecated_get_device_gate_sets_parameter(param_name='gate_sets'):
def decorator(func):
signature = inspect.signature(func)
gate_sets_param = signature.parameters[param_name]
assert gate_sets_param.default is ()
assert gate_sets_param.default is () or gate_sets_param.default is None
idx = list(signature.parameters).index(param_name)

deprecation_decorator = cirq._compat.deprecated_parameter(
Expand Down
35 changes: 15 additions & 20 deletions cirq-google/cirq_google/engine/virtual_engine_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import cirq
from cirq_google.api import v2
from cirq_google.engine import calibration, engine_validator, simulated_local_processor, util
from cirq_google.devices import serializable_device
from cirq_google.serialization.gate_sets import FSIM_GATESET
from cirq_google.devices import grid_device, serializable_device
from cirq_google.serialization import serializable_gate_set
from cirq_google.serialization.gate_sets import FSIM_GATESET
from cirq_google.engine.simulated_local_engine import SimulatedLocalEngine
from cirq_google.engine.simulated_local_processor import SimulatedLocalProcessor

# TODO(verult) update to new DeviceSpecification proto
MOST_RECENT_TEMPLATES = {
'rainbow': 'rainbow_2021_12_10_device_spec.proto.txt',
'weber': 'weber_2021_12_10_device_spec.proto.txt',
Expand Down Expand Up @@ -203,6 +204,7 @@ def create_noiseless_virtual_engine_from_device(
return SimulatedLocalEngine([_create_virtual_processor_from_device(processor_id, device)])


@util.deprecated_get_device_gate_sets_parameter()
def create_noiseless_virtual_processor_from_proto(
processor_id: str,
device_specification: v2.device_pb2.DeviceSpecification,
Expand All @@ -219,17 +221,15 @@ def create_noiseless_virtual_processor_from_proto(
in QCS.
device_specification: `v2.device_pb2.DeviceSpecification` proto to create
a validating device from.
gate_sets: Iterable of serializers to use in the processor. Defaults
to the FSIM_GATESET.
gate_sets: Iterable of serializers to use in the processor.
"""
if gate_sets is None:
gate_sets = [FSIM_GATESET]

device = serializable_device.SerializableDevice.from_proto(device_specification, gate_sets)
device = grid_device.GridDevice.from_proto(device_specification)
processor = _create_virtual_processor_from_device(processor_id, device)
return processor


@util.deprecated_get_device_gate_sets_parameter()
def create_noiseless_virtual_engine_from_proto(
processor_ids: Union[str, List[str]],
device_specifications: Union[
Expand All @@ -250,14 +250,11 @@ def create_noiseless_virtual_engine_from_proto(
validating devices from. This can be a single DeviceSpecification
or a list of them. There should be one DeviceSpecification for each
processor_id.
gate_sets: Iterable of serializers to use in the processor. Defaults
to the FSIM_GATESET.
gate_sets: Iterable of serializers to use in the processor.
Raises:
ValueError: if processor_ids and device_specifications are not the same length.
"""
if gate_sets is None:
gate_sets = [FSIM_GATESET]
if isinstance(processor_ids, str):
processor_ids = [processor_ids]
if isinstance(device_specifications, v2.device_pb2.DeviceSpecification):
Expand All @@ -267,7 +264,7 @@ def create_noiseless_virtual_engine_from_proto(

return SimulatedLocalEngine(
processors=[
create_noiseless_virtual_processor_from_proto(processor_id, device_spec, gate_sets)
create_noiseless_virtual_processor_from_proto(processor_id, device_spec)
for device_spec, processor_id in zip(device_specifications, processor_ids)
]
)
Expand Down Expand Up @@ -300,6 +297,7 @@ def create_device_from_processor_id(processor_id: str) -> serializable_device.Se
return serializable_device.SerializableDevice.from_proto(device_specification, [FSIM_GATESET])


@util.deprecated_get_device_gate_sets_parameter()
def create_noiseless_virtual_processor_from_template(
processor_id: str,
template_name: str,
Expand All @@ -313,16 +311,14 @@ def create_noiseless_virtual_processor_from_template(
in QCS.
template_name: File name of the device specification template, see
cirq_google/devices/specifications for valid templates.
gate_sets: Iterable of serializers to use in the processor. Defaults
to the FSIM_GATESET.
gate_sets: Iterable of serializers to use in the processor.
"""
return create_noiseless_virtual_processor_from_proto(
processor_id,
device_specification=_create_device_spec_from_template(template_name),
gate_sets=gate_sets,
processor_id, device_specification=_create_device_spec_from_template(template_name)
)


@util.deprecated_get_device_gate_sets_parameter()
def create_noiseless_virtual_engine_from_templates(
processor_ids: Union[str, List[str]],
template_names: Union[str, List[str]],
Expand All @@ -339,8 +335,7 @@ def create_noiseless_virtual_engine_from_templates(
cirq_google/devices/specifications for valid templates. There can
be a single str for a template name or a list of strings. Each
template name should be matched to a single processor id.
gate_sets: Iterable of serializers to use in the processor. Defaults
to the FSIM_GATESET.
gate_sets: Iterable of serializers to use in the processor.
Raises:
ValueError: if processor_ids and template_names are not the same length.
Expand All @@ -355,7 +350,7 @@ def create_noiseless_virtual_engine_from_templates(
specifications = [
_create_device_spec_from_template(template_name) for template_name in template_names
]
return create_noiseless_virtual_engine_from_proto(processor_ids, specifications, gate_sets)
return create_noiseless_virtual_engine_from_proto(processor_ids, specifications)


def create_noiseless_virtual_engine_from_latest_templates() -> SimulatedLocalEngine:
Expand Down
25 changes: 14 additions & 11 deletions cirq-google/cirq_google/engine/virtual_engine_factory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.
import pytest
import numpy as np
import google.protobuf.text_format as text_format

import cirq
import cirq_google as cg
import cirq_google.api.v2 as v2
Expand Down Expand Up @@ -122,17 +124,18 @@ def test_device_zphase_bad_processor():
def test_create_from_proto():

# Create a minimal gate specification that can handle the test.
device_spec = v2.device_pb2.DeviceSpecification()
device_spec.valid_qubits.extend(['5_4'])
gs = device_spec.valid_gate_sets.add()
gs.name = 'fsim'
gs.valid_gates.add().id = 'fsim'
gs.valid_gates.add().id = 'xyz'
gs.valid_gates.add().id = 'xy'
gs.valid_gates.add().id = 'z'
gs.valid_gates.add().id = 'meas'
gs.valid_gates.add().id = 'wait'
gs.valid_gates.add().id = 'circuit'
device_spec = text_format.Merge(
"""
valid_qubits: "5_4"
valid_gates {
phased_xz {}
}
valid_gates {
meas {}
}
""",
v2.device_pb2.DeviceSpecification(),
)
engine = factory.create_noiseless_virtual_engine_from_proto('sycamore', device_spec)
_test_processor(engine.get_processor('sycamore'))

Expand Down

0 comments on commit 2e9ef33

Please sign in to comment.