QVM Basic Example

View on QuantumAI Run in Google Colab View source on GitHub Download notebook

This notebook walks through running a simple circuit on the Quantum Virtual Machine, including the necessary constraints on a device-runnable circuit and how to satisfy them.

Install Cirq and qsim

Setup

Create a Quantum Virtual Machine

The following cell builds a Quantum Virtual Machine that mimics a particular Google quantum hardware device (currently Willow-pink, Rainbow or Weber) using the following customizable steps:

  • Constructing a cirq.NoiseModel object from device calibration data saved in Cirq. See Representing Noise for more on noise models.
  • Building a qsimcirq.QsimSimulator that uses this noise model. See Noisy Simulation and Noise simulation with qsim for more.
  • Creating a cirq.Device that imposes the same constraints on circuits that the original device would. See Devices for more on these constraint objects.
  • Packaging the simulator and device into an object that implements the cirq.Engine interface that the hardware device would use.

If you don't need this level of control, you can also instantiate a QVM with cirq_google.engine.create_default_noisy_quantum_virtual_machine, as in QVM Creation Template.

Your quantum virtual machine willow_pink is ready, here is the qubit grid: 
========================

                                                        (0, 6)────(0, 7)────(0, 8)
                                                        │         │         │
                                                        │         │         │
                                              (1, 5)────(1, 6)────(1, 7)────(1, 8)
                                              │         │         │         │
                                              │         │         │         │
                                    (2, 4)────(2, 5)────(2, 6)────(2, 7)────(2, 8)────(2, 9)────(2, 10)
                                    │         │         │         │         │         │         │
                                    │         │         │         │         │         │         │
                           (3, 3)───(3, 4)────(3, 5)────(3, 6)────(3, 7)────(3, 8)────(3, 9)────(3, 10)
                           │        │         │         │         │         │         │         │
                           │        │         │         │         │         │         │         │
                  (4, 2)───(4, 3)───(4, 4)────(4, 5)────(4, 6)────(4, 7)────(4, 8)────(4, 9)────(4, 10)────(4, 11)───(4, 12)
                  │        │        │         │         │         │         │         │         │          │         │
                  │        │        │         │         │         │         │         │         │          │         │
         (5, 1)───(5, 2)───(5, 3)───(5, 4)────(5, 5)────(5, 6)────(5, 7)────(5, 8)────(5, 9)────(5, 10)────(5, 11)───(5, 12)
         │        │        │        │         │         │         │         │         │         │          │         │
         │        │        │        │         │         │         │         │         │         │          │         │
(6, 0)───(6, 1)───(6, 2)───(6, 3)───(6, 4)────(6, 5)────(6, 6)────(6, 7)────(6, 8)────(6, 9)────(6, 10)────(6, 11)───(6, 12)───(6, 13)───(6, 14)
                  │        │        │         │         │         │         │         │         │          │         │         │
                  │        │        │         │         │         │         │         │         │          │         │         │
                  (7, 2)───(7, 3)───(7, 4)────(7, 5)────(7, 6)────(7, 7)────(7, 8)────(7, 9)────(7, 10)────(7, 11)───(7, 12)───(7, 13)
                  │        │        │         │         │         │         │         │         │          │         │
                  │        │        │         │         │         │         │         │         │          │         │
                  (8, 2)───(8, 3)───(8, 4)────(8, 5)────(8, 6)────(8, 7)────(8, 8)────(8, 9)────(8, 10)────(8, 11)───(8, 12)
                                    │         │         │         │         │         │         │          │
                                    │         │         │         │         │         │         │          │
                                    (9, 4)────(9, 5)────(9, 6)────(9, 7)────(9, 8)────(9, 9)────(9, 10)────(9, 11)
                                    │         │         │         │         │         │         │
                                    │         │         │         │         │         │         │
                                    (10, 4)───(10, 5)───(10, 6)───(10, 7)───(10, 8)───(10, 9)───(10, 10)
                                                        │         │         │         │
                                                        │         │         │         │
                                                        (11, 6)───(11, 7)───(11, 8)───(11, 9)
                                                        │         │         │
                                                        │         │         │
                                                        (12, 6)───(12, 7)───(12, 8)

