Selecting qubits and calibrating circuits for an experiment

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

This tutorial shows how to select good qubits and calibrate circuits for an experiment. The high-level steps are:

  1. Using the latest calibration data, identify and select a good set of qubits.
  2. Calibrate qubit pairs in your circuit using Floquet calibration and/or XEB calibration.
  3. Run a Loschmidt echo benchmark with and without calibration.
  4. Apply corrections to your circuit from the calibration which performs best on the Loschmidt echo.

These steps are recommended as a minimum procedure for running on the Quantum Computing Service (QCS). Additional steps to mitigating errors, perhaps specific to your experiment, can be added on top of this workflow.

Disclaimer: The data shown in this tutorial is exemplary and not representative of the QCS in production.

Setup

try:
    import cirq
except ImportError:
    !pip install cirq --quiet

Getting OAuth2 credentials.
Press enter after entering the verification code.
Authentication complete.
Successful authentication to Google Cloud.
import cirq
from cirq.experiments import random_quantum_circuit_generation as rqcg
import cirq_google as cg

import matplotlib.pyplot as plt
import numpy as np
import tqdm

Helper functions

Select qubits

Below, select a processor and (comma-separated list of) calibration metric(s) to visualize the latest calibration report.

png

Using this report as a guide, select a set of qubits.

# Select qubit indices here.
qubit_indices = [
    (2, 5), (2, 6), (2, 7), (2, 8), (3, 8), 
    (3, 7), (3, 6), (3, 5), (4, 5), (4, 6)  
]
qubits = [cirq.GridQubit(*idx) for idx in qubit_indices]

Calibrate qubit pairs

In the next cell, define the quantum circuit for your experiment.

"""Define the quantum circuit for your experiment here."""
circuit = create_random_circuit(qubits, cycles=10)

The structure of the circuit instructs the calibration API on which qubit pairs to calibrate.

Floquet calibration

Specify the parameters of the cirq.FSimGate to characterize with Floquet calibration below.

floquet_options = cg.FloquetPhasedFSimCalibrationOptions(
    characterize_theta=False,
    characterize_zeta=True,
    characterize_chi=False,
    characterize_gamma=True,
    characterize_phi=False,
)

Execute the next cell to run Floquet calibration.

sampler = cg.get_engine_sampler(
    project_id=project_id,
    processor_id=processor_id, 
    gate_set_name="sqrt_iswap",
)

# Get characterization requests.
_, floquet_characterization_requests = cg.prepare_characterization_for_moments(
    circuit,
    options=floquet_options,
)

# Characterize the requests on the engine.
floquet_characterizations = cg.run_calibrations(
    floquet_characterization_requests, 
    sampler,
)

The floquet_characterizations can now be used to make corrections to circuits (shown below).

XEB calibration

Specify the cycle depths and other options for XEB calibration below. Note that all cirq.FSimGate parameters are characterized by default.

xeb_options = cg.LocalXEBPhasedFSimCalibrationOptions(
    cycle_depths=(5, 25, 50, 100),
    n_processes=1,
    fsim_options=cirq.experiments.XEBPhasedFSimCharacterizationOptions(
        characterize_theta=False,
        characterize_zeta=True,
        characterize_chi=True,
        characterize_gamma=True,
        characterize_phi=False,
    ),
)

Execute the next cell to run XEB calibration.

# Get characterization requests.
_, xeb_characterization_requests = cg.prepare_characterization_for_moments(
    circuit,
    options=xeb_options,
)

# Characterize the requests on the engine.
xeb_characterizations = cg.run_calibrations(
    xeb_characterization_requests, 
    sampler,
)
100%|██████████| 45/45 [00:48<00:00,  1.07s/it]
100%|██████████| 45/45 [00:45<00:00,  1.00s/it]
100%|██████████| 45/45 [00:55<00:00,  1.23s/it]
100%|██████████| 45/45 [07:04<00:00,  9.44s/it]

The xeb_characterizations can now be used to make corrections to circuits (shown below).

Run a Loschmidt echo benchmark

