|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 | 15 | """Functions to instantiate SimulatedLocalEngines to simulate various Google Devices."""
|
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
16 | 19 | import json
|
17 | 20 | import pathlib
|
18 | 21 | import time
|
19 |
| -from typing import cast, List, Optional, Type, Union |
| 22 | +from typing import cast, Dict, List, Optional, Type, TYPE_CHECKING, Union |
20 | 23 |
|
21 | 24 | import google.protobuf.text_format as text_format
|
22 | 25 |
|
23 | 26 | import cirq
|
24 |
| -from cirq.sim.simulator import SimulatesSamples |
25 | 27 | from cirq_google.api import v2
|
26 | 28 | from cirq_google.devices import grid_device
|
27 | 29 | from cirq_google.devices.google_noise_properties import NoiseModelFromGoogleNoiseProperties
|
28 | 30 | from cirq_google.engine import calibration, engine_validator, simulated_local_processor, util
|
29 | 31 | from cirq_google.engine.calibration_to_noise_properties import noise_properties_from_calibration
|
30 | 32 | from cirq_google.engine.simulated_local_engine import SimulatedLocalEngine
|
31 | 33 | from cirq_google.engine.simulated_local_processor import SimulatedLocalProcessor
|
| 34 | +from cirq_google.ops import fsim_gate_family |
| 35 | + |
| 36 | +if TYPE_CHECKING: |
| 37 | + import cirq_google |
32 | 38 |
|
33 | 39 | MOST_RECENT_TEMPLATES = {
|
34 | 40 | 'rainbow': 'rainbow_2021_12_10_device_spec_for_grid_device.proto.txt',
|
@@ -303,8 +309,8 @@ def create_device_spec_from_processor_id(processor_id: str) -> v2.device_pb2.Dev
|
303 | 309 | return _create_device_spec_from_template(template_name)
|
304 | 310 |
|
305 | 311 |
|
306 |
| -def create_device_from_processor_id(processor_id: str) -> cirq.Device: |
307 |
| - """Generates a `cirq.Device` for a given processor ID. |
| 312 | +def create_device_from_processor_id(processor_id: str) -> grid_device.GridDevice: |
| 313 | + """Generates a `cirq_google.GridDevice` for a given processor ID. |
308 | 314 |
|
309 | 315 | Args:
|
310 | 316 | processor_id: name of the processor to simulate.
|
@@ -385,7 +391,7 @@ def create_noiseless_virtual_engine_from_latest_templates() -> SimulatedLocalEng
|
385 | 391 |
|
386 | 392 |
|
387 | 393 | def create_default_noisy_quantum_virtual_machine(
|
388 |
| - processor_id: str, simulator_class: Optional[Type[SimulatesSamples]] = None, **kwargs |
| 394 | + processor_id: str, simulator_class: Optional[Type[cirq.SimulatesSamples]] = None, **kwargs |
389 | 395 | ) -> SimulatedLocalEngine:
|
390 | 396 | """Creates a virtual engine with a noisy simulator based on a processor id.
|
391 | 397 |
|
@@ -425,3 +431,40 @@ def create_default_noisy_quantum_virtual_machine(
|
425 | 431 | )
|
426 | 432 |
|
427 | 433 | return SimulatedLocalEngine([simulated_processor])
|
| 434 | + |
| 435 | + |
| 436 | +def extract_gate_times_ns_from_device( |
| 437 | + device: cirq_google.GridDevice, |
| 438 | +) -> Dict[Type[cirq.Gate], float]: |
| 439 | + """Extract a dictionary of gate durations in nanoseconds from GridDevice object. |
| 440 | +
|
| 441 | + The durations are obtained from `GridDevice.metadata` field which is |
| 442 | + provided for devices obtained with `create_device_from_processor_id`. |
| 443 | +
|
| 444 | + Args: |
| 445 | + device: Object representing Google devices with a grid qubit layout. |
| 446 | +
|
| 447 | + Returns: |
| 448 | + A dictionary of gate durations versus supported gate types. Returns an |
| 449 | + empty dictionary when `device.metadata` do not provide gate durations. |
| 450 | + """ |
| 451 | + gate_times_ns: Dict[Type[cirq.Gate], float] = {} |
| 452 | + if not device.metadata.gate_durations: |
| 453 | + return gate_times_ns |
| 454 | + gate_type: Type[cirq.Gate] # pragma: no cover |
| 455 | + for gate_family, duration in device.metadata.gate_durations.items(): |
| 456 | + if isinstance(gate_family, fsim_gate_family.FSimGateFamily): |
| 457 | + for g in gate_family.gates_to_accept: |
| 458 | + gate_type = g if isinstance(g, type) else type(g) |
| 459 | + gate_times_ns[gate_type] = duration.total_nanos() |
| 460 | + continue |
| 461 | + # ordinary GateFamily here |
| 462 | + gate_type = ( |
| 463 | + gate_family.gate if isinstance(gate_family.gate, type) else type(gate_family.gate) |
| 464 | + ) |
| 465 | + gate_times_ns[gate_type] = duration.total_nanos() |
| 466 | + # cirq.IdentityGate can leak from FSimGateFamily. Exclude to default to zero duration. |
| 467 | + _ = gate_times_ns.pop(cirq.IdentityGate, None) |
| 468 | + # cirq.WaitGate has variable duration and should not be included here. |
| 469 | + _ = gate_times_ns.pop(cirq.WaitGate, None) |
| 470 | + return gate_times_ns |
0 commit comments