Create a circuit, transform it (to make it executable on Google quantum hardware) and choose qubits on the processor.

The circuit you use needs to be device ready, which means it:

  • Is comprised of operations from the device's gate set.
  • Is applied to qubits that exist on the device.
  • Respects the connectivity of qubits on the device.

Below is an example of a circuit that has the correct topology to be placed on the Willow-pink device, and how it is prepared to be run on the QVM.

Create a GHZ state builder circuit

The generalized Greenberger–Horne–Zeilinger (GHZ) state has the form \(\frac{|00..0⟩ + |11..1⟩}{\sqrt{2} }\) and, in this case, will be constructed using 17 qubits, with a Hadamard and a sequence of CNOT gates:

# Define an abstract line of 17 qubits
number_of_qubits = 17
qubits = cirq.LineQubit.range(number_of_qubits)

# Create a GHZ circuit on this qubit line
ghz_circuit = cirq.Circuit(
    cirq.H(qubits[0]),
    *[cirq.CNOT(qubits[i - 1], qubits[i]) for i in range(1, number_of_qubits)],
    cirq.measure(*qubits, key='out'),
)
print(ghz_circuit)
0: ────H───@───────────────────────────────────────────────────────────────M('out')───
           │                                                               │
1: ────────X───@───────────────────────────────────────────────────────────M──────────
               │                                                           │
2: ────────────X───@───────────────────────────────────────────────────────M──────────
                   │                                                       │
3: ────────────────X───@───────────────────────────────────────────────────M──────────
                       │                                                   │
4: ────────────────────X───@───────────────────────────────────────────────M──────────
                           │                                               │
5: ────────────────────────X───@───────────────────────────────────────────M──────────
                               │                                           │
6: ────────────────────────────X───@───────────────────────────────────────M──────────
                                   │                                       │
7: ────────────────────────────────X───@───────────────────────────────────M──────────
                                       │                                   │
8: ────────────────────────────────────X───@───────────────────────────────M──────────
                                           │                               │
9: ────────────────────────────────────────X───@───────────────────────────M──────────
                                               │                           │
10: ───────────────────────────────────────────X───@───────────────────────M──────────
                                                   │                       │
11: ───────────────────────────────────────────────X───@───────────────────M──────────
                                                       │                   │
12: ───────────────────────────────────────────────────X───@───────────────M──────────
                                                           │               │
13: ───────────────────────────────────────────────────────X───@───────────M──────────
                                                               │           │
14: ───────────────────────────────────────────────────────────X───@───────M──────────
                                                                   │       │
15: ───────────────────────────────────────────────────────────────X───@───M──────────
                                                                       │   │
16: ───────────────────────────────────────────────────────────────────X───M──────────

Notice that this circuit consists of a sequence of CNOT gates applied consecutively to a single chain of qubits. The connectivity required by the circuit is simple in the sense that it needs a line of qubits which are only connected to their adjacent neighbor in the sequence. As constructed, this consists of 17 cirq.LineQubits indexed 0, 1, 2...16.

Transform the circuit

Before executing a circuit on (virtual) quantum hardware, the operations in the circuit need to be translated to use the types of gates the device supports. The cirq.optimize_for_target_gateset function does this for you, transforming the operations to use the cirq.CZTargetGateset, which is supported by the Willow-pink processor that this QVM is based on. Learn more about the gate set constraints of Google hardware at the Hardware page.

# Convert the gates in the GHZ circuit to the "CZ" gateset, which the device uses.
translated_ghz_circuit = cirq.optimize_for_target_gateset(
    ghz_circuit, context=cirq.TransformerContext(deep=True), gateset=cirq.CZTargetGateset()
)
print(translated_ghz_circuit)
0: ────PhXZ(a=0.5,x=-0.5,z=1)───@───PhXZ(a=0,x=0,z=1)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M('out')───
                                │                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               │
