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
cycle_depths = np.arange(3, 100, 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}
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,
)
sampled_df
```

100%|██████████| 108/108 [00:36<00:00, 2.93it/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 0x7f0ebc13a320>

## 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.576 Simulating with theta = -0.785 zeta = 0.1 chi = 0 gamma = 0 phi = 0 Loss: 0.566 Simulating with theta = -0.785 zeta = 0 chi = 0.1 gamma = 0 phi = 0 Loss: 0.57 Simulating with theta = -0.785 zeta = 0 chi = 0 gamma = 0.1 phi = 0 Loss: 0.591 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.619 Simulating with theta = -0.775 zeta = 0.01 chi = 0.01 gamma = 0.05 phi = 0.01 Loss: 0.557 Simulating with theta = -0.881 zeta = 0.044 chi = 0.044 gamma = 0.02 phi = 0.044 Loss: 0.602 Simulating with theta = -0.734 zeta = 0.011 chi = 0.011 gamma = 0.005 phi = 0.011 Loss: 0.552 Simulating with theta = -0.761 zeta = 0.0484 chi = -0.0916 gamma = 0.022 phi = 0.0484 Loss: 0.588 Simulating with theta = -0.779 zeta = 0.0121 chi = 0.0521 gamma = 0.0055 phi = 0.0121 Loss: 0.546 Simulating with theta = -0.759 zeta = -0.0868 chi = 0.0292 gamma = 0.0242 phi = 0.0532 Loss: 0.581 Simulating with theta = -0.779 zeta = 0.0533 chi = 0.00731 gamma = 0.00605 phi = 0.0133 Loss: 0.549 Simulating with theta = -0.77 zeta = 0.0206 chi = 0.0182 gamma = -0.0434 phi = 0.0446 Loss: 0.544 Simulating with theta = -0.754 zeta = 0.0388 chi = 0.0354 gamma = -0.0107 phi = -0.0676 Loss: 0.575 Simulating with theta = -0.777 zeta = 0.0097 chi = 0.00886 gamma = -0.00268 phi = 0.0581 Loss: 0.543 Simulating with theta = -0.822 zeta = 0.0273 chi = 0.0236 gamma = -0.0188 phi = 0.0402 Loss: 0.545 Simulating with theta = -0.795 zeta = -0.0255 chi = 0.0338 gamma = -0.0298 phi = 0.0487 Loss: 0.543 Simulating with theta = -0.801 zeta = 0.000729 chi = -0.0184 gamma = -0.0434 phi = 0.0645 Loss: 0.539 Simulating with theta = -0.749 zeta = -0.0251 chi = -0.0066 gamma = -0.0289 phi = 0.0461 Loss: 0.542 Simulating with theta = -0.793 zeta = -0.0366 chi = -0.0111 gamma = 0.00149 phi = 0.0424 Loss: 0.543 Simulating with theta = -0.792 zeta = -0.0443 chi = -0.00977 gamma = -0.0375 phi = 0.0226 Loss: 0.537 Simulating with theta = -0.773 zeta = -0.0166 chi = -0.0521 gamma = -0.0135 phi = 0.0216 Loss: 0.539 Simulating with theta = -0.767 zeta = 0.00252 chi = -0.0236 gamma = -0.0508 phi = 0.0195 Loss: 0.541 Simulating with theta = -0.818 zeta = 0.00201 chi = -0.0349 gamma = -0.0292 phi = 0.00517 Loss: 0.541 Simulating with theta = -0.821 zeta = -0.0258 chi = -0.0224 gamma = 0.00136 phi = 0.026 Loss: 0.543 Simulating with theta = -0.78 zeta = -0.00455 chi = -0.0233 gamma = -0.0378 phi = 0.0212 Loss: 0.534 Simulating with theta = -0.755 zeta = -0.0279 chi = -0.00648 gamma = -0.0237 phi = 0.0468 Loss: 0.541 Simulating with theta = -0.802 zeta = -0.00546 chi = -0.0278 gamma = -0.0278 phi = 0.0156 Loss: 0.533 Simulating with theta = -0.773 zeta = -0.0291 chi = -0.0269 gamma = -0.0033 phi = -0.0322 Loss: 0.529 Simulating with theta = -0.759 zeta = -0.044 chi = -0.0311 gamma = 0.0167 phi = -0.0805 Loss: 0.534 Simulating with theta = -0.8 zeta = -0.0167 chi = 0.017 gamma = -0.0291 phi = -0.0107 Loss: 0.539 Simulating with theta = -0.793 zeta = -0.0167 chi = -0.000288 gamma = -0.0252 phi = -0.00264 Loss: 0.531 Simulating with theta = -0.782 zeta = 0.0219 chi = -0.0216 gamma = -8.61e-05 phi = -0.0218 Loss: 0.533 Simulating with theta = -0.794 zeta = -0.00717 chi = -0.00728 gamma = 0.0152 phi = -0.0376 Loss: 0.528 Simulating with theta = -0.8 zeta = -0.00848 chi = 0.000754 gamma = 0.0417 phi = -0.0669 Loss: 0.533 Simulating with theta = -0.768 zeta = -0.00694 chi = 0.00544 gamma = 0.0225 phi = -0.0533 Loss: 0.532 Simulating with theta = -0.784 zeta = -0.0459 chi = 0.00996 gamma = 0.00378 phi = -0.0284 Loss: 0.534 Simulating with theta = -0.782 zeta = 0.00498 chi = -0.0137 gamma = 0.000881 phi = -0.0235 Loss: 0.529 Simulating with theta = -0.802 zeta = -0.0122 chi = -0.0247 gamma = -0.0274 phi = 0.0149 Loss: 0.533 Simulating with theta = -0.777 zeta = -0.00827 chi = -0.00209 gamma = 0.01 phi = -0.0362 Loss: 0.529 Simulating with theta = -0.771 zeta = 0.000881 chi = -0.0197 gamma = 0.0343 phi = -0.0491 Loss: 0.534 Simulating with theta = -0.788 zeta = -0.0123 chi = -0.00513 gamma = -0.0103 phi = -0.0143 Loss: 0.528 Simulating with theta = -0.78 zeta = -0.0207 chi = -0.022 gamma = 0.005 phi = -0.0575 Loss: 0.527 Simulating with theta = -0.777 zeta = -0.0311 chi = -0.033 gamma = 0.0075 phi = -0.0862 Loss: 0.531 Simulating with theta = -0.782 zeta = -0.036 chi = -0.0117 gamma = 0.00577 phi = -0.0476 Loss: 0.528 Simulating with theta = -0.789 zeta = -0.0338 chi = -0.0271 gamma = -0.00506 phi = -0.0394 Loss: 0.528 Simulating with theta = -0.8 zeta = -0.0149 chi = -0.00242 gamma = 0.00755 phi = -0.0464 Loss: 0.529 Simulating with theta = -0.78 zeta = -0.0255 chi = -0.0207 gamma = -0.000587 phi = -0.0357 Loss: 0.527 Simulating with theta = -0.782 zeta = -0.037 chi = -0.0304 gamma = 0.0184 phi = -0.0729 Loss: 0.527 Simulating with theta = -0.772 zeta = -0.0541 chi = -0.0375 gamma = -0.00579 phi = -0.0637 Loss: 0.536 Simulating with theta = -0.788 zeta = -0.0189 chi = -0.0148 gamma = 0.00997 phi = -0.0441 Loss: 0.526 Simulating with theta = -0.775 zeta = -0.0214 chi = -0.0128 gamma = 0.0205 phi = -0.0637 Loss: 0.527 Simulating with theta = -0.78 zeta = -0.0135 chi = -0.0286 gamma = 0.0156 phi = -0.0619 Loss: 0.527 Simulating with theta = -0.788 zeta = -0.0248 chi = -0.0339 gamma = -0.00114 phi = -0.0452 Loss: 0.527 Simulating with theta = -0.779 zeta = -0.0223 chi = -0.018 gamma = 0.0151 phi = -0.0591 Loss: 0.526 Simulating with theta = -0.78 zeta = -0.00335 chi = -0.0113 gamma = -0.000434 phi = -0.0305 Loss: 0.528 Simulating with theta = -0.782 zeta = -0.0286 chi = -0.0256 gamma = 0.0137 phi = -0.0623 Loss: 0.526 Simulating with theta = -0.784 zeta = -0.016 chi = -0.0229 gamma = 0.0243 phi = -0.0782 Loss: 0.526 Simulating with theta = -0.785 zeta = -0.019 chi = -0.022 gamma = 0.0265 phi = -0.0647 Loss: 0.526 Simulating with theta = -0.787 zeta = -0.0285 chi = -0.0127 gamma = 0.0203 phi = -0.0614 Loss: 0.527 Simulating with theta = -0.782 zeta = -0.0172 chi = -0.0246 gamma = 0.0167 phi = -0.0618 Loss: 0.526 Simulating with theta = -0.78 zeta = -0.0222 chi = -0.0204 gamma = 0.00547 phi = -0.0574 Loss: 0.527 Simulating with theta = -0.784 zeta = -0.0198 chi = -0.0216 gamma = 0.0212 phi = -0.0629 Loss: 0.526 Simulating with theta = -0.782 zeta = -0.0267 chi = -0.019 gamma = 0.00637 phi = -0.0378 Loss: 0.527 Simulating with theta = -0.783 zeta = -0.0187 chi = -0.0219 gamma = 0.0198 phi = -0.0681 Loss: 0.526 Simulating with theta = -0.789 zeta = -0.019 chi = -0.0254 gamma = 0.0175 phi = -0.0606 Loss: 0.526 Simulating with theta = -0.794 zeta = -0.0174 chi = -0.0291 gamma = 0.0187 phi = -0.0614 Loss: 0.527 Simulating with theta = -0.779 zeta = -0.0224 chi = -0.0329 gamma = 0.0256 phi = -0.0822 Loss: 0.527 Simulating with theta = -0.786 zeta = -0.0198 chi = -0.0193 gamma = 0.0139 phi = -0.0536 Loss: 0.526 Simulating with theta = -0.788 zeta = -0.00919 chi = -0.0195 gamma = 0.0219 phi = -0.0606 Loss: 0.527 Simulating with theta = -0.783 zeta = -0.0237 chi = -0.0241 gamma = 0.0158 phi = -0.0618 Loss: 0.526 Simulating with theta = -0.788 zeta = -0.0232 chi = -0.0203 gamma = 0.0186 phi = -0.061 Loss: 0.526 Simulating with theta = -0.788 zeta = -0.022 chi = -0.0228 gamma = 0.013 phi = -0.0592 Loss: 0.526 Simulating with theta = -0.79 zeta = -0.0231 chi = -0.0234 gamma = 0.0089 phi = -0.0573 Loss: 0.526 Simulating with theta = -0.79 zeta = -0.0244 chi = -0.0229 gamma = 0.0117 phi = -0.0504 Loss: 0.526 Simulating with theta = -0.785 zeta = -0.0201 chi = -0.0222 gamma = 0.0178 phi = -0.0637 Loss: 0.526 Simulating with theta = -0.784 zeta = -0.0187 chi = -0.0252 gamma = 0.0126 phi = -0.0585 Loss: 0.526 Simulating with theta = -0.782 zeta = -0.0227 chi = -0.0201 gamma = 0.0117 phi = -0.0581 Loss: 0.526 Simulating with theta = -0.787 zeta = -0.0199 chi = -0.0241 gamma = 0.0161 phi = -0.06 Loss: 0.526 Simulating with theta = -0.788 zeta = -0.0236 chi = -0.0198 gamma = 0.018 phi = -0.0608 Loss: 0.526 Simulating with theta = -0.785 zeta = -0.0199 chi = -0.0239 gamma = 0.014 phi = -0.0591 Loss: 0.526 Simulating with theta = -0.789 zeta = -0.0169 chi = -0.0208 gamma = 0.0141 phi = -0.0564 Loss: 0.526 Simulating with theta = -0.785 zeta = -0.022 chi = -0.0233 gamma = 0.0154 phi = -0.0605 Loss: 0.526 Simulating with theta = -0.786 zeta = -0.0218 chi = -0.0271 gamma = 0.0166 phi = -0.0674 Loss: 0.526 Simulating with theta = -0.786 zeta = -0.0203 chi = -0.0213 gamma = 0.0146 phi = -0.0571 Loss: 0.526

```
characterization_result.final_params
```

{(cirq.GridQubit(4, 4), cirq.GridQubit(4, 5)): {'theta': -0.7846002334083311, 'zeta': -0.022043544658316507, 'chi': -0.023274109083803486, 'gamma': 0.015360241207523483, 'phi': -0.06048059388359335} }

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