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 0x7f81575414b0>

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.588
Simulating with theta =  -0.785 zeta  =     0.1 chi   =       0 gamma =       0 phi   =       0 
Loss:   0.556
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.579
Simulating with theta =  -0.785 zeta  =       0 chi   =       0 gamma =       0 phi   =     0.1 
Loss:   0.556
Simulating with theta =  -0.885 zeta  =    0.04 chi   =    0.04 gamma =    0.04 phi   =    0.04 
Loss:    0.59
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.566
Simulating with theta =   -0.77 zeta  =   0.033 chi   =   0.033 gamma =  -0.047 phi   =   0.033 
Loss:   0.541
Simulating with theta =  -0.759 zeta  = -0.0828 chi   =  0.0572 gamma = -0.0148 phi   =  0.0572 
Loss:   0.563
Simulating with theta =  -0.779 zeta  =  0.0543 chi   =  0.0143 gamma = -0.0037 phi   =  0.0143 
Loss:   0.542
Simulating with theta =  -0.757 zeta  =  0.0389 chi   =  0.0629 gamma = -0.0163 phi   = -0.0771 
Loss:   0.565
Simulating with theta =  -0.778 zeta  = 0.00973 chi   =  0.0157 gamma = -0.00407 phi   =  0.0557 
Loss:   0.544
Simulating with theta =  -0.754 zeta  =  0.0428 chi   = -0.0708 gamma = -0.0179 phi   =  0.0452 
Loss:   0.546
Simulating with theta =  -0.811 zeta  =  0.0459 chi   = -0.0131 gamma = -0.0391 phi   =  0.0493 
Loss:   0.535
Simulating with theta =  -0.816 zeta  =  0.0144 chi   =  0.0908 gamma = -0.0196 phi   =  0.0157 
Loss:   0.552
Simulating with theta =  -0.769 zeta  =  0.0357 chi   = -0.0304 gamma = -0.0183 phi   =  0.0378 
Loss:   0.534
Simulating with theta =  -0.788 zeta  =  0.0578 chi   = -0.0142 gamma = -0.0392 phi   = -0.00196 
Loss:   0.534
Simulating with theta =  -0.791 zeta  =  0.0147 chi   = -0.0242 gamma = -0.0537 phi   =   0.033 
Loss:   0.531
Simulating with theta =  -0.808 zeta  =  0.0287 chi   = -0.0658 gamma = -0.0131 phi   =  0.0143 
Loss:    0.53
Simulating with theta =  -0.826 zeta  =  0.0265 chi   =  -0.115 gamma = 0.00381 phi   = 0.00489 
Loss:   0.541
Simulating with theta =  -0.765 zeta  = 0.00883 chi   = -0.0407 gamma = -0.0107 phi   = -0.0161 
Loss:   0.533
Simulating with theta =  -0.805 zeta  = 0.00831 chi   = -0.0276 gamma = -0.0283 phi   = -0.0261 
Loss:   0.536
Simulating with theta =  -0.778 zeta  =  0.0289 chi   = -0.0297 gamma = -0.0208 phi   =  0.0218 
Loss:   0.528
Simulating with theta =  -0.783 zeta  = -0.0254 chi   = -0.0499 gamma = -0.000177 phi   =  0.0232 
Loss:   0.533
Simulating with theta =  -0.813 zeta  = 0.00989 chi   = -0.0271 gamma = -0.0245 phi   =  0.0529 
Loss:    0.53
Simulating with theta =  -0.807 zeta  =  0.0583 chi   = -0.00877 gamma = -0.0447 phi   =  0.0256 
Loss:   0.534
Simulating with theta =  -0.789 zeta  = -0.0045 chi   = -0.0396 gamma = -0.0113 phi   =  0.0238 
Loss:   0.527
Simulating with theta =  -0.798 zeta  =  0.0105 chi   = -0.0407 gamma =  0.0258 phi   =  0.0122 
Loss:   0.537
Simulating with theta =  -0.793 zeta  =  0.0136 chi   = -0.0283 gamma = -0.0338 phi   =  0.0278 
Loss:   0.525
Simulating with theta =  -0.768 zeta  =  0.0168 chi   = -0.0383 gamma = -0.00717 phi   = -0.0179 
Loss:   0.531
Simulating with theta =  -0.802 zeta  =  0.0116 chi   = -0.0299 gamma = -0.0201 phi   =  0.0352 
Loss:   0.526
Simulating with theta =  -0.802 zeta  =  0.0313 chi   = -0.0773 gamma = -0.0397 phi   =  0.0492 
Loss:   0.533
Simulating with theta =   -0.79 zeta  = 0.00783 chi   = -0.0193 gamma = -0.00993 phi   =  0.0123 
Loss:   0.526
Simulating with theta =  -0.773 zeta  = -0.0057 chi   = 0.00702 gamma = -0.0253 phi   =  0.0341 
Loss:   0.532
Simulating with theta =  -0.799 zeta  =  0.0201 chi   = -0.0476 gamma = -0.0162 phi   =  0.0192 
Loss:   0.526
Simulating with theta =  -0.811 zeta  = -0.00939 chi   = -0.0362 gamma = -0.0157 phi   =  0.0255 
Loss:   0.526
Simulating with theta =  -0.808 zeta  =   0.022 chi   = -0.0249 gamma =  -0.027 phi   =  0.0242 
Loss:   0.526
Simulating with theta =  -0.786 zeta  =  0.0395 chi   = -0.0238 gamma = -0.0271 phi   =   0.022 
Loss:   0.528
Simulating with theta =  -0.804 zeta  = 0.00282 chi   = -0.0331 gamma = -0.0186 phi   =  0.0246 
Loss:   0.525
Simulating with theta =  -0.787 zeta  = 0.000383 chi   = -0.0384 gamma = -0.0124 phi   =  0.0234 
Loss:   0.527
Simulating with theta =  -0.803 zeta  =  0.0166 chi   = -0.0283 gamma = -0.0234 phi   =   0.024 
Loss:   0.525
Simulating with theta =  -0.811 zeta  =  0.0181 chi   = -0.0475 gamma = -0.0349 phi   =  0.0401 
Loss:   0.528
Simulating with theta =  -0.795 zeta  =  0.0104 chi   = -0.0264 gamma = -0.0162 phi   =  0.0192 
Loss:   0.525
Simulating with theta =  -0.796 zeta  =  0.0138 chi   = -0.0356 gamma = -0.0231 phi   =  0.0107 
Loss:   0.525
Simulating with theta =  -0.793 zeta  =  0.0149 chi   = -0.0384 gamma = -0.0246 phi   = -0.00156 
Loss:   0.527
Simulating with theta =  -0.797 zeta  = 0.00282 chi   = -0.0131 gamma = -0.0299 phi   =  0.0233 
Loss:   0.526
Simulating with theta =  -0.798 zeta  = 0.00713 chi   = -0.0217 gamma = -0.0264 phi   =  0.0223 
Loss:   0.525
Simulating with theta =  -0.791 zeta  = 0.00251 chi   = -0.0298 gamma = -0.0239 phi   =  0.0178 
Loss:   0.525
Simulating with theta =  -0.801 zeta  = 0.00102 chi   = -0.0303 gamma = -0.00942 phi   =  0.0101 
Loss:   0.525
Simulating with theta =  -0.806 zeta  =  0.0116 chi   = -0.0291 gamma = -0.0136 phi   =   0.017 
Loss:   0.526
Simulating with theta =  -0.795 zeta  = 0.00477 chi   = -0.0296 gamma = -0.0213 phi   =  0.0176 
Loss:   0.525
Simulating with theta =  -0.803 zeta  = 0.00143 chi   = -0.0337 gamma = -0.0234 phi   =  0.0149 
Loss:   0.525
Simulating with theta =  -0.797 zeta  = 0.00815 chi   = -0.0282 gamma =  -0.018 phi   =  0.0181 
Loss:   0.525
Simulating with theta =  -0.795 zeta  =  0.0136 chi   =  -0.029 gamma = -0.0335 phi   =  0.0272 
Loss:   0.525
Simulating with theta =  -0.799 zeta  = 0.00418 chi   =   -0.03 gamma = -0.0154 phi   =  0.0144 
Loss:   0.525
Simulating with theta =   -0.79 zeta  =  0.0124 chi   = -0.0249 gamma = -0.0231 phi   = 0.00864 
Loss:   0.525
Simulating with theta =  -0.801 zeta  = 0.00521 chi   = -0.0311 gamma = -0.0197 phi   =  0.0206 
Loss:   0.525
Simulating with theta =    -0.8 zeta  = -0.00202 chi   = -0.0206 gamma = -0.0172 phi   =  0.0265 
Loss:   0.526
Simulating with theta =  -0.797 zeta  = 0.00984 chi   = -0.0318 gamma = -0.0216 phi   =  0.0147 
Loss:   0.525
Simulating with theta =  -0.798 zeta  = 0.00573 chi   = -0.0386 gamma =  -0.012 phi   =  0.0119 
Loss:   0.525
Simulating with theta =  -0.798 zeta  = 0.00678 chi   = -0.0259 gamma = -0.0228 phi   =  0.0197 
Loss:   0.525
Simulating with theta =  -0.799 zeta  = 0.00417 chi   = -0.0311 gamma = -0.0224 phi   =  0.0166 
Loss:   0.525
Simulating with theta =  -0.797 zeta  = 0.00715 chi   = -0.0289 gamma = -0.0191 phi   =  0.0178 
Loss:   0.525
Simulating with theta =  -0.796 zeta  = 0.00933 chi   =  -0.029 gamma = -0.0264 phi   =  0.0218 
Loss:   0.525
Simulating with theta =    -0.8 zeta  =  0.0106 chi   = -0.0291 gamma = -0.0225 phi   =  0.0202 
Loss:   0.525
Simulating with theta =  -0.802 zeta  = 0.00649 chi   = -0.0298 gamma = -0.0159 phi   =  0.0154 
Loss:   0.525
Simulating with theta =  -0.797 zeta  = 0.00862 chi   = -0.0292 gamma = -0.0238 phi   =  0.0202 
Loss:   0.525
Simulating with theta =  -0.795 zeta  =   0.012 chi   = -0.0269 gamma = -0.0242 phi   =  0.0164 
Loss:   0.525
Simulating with theta =  -0.799 zeta  =  0.0069 chi   =   -0.03 gamma = -0.0208 phi   =  0.0196 
Loss:   0.525
Simulating with theta =  -0.795 zeta  = 0.00516 chi   = -0.0292 gamma = -0.0207 phi   =  0.0165 
Loss:   0.525
Simulating with theta =  -0.799 zeta  = 0.00921 chi   = -0.0291 gamma = -0.0221 phi   =  0.0193 
Loss:   0.525
characterization_result.final_params
{(cirq.GridQubit(4, 4), cirq.GridQubit(4, 5)): {'theta': -0.7990482518698238,
  'zeta': 0.009208657582893263,
  'chi': -0.029136675661585624,
  'gamma': -0.0220853288225957,
  'phi': 0.019277642086914897} }
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