|
14 | 14 |
|
15 | 15 | """Functions to instantiate SimulatedLocalEngines to simulate various Google Devices."""
|
16 | 16 | import json
|
17 |
| -from typing import cast, Iterable, List, Optional, Union |
| 17 | +from typing import cast, Iterable, List, Optional, Union, Type |
18 | 18 | import pathlib
|
19 | 19 | import time
|
20 | 20 |
|
21 | 21 | import google.protobuf.text_format as text_format
|
22 | 22 | import cirq
|
| 23 | +from cirq.sim.simulator import SimulatesSamples |
23 | 24 | from cirq_google.api import v2
|
24 | 25 | from cirq_google.engine import calibration, engine_validator, simulated_local_processor, util
|
25 | 26 | from cirq_google.devices import grid_device
|
| 27 | +from cirq_google.devices.google_noise_properties import NoiseModelFromGoogleNoiseProperties |
26 | 28 | from cirq_google.serialization import serializable_gate_set
|
| 29 | +from cirq_google.engine.calibration_to_noise_properties import noise_properties_from_calibration |
27 | 30 | from cirq_google.engine.simulated_local_engine import SimulatedLocalEngine
|
28 | 31 | from cirq_google.engine.simulated_local_processor import SimulatedLocalProcessor
|
29 | 32 |
|
@@ -367,3 +370,44 @@ def create_noiseless_virtual_engine_from_latest_templates() -> SimulatedLocalEng
|
367 | 370 | processor_ids = list(MOST_RECENT_TEMPLATES.keys())
|
368 | 371 | template_names = [MOST_RECENT_TEMPLATES[k] for k in processor_ids]
|
369 | 372 | return create_noiseless_virtual_engine_from_templates(processor_ids, template_names)
|
| 373 | + |
| 374 | + |
| 375 | +def create_default_noisy_quantum_virtual_machine( |
| 376 | + processor_id: str, simulator_class: Type[SimulatesSamples] = None, **kwargs |
| 377 | +) -> SimulatedLocalEngine: |
| 378 | + """Creates a virtual engine with a noisy simulator based on a processor id. |
| 379 | +
|
| 380 | + Args: |
| 381 | + processor_id: The string name of a processor that has available noise data. |
| 382 | + simulator_class: The class of the type of simulator to be initialized. The |
| 383 | + simulator class initializer needs to support the `noise` parameter. |
| 384 | + kwargs: Other arguments which are passed through to the simulator initializer. |
| 385 | + The 'noise' argument will be overwritten with a new noise model. |
| 386 | +
|
| 387 | + Returns: |
| 388 | + A SimulatedLocalEngine that uses a simulator of type simulator_class with a |
| 389 | + noise model based on available noise data for the processor processor_id. |
| 390 | + """ |
| 391 | + |
| 392 | + if simulator_class is None: |
| 393 | + try: # coverage: ignore |
| 394 | + import qsimcirq # type: ignore |
| 395 | + |
| 396 | + simulator_class = qsimcirq.Simulator # coverage: ignore |
| 397 | + except ImportError: |
| 398 | + simulator_class = cirq.Simulator # coverage: ignore |
| 399 | + |
| 400 | + calibration = load_median_device_calibration(processor_id) |
| 401 | + noise_properties = noise_properties_from_calibration(calibration) |
| 402 | + noise_model = NoiseModelFromGoogleNoiseProperties(noise_properties) |
| 403 | + simulator = simulator_class(noise=noise_model, **kwargs) # type: ignore |
| 404 | + |
| 405 | + device = create_device_from_processor_id(processor_id) |
| 406 | + simulated_processor = SimulatedLocalProcessor( |
| 407 | + processor_id=processor_id, |
| 408 | + sampler=simulator, |
| 409 | + device=device, |
| 410 | + calibrations={calibration.timestamp // 1000: calibration}, |
| 411 | + ) |
| 412 | + |
| 413 | + return SimulatedLocalEngine([simulated_processor]) |
0 commit comments