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 --quiet 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:23<00:00,  4.57it/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 0x7f09a8e09e10>

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.531
Simulating with theta =  -0.685 zeta  =       0 chi   =       0 gamma =       0 phi   =       0 
Loss:   0.611
Simulating with theta =  -0.785 zeta  =     0.1 chi   =       0 gamma =       0 phi   =       0 
Loss:   0.571
Simulating with theta =  -0.785 zeta  =       0 chi   =     0.1 gamma =       0 phi   =       0 
Loss:   0.557
Simulating with theta =  -0.785 zeta  =       0 chi   =       0 gamma =     0.1 phi   =       0 
Loss:   0.599
Simulating with theta =  -0.785 zeta  =       0 chi   =       0 gamma =       0 phi   =     0.1 
Loss:   0.559
Simulating with theta =  -0.885 zeta  =    0.04 chi   =    0.04 gamma =    0.04 phi   =    0.04 
Loss:   0.635
Simulating with theta =  -0.735 zeta  =    0.01 chi   =    0.01 gamma =    0.01 phi   =    0.01 
Loss:   0.551
Simulating with theta =  -0.765 zeta  =   0.044 chi   =   0.044 gamma =  -0.096 phi   =   0.044 
Loss:   0.578
Simulating with theta =   -0.77 zeta  =   0.033 chi   =   0.033 gamma =  -0.047 phi   =   0.033 
Loss:   0.549
Simulating with theta =  -0.759 zeta  = -0.0828 chi   =  0.0572 gamma = -0.0148 phi   =  0.0572 
Loss:   0.562
Simulating with theta =  -0.766 zeta  = -0.0371 chi   =  0.0429 gamma = -0.0111 phi   =  0.0429 
Loss:   0.536
Simulating with theta =  -0.752 zeta  = 0.00236 chi   =  0.0744 gamma = -0.0192 phi   = -0.0656 
Loss:   0.573
Simulating with theta =  -0.777 zeta  = 0.00059 chi   =  0.0186 gamma = -0.00481 phi   =  0.0586 
Loss:   0.535
Simulating with theta =  -0.748 zeta  =  0.0026 chi   = -0.0582 gamma = -0.0212 phi   =  0.0578 
Loss:   0.549
Simulating with theta =  -0.803 zeta  = -0.0104 chi   = 0.00451 gamma = -0.0436 phi   =  0.0669 
Loss:   0.542
Simulating with theta =  -0.813 zeta  = -0.00815 chi   =  0.0978 gamma = -0.0215 phi   =  0.0228 
Loss:   0.574
Simulating with theta =  -0.764 zeta  = -8.96e-05 chi   = -0.0192 gamma = -0.0212 phi   =   0.049 
Loss:   0.534
Simulating with theta =  -0.788 zeta  = -0.0518 chi   = -0.0143 gamma =  0.0147 phi   =   0.054 
Loss:   0.558
Simulating with theta =  -0.775 zeta  =  0.0118 chi   =  0.0212 gamma = -0.0316 phi   =  0.0382 
Loss:   0.536
Simulating with theta =  -0.744 zeta  = 0.000447 chi   =  0.0209 gamma =  0.0161 phi   = 0.00859 
Loss:   0.542
Simulating with theta =  -0.788 zeta  = -0.00766 chi   =  0.0086 gamma = -0.0287 phi   =  0.0523 
Loss:   0.533
Simulating with theta =  -0.778 zeta  = -0.0295 chi   = -0.000823 gamma = 0.00524 phi   =  0.0429 
Loss:   0.538
Simulating with theta =  -0.775 zeta  = 0.00148 chi   =  0.0157 gamma = -0.0224 phi   =  0.0394 
Loss:   0.532
Simulating with theta =   -0.79 zeta  =  0.0348 chi   = -0.0334 gamma = -0.0197 phi   =  0.0368 
Loss:   0.542
Simulating with theta =  -0.772 zeta  = -0.0191 chi   =  0.0238 gamma = -0.0133 phi   =  0.0414 
Loss:   0.531
Simulating with theta =  -0.777 zeta  = -0.0107 chi   = -0.00703 gamma = -0.0294 phi   =  0.0143 
Loss:   0.535
Simulating with theta =  -0.777 zeta  = -0.00791 chi   = -0.000625 gamma = -0.0233 phi   =  0.0254 
Loss:   0.532
Simulating with theta =  -0.795 zeta  = -0.0132 chi   =  0.0382 gamma = -0.0138 phi   =  0.0144 
Loss:   0.541
Simulating with theta =  -0.772 zeta  = -0.00337 chi   = -0.00485 gamma = -0.0194 phi   =  0.0404 
Loss:   0.531
Simulating with theta =  -0.764 zeta  = -0.00391 chi   =   0.005 gamma = -0.00262 phi   = 0.00627 
Loss:   0.531
Simulating with theta =  -0.773 zeta  = -0.0152 chi   = -0.00634 gamma = -0.00104 phi   = 0.00594 
Loss:    0.53
Simulating with theta =  -0.772 zeta  = -0.0235 chi   = -0.0174 gamma = 0.00963 phi   = -0.0108 
Loss:   0.534
Simulating with theta =   -0.77 zeta  = -0.00872 chi   = 0.00768 gamma = 0.00874 phi   =  0.0122 
Loss:    0.53
Simulating with theta =  -0.766 zeta  = -0.00913 chi   =  0.0118 gamma =  0.0247 phi   = 0.00567 
Loss:   0.534
Simulating with theta =  -0.784 zeta  = -0.0147 chi   = 0.00312 gamma = -0.00735 phi   =  0.0337 
Loss:   0.532
Simulating with theta =  -0.769 zeta  = -0.00659 chi   = 0.00453 gamma = -0.0038 phi   =  0.0131 
Loss:    0.53
Simulating with theta =  -0.776 zeta  = -0.0165 chi   =  0.0167 gamma =  0.0156 phi   = -0.0113 
Loss:    0.53
Simulating with theta =  -0.777 zeta  = 0.000318 chi   = -0.0148 gamma =  0.0211 phi   = -0.0334 
Loss:   0.532
Simulating with theta =  -0.773 zeta  = -0.0143 chi   =  0.0142 gamma = -0.00468 phi   =  0.0227 
Loss:    0.53
Simulating with theta =  -0.759 zeta  = -0.0245 chi   =  0.0147 gamma = 0.00595 phi   =  0.0171 
Loss:   0.533
Simulating with theta =  -0.779 zeta  = -0.00613 chi   = 0.00368 gamma = 0.00149 phi   = 0.00427 
Loss:   0.529
Simulating with theta =  -0.774 zeta  = -0.00568 chi   =  0.0251 gamma = 0.00799 phi   =  0.0105 
Loss:    0.53
Simulating with theta =  -0.779 zeta  = -0.0109 chi   =   0.018 gamma = -0.00209 phi   = 0.00348 
Loss:    0.53
Simulating with theta =  -0.772 zeta  = -0.00928 chi   =  0.0103 gamma = 0.00604 phi   =    0.01 
Loss:   0.529
Simulating with theta =  -0.771 zeta  = -0.000287 chi   = 0.00635 gamma = -0.0128 phi   =  0.0355 
Loss:   0.531
Simulating with theta =  -0.775 zeta  = -0.0124 chi   =  0.0141 gamma = 0.00852 phi   = 0.000417 
Loss:   0.529
Simulating with theta =  -0.779 zeta  = -0.0125 chi   =  0.0224 gamma =  0.0115 phi   = 0.00603 
Loss:    0.53
Simulating with theta =  -0.772 zeta  = -0.00807 chi   = 0.00899 gamma = 3.39e-05 phi   =  0.0114 
Loss:   0.529
Simulating with theta =  -0.775 zeta  = -0.00238 chi   =  0.0107 gamma =  0.0143 phi   = -0.00807 
Loss:   0.529
Simulating with theta =  -0.776 zeta  = 0.00356 chi   = 0.00893 gamma =  0.0238 phi   = -0.0235 
Loss:   0.529
Simulating with theta =  -0.775 zeta  = -0.00964 chi   = -0.00596 gamma = 0.00416 phi   = -0.00327 
Loss:    0.53
Simulating with theta =  -0.774 zeta  = -0.00667 chi   =  0.0173 gamma = 0.00703 phi   = 0.00704 
Loss:   0.529
Simulating with theta =  -0.778 zeta  =  -0.005 chi   =  0.0117 gamma = 0.00651 phi   = -0.00404 
Loss:   0.529
Simulating with theta =  -0.771 zeta  = -0.00769 chi   =  0.0214 gamma =  0.0131 phi   = -0.00159 
Loss:    0.53
Simulating with theta =  -0.777 zeta  = -0.00652 chi   = 0.00811 gamma = 0.00438 phi   = 0.00281 
Loss:   0.529
Simulating with theta =  -0.776 zeta  = 0.000983 chi   = 0.00856 gamma = 0.00439 phi   = 0.00322 
Loss:   0.529
Simulating with theta =   -0.78 zeta  = 0.000244 chi   =  0.0135 gamma =  0.0146 phi   =  -0.011 
Loss:   0.529
Simulating with theta =  -0.778 zeta  = -0.00911 chi   =  0.0159 gamma =  0.0144 phi   = -0.00852 
Loss:   0.529
Simulating with theta =  -0.777 zeta  = -0.00659 chi   =  0.0141 gamma =  0.0119 phi   = -0.00558 
Loss:   0.529
Simulating with theta =  -0.781 zeta  = -0.00143 chi   = 0.00593 gamma =  0.0136 phi   = -0.0174 
Loss:   0.529
Simulating with theta =  -0.776 zeta  = -0.00536 chi   =  0.0145 gamma = 0.00868 phi   = 0.000932 
Loss:   0.529
Simulating with theta =  -0.776 zeta  = -0.00324 chi   =  0.0127 gamma =   0.015 phi   = -0.00432 
Loss:   0.529
Simulating with theta =  -0.777 zeta  = -0.000409 chi   =  0.0181 gamma =  0.0214 phi   =  -0.014 
Loss:   0.529
Simulating with theta =  -0.777 zeta  = -0.00499 chi   =  0.0106 gamma = 0.00864 phi   = -0.0014 
Loss:   0.529
Simulating with theta =  -0.778 zeta  = -0.00439 chi   =  0.0127 gamma = 0.00822 phi   = -0.00572 
Loss:   0.529
Simulating with theta =  -0.777 zeta  = -0.0041 chi   =  0.0127 gamma = 0.00992 phi   = -0.00537 
Loss:   0.529
Simulating with theta =  -0.773 zeta  = -0.00961 chi   =  0.0115 gamma = 0.00675 phi   = 0.00318 
Loss:   0.529
Simulating with theta =  -0.778 zeta  = -0.00222 chi   =   0.013 gamma =  0.0126 phi   = -0.00744 
Loss:   0.529
Simulating with theta =  -0.779 zeta  = -0.00692 chi   =  0.0153 gamma =  0.0064 phi   = 0.000529 
Loss:   0.529
Simulating with theta =  -0.776 zeta  = -0.00351 chi   =  0.0118 gamma =  0.0123 phi   = -0.00592 
Loss:   0.529
Simulating with theta =  -0.779 zeta  = -0.00321 chi   =  0.0104 gamma =  0.0135 phi   = -0.0112 
Loss:   0.529
Simulating with theta =  -0.778 zeta  = -0.000626 chi   = 0.00931 gamma =  0.0109 phi   = -0.00696 
Loss:   0.529
Simulating with theta =  -0.777 zeta  = -0.0051 chi   =  0.0129 gamma =  0.0116 phi   = -0.00593 
Loss:   0.529
Simulating with theta =  -0.778 zeta  = -0.00226 chi   =  0.0137 gamma =  0.0154 phi   =  -0.013 
Loss:   0.529
Simulating with theta =  -0.779 zeta  = -0.000899 chi   =  0.0153 gamma =  0.0187 phi   = -0.0187 
Loss:   0.529
Simulating with theta =  -0.776 zeta  = -0.00367 chi   =  0.0152 gamma =  0.0113 phi   = -0.00383 
Loss:   0.529
Simulating with theta =  -0.777 zeta  = -0.00261 chi   =   0.014 gamma =  0.0154 phi   = -0.00905 
Loss:   0.529
Simulating with theta =  -0.777 zeta  = -0.000613 chi   =  0.0142 gamma =  0.0152 phi   = -0.00975 
Loss:   0.529
Simulating with theta =  -0.778 zeta  = -0.000815 chi   =  0.0115 gamma =  0.0171 phi   = -0.0142 
Loss:   0.529
Simulating with theta =  -0.778 zeta  = -0.00395 chi   =  0.0114 gamma =  0.0139 phi   = -0.0101 
Loss:   0.529
Simulating with theta =   -0.78 zeta  = -0.00123 chi   =  0.0136 gamma =  0.0174 phi   = -0.0156 
Loss:   0.529
Simulating with theta =  -0.777 zeta  = -0.00294 chi   =  0.0123 gamma =  0.0136 phi   = -0.00834 
Loss:   0.529
Simulating with theta =  -0.777 zeta  = -0.000384 chi   =  0.0144 gamma =  0.0157 phi   = -0.0107 
Loss:   0.529
Simulating with theta =  -0.778 zeta  = -0.00306 chi   =  0.0122 gamma =  0.0144 phi   = -0.0102 
Loss:   0.529
Simulating with theta =  -0.777 zeta  = -0.00246 chi   =  0.0125 gamma =  0.0177 phi   = -0.0145 
Loss:   0.529
characterization_result.final_params
{(cirq.GridQubit(4, 4), cirq.GridQubit(4, 5)): {'theta': -0.778225012034631,
  'zeta': -0.00226323301143552,
  'chi': 0.013727090788150039,
  'gamma': 0.015359082663484815,
  'phi': -0.012950433459515836} }
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