Device object representing Google devices with a grid qubit layout.

For end users, instances of this class are typically accessed via Engine.get_processor('processor_name').get_device().

This class is compliant with the core cirq.Device abstraction. In particular:

* Device information is captured in the `metadata` property.
* An instance of `GridDevice` can be used to validate circuits, moments, and operations.

Get an instance of a Google grid device.

>>> device = cirq_google.engine.create_device_from_processor_id("rainbow")

Print the grid layout of the device.

>>> print(device)
                  (3, 2)
         (4, 1)───(4, 2)───(4, 3)
         │        │        │
         │        │        │
(5, 0)───(5, 1)───(5, 2)───(5, 3)───(5, 4)
         │        │        │        │
         │        │        │        │
         (6, 1)───(6, 2)───(6, 3)───(6, 4)───(6, 5)
                  │        │        │        │
                  │        │        │        │
                  (7, 2)───(7, 3)───(7, 4)───(7, 5)───(7, 6)
                           │        │        │
                           │        │        │
                           (8, 3)───(8, 4)───(8, 5)
                                    (9, 4)

Determine whether a circuit can be run on the device.

>>> circuit = cirq.Circuit(cirq.X(cirq.q(5, 1)))
>>> device.validate_circuit(circuit)  # Raises a ValueError if the circuit is invalid.

Determine whether an operation can be run on the device.

>>> operation = cirq.X(cirq.q(5, 1))
>>> device.validate_operation(operation)  # Raises a ValueError if the operation is invalid.

Get the cirq.Gateset containing valid gates for the device, and inspect the full list of valid gates.

>>> gateset = device.metadata.gateset
>>> print(gateset)

Determine whether a gate is available on the device.

>>> gate = cirq.X
>>> gate in device.metadata.gateset
  • Get a collection of valid qubits on the device.
>>> device.metadata.qubit_set
frozenset({...cirq.GridQubit(6, 4)...})
  • Get a collection of valid qubit pairs for two-qubit gates.
>>> device.metadata.qubit_pairs
  • Get a collection of isolated qubits, i.e. qubits which are not part of any qubit pair.
>>> device.metadata.isolated_qubits
  • Get a collection of approximate gate durations for every gate supported by the device.
>>> device.metadata.gate_durations
  • Get a collection of valid CompilationTargetGatesets for the device, which can be used to transform a circuit to one which only contains gates from a native target gateset supported by the device.
>>> device.metadata.compilation_target_gatesets
  • Assuming valid CompilationTargetGatesets exist for the device, select the first one and use it to transform a circuit to one which only contains gates from a native target gateset supported by the device.
>>> circuit = cirq.optimize_for_target_gateset(
...     circuit,
...     gateset=device.metadata.compilation_target_gatesets[0]
... )
>>> print(circuit)
(5, 1): ───PhXZ(a=0,x=1,z=0)───

Notes about CompilationTargetGatesets:

  • If a device contains gates which yield multiple compilation target gatesets, the user can only choose one target gateset to compile to. For example, a device may contain both SYC and SQRT_ISWAP gates which yield two separate target gatesets, but a circuit can only be compiled to either SYC or SQRT_ISWAP for its two-qubit gates, not both.
  • For a given compilation target gateset, gates which are part of the device's gateset but not the target gateset are not decomposed. However, they may still be merged with other gates in the circuit.
  • A circuit which contains cirq.WaitGates will be dropped if it is transformed using CompilationTargetGatesets generated by GridDevice. To better control circuit timing, insert WaitGates after the circuit has been transformed.

Notes for cirq_google internal implementation:

For Google devices, the DeviceSpecification proto is the main specification for device information surfaced by the Quantum Computing Service. Thus, this class should typically be instantiated using a DeviceSpecification proto via the from_proto() class method.

metadata Get metadata information for the device.



View source

Deserializes the DeviceSpecification to a GridDevice.

proto The DeviceSpecification proto describing a Google device.

ValueError If the given DeviceSpecification is invalid. It is invalid if:


View source

Serializes the GridDevice to a DeviceSpecification.

out Optional DeviceSpecification to be populated. Fields are populated in-place.

The populated DeviceSpecification if out is specified, or the newly created DeviceSpecification.


Raises an exception if a circuit is not valid.

circuit The circuit to validate.

ValueError The circuit isn't valid for this device.


Raises an exception if a moment is not valid.

moment The moment to validate.

ValueError The moment isn't valid for this device.


View source

Raises an exception if an operation is not valid.

An operation is valid if

* The operation is in the device gateset.
* The operation targets a valid qubit
* The operation targets a valid qubit pair, if it is a two-qubit operation.

operation The operation to validate.

ValueError The operation isn't valid for this device.