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:22<00:00,  4.88it/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 0x7f0b075bdf60>

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.528
Simulating with theta =  -0.685 zeta  =       0 chi   =       0 gamma =       0 phi   =       0 
Loss:     0.6
Simulating with theta =  -0.785 zeta  =     0.1 chi   =       0 gamma =       0 phi   =       0 
Loss:   0.563
Simulating with theta =  -0.785 zeta  =       0 chi   =     0.1 gamma =       0 phi   =       0 
Loss:   0.568
Simulating with theta =  -0.785 zeta  =       0 chi   =       0 gamma =     0.1 phi   =       0 
Loss:   0.582
Simulating with theta =  -0.785 zeta  =       0 chi   =       0 gamma =       0 phi   =     0.1 
Loss:   0.549
Simulating with theta =  -0.885 zeta  =    0.04 chi   =    0.04 gamma =    0.04 phi   =    0.04 
Loss:   0.625
Simulating with theta =  -0.735 zeta  =    0.01 chi   =    0.01 gamma =    0.01 phi   =    0.01 
Loss:   0.557
Simulating with theta =  -0.765 zeta  =   0.044 chi   =   0.044 gamma =  -0.096 phi   =   0.044 
Loss:   0.571
Simulating with theta =   -0.77 zeta  =   0.033 chi   =   0.033 gamma =  -0.047 phi   =   0.033 
Loss:    0.54
Simulating with theta =  -0.759 zeta  =  0.0572 chi   = -0.0828 gamma = -0.0148 phi   =  0.0572 
Loss:    0.57
Simulating with theta =  -0.779 zeta  =  0.0143 chi   =  0.0543 gamma = -0.0037 phi   =  0.0143 
Loss:   0.535
Simulating with theta =  -0.757 zeta  = -0.0771 chi   =  0.0389 gamma = -0.0163 phi   =  0.0629 
Loss:   0.577
Simulating with theta =  -0.778 zeta  =  0.0557 chi   = 0.00973 gamma = -0.00407 phi   =  0.0157 
Loss:   0.537
Simulating with theta =  -0.824 zeta  =  0.0312 chi   =  0.0288 gamma = -0.0319 phi   =  0.0552 
Loss:    0.54
Simulating with theta =  -0.789 zeta  =  0.0537 chi   =  0.0503 gamma = -0.0347 phi   = -0.0527 
Loss:   0.572
Simulating with theta =  -0.786 zeta  =  0.0134 chi   =  0.0126 gamma = -0.00867 phi   =  0.0618 
Loss:   0.529
Simulating with theta =  -0.811 zeta  =  0.0129 chi   = 0.00917 gamma =  0.0277 phi   =  0.0258 
Loss:   0.535
Simulating with theta =  -0.752 zeta  = 0.00732 chi   =  0.0055 gamma =  0.0364 phi   = -0.00814 
Loss:   0.547
Simulating with theta =  -0.806 zeta  =  0.0252 chi   =   0.023 gamma = -0.0148 phi   =  0.0394 
Loss:   0.529
Simulating with theta =  -0.809 zeta  = -0.0294 chi   =  0.0299 gamma = 0.00425 phi   =  0.0408 
Loss:    0.54
Simulating with theta =  -0.786 zeta  =  0.0344 chi   =  0.0148 gamma = -0.00199 phi   =   0.022 
Loss:   0.529
Simulating with theta =  -0.811 zeta  =  0.0201 chi   = -0.0305 gamma = 0.00457 phi   =  0.0453 
Loss:   0.536
Simulating with theta =  -0.787 zeta  =  0.0157 chi   =  0.0331 gamma = -0.00163 phi   =  0.0221 
Loss:   0.527
Simulating with theta =  -0.769 zeta  =  0.0227 chi   =  0.0242 gamma = -0.0385 phi   =  0.0323 
Loss:   0.536
Simulating with theta =    -0.8 zeta  =  0.0153 chi   =  0.0129 gamma =  0.0111 phi   =  0.0274 
Loss:   0.527
Simulating with theta =    -0.8 zeta  = -0.00656 chi   =  0.0179 gamma = -0.00362 phi   =  0.0383 
Loss:   0.526
Simulating with theta =  -0.807 zeta  = -0.0271 chi   =  0.0194 gamma = -0.00443 phi   =  0.0464 
Loss:   0.534
Simulating with theta =  -0.778 zeta  = -0.0101 chi   = 0.00761 gamma =  0.0137 phi   =  0.0205 
Loss:   0.532
Simulating with theta =  -0.799 zeta  =  0.0164 chi   =  0.0191 gamma = -0.0077 phi   =  0.0346 
Loss:   0.525
Simulating with theta =  -0.802 zeta  = 0.00294 chi   =  0.0206 gamma = 0.00794 phi   = -0.0129 
Loss:   0.529
Simulating with theta =   -0.79 zeta  =  0.0108 chi   =  0.0146 gamma = -0.00452 phi   =  0.0432 
Loss:   0.526
Simulating with theta =  -0.805 zeta  =  0.0207 chi   =  0.0391 gamma = -0.00254 phi   =  0.0662 
Loss:   0.535
Simulating with theta =   -0.79 zeta  = 0.00517 chi   = 0.00976 gamma = -0.000634 phi   =  0.0166 
Loss:   0.525
Simulating with theta =  -0.805 zeta  = 0.000713 chi   = -0.00338 gamma = -0.000505 phi   =   0.042 
Loss:   0.527
Simulating with theta =  -0.794 zeta  = -0.0047 chi   =  0.0103 gamma = -0.0179 phi   =  0.0424 
Loss:   0.525
Simulating with theta =  -0.784 zeta  = 0.00774 chi   =   0.032 gamma = -0.0132 phi   =   0.028 
Loss:   0.528
Simulating with theta =    -0.8 zeta  = 0.00247 chi   = 0.00547 gamma = -0.00369 phi   =  0.0385 
Loss:   0.525
Simulating with theta =  -0.789 zeta  =  0.0186 chi   = 0.00583 gamma = -0.0102 phi   =  0.0318 
Loss:   0.526
Simulating with theta =  -0.792 zeta  =  0.0123 chi   = 0.00884 gamma = -0.00852 phi   =  0.0334 
Loss:   0.525
Simulating with theta =  -0.799 zeta  = 0.00187 chi   =  0.0068 gamma = -0.0109 phi   =  0.0231 
Loss:   0.525
Simulating with theta =  -0.791 zeta  = -0.00956 chi   = -0.00269 gamma = -0.00895 phi   =  0.0269 
Loss:   0.526
Simulating with theta =  -0.797 zeta  = 0.00992 chi   =  0.0137 gamma = -0.00801 phi   =  0.0327 
Loss:   0.525
Simulating with theta =   -0.79 zeta  = 0.00821 chi   =  0.0124 gamma = -0.00464 phi   =  0.0424 
Loss:   0.526
Simulating with theta =  -0.797 zeta  = 0.00345 chi   =  0.0082 gamma = -0.00931 phi   =  0.0279 
Loss:   0.525
Simulating with theta =  -0.788 zeta  =   0.008 chi   =  0.0148 gamma = -0.0141 phi   =  0.0227 
Loss:   0.526
Simulating with theta =  -0.797 zeta  = 0.00385 chi   = 0.00781 gamma = -0.00628 phi   =  0.0345 
Loss:   0.525
Simulating with theta =  -0.796 zeta  =  0.0186 chi   = 0.00905 gamma =  0.0048 phi   =  0.0157 
Loss:   0.526
Simulating with theta =  -0.794 zeta  = 0.00112 chi   = 0.00996 gamma = -0.0122 phi   =  0.0357 
Loss:   0.525
Simulating with theta =    -0.8 zeta  =  0.0071 chi   = 0.00964 gamma = -0.0171 phi   =  0.0492 
Loss:   0.525
Simulating with theta =  -0.793 zeta  = 0.00565 chi   = 0.00973 gamma = -0.00475 phi   =  0.0247 
Loss:   0.525
Simulating with theta =  -0.799 zeta  = -0.00273 chi   =  0.0109 gamma = -0.00771 phi   =  0.0288 
Loss:   0.525
Simulating with theta =  -0.797 zeta  = 0.00104 chi   =  0.0104 gamma = -0.00791 phi   =    0.03 
Loss:   0.524
Simulating with theta =  -0.794 zeta  = -0.00387 chi   = 0.00476 gamma = -0.00818 phi   =  0.0284 
Loss:   0.525
Simulating with theta =  -0.796 zeta  = 0.00647 chi   =  0.0115 gamma = -0.00805 phi   =  0.0316 
Loss:   0.524
Simulating with theta =  -0.794 zeta  =  0.0038 chi   =  0.0115 gamma = -0.00638 phi   =  0.0347 
Loss:   0.525
Simulating with theta =  -0.799 zeta  = 0.00086 chi   =  0.0107 gamma = -0.0116 phi   =  0.0419 
Loss:   0.525
Simulating with theta =  -0.794 zeta  = 0.00446 chi   = 0.00998 gamma = -0.00646 phi   =   0.029 
Loss:   0.524
Simulating with theta =  -0.798 zeta  = 0.00673 chi   =  0.0105 gamma = -0.00181 phi   =  0.0282 
Loss:   0.525
Simulating with theta =  -0.795 zeta  = 0.00252 chi   =  0.0101 gamma = -0.00962 phi   =  0.0338 
Loss:   0.524
characterization_result.final_params
{(cirq.GridQubit(4, 4), cirq.GridQubit(4, 5)): {'theta': -0.7949597090484766,
  'zeta': 0.002522882039776196,
  'chi': 0.010100591038089794,
  'gamma': -0.009623918466707756,
  'phi': 0.03384923972295669} }
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