Isolated XEB

View on QuantumAI Run in Google Colab View source on GitHub Download notebook
try:
    import cirq
except ImportError:
    print("installing cirq...")
    !pip install cirq
    print("installed cirq.")

This notebook demonstrates how to use the functionality in cirq.experiments to run Isolated XEB end-to-end. "Isolated" means we do one pair of qubits at a time.

import cirq
import numpy as np

Set up Random Circuits

We create a library of 20 random, two-qubit circuits using the sqrt(ISWAP) gate on the two qubits we've chosen.

from cirq.experiments import random_quantum_circuit_generation as rqcg

circuits = rqcg.generate_library_of_2q_circuits(
    n_library_circuits=20,
    two_qubit_gate=cirq.ISWAP**0.5,
    q0=cirq.GridQubit(4, 4),
    q1=cirq.GridQubit(4, 5),
)
print(len(circuits))
20
# We will truncate to these lengths
max_depth = 100
cycle_depths = np.arange(3, max_depth, 20)
cycle_depths
array([ 3, 23, 43, 63, 83])

Set up a Sampler.

For demonstration, we'll use a density matrix simulator to sample noisy samples. However, input a device_name (and have an authenticated Google Cloud project name set as your GOOGLE_CLOUD_PROJECT environment variable) to run on a real device.

device_name = None  # change me!

if device_name is None:
    sampler = cirq.DensityMatrixSimulator(noise=cirq.depolarize(5e-3))
else:
    import cirq_google as cg

    sampler = cg.get_engine_sampler(device_name, gate_set_name='sqrt_iswap')
    device = cg.get_engine_device(device_name)

    import cirq.contrib.routing as ccr

    graph = ccr.gridqubits_to_graph_device(device.qubits)
    pos = {q: (q.row, q.col) for q in graph.nodes}
    import networkx as nx

    nx.draw_networkx(graph, pos=pos)

Take Data

from cirq.experiments.xeb_sampling import sample_2q_xeb_circuits

sampled_df = sample_2q_xeb_circuits(
    sampler=sampler, circuits=circuits, cycle_depths=cycle_depths, repetitions=10_000
)
sampled_df
100%|██████████| 108/108 [00:18<00:00,  5.78it/s]

Benchmark fidelities

from cirq.experiments.xeb_fitting import benchmark_2q_xeb_fidelities

fids = benchmark_2q_xeb_fidelities(
    sampled_df=sampled_df, circuits=circuits, cycle_depths=cycle_depths
)
fids
%matplotlib inline
from matplotlib import pyplot as plt

# Exponential reference
xx = np.linspace(0, fids['cycle_depth'].max())
plt.plot(xx, (1 - 5e-3) ** (4 * xx), label=r'Exponential Reference')


def _p(fids):
    plt.plot(fids['cycle_depth'], fids['fidelity'], 'o-', label=fids.name)


fids.name = 'Sampled'
_p(fids)

plt.ylabel('Circuit fidelity')
plt.xlabel('Cycle Depth $d$')
plt.legend(loc='best')
<matplotlib.legend.Legend at 0x7fbe89477d50>

png

Optimize PhasedFSimGate parameters

We know what circuits we requested, and in this simulated example, we know what coherent error has happened. But in a real experiment, there is likely unknown coherent error that you would like to characterize. Therefore, we make the five angles in PhasedFSimGate free parameters and use a classical optimizer to find which set of parameters best describes the data we collected from the noisy simulator (or device, if this was a real experiment).

import multiprocessing

pool = multiprocessing.get_context('spawn').Pool()
from cirq.experiments.xeb_fitting import (
    parameterize_circuit,
    characterize_phased_fsim_parameters_with_xeb,
    SqrtISwapXEBOptions,
)

# Set which angles we want to characterize (all)
options = SqrtISwapXEBOptions(
    characterize_theta=True,
    characterize_zeta=True,
    characterize_chi=True,
    characterize_gamma=True,
    characterize_phi=True,
)
# Parameterize the sqrt(iswap)s in our circuit library
pcircuits = [parameterize_circuit(circuit, options) for circuit in circuits]