Execute the following cells to run a Loschmidt echo benchmark with and without calibration to compare results.

"""Setup the Loschmidt echo experiment."""
cycle_values = range(0, 40 + 1, 4)
nreps = 20_000
trials = 10

loschmidt_echo_batch = [
    create_loschmidt_echo_circuit(qubits, cycles=c, seed=trial)
    for trial in range(trials) for c in cycle_values
]
"""Run a Loschmidt echo with no / Floquet / XEB calibration."""
calibrations = {
    "No calibration": (None, None),                                
    "Floquet calibration": (floquet_options, floquet_characterizations),
    "XEB calibration": (xeb_options, xeb_characterizations),
}

all_probs = []
for options, characterizations in tqdm.tqdm(calibrations.values()):
    if options is None:
        batch = loschmidt_echo_batch
    else:
        # Apply corrections to the batch from the characterizations.
        characterized_batch = [
            cg.prepare_characterization_for_moments(circuit, options)[0] 
            for circuit in loschmidt_echo_batch
        ]
        batch = [
            cg.make_zeta_chi_gamma_compensation_for_moments(circ, characterizations).circuit
            for circ in characterized_batch
        ]

    # Run the Loschmidt echo batch.
    results = sampler.run_batch(programs=batch, repetitions=nreps)

    # Compute survival probabilities.
    probs = [to_ground_state_prob(*res) for res in results]
    all_probs.append(np.array(probs).reshape(trials, len(cycle_values)))
100%|██████████| 3/3 [14:34<00:00, 291.52s/it]

The next cell plots the results.

for probs, label in zip(all_probs, calibrations.keys()):
    plt.semilogy(cycle_values, np.average(probs, axis=0), lw=3, label=label)

plt.xlabel("Cycles")
plt.ylabel("Survival probability")
plt.legend();

png

Apply corrections to your circuit

Now, use the calibration which gives the best results on the Loschmidt echo benchmark. (The slower the survival probability decays, the better the result.) The following cell shows how to apply corrections to your circuit from either calibration.

# Pick Floquet or XEB options/characterizations here.
options = xeb_options
characterizations = xeb_characterizations

characterized_circuit, _ = cg.prepare_characterization_for_moments(
    circuit, options=options,
)
calibrated_circuit = cg.make_zeta_chi_gamma_compensation_for_moments(
    characterized_circuit,
    characterizations,
)
print("Calibrated circuit:\n")
print(calibrated_circuit.circuit)
Calibrated circuit:

                                          ┌──────────────────────────────────────────┐                                                ┌────────────────────────────┐                                                                                                                                                                                                                                                                                                        ┌──────────────────────────────────────────┐                                                ┌────────────────────────────┐                                                ┌──────────────────────────────────────────┐                                                ┌────────────────────────────┐
(2, 5): ───PhX(0.25)^0.5───────────────────────────────────────────────────────────────────────────────PhX(-0.5)^0.5────Rz(0.005π)─────FSim(0.25π, 0)──────────────────Rz(0.035π)────PhX(-0.75)^0.5───Rz(0.037π)────FSim(0.25π, 0)───Rz(0.079π)────PhX(0.75)^0.5─────────────────────────────────────────────────PhX(-0.75)^0.5───Rz(0.037π)────FSim(0.25π, 0)───Rz(0.079π)────PhX(0.5)^0.5──────────────────────────────────────────────────PhX(-0.75)^0.5──────────────────────────────────────────────────────────────────────────────PhX(-0.5)^0.5────Rz(0.005π)─────FSim(0.25π, 0)──────────────────Rz(0.035π)────PhX(0)^0.5──────────────────────────────────────────────────────────────────────────────────PhX(-0.25)^0.5───Rz(0.005π)─────FSim(0.25π, 0)──────────────────Rz(0.035π)────PhX(0.75)^0.5────
                                                                                                                                       │                                                                            │                                                                                                                           │                                                                                                                                                                                                                        │                                                                                                                                                                         │