1: ────PhXZ(a=0.5,x=0.5,z=0)────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                     │                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          │
2: ─────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                          │                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     │
3: ──────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                               │                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                │
4: ───────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                    │                                                                                                                                                                                                                                                                                                                                                                                                                                                           │
5: ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                         │                                                                                                                                                                                                                                                                                                                                                                                                                      │
6: ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                              │                                                                                                                                                                                                                                                                                                                                                                                 │
7: ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                   │                                                                                                                                                                                                                                                                                                                                            │
8: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                                                        │                                                                                                                                                                                                                                                                                                       │
9: ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                                                                                             │                                                                                                                                                                                                                                                                  │
10: ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                                                                                                                                  │                                                                                                                                                                                                                             │
11: ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                                                                                                                                                                       │                                                                                                                                                                                        │
12: ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            │                                                                                                                                                   │
13: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)──────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 │                                                                                                              │
14: ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)─────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      │                                                                         │
15: ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)────────────────M──────────
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           │                                    │
16: ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───M──────────

Choose qubits on the virtual device

Choose qubits on the device to execute your device ready circuit on. Look at the device map (as above) and choose a set of qubits that fit your circuit (eg a line or a block). The Willow-pink, Rainbow and Weber devices have different topologies, some qubit maps may be possible on only one of these devices. As noted, the GHZ example circuit as constructed will fit on a 17 qubit chain of adjacent qubits on the device, so you only need to find this consecutive line of qubits. See Qubit Picking for more advice and methods for selecting qubits.

# Choose qubits on the virtual device
device_qubit_chain = [
    cirq.GridQubit(5, 6),
    cirq.GridQubit(5, 7),
    cirq.GridQubit(4, 7),
    cirq.GridQubit(4, 6),
    cirq.GridQubit(4, 5),
    cirq.GridQubit(5, 5),
    cirq.GridQubit(6, 5),
    cirq.GridQubit(6, 6),
    cirq.GridQubit(6, 7),
    cirq.GridQubit(6, 8),
    cirq.GridQubit(6, 9),
    cirq.GridQubit(7, 9),
    cirq.GridQubit(8, 9),
    cirq.GridQubit(8, 8),
    cirq.GridQubit(8, 7),
    cirq.GridQubit(7, 7),
    cirq.GridQubit(7, 8),
]
# Layout:
#
# q(4, 5)───q(4, 6)───q(4, 7)
# │                   │
# │                   │
# q(5, 5)   q(5, 6)───q(5, 7)
# │
# │
# q(6, 5)───q(6, 6)───q(6, 7)───q(6, 8)───q(6, 9)
#                                         │
#                                         │
#                     q(7, 7)───q(7, 8)   q(7, 9)
#                     │                   │
#                     │                   │
#                     q(8, 7)───q(8, 8)───q(8, 9)

Map the transformed circuit to the qubits you chose on the device

The transformed GHZ circuit still needs to be re-mapped to use the selected qubits. The transform_qubits function of cirq.Circuit does this automatically, returning a new transformed circuit when given a qubit mapping function.

Because the connectivity qubit chain in the GHZ circuit example follows the sequence of cirq.LineQubits in qubits, it suffices to zip the circuit's qubits together with the chosen device qubits, since both are connected chains of 17 qubits. The dictionary built from these paired qubits can then be turned into a simple lambda function for use with transform_qubits, as follows:

# Map the line of circuit qubits to the chosen line of device qubits.
qubit_map = dict(zip(qubits, device_qubit_chain))
# Then replace qubits in the circuit according to that map.
device_ready_ghz_circuit = translated_ghz_circuit.transform_qubits(lambda q: qubit_map[q])
print(device_ready_ghz_circuit)
(4, 5): ──────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                   │                                    │                                                                                                                                                                                                                                                                                                                                                                                                                                                           │
(4, 6): ─────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                              │                                                                         │                                                                                                                                                                                                                                                                                                                                                                                                                                                           │
(4, 7): ────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)─────────────────────────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                         │                                                                                                              │                                                                                                                                                                                                                                                                                                                                                                                                                                                           │
(5, 5): ─────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                         │                                                                                                                                                   │                                                                                                                                                                                                                                                                                                                                                                                                                      │
(5, 6): ───PhXZ(a=0.5,x=-0.5,z=1)───@───PhXZ(a=0,x=0,z=1)────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M('out')───
                                    │                                    │                                                                                                                                                   │                                                                                                                                                                                                                                                                                                                                                                                                                      │