# Run the characterization loop
characterization_result = characterize_phased_fsim_parameters_with_xeb(
    sampled_df,
    pcircuits,
    cycle_depths,
    options,
    pool=pool,
    # ease tolerance so it converges faster:
    fatol=5e-3,
    xatol=5e-3,
)
Simulating with theta =  -0.785 zeta  =       0 chi   =       0 gamma =       0 phi   =       0 
Loss:    0.53
Simulating with theta =  -0.685 zeta  =       0 chi   =       0 gamma =       0 phi   =       0 
Loss:   0.591
Simulating with theta =  -0.785 zeta  =     0.1 chi   =       0 gamma =       0 phi   =       0 
Loss:    0.54
Simulating with theta =  -0.785 zeta  =       0 chi   =     0.1 gamma =       0 phi   =       0 
Loss:   0.566
Simulating with theta =  -0.785 zeta  =       0 chi   =       0 gamma =     0.1 phi   =       0 
Loss:   0.586
Simulating with theta =  -0.785 zeta  =       0 chi   =       0 gamma =       0 phi   =     0.1 
Loss:   0.537
Simulating with theta =  -0.885 zeta  =    0.04 chi   =    0.04 gamma =    0.04 phi   =    0.04 
Loss:   0.612
Simulating with theta =  -0.735 zeta  =    0.01 chi   =    0.01 gamma =    0.01 phi   =    0.01 
Loss:   0.548
Simulating with theta =  -0.765 zeta  =   0.044 chi   =   0.044 gamma =  -0.096 phi   =   0.044 
Loss:   0.549
Simulating with theta =  -0.757 zeta  =  0.0616 chi   = -0.0784 gamma = -0.0344 phi   =  0.0616 
Loss:   0.552
Simulating with theta =  -0.764 zeta  =  0.0462 chi   = -0.0338 gamma = -0.0258 phi   =  0.0462 
Loss:    0.53
Simulating with theta =  -0.777 zeta  =  0.0185 chi   = -0.0535 gamma =  0.0897 phi   =  0.0185 
Loss:   0.584
Simulating with theta =  -0.768 zeta  =  0.0376 chi   =  0.0196 gamma = -0.0496 phi   =  0.0376 
Loss:    0.53
Simulating with theta =   -0.82 zeta  =  0.0635 chi   = -0.0157 gamma = -0.0402 phi   =  0.0635 
Loss:   0.544
Simulating with theta =  -0.799 zeta  =  0.0501 chi   = -0.00925 gamma = -0.0276 phi   =  0.0501 
Loss:    0.53
Simulating with theta =  -0.776 zeta  = -0.0464 chi   = -0.00937 gamma = -0.0412 phi   =  0.0936 
Loss:   0.532
Simulating with theta =  -0.772 zeta  =   0.035 chi   = -0.0131 gamma = -0.0577 phi   = -0.00898 
Loss:   0.553
Simulating with theta =  -0.782 zeta  = 0.00876 chi   = -0.00328 gamma = -0.0144 phi   =  0.0728 
Loss:   0.524
Simulating with theta =  -0.784 zeta  =   0.104 chi   = -0.00131 gamma = -0.00577 phi   = -0.0109 
Loss:   0.542
Simulating with theta =  -0.778 zeta  = -0.00893 chi   = -0.00736 gamma = -0.0323 phi   =  0.0675 
Loss:   0.524
Simulating with theta =  -0.795 zeta  = 0.000847 chi   = -0.0411 gamma = 0.00951 phi   =   0.057 
Loss:   0.531
Simulating with theta =  -0.775 zeta  =  0.0284 chi   = 0.00444 gamma = -0.0348 phi   =  0.0425 
Loss:   0.526
Simulating with theta =  -0.803 zeta  = -0.0148 chi   =  0.0276 gamma = -0.0179 phi   =  0.0469 
Loss:   0.535
Simulating with theta =  -0.774 zeta  =  0.0309 chi   = -0.0184 gamma = -0.0238 phi   =  0.0464 
Loss:   0.525
Simulating with theta =  -0.759 zeta  = -0.0265 chi   = -0.000603 gamma = -0.0145 phi   =  0.0415 
Loss:   0.533
Simulating with theta =  -0.789 zeta  =   0.031 chi   = -0.00709 gamma = -0.0243 phi   =   0.048 
Loss:   0.525
Simulating with theta =  -0.774 zeta  =  0.0361 chi   = -0.0127 gamma = -0.0519 phi   =   0.111 
Loss:   0.523
Simulating with theta =  -0.768 zeta  =  0.0541 chi   =  -0.019 gamma = -0.0778 phi   =   0.166 
Loss:   0.537
Simulating with theta =  -0.784 zeta  =  0.0107 chi   =  -0.024 gamma = -0.0239 phi   =  0.0957 
Loss:   0.524
Simulating with theta =  -0.767 zeta  = 2.18e-05 chi   = -0.0192 gamma = -0.0342 phi   =   0.109 
Loss:   0.525
Simulating with theta =  -0.784 zeta  =  0.0232 chi   = -0.0101 gamma = -0.0268 phi   =  0.0633 
Loss:   0.523
Simulating with theta =  -0.786 zeta  =  -0.003 chi   = -0.00453 gamma = -0.0359 phi   =   0.118 
Loss:   0.526
Simulating with theta =  -0.777 zeta  =  0.0225 chi   =  -0.015 gamma = -0.0268 phi   =  0.0642 
Loss:   0.522
Simulating with theta =  -0.774 zeta  =  0.0219 chi   = 0.00462 gamma =  -0.037 phi   =  0.0557 
Loss:   0.524
Simulating with theta =  -0.781 zeta  =  0.0135 chi   = -0.0168 gamma = -0.0272 phi   =  0.0857 
Loss:   0.522
Simulating with theta =  -0.781 zeta  =  0.0506 chi   = -0.0158 gamma = -0.0265 phi   =  0.0912 
Loss:   0.526
Simulating with theta =  -0.779 zeta  = 0.00594 chi   = -0.00947 gamma = -0.0309 phi   =  0.0734 
Loss:   0.522
Simulating with theta =  -0.776 zeta  =  0.0317 chi   = -0.0224 gamma =  -0.051 phi   =  0.0862 
Loss:   0.524
Simulating with theta =   -0.78 zeta  =  0.0145 chi   = -0.00805 gamma = -0.0236 phi   =  0.0761 
Loss:   0.522
Simulating with theta =  -0.787 zeta  = -0.00421 chi   = -0.0111 gamma = -0.00223 phi   =  0.0343 
Loss:   0.525
Simulating with theta =  -0.777 zeta  =   0.026 chi   = -0.0123 gamma = -0.0395 phi   =  0.0917 
Loss:   0.522
Simulating with theta =  -0.774 zeta  = 0.00971 chi   = -0.0145 gamma = -0.0324 phi   =  0.0931 
Loss:   0.522
Simulating with theta =  -0.779 zeta  = 0.00541 chi   = -0.0095 gamma = -0.0346 phi   =   0.104 
Loss:   0.523
Simulating with theta =  -0.778 zeta  =  0.0182 chi   = -0.0136 gamma = -0.0288 phi   =  0.0741 
Loss:   0.522
Simulating with theta =  -0.774 zeta  =  0.0162 chi   = -0.00634 gamma = -0.0348 phi   =  0.0777 
Loss:   0.522
Simulating with theta =   -0.77 zeta  =  0.0176 chi   = -0.00109 gamma = -0.0387 phi   =  0.0737 
Loss:   0.523
Simulating with theta =  -0.781 zeta  =  0.0226 chi   = -0.00538 gamma = -0.0306 phi   =  0.0641 
Loss:   0.523
Simulating with theta =  -0.776 zeta  =  0.0129 chi   = -0.0122 gamma = -0.0319 phi   =  0.0859 
Loss:   0.522
Simulating with theta =  -0.773 zeta  =  0.0172 chi   = -0.0135 gamma = -0.0428 phi   =   0.085 
Loss:   0.522
Simulating with theta =  -0.778 zeta  =  0.0152 chi   = -0.00942 gamma = -0.0284 phi   =  0.0783 
Loss:   0.522
Simulating with theta =  -0.775 zeta  =  0.0295 chi   = -0.0121 gamma = -0.0345 phi   =  0.0897 
Loss:   0.522
Simulating with theta =  -0.778 zeta  =  0.0118 chi   = -0.0101 gamma = -0.0318 phi   =  0.0775 
Loss:   0.522
Simulating with theta =  -0.775 zeta  =  0.0147 chi   = -0.00656 gamma = -0.0378 phi   =  0.0903 
Loss:   0.522
Simulating with theta =  -0.774 zeta  =  0.0129 chi   = -0.00304 gamma = -0.0423 phi   =  0.0984 
Loss:   0.522
Simulating with theta =  -0.776 zeta  = 0.00234 chi   = -0.00558 gamma = -0.0264 phi   =  0.0722 
Loss:   0.522
Simulating with theta =  -0.777 zeta  =  0.0201 chi   = -0.0106 gamma = -0.0362 phi   =  0.0868 
Loss:   0.522
Simulating with theta =   -0.78 zeta  =  0.0137 chi   = -0.0132 gamma = -0.0316 phi   =  0.0898 
Loss:   0.522
Simulating with theta =  -0.775 zeta  =  0.0156 chi   = -0.00806 gamma =  -0.034 phi   =  0.0807 
Loss:   0.522
Simulating with theta =  -0.774 zeta  =  0.0149 chi   = -0.00962 gamma = -0.0403 phi   =  0.0901 
Loss:   0.522
Simulating with theta =  -0.777 zeta  =  0.0151 chi   = -0.00947 gamma = -0.0314 phi   =  0.0813 
Loss:   0.522
Simulating with theta =  -0.777 zeta  =   0.018 chi   = -0.0057 gamma = -0.0365 phi   =  0.0808 
Loss:   0.522
Simulating with theta =  -0.775 zeta  =  0.0215 chi   = -0.00604 gamma = -0.0386 phi   =  0.0905 
Loss:   0.522
Simulating with theta =  -0.776 zeta  =  0.0191 chi   = -0.00706 gamma = -0.0369 phi   =  0.0872 
Loss:   0.522
Simulating with theta =  -0.777 zeta  =  0.0205 chi   = -0.0098 gamma = -0.0322 phi   =  0.0764 
Loss:   0.522
Simulating with theta =  -0.776 zeta  =  0.0161 chi   = -0.00737 gamma = -0.0364 phi   =  0.0868 
Loss:   0.522
Simulating with theta =  -0.775 zeta  =  0.0164 chi   = -0.0113 gamma = -0.0334 phi   =  0.0884 
Loss:   0.522
Simulating with theta =  -0.777 zeta  =  0.0176 chi   = -0.00711 gamma = -0.0358 phi   =  0.0827 
Loss:   0.522
characterization_result.final_params
{(cirq.GridQubit(4, 4),
  cirq.GridQubit(4, 5)): {'theta': np.float64(-0.7766190436351619), 'zeta': np.float64(0.017582753361376283), 'chi': np.float64(-0.0071055864609924635), 'gamma': np.float64(-0.03575806139006697), 'phi': np.float64(0.08267429314279195)} }
characterization_result.fidelities_df
from cirq.experiments.xeb_fitting import before_and_after_characterization

before_after_df = before_and_after_characterization(fids, characterization_result)
before_after_df
from cirq.experiments.xeb_fitting import exponential_decay

for i, row in before_after_df.iterrows():
    plt.axhline(1, color='grey', ls='--')
    plt.plot(row['cycle_depths_0'], row['fidelities_0'], '*', color='red')
    plt.plot(row['cycle_depths_c'], row['fidelities_c'], 'o', color='blue')

    xx = np.linspace(0, np.max(row['cycle_depths_0']))
    plt.plot(xx, exponential_decay(xx, a=row['a_0'], layer_fid=row['layer_fid_0']), color='red')
    plt.plot(xx, exponential_decay(xx, a=row['a_c'], layer_fid=row['layer_fid_c']), color='blue')

    plt.show()

png