(2, 6): ───PhX(0)^0.5───────Rz(0.056π)─────FSim(0.25π, 0)────────────────────────────────Rz(0.056π)────PhX(0.75)^0.5───────────────────┼─────────────────────────────────────────────PhX(0.5)^0.5─────Rz(0.006π)────FSim(0.25π, 0)───Rz(-0.035π)───PhX(1)^0.5───────Rz(0.002π)────FSim(0.25π, 0)───Rz(0.01π)─────PhX(-0.25)^0.5───Rz(0.006π)────FSim(0.25π, 0)───Rz(-0.035π)───PhX(0.25)^0.5────Rz(0.002π)────FSim(0.25π, 0)───Rz(0.01π)─────PhX(-0.5)^0.5────Rz(0.056π)─────FSim(0.25π, 0)────────────────────────────────Rz(0.056π)────PhX(0.75)^0.5───────────────────┼─────────────────────────────────────────────PhX(-0.75)^0.5───Rz(0.056π)─────FSim(0.25π, 0)────────────────────────────────Rz(0.056π)────PhX(1)^0.5──────────────────────┼─────────────────────────────────────────────PhX(-0.5)^0.5────
                                           │                                                                                           │                                                                                                                                          │                                                                                                                           │                                                              │                                                                                           │                                                                             │                                                                                           │
(2, 7): ───PhX(-0.75)^0.5──────────────────┼───────────────────────────────────────────────────────────PhX(0)^0.5───────Rz(-0.004π)────┼─────────────FSim(0.25π, 0)────Rz(-0.005π)───PhX(-0.25)^0.5───Rz(0.008π)────FSim(0.25π, 0)───Rz(-0.003π)───PhX(0.5)^0.5─────Rz(0.025π)────FSim(0.25π, 0)───Rz(0.018π)────PhX(-0.5)^0.5────Rz(0.008π)────FSim(0.25π, 0)───Rz(-0.003π)───PhX(0.25)^0.5────Rz(0.025π)────FSim(0.25π, 0)───Rz(0.018π)────PhX(1)^0.5──────────────────────┼───────────────────────────────────────────────────────────PhX(0.25)^0.5────Rz(-0.004π)────┼─────────────FSim(0.25π, 0)────Rz(-0.005π)───PhX(-0.25)^0.5──────────────────┼───────────────────────────────────────────────────────────PhX(0.75)^0.5────Rz(-0.004π)────┼─────────────FSim(0.25π, 0)────Rz(-0.005π)───PhX(0.25)^0.5────
                                           │                                                                                           │             │                                                              │                                                                                                                           │                                                                                                                            │                                                                                           │             │                                                               │                                                                                           │             │
(2, 8): ───PhX(-0.5)^0.5────Rz(0.016π)─────┼─────────────FSim(0.25π, 0)──────────────────Rz(0.015π)────PhX(-0.75)^0.5──────────────────┼─────────────┼───────────────────────────────PhX(0.5)^0.5─────Rz(-0.012π)───FSim(0.25π, 0)───Rz(-0.001π)───PhX(0.25)^0.5─────────────────────────────────────────────────PhX(-0.25)^0.5───Rz(-0.012π)───FSim(0.25π, 0)───Rz(-0.001π)───PhX(-0.75)^0.5────────────────────────────────────────────────PhX(1)^0.5───────Rz(0.016π)─────┼─────────────FSim(0.25π, 0)──────────────────Rz(0.015π)────PhX(-0.75)^0.5──────────────────┼─────────────┼───────────────────────────────PhX(0.25)^0.5────Rz(0.016π)─────┼─────────────FSim(0.25π, 0)──────────────────Rz(0.015π)────PhX(-0.5)^0.5───────────────────┼─────────────┼───────────────────────────────PhX(-0.75)^0.5───
                                           │             │                                                                             │             │                                                                                                                                                                                                                                                                                                                       │             │                                                                             │             │                                                               │             │                                                                             │             │