(5, 7): ───PhXZ(a=0.5,x=0.5,z=0)────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                             │                                                                                                                                                                                                                                                                                                                                                                                                                      │
(6, 5): ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                  │                                                                                                                                                                                                                                                                                                                                                                                 │
(6, 6): ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                       │                                                                                                                                                                                                                                                                                                                                            │
(6, 7): ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                                                            │                                                                                                                                                                                                                                                                                                       │
(6, 8): ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                                                                                                 │                                                                                                                                                                                                                                                                  │
(6, 9): ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                                                                                                                                      │                                                                                                                                                                                                                             │
(7, 7): ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)────────────────M──────────
                                                                                                                                                                                                                                                                                                                                                                                                                      │                                                                                                                                                   │                                    │                                    │
(7, 8): ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼───PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───M──────────
                                                                                                                                                                                                                                                                                                                                                                                                                      │                                                                                                                                                   │                                                                         │
(7, 9): ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)──────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                                                                                                                                                                           │                                                                                                              │                                                                         │
(8, 7): ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)─────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                                                                                                                                                                           │                                                                         │                                                                                                              │
(8, 8): ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼───PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)──────────────────────────────────────────────────────────────────────────────────────────M──────────
                                                                                                                                                                                                                                                                                                                                                                                                                                                           │                                    │                                                                                                                                                   │
(8, 9): ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────PhXZ(a=0.5,x=0.5,z=0)────────────@───PhXZ(a=-0.5,x=0.5,z=-8.88e-16)───@───PhXZ(a=0,x=0,z=1)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────M──────────

Running other circuits

In principle, you can run any custom Circuit with a quantum virtual machine, but realistically there are some constraints. As mentioned, the circuits need to be mappable to the device. Additionally, the number of qubits that are simulatable depends highly on the hardware available to you and how long you are able to run your simulation. As the QVM is instantiated as above, with a qsimcirq.QSimSimulator, it only uses the default, local qsim simulator. However, qsim has plenty of support for being run in a Google Cloud instance, with a variable amount of compute power. In order to get the most capacity possible for qsim, use Multinode Simulation.

For an example of building and running a much larger circuit, see the QVM Stabilizer Example tutorial.

Execute Your Circuit on the Quantum Virtual Machine

You can run the now device-ready circuit, as you would with any other cirq.Engine instance, by getting a sampler from it and using the run function on the circuits. Your choice of repetitions is intrinsically related to the accuracy of your simulated results. We recommend 3000 repetitions for trial runs, and 10,000 repetitions for accuracy-critical runs, but you can stick to one to ten repetitions when testing a code pipeline. You can read more about this in this paper.

Circuit successfully executed on your quantum virtual machine willow_pink
QVM runtime: 11.92s (3000 repetitions)
You can now print or plot "results"

Visualize Output

Finally, you can use a state histogram to plot the measured results.

ax = cirq.plot_state_histogram(results.histogram(key='out'))
ax.get_xaxis().set_ticks([])
plt.gcf().set_size_inches(10, 4)
plt.show(ax)

png

The leftmost and rightmost bars correspond to \(|00..0\rangle\) and \(|11..1\rangle\), respectively, which matches expectation for a GHZ state. Natural \(|1\rangle → |0\rangle\) decay causes \(|11..1\rangle\) to be much less common than \(|00..0\rangle\), and other states also appear due to the various error mechanisms in the hardware that are mimicked by the simulated noise model. Learn more about these errors here.