Skip to content
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
38 changes: 37 additions & 1 deletion cirq-google/cirq_google/engine/virtual_engine_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@

"""Functions to instantiate SimulatedLocalEngines to simulate various Google Devices."""
import json
from typing import cast, Iterable, List, Optional, Union
from typing import cast, Iterable, List, Optional, Union, Type
import pathlib
import time

import google.protobuf.text_format as text_format
import cirq
from cirq.sim.simulator import SimulatesSamples
from cirq_google.api import v2
from cirq_google.engine import calibration, engine_validator, simulated_local_processor, util
from cirq_google.devices import grid_device
from cirq_google.devices.google_noise_properties import NoiseModelFromGoogleNoiseProperties
from cirq_google.serialization import serializable_gate_set
from cirq_google.engine.calibration_to_noise_properties import noise_properties_from_calibration
from cirq_google.engine.simulated_local_engine import SimulatedLocalEngine
from cirq_google.engine.simulated_local_processor import SimulatedLocalProcessor

Expand Down Expand Up @@ -367,3 +370,36 @@ def create_noiseless_virtual_engine_from_latest_templates() -> SimulatedLocalEng
processor_ids = list(MOST_RECENT_TEMPLATES.keys())
template_names = [MOST_RECENT_TEMPLATES[k] for k in processor_ids]
return create_noiseless_virtual_engine_from_templates(processor_ids, template_names)


def create_noisy_virtual_engine_from_processor_id_and_simulator_class(
Comment thread
augustehirth marked this conversation as resolved.
Outdated
processor_id: str, simulator_class: Type[SimulatesSamples], **kwargs
Comment thread
augustehirth marked this conversation as resolved.
Outdated
) -> SimulatedLocalEngine:
"""Creates a virtual engine with a noisy simulator based on a processor id.

Args:
processor_id: The string name of a processor that has available noise data.
simulator_class: The class of the type of simulator to be initialized.
Comment thread
augustehirth marked this conversation as resolved.
Outdated
kwargs: Other arguments which are passed through to the simulator initializer.
The 'noise' argument will be overwritten with a new noise model.

Returns:
A SimulatedLocalEngine that uses a simulator of type simulator_class with a
noise model based on available noise data for the processor processor_id.
"""

calibration = load_median_device_calibration(processor_id)
noise_properties = noise_properties_from_calibration(calibration)
noise_model = NoiseModelFromGoogleNoiseProperties(noise_properties)
kwargs["noise"] = noise_model
simulator = simulator_class(**kwargs)
Comment thread
augustehirth marked this conversation as resolved.
Outdated

device = create_device_from_processor_id(processor_id)
simulated_processor = SimulatedLocalProcessor(
processor_id=processor_id,
sampler=simulator,
device=device,
calibrations={calibration.timestamp // 1000: calibration},
)

return SimulatedLocalEngine([simulated_processor])
17 changes: 17 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 @@ -175,3 +175,20 @@ def test_create_from_proto_no_qubits():
_ = factory.create_noiseless_virtual_engine_from_device(
'sycamore', cirq.UNCONSTRAINED_DEVICE
)


def test_create_noisy_virtual_engine_from_processor_id_and_simulator_class():
for processor_id in ["rainbow", "weber"]:
simulator_class = cirq.Simulator
Comment thread
augustehirth marked this conversation as resolved.
Outdated
engine = factory.create_noisy_virtual_engine_from_processor_id_and_simulator_class(
processor_id=processor_id, simulator_class=simulator_class
)
processor = engine.get_processor(processor_id)
bad_qubit = cirq.GridQubit(10, 10)
circuit = cirq.Circuit(cirq.X(bad_qubit), cirq.measure(bad_qubit))
with pytest.raises(ValueError, match='Qubit not on device'):
_ = processor.run(circuit, repetitions=100)
good_qubit = cirq.GridQubit(5, 4)
circuit = cirq.Circuit(cirq.H(good_qubit), cirq.measure(good_qubit))
with pytest.raises(ValueError, match='.* contains a gate which is not supported.'):
_ = processor.run(circuit, repetitions=100)