(3, 5): ───PhX(0)^0.5───────Rz(-0.007π)────┼─────────────┼─────────────FSim(0.25π, 0)────Rz(0.008π)────PhX(-0.5)^0.5────Rz(0.018π)─────FSim(0.25π, 0)┼─────────────────Rz(-0.012π)───PhX(-0.25)^0.5────────────────────────────────────────────────PhX(0.5)^0.5─────Rz(0.005π)────FSim(0.25π, 0)───Rz(-0.01π)────PhX(0.25)^0.5─────────────────────────────────────────────────PhX(1)^0.5───────Rz(0.005π)────FSim(0.25π, 0)───Rz(-0.01π)────PhX(-0.25)^0.5───Rz(-0.007π)────┼─────────────┼─────────────FSim(0.25π, 0)────Rz(0.008π)────PhX(-0.75)^0.5───Rz(0.018π)─────FSim(0.25π, 0)┼─────────────────Rz(-0.012π)───PhX(0.5)^0.5─────Rz(-0.007π)────┼─────────────┼─────────────FSim(0.25π, 0)────Rz(0.008π)────PhX(-0.75)^0.5───Rz(0.018π)─────FSim(0.25π, 0)┼─────────────────Rz(-0.012π)───PhX(-0.5)^0.5────
                                           │             │             │                                                                             │                                                                                                                            │                                                                                                                           │                                                              │             │             │                                                                             │                                                               │             │             │                                                                             │
(3, 6): ───PhX(-0.25)^0.5───Rz(0.009π)─────FSim(0.25π, 0)┼─────────────┼─────────────────Rz(0.009π)────PhX(0.5)^0.5─────Rz(-0.042π)────FSim(0.25π, 0)┼─────────────────Rz(-0.026π)───PhX(-0.75)^0.5───Rz(0.025π)────FSim(0.25π, 0)───Rz(0.029π)────PhX(0)^0.5───────Rz(-0.017π)───FSim(0.25π, 0)───Rz(-0.002π)───PhX(-0.5)^0.5────Rz(0.025π)────FSim(0.25π, 0)───Rz(0.029π)────PhX(-0.75)^0.5───Rz(-0.017π)───FSim(0.25π, 0)───Rz(-0.002π)───PhX(0)^0.5───────Rz(0.009π)─────FSim(0.25π, 0)┼─────────────┼─────────────────Rz(0.009π)────PhX(-0.5)^0.5────Rz(-0.042π)────FSim(0.25π, 0)┼─────────────────Rz(-0.026π)───PhX(-0.25)^0.5───Rz(0.009π)─────FSim(0.25π, 0)┼─────────────┼─────────────────Rz(0.009π)────PhX(-0.75)^0.5───Rz(-0.042π)────FSim(0.25π, 0)┼─────────────────Rz(-0.026π)───PhX(0.25)^0.5────
                                                         │             │                                                               │             │                                                              │                                                                                                                           │                                                                                                                                          │             │                                                               │             │                                                                             │             │                                                               │             │
(3, 7): ───PhX(0.75)^0.5─────────────────────────────────┼─────────────┼───────────────────────────────PhX(0)^0.5───────Rz(0.015π)─────┼─────────────FSim(0.25π, 0)────Rz(0.016π)────PhX(-0.5)^0.5────Rz(0.011π)────FSim(0.25π, 0)───Rz(0.006π)────PhX(-0.75)^0.5───Rz(0.012π)────FSim(0.25π, 0)───Rz(0.003π)────PhX(0.75)^0.5────Rz(0.011π)────FSim(0.25π, 0)───Rz(0.006π)────PhX(1)^0.5───────Rz(0.012π)────FSim(0.25π, 0)───Rz(0.003π)────PhX(-0.25)^0.5────────────────────────────────┼─────────────┼───────────────────────────────PhX(-0.75)^0.5───Rz(0.015π)─────┼─────────────FSim(0.25π, 0)────Rz(0.016π)────PhX(0)^0.5────────────────────────────────────┼─────────────┼───────────────────────────────PhX(-0.5)^0.5────Rz(0.015π)─────┼─────────────FSim(0.25π, 0)────Rz(0.016π)────PhX(1)^0.5───────
                                                         │             │                                                               │                                                                                                                                          │                                                                                                                           │                                                                            │             │                                                               │                                                                                           │             │                                                               │
(3, 8): ───PhX(-0.75)^0.5───Rz(-0.016π)──────────────────FSim(0.25π, 0)┼─────────────────Rz(-0.015π)───PhX(-0.25)^0.5──────────────────┼─────────────────────────────────────────────PhX(0.25)^0.5─────────────────────────────────────────────────PhX(-0.25)^0.5───Rz(-0.016π)───FSim(0.25π, 0)───Rz(-0.007π)───PhX(0.25)^0.5─────────────────────────────────────────────────PhX(1)^0.5───────Rz(-0.016π)───FSim(0.25π, 0)───Rz(-0.007π)───PhX(-0.75)^0.5───Rz(-0.016π)──────────────────FSim(0.25π, 0)┼─────────────────Rz(-0.015π)───PhX(0.75)^0.5───────────────────┼─────────────────────────────────────────────PhX(0)^0.5───────Rz(-0.016π)──────────────────FSim(0.25π, 0)┼─────────────────Rz(-0.015π)───PhX(0.75)^0.5───────────────────┼─────────────────────────────────────────────PhX(-0.75)^0.5───
                                                                       │                                                               │                                                                                                                                                                                                                                                                                                                                                                 │                                                               │                                                                                                         │                                                               │
(4, 5): ───PhX(-0.25)^0.5───Rz(-0.013π)────────────────────────────────FSim(0.25π, 0)────Rz(-0.028π)───PhX(-0.5)^0.5───────────────────┼─────────────────────────────────────────────PhX(-0.75)^0.5───Rz(-0.053π)───FSim(0.25π, 0)───Rz(-0.045π)───PhX(-0.5)^0.5─────────────────────────────────────────────────PhX(1)^0.5───────Rz(-0.053π)───FSim(0.25π, 0)───Rz(-0.045π)───PhX(-0.5)^0.5─────────────────────────────────────────────────PhX(1)^0.5───────Rz(-0.013π)────────────────────────────────FSim(0.25π, 0)────Rz(-0.028π)───PhX(0.5)^0.5────────────────────┼─────────────────────────────────────────────PhX(0.25)^0.5────Rz(-0.013π)────────────────────────────────FSim(0.25π, 0)────Rz(-0.028π)───PhX(1)^0.5──────────────────────┼─────────────────────────────────────────────PhX(0.5)^0.5─────
                                                                                                                                       │                                                                            │                                                                                                                           │                                                                                                                                                                                                                        │                                                                                                                                                                         │
(4, 6): ───PhX(0)^0.5──────────────────────────────────────────────────────────────────────────────────PhX(-0.5)^0.5────Rz(0.031π)─────FSim(0.25π, 0)──────────────────Rz(0.015π)────PhX(0.25)^0.5────Rz(-0.01π)────FSim(0.25π, 0)───Rz(-0.018π)───PhX(-0.75)^0.5────────────────────────────────────────────────PhX(0)^0.5───────Rz(-0.01π)────FSim(0.25π, 0)───Rz(-0.018π)───PhX(0.5)^0.5──────────────────────────────────────────────────PhX(1)^0.5──────────────────────────────────────────────────────────────────────────────────PhX(0.25)^0.5────Rz(0.031π)─────FSim(0.25π, 0)──────────────────Rz(0.015π)────PhX(0)^0.5──────────────────────────────────────────────────────────────────────────────────PhX(0.75)^0.5────Rz(0.031π)─────FSim(0.25π, 0)──────────────────Rz(0.015π)────PhX(0)^0.5───────
                                          └──────────────────────────────────────────┘                                                └────────────────────────────┘                                                                                                                                                                                                                                                                                                        └──────────────────────────────────────────┘                                                └────────────────────────────┘                                                └──────────────────────────────────────────┘                                                └────────────────────────────┘

You can now run this calibrated circuit on the Quantum Computing Service.