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:35<00:00, 3.06it/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 0x7f401e5a3050>

## 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.532 Simulating with theta = -0.685 zeta = 0 chi = 0 gamma = 0 phi = 0 Loss: 0.597 Simulating with theta = -0.785 zeta = 0.1 chi = 0 gamma = 0 phi = 0 Loss: 0.575 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.617 Simulating with theta = -0.785 zeta = 0 chi = 0 gamma = 0 phi = 0.1 Loss: 0.575 Simulating with theta = -0.745 zeta = 0.04 chi = 0.04 gamma = -0.1 phi = 0.04 Loss: 0.59 Simulating with theta = -0.869 zeta = 0.056 chi = 0.056 gamma = -0.04 phi = 0.056 Loss: 0.599 Simulating with theta = -0.731 zeta = 0.014 chi = 0.014 gamma = -0.01 phi = 0.014 Loss: 0.564 Simulating with theta = -0.804 zeta = 0.0056 chi = 0.0056 gamma = 0.096 phi = 0.0056 Loss: 0.613 Simulating with theta = -0.76 zeta = 0.0314 chi = 0.0314 gamma = -0.051 phi = 0.0314 Loss: 0.56 Simulating with theta = -0.754 zeta = -0.0818 chi = 0.0582 gamma = -0.0244 phi = 0.0582 Loss: 0.589 Simulating with theta = -0.777 zeta = 0.0545 chi = 0.0145 gamma = -0.0061 phi = 0.0145 Loss: 0.555 Simulating with theta = -0.75 zeta = 0.04 chi = 0.064 gamma = -0.0268 phi = -0.076 Loss: 0.579 Simulating with theta = -0.777 zeta = 0.00999 chi = 0.016 gamma = -0.00671 phi = 0.056 Loss: 0.554 Simulating with theta = -0.747 zeta = 0.044 chi = -0.0696 gamma = -0.0295 phi = 0.0464 Loss: 0.584 Simulating with theta = -0.776 zeta = 0.011 chi = 0.0576 gamma = -0.00738 phi = 0.0116 Loss: 0.55 Simulating with theta = -0.819 zeta = 0.0288 chi = 0.0338 gamma = -0.0185 phi = 0.0314 Loss: 0.546 Simulating with theta = -0.814 zeta = 0.0103 chi = 0.0174 gamma = 0.0355 phi = 0.014 Loss: 0.559 Simulating with theta = -0.8 zeta = 0.0156 chi = 0.0209 gamma = 0.0139 phi = 0.0184 Loss: 0.547 Simulating with theta = -0.805 zeta = -0.0284 chi = 0.0368 gamma = -0.00137 phi = 0.0324 Loss: 0.548 Simulating with theta = -0.817 zeta = 0.000787 chi = 0.0436 gamma = 0.00138 phi = -0.0185 Loss: 0.533 Simulating with theta = -0.835 zeta = -0.00429 chi = -0.00356 gamma = 0.00556 phi = 0.0139 Loss: 0.542 Simulating with theta = -0.817 zeta = 0.0447 chi = 0.00113 gamma = 0.00231 phi = -0.0143 Loss: 0.546 Simulating with theta = -0.829 zeta = 0.0124 chi = 0.00913 gamma = -0.0176 phi = -0.0134 Loss: 0.538 Simulating with theta = -0.817 zeta = -0.0297 chi = 0.0321 gamma = -0.014 phi = 0.0197 Loss: 0.539 Simulating with theta = -0.815 zeta = -0.0371 chi = -0.0013 gamma = 0.00863 phi = -0.0307 Loss: 0.533 Simulating with theta = -0.791 zeta = -0.0171 chi = 0.037 gamma = -0.0142 phi = -0.031 Loss: 0.531 Simulating with theta = -0.768 zeta = -0.0235 chi = 0.0572 gamma = -0.024 phi = -0.0535 Loss: 0.553 Simulating with theta = -0.798 zeta = 0.0133 chi = 0.0033 gamma = 0.00526 phi = -0.0572 Loss: 0.53 Simulating with theta = -0.789 zeta = 0.0347 chi = -0.0111 gamma = 0.0149 phi = -0.0956 Loss: 0.55 Simulating with theta = -0.773 zeta = -0.0285 chi = 0.0239 gamma = 0.018 phi = -0.0416 Loss: 0.534 Simulating with theta = -0.787 zeta = -0.0182 chi = 0.0202 gamma = 0.00912 phi = -0.0345 Loss: 0.526 Simulating with theta = -0.777 zeta = 0.0286 chi = 0.0429 gamma = -0.00799 phi = -0.0258 Loss: 0.542 Simulating with theta = -0.805 zeta = -0.0207 chi = 0.00976 gamma = 0.00447 phi = -0.0295 Loss: 0.526 Simulating with theta = -0.769 zeta = -0.0179 chi = -0.0155 gamma = 0.000494 phi = -0.0424 Loss: 0.538 Simulating with theta = -0.805 zeta = -0.00388 chi = 0.0288 gamma = 0.00116 phi = -0.0245 Loss: 0.526 Simulating with theta = -0.809 zeta = -0.0187 chi = 0.0396 gamma = 0.00234 phi = -0.0707 Loss: 0.534 Simulating with theta = -0.791 zeta = -0.00467 chi = 0.00991 gamma = 0.000584 phi = -0.0177 Loss: 0.526 Simulating with theta = -0.804 zeta = 0.00345 chi = -0.00816 gamma = 0.0224 phi = -0.0343 Loss: 0.533 Simulating with theta = -0.794 zeta = -0.012 chi = 0.0257 gamma = -0.00503 phi = -0.0318 Loss: 0.525 Simulating with theta = -0.795 zeta = -0.0371 chi = 0.0345 gamma = -0.00114 phi = 0.00196 Loss: 0.539 Simulating with theta = -0.797 zeta = 0.00069 chi = 0.0111 gamma = 0.00366 phi = -0.0424 Loss: 0.525 Simulating with theta = -0.81 zeta = 0.00204 chi = 0.0139 gamma = -0.00718 phi = -0.0238 Loss: 0.527 Simulating with theta = -0.793 zeta = -0.0132 chi = 0.0186 gamma = 0.00505 phi = -0.0318 Loss: 0.525 Simulating with theta = -0.807 zeta = -0.0149 chi = 0.0277 gamma = 0.00314 phi = -0.0463 Loss: 0.525 Simulating with theta = -0.793 zeta = -0.0201 chi = 0.00831 gamma = 0.00336 phi = -0.0483 Loss: 0.526 Simulating with theta = -0.796 zeta = -0.0161 chi = 0.0134 gamma = 0.00281 phi = -0.0423 Loss: 0.524 Simulating with theta = -0.79 zeta = -0.00153 chi = 0.0289 gamma = -0.00062 phi = -0.0484 Loss: 0.527 Simulating with theta = -0.801 zeta = -0.0159 chi = 0.0145 gamma = 0.0032 phi = -0.0342 Loss: 0.524 Simulating with theta = -0.804 zeta = -0.0118 chi = 0.00847 gamma = 0.0122 phi = -0.047 Loss: 0.525 Simulating with theta = -0.79 zeta = -0.00755 chi = -0.00123 gamma = 0.00761 phi = -0.0328 Loss: 0.527 Simulating with theta = -0.803 zeta = -0.0131 chi = 0.0205 gamma = 0.00426 phi = -0.0429 Loss: 0.524 Simulating with theta = -0.792 zeta = -0.0112 chi = 0.0228 gamma = -0.00458 phi = -0.0305 Loss: 0.525 Simulating with theta = -0.801 zeta = -0.0116 chi = 0.0121 gamma = 0.00798 phi = -0.0429 Loss: 0.524 Simulating with theta = -0.8 zeta = -0.0286 chi = 0.0206 gamma = 0.00566 phi = -0.0353 Loss: 0.527 Simulating with theta = -0.798 zeta = -0.00664 chi = 0.0135 gamma = 0.00416 phi = -0.0406 Loss: 0.524 Simulating with theta = -0.807 zeta = -0.0122 chi = 0.0109 gamma = 0.00392 phi = -0.0493 Loss: 0.525 Simulating with theta = -0.797 zeta = -0.0129 chi = 0.0167 gamma = 0.00476 phi = -0.0362 Loss: 0.524 Simulating with theta = -0.804 zeta = -0.00799 chi = 0.0174 gamma = 0.00694 phi = -0.0364 Loss: 0.524 Simulating with theta = -0.799 zeta = -0.00503 chi = 0.0175 gamma = 0.00804 phi = -0.0454 Loss: 0.524 Simulating with theta = -0.798 zeta = 0.000392 chi = 0.019 gamma = 0.0105 phi = -0.051 Loss: 0.525 Simulating with theta = -0.795 zeta = -0.0117 chi = 0.0146 gamma = 0.00475 phi = -0.0468 Loss: 0.524 Simulating with theta = -0.796 zeta = -0.00813 chi = 0.0211 gamma = 0.00241 phi = -0.0419 Loss: 0.524 Simulating with theta = -0.8 zeta = -0.00494 chi = 0.0181 gamma = 0.00468 phi = -0.0509 Loss: 0.524 Simulating with theta = -0.797 zeta = -0.0109 chi = 0.0171 gamma = 0.00474 phi = -0.0399 Loss: 0.524 Simulating with theta = -0.801 zeta = -0.0108 chi = 0.0122 gamma = 0.00797 phi = -0.0443 Loss: 0.524 Simulating with theta = -0.8 zeta = -0.0102 chi = 0.0144 gamma = 0.00658 phi = -0.0437 Loss: 0.524 Simulating with theta = -0.804 zeta = -0.0066 chi = 0.0185 gamma = 0.00637 phi = -0.0382 Loss: 0.524 Simulating with theta = -0.797 zeta = -0.0105 chi = 0.0156 gamma = 0.00515 phi = -0.0447 Loss: 0.524 Simulating with theta = -0.801 zeta = -0.0132 chi = 0.0206 gamma = 0.00735 phi = -0.046 Loss: 0.524 Simulating with theta = -0.802 zeta = -0.0165 chi = 0.0241 gamma = 0.00895 phi = -0.0488 Loss: 0.524 Simulating with theta = -0.795 zeta = -0.00683 chi = 0.0136 gamma = 0.00849 phi = -0.0449 Loss: 0.524 Simulating with theta = -0.801 zeta = -0.0115 chi = 0.0187 gamma = 0.00532 phi = -0.0434 Loss: 0.524 Simulating with theta = -0.799 zeta = -0.0175 chi = 0.017 gamma = 0.00362 phi = -0.0417 Loss: 0.524 Simulating with theta = -0.799 zeta = -0.00815 chi = 0.0174 gamma = 0.00694 phi = -0.0445 Loss: 0.524 Simulating with theta = -0.798 zeta = -0.0115 chi = 0.0213 gamma = 0.00522 phi = -0.0437 Loss: 0.524 Simulating with theta = -0.801 zeta = -0.0117 chi = 0.0224 gamma = 0.00667 phi = -0.0423 Loss: 0.524 Simulating with theta = -0.798 zeta = -0.0108 chi = 0.0173 gamma = 0.00553 phi = -0.0441 Loss: 0.524 Simulating with theta = -0.8 zeta = -0.0103 chi = 0.0151 gamma = 0.00674 phi = -0.0435 Loss: 0.524 Simulating with theta = -0.802 zeta = -0.0107 chi = 0.0186 gamma = 0.00801 phi = -0.0487 Loss: 0.524 Simulating with theta = -0.799 zeta = -0.0109 chi = 0.0174 gamma = 0.00556 phi = -0.0421 Loss: 0.524 Simulating with theta = -0.799 zeta = -0.0115 chi = 0.0215 gamma = 0.00554 phi = -0.0446 Loss: 0.524 Simulating with theta = -0.8 zeta = -0.0106 chi = 0.0167 gamma = 0.00644 phi = -0.0438 Loss: 0.524 Simulating with theta = -0.798 zeta = -0.00753 chi = 0.0145 gamma = 0.00456 phi = -0.0411 Loss: 0.524 Simulating with theta = -0.8 zeta = -0.0118 chi = 0.019 gamma = 0.00665 phi = -0.0448 Loss: 0.524

```
characterization_result.final_params
```

{(cirq.GridQubit(4, 4), cirq.GridQubit(4, 5)): {'theta': -0.7999761444482455, 'zeta': -0.011804015905981482, 'chi': 0.019047531036615148, 'gamma': 0.00665419599341657, 'phi': -0.044808635388633576} }

```
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()
```