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.52it/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 0x7f6436d22020>

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.582
Simulating with theta =  -0.785 zeta  =     0.1 chi   =       0 gamma =       0 phi   =       0 
Loss:   0.569
Simulating with theta =  -0.785 zeta  =       0 chi   =     0.1 gamma =       0 phi   =       0 
Loss:   0.554
Simulating with theta =  -0.785 zeta  =       0 chi   =       0 gamma =     0.1 phi   =       0 
Loss:   0.592
Simulating with theta =  -0.785 zeta  =       0 chi   =       0 gamma =       0 phi   =     0.1 
Loss:   0.557
Simulating with theta =  -0.745 zeta  =    0.04 chi   =    0.04 gamma =    -0.1 phi   =    0.04 
Loss:   0.586
Simulating with theta =  -0.755 zeta  =    0.03 chi   =    0.03 gamma =   -0.05 phi   =    0.03 
Loss:   0.542
Simulating with theta =  -0.873 zeta  =   0.052 chi   =   0.052 gamma =   -0.02 phi   =   0.052 
Loss:     0.6
Simulating with theta =  -0.732 zeta  =   0.013 chi   =   0.013 gamma =  -0.005 phi   =   0.013 
Loss:   0.546
Simulating with theta =  -0.752 zeta  = -0.0828 chi   =  0.0572 gamma =  -0.022 phi   =  0.0572 
Loss:   0.566
Simulating with theta =   -0.76 zeta  = -0.0371 chi   =  0.0429 gamma = -0.0165 phi   =  0.0429 
Loss:   0.534
Simulating with theta =  -0.742 zeta  = 0.00236 chi   =  0.0744 gamma = -0.0286 phi   = -0.0656 
Loss:    0.57
Simulating with theta =  -0.775 zeta  = 0.00059 chi   =  0.0186 gamma = -0.00715 phi   =  0.0586 
Loss:   0.534
Simulating with theta =  -0.738 zeta  =  0.0026 chi   = -0.0582 gamma = -0.0315 phi   =  0.0578 
Loss:   0.555
Simulating with theta =  -0.774 zeta  = 0.000649 chi   =  0.0604 gamma = -0.00787 phi   =  0.0144 
Loss:   0.528
Simulating with theta =  -0.807 zeta  = -0.0153 chi   =  0.0478 gamma = -0.0276 phi   =  0.0454 
Loss:   0.536
Simulating with theta =  -0.805 zeta  = -0.0505 chi   =  0.0379 gamma =  0.0264 phi   =  0.0345 
Loss:   0.557
Simulating with theta =  -0.768 zeta  = 0.00988 chi   =   0.032 gamma = -0.0309 phi   =  0.0311 
Loss:   0.528
Simulating with theta =  -0.737 zeta  = 0.00495 chi   =  0.0138 gamma = 0.00264 phi   =  0.0135 
Loss:   0.543
Simulating with theta =   -0.79 zeta  = -0.0103 chi   =  0.0393 gamma =   -0.02 phi   =  0.0374 
Loss:   0.526
Simulating with theta =  -0.776 zeta  = -0.0153 chi   =  0.0512 gamma =  -0.023 phi   = -0.00824 
Loss:   0.531
Simulating with theta =  -0.797 zeta  =  0.0311 chi   =  0.0303 gamma = -0.0162 phi   =  -0.013 
Loss:   0.536
Simulating with theta =   -0.77 zeta  = -0.0201 chi   =  0.0397 gamma = -0.0164 phi   =  0.0289 
Loss:   0.526
Simulating with theta =  -0.778 zeta  = 0.00741 chi   =  0.0173 gamma = -0.00712 phi   =   0.053 
Loss:   0.533
Simulating with theta =  -0.777 zeta  = -0.00964 chi   =  0.0428 gamma =  -0.019 phi   = 0.00707 
Loss:   0.525
Simulating with theta =  -0.766 zeta  = -0.0118 chi   =  0.0857 gamma = -0.0377 phi   =  0.0476 
Loss:   0.548
Simulating with theta =   -0.78 zeta  = -0.00294 chi   =  0.0214 gamma = -0.00943 phi   =  0.0119 
Loss:   0.525
Simulating with theta =   -0.78 zeta  = -0.0139 chi   = 0.00963 gamma = -0.0305 phi   =  0.0321 
Loss:   0.528
Simulating with theta =  -0.775 zeta  = -0.00298 chi   =  0.0477 gamma = -0.0135 phi   =  0.0189 
Loss:   0.525
Simulating with theta =  -0.789 zeta  = -0.0282 chi   =  0.0444 gamma = -0.000461 phi   =  0.0105 
Loss:    0.53
Simulating with theta =  -0.773 zeta  = 0.00035 chi   =  0.0351 gamma = -0.0233 phi   =   0.026 
Loss:   0.525
Simulating with theta =   -0.76 zeta  = -0.00384 chi   =  0.0354 gamma = -0.0126 phi   = -0.0003 
Loss:   0.526
Simulating with theta =  -0.777 zeta  =  0.0124 chi   =  0.0332 gamma = -0.0147 phi   = -0.00352 
Loss:   0.527
Simulating with theta =  -0.771 zeta  = -0.0119 chi   =  0.0381 gamma =  -0.016 phi   =  0.0208 
Loss:   0.525
Simulating with theta =  -0.791 zeta  = -0.00702 chi   =  0.0386 gamma = -0.0199 phi   =  0.0342 
Loss:   0.526
Simulating with theta =  -0.783 zeta  = -0.00623 chi   =  0.0378 gamma = -0.0181 phi   =  0.0255 
Loss:   0.525
Simulating with theta =  -0.776 zeta  = 0.00015 chi   =  0.0293 gamma = -0.0131 phi   =  0.0342 
Loss:   0.526
Simulating with theta =  -0.777 zeta  = -0.00719 chi   =  0.0394 gamma = -0.0175 phi   =  0.0138 
Loss:   0.524
Simulating with theta =  -0.771 zeta  = -0.00825 chi   =  0.0578 gamma = -0.0259 phi   =  0.0301 
Loss:   0.529
Simulating with theta =  -0.778 zeta  = -0.00427 chi   =  0.0305 gamma = -0.0136 phi   =  0.0165 
Loss:   0.524
Simulating with theta =  -0.778 zeta  = -0.00873 chi   =  0.0246 gamma = -0.0219 phi   =  0.0222 
Loss:   0.525
Simulating with theta =  -0.776 zeta  = -0.00442 chi   =   0.042 gamma = -0.0156 phi   =  0.0197 
Loss:   0.524
Simulating with theta =  -0.781 zeta  =  -0.014 chi   =  0.0401 gamma = -0.00901 phi   =  0.0126 
Loss:   0.525
Simulating with theta =  -0.775 zeta  = -0.00323 chi   =  0.0363 gamma = -0.0197 phi   =  0.0226 
Loss:   0.524
Simulating with theta =  -0.784 zeta  =  0.0018 chi   =  0.0363 gamma = -0.0178 phi   =  0.0184 
Loss:   0.525
Simulating with theta =  -0.773 zeta  = -0.000699 chi   =   0.036 gamma = -0.0156 phi   =  0.0109 
Loss:   0.524
Simulating with theta =  -0.767 zeta  = -0.00972 chi   =  0.0374 gamma =  -0.015 phi   =   0.015 
Loss:   0.525
Simulating with theta =   -0.78 zeta  = -0.00108 chi   =  0.0366 gamma = -0.0171 phi   =  0.0176 
Loss:   0.524
Simulating with theta =  -0.781 zeta  = -0.00738 chi   =  0.0379 gamma = -0.0178 phi   =  0.0252 
Loss:   0.524
Simulating with theta =  -0.775 zeta  = -0.00237 chi   =  0.0365 gamma = -0.0162 phi   =  0.0145 
Loss:   0.524
Simulating with theta =  -0.777 zeta  = 0.00105 chi   =  0.0333 gamma = -0.0153 phi   =  0.0225 
Loss:   0.524
Simulating with theta =  -0.779 zeta  = -0.00121 chi   =  0.0352 gamma = -0.0114 phi   =  0.0136 
Loss:   0.524
Simulating with theta =  -0.781 zeta  = -0.000196 chi   =  0.0347 gamma = -0.00719 phi   = 0.00915 
Loss:   0.524
Simulating with theta =  -0.778 zeta  = -0.00639 chi   =   0.039 gamma = -0.0142 phi   =  0.0103 
Loss:   0.524
Simulating with theta =  -0.781 zeta  = -0.00171 chi   =  0.0291 gamma = -0.0134 phi   = 0.00926 
Loss:   0.524
Simulating with theta =  -0.779 zeta  = -0.00239 chi   =  0.0323 gamma = -0.0139 phi   =  0.0119 
Loss:   0.524
Simulating with theta =  -0.779 zeta  = -0.0011 chi   =  0.0413 gamma = -0.0155 phi   =  0.0107 
Loss:   0.524
Simulating with theta =  -0.778 zeta  = -0.00348 chi   =  0.0332 gamma = -0.0141 phi   =   0.015 
Loss:   0.524
Simulating with theta =  -0.776 zeta  = -0.00525 chi   =  0.0339 gamma = -0.0108 phi   = 0.00852 
Loss:   0.524
Simulating with theta =  -0.782 zeta  = -0.00511 chi   =   0.033 gamma = -0.00956 phi   = 0.00925 
Loss:   0.524
Simulating with theta =  -0.779 zeta  = -0.000587 chi   =  0.0281 gamma = -0.00968 phi   =  0.0131 
Loss:   0.524
Simulating with theta =  -0.779 zeta  = -0.00494 chi   =  0.0363 gamma = -0.0131 phi   =   0.011 
Loss:   0.524
Simulating with theta =  -0.778 zeta  = -0.00561 chi   =  0.0363 gamma = -0.00962 phi   =  0.0111 
Loss:   0.524
Simulating with theta =  -0.775 zeta  = -0.00308 chi   =   0.037 gamma =  -0.014 phi   =  0.0144 
Loss:   0.524
Simulating with theta =  -0.776 zeta  = -0.00359 chi   =   0.036 gamma = -0.0129 phi   =  0.0131 
Loss:   0.524
Simulating with theta =  -0.777 zeta  = -0.00476 chi   =  0.0379 gamma = -0.00904 phi   = 0.00793 
Loss:   0.524
Simulating with theta =  -0.777 zeta  = -0.0054 chi   =  0.0402 gamma = -0.00653 phi   = 0.00439 
Loss:   0.524
Simulating with theta =  -0.776 zeta  = -0.00323 chi   =  0.0355 gamma = -0.00841 phi   =  0.0108 
Loss:   0.524
Simulating with theta =  -0.778 zeta  = -0.00451 chi   =  0.0361 gamma = -0.0119 phi   =  0.0109 
Loss:   0.524
Simulating with theta =   -0.78 zeta  = -0.00262 chi   =  0.0386 gamma = -0.0111 phi   =  0.0142 
Loss:   0.524
Simulating with theta =  -0.777 zeta  = -0.00459 chi   =  0.0351 gamma = -0.0109 phi   = 0.00993 
Loss:   0.524
Simulating with theta =   -0.78 zeta  = -0.00468 chi   =  0.0362 gamma = -0.00823 phi   = 0.00826 
Loss:   0.524
Simulating with theta =  -0.778 zeta  = -0.00229 chi   =  0.0359 gamma = -0.0109 phi   = 0.00918 
Loss:   0.524
Simulating with theta =  -0.778 zeta  = -0.000631 chi   =  0.0357 gamma = -0.0116 phi   = 0.00824 
Loss:   0.524
characterization_result.final_params
{(cirq.GridQubit(4, 4), cirq.GridQubit(4, 5)): {'theta': -0.778232976741861,
  'zeta': -0.0022901040111069438,
  'chi': 0.03588533873863812,
  'gamma': -0.010947609912514509,
  'phi': 0.00918394807046469} }
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