Skip to content

Commit

Permalink
Engine creator helper function (quantumlib#5658)
Browse files Browse the repository at this point in the history
Add a helper function to create a virtual engine with a noisy simulator.
  • Loading branch information
augustehirth authored and rht committed May 1, 2023
1 parent 71edec5 commit 7655251
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
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 @@ -72,6 +72,7 @@
from cirq_google.engine.validating_sampler import ValidatingSampler

from cirq_google.engine.virtual_engine_factory import (
create_default_noisy_quantum_virtual_machine,
create_device_from_processor_id,
create_noiseless_virtual_engine_from_device,
create_noiseless_virtual_engine_from_proto,
Expand Down
46 changes: 45 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,44 @@ 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_default_noisy_quantum_virtual_machine(
processor_id: str, simulator_class: Type[SimulatesSamples] = None, **kwargs
) -> 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. The
simulator class initializer needs to support the `noise` parameter.
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.
"""

if simulator_class is None:
try: # coverage: ignore
import qsimcirq # type: ignore

simulator_class = qsimcirq.Simulator # coverage: ignore
except ImportError:
simulator_class = cirq.Simulator # coverage: ignore

calibration = load_median_device_calibration(processor_id)
noise_properties = noise_properties_from_calibration(calibration)
noise_model = NoiseModelFromGoogleNoiseProperties(noise_properties)
simulator = simulator_class(noise=noise_model, **kwargs) # type: ignore

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])
16 changes: 16 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,19 @@ def test_create_from_proto_no_qubits():
_ = factory.create_noiseless_virtual_engine_from_device(
'sycamore', cirq.UNCONSTRAINED_DEVICE
)


def test_create_default_noisy_quantum_virtual_machine():
for processor_id in ["rainbow", "weber"]:
engine = factory.create_default_noisy_quantum_virtual_machine(
processor_id=processor_id, simulator_class=cirq.Simulator
)
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)

0 comments on commit 7655251

Please sign in to comment.