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:21<00:00, 5.13it/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 0x7f3db0c56ca0>

## 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.579 Simulating with theta = -0.785 zeta = 0.1 chi = 0 gamma = 0 phi = 0 Loss: 0.552 Simulating with theta = -0.785 zeta = 0 chi = 0.1 gamma = 0 phi = 0 Loss: 0.549 Simulating with theta = -0.785 zeta = 0 chi = 0 gamma = 0.1 phi = 0 Loss: 0.577 Simulating with theta = -0.785 zeta = 0 chi = 0 gamma = 0 phi = 0.1 Loss: 0.546 Simulating with theta = -0.885 zeta = 0.04 chi = 0.04 gamma = 0.04 phi = 0.04 Loss: 0.598 Simulating with theta = -0.735 zeta = 0.01 chi = 0.01 gamma = 0.01 phi = 0.01 Loss: 0.541 Simulating with theta = -0.765 zeta = 0.044 chi = 0.044 gamma = -0.096 phi = 0.044 Loss: 0.568 Simulating with theta = -0.77 zeta = 0.033 chi = 0.033 gamma = -0.047 phi = 0.033 Loss: 0.537 Simulating with theta = -0.759 zeta = -0.0828 chi = 0.0572 gamma = -0.0148 phi = 0.0572 Loss: 0.555 Simulating with theta = -0.779 zeta = 0.0543 chi = 0.0143 gamma = -0.0037 phi = 0.0143 Loss: 0.536 Simulating with theta = -0.757 zeta = 0.0389 chi = -0.0771 gamma = -0.0163 phi = 0.0629 Loss: 0.551 Simulating with theta = -0.778 zeta = 0.00973 chi = 0.0557 gamma = -0.00407 phi = 0.0157 Loss: 0.537 Simulating with theta = -0.754 zeta = 0.0428 chi = 0.0452 gamma = -0.0179 phi = -0.0708 Loss: 0.555 Simulating with theta = -0.778 zeta = 0.0107 chi = 0.0113 gamma = -0.00448 phi = 0.0573 Loss: 0.536 Simulating with theta = -0.821 zeta = 0.0331 chi = 0.0357 gamma = -0.0337 phi = 0.0381 Loss: 0.539 Simulating with theta = -0.799 zeta = 0.0273 chi = 0.0293 gamma = -0.0228 phi = 0.0311 Loss: 0.533 Simulating with theta = -0.786 zeta = 0.0404 chi = -0.0206 gamma = -0.0271 phi = 0.0386 Loss: 0.529 Simulating with theta = -0.791 zeta = 0.0557 chi = -0.0587 gamma = -0.0386 phi = 0.05 Loss: 0.536 Simulating with theta = -0.801 zeta = 0.0201 chi = -0.0193 gamma = 0.0238 phi = 0.0235 Loss: 0.54 Simulating with theta = -0.778 zeta = 0.0298 chi = 0.0199 gamma = -0.0293 phi = 0.0306 Loss: 0.531 Simulating with theta = -0.794 zeta = 0.05 chi = 0.00588 gamma = -0.0287 phi = -0.0115 Loss: 0.532 Simulating with theta = -0.798 zeta = 0.0047 chi = -0.000481 gamma = -0.0394 phi = 0.0212 Loss: 0.53 Simulating with theta = -0.777 zeta = 0.0226 chi = -0.0274 gamma = -0.027 phi = 0.00047 Loss: 0.528 Simulating with theta = -0.766 zeta = 0.0203 chi = -0.0557 gamma = -0.0292 phi = -0.0148 Loss: 0.535 Simulating with theta = -0.776 zeta = -0.011 chi = -0.0173 gamma = -0.0205 phi = 0.0478 Loss: 0.531 Simulating with theta = -0.781 zeta = 0.00425 chi = -0.0115 gamma = -0.0225 phi = 0.033 Loss: 0.529 Simulating with theta = -0.793 zeta = -0.00098 chi = -0.0439 gamma = -0.0171 phi = 0.00667 Loss: 0.53 Simulating with theta = -0.789 zeta = 0.00671 chi = -0.0279 gamma = -0.0202 phi = 0.0127 Loss: 0.528 Simulating with theta = -0.769 zeta = 0.0249 chi = -0.0345 gamma = 0.000698 phi = 0.0126 Loss: 0.534 Simulating with theta = -0.791 zeta = 0.00975 chi = -0.00898 gamma = -0.0294 phi = 0.0191 Loss: 0.528 Simulating with theta = -0.785 zeta = 0.0335 chi = -0.0386 gamma = -0.0505 phi = 0.0415 Loss: 0.529 Simulating with theta = -0.785 zeta = 0.0251 chi = -0.0289 gamma = -0.0379 phi = 0.0311 Loss: 0.528 Simulating with theta = -0.783 zeta = -0.013 chi = -0.0213 gamma = -0.0277 phi = -1.84e-05 Loss: 0.529 Simulating with theta = -0.789 zeta = 0.0162 chi = -0.0343 gamma = -0.0344 phi = -0.00767 Loss: 0.53 Simulating with theta = -0.783 zeta = 0.00724 chi = -0.0172 gamma = -0.0255 phi = 0.0228 Loss: 0.528 Simulating with theta = -0.787 zeta = 0.0416 chi = -0.0229 gamma = -0.0283 phi = 0.0345 Loss: 0.529 Simulating with theta = -0.784 zeta = 0.000638 chi = -0.0217 gamma = -0.0279 phi = 0.00861 Loss: 0.528 Simulating with theta = -0.779 zeta = 0.0194 chi = -0.0137 gamma = -0.0389 phi = 0.0202 Loss: 0.528 Simulating with theta = -0.781 zeta = 0.0163 chi = -0.0173 gamma = -0.0342 phi = 0.0183 Loss: 0.528 Simulating with theta = -0.792 zeta = 0.000971 chi = -0.0102 gamma = -0.0349 phi = 0.0395 Loss: 0.529 Simulating with theta = -0.781 zeta = 0.0172 chi = -0.0231 gamma = -0.029 phi = 0.0102 Loss: 0.527 Simulating with theta = -0.775 zeta = 0.0168 chi = -0.0343 gamma = -0.0324 phi = 0.0174 Loss: 0.528 Simulating with theta = -0.787 zeta = 0.0115 chi = -0.0153 gamma = -0.0302 phi = 0.0186 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0211 chi = -0.0253 gamma = -0.0382 phi = 0.0119 Loss: 0.528 Simulating with theta = -0.783 zeta = 0.0107 chi = -0.0192 gamma = -0.0287 phi = 0.0201 Loss: 0.528 Simulating with theta = -0.783 zeta = 0.0317 chi = -0.0198 gamma = -0.0361 phi = 0.0308 Loss: 0.528 Simulating with theta = -0.781 zeta = 0.00984 chi = -0.00899 gamma = -0.0254 phi = 0.00809 Loss: 0.528 Simulating with theta = -0.783 zeta = -0.00548 chi = -0.0137 gamma = -0.0229 phi = -0.000606 Loss: 0.528 Simulating with theta = -0.783 zeta = 0.0224 chi = -0.0183 gamma = -0.0328 phi = 0.0229 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0124 chi = -0.0167 gamma = -0.0242 phi = 0.0137 Loss: 0.527 Simulating with theta = -0.786 zeta = 0.0199 chi = -0.0281 gamma = -0.0325 phi = 0.0261 Loss: 0.528 Simulating with theta = -0.783 zeta = 0.0123 chi = -0.0138 gamma = -0.0272 phi = 0.0126 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0197 chi = -0.0156 gamma = -0.0287 phi = 0.0111 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0241 chi = -0.0138 gamma = -0.0287 phi = 0.00665 Loss: 0.527 Simulating with theta = -0.779 zeta = 0.0221 chi = -0.0197 gamma = -0.0266 phi = 0.00958 Loss: 0.527 Simulating with theta = -0.781 zeta = 0.0194 chi = -0.0186 gamma = -0.0275 phi = 0.0118 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0173 chi = -0.0101 gamma = -0.0271 phi = 0.0187 Loss: 0.527 Simulating with theta = -0.782 zeta = 0.0172 chi = -0.0199 gamma = -0.0285 phi = 0.0123 Loss: 0.527 Simulating with theta = -0.78 zeta = 0.024 chi = -0.0178 gamma = -0.0337 phi = 0.0146 Loss: 0.528 Simulating with theta = -0.784 zeta = 0.0153 chi = -0.017 gamma = -0.0266 phi = 0.0139 Loss: 0.527 Simulating with theta = -0.783 zeta = 0.0253 chi = -0.022 gamma = -0.0304 phi = 0.0163 Loss: 0.527 Simulating with theta = -0.783 zeta = 0.0164 chi = -0.0189 gamma = -0.0239 phi = 0.00329 Loss: 0.527 Simulating with theta = -0.783 zeta = 0.0134 chi = -0.0192 gamma = -0.0194 phi = -0.00653 Loss: 0.527 Simulating with theta = -0.783 zeta = 0.00993 chi = -0.014 gamma = -0.0236 phi = 0.00475 Loss: 0.527 Simulating with theta = -0.783 zeta = 0.0214 chi = -0.02 gamma = -0.0287 phi = 0.0134 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0197 chi = -0.0162 gamma = -0.0256 phi = 0.0091 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0209 chi = -0.0144 gamma = -0.0241 phi = 0.00748 Loss: 0.527 Simulating with theta = -0.786 zeta = 0.018 chi = -0.0157 gamma = -0.0253 phi = 0.00784 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0232 chi = -0.0169 gamma = -0.0257 phi = 0.00332 Loss: 0.527 Simulating with theta = -0.786 zeta = 0.0178 chi = -0.0126 gamma = -0.0223 phi = -0.000165 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0189 chi = -0.0158 gamma = -0.0199 phi = -0.00243 Loss: 0.527 Simulating with theta = -0.786 zeta = 0.0185 chi = -0.0158 gamma = -0.0155 phi = -0.00921 Loss: 0.527 Simulating with theta = -0.788 zeta = 0.0231 chi = -0.0112 gamma = -0.023 phi = 0.00313 Loss: 0.527 Simulating with theta = -0.787 zeta = 0.0163 chi = -0.011 gamma = -0.0201 phi = 0.00302 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0215 chi = -0.0154 gamma = -0.0243 phi = 0.00324 Loss: 0.527 Simulating with theta = -0.786 zeta = 0.0231 chi = -0.0164 gamma = -0.0243 phi = 0.00787 Loss: 0.527 Simulating with theta = -0.786 zeta = 0.0258 chi = -0.0183 gamma = -0.0253 phi = 0.0119 Loss: 0.527 Simulating with theta = -0.783 zeta = 0.0178 chi = -0.0198 gamma = -0.0241 phi = 0.00647 Loss: 0.527 Simulating with theta = -0.787 zeta = 0.0218 chi = -0.0134 gamma = -0.0233 phi = 0.00396 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0245 chi = -0.0144 gamma = -0.0211 phi = 0.000212 Loss: 0.527 Simulating with theta = -0.786 zeta = 0.0196 chi = -0.0154 gamma = -0.0242 phi = 0.00593 Loss: 0.527 Simulating with theta = -0.787 zeta = 0.0211 chi = -0.0162 gamma = -0.0223 phi = -5.25e-05 Loss: 0.527 Simulating with theta = -0.788 zeta = 0.0212 chi = -0.0171 gamma = -0.0214 phi = -0.00382 Loss: 0.527 Simulating with theta = -0.787 zeta = 0.0203 chi = -0.0154 gamma = -0.0213 phi = 0.00287 Loss: 0.527 Simulating with theta = -0.787 zeta = 0.0235 chi = -0.015 gamma = -0.0263 phi = 0.0107 Loss: 0.527 Simulating with theta = -0.788 zeta = 0.0243 chi = -0.0151 gamma = -0.0228 phi = 0.00419 Loss: 0.527 Simulating with theta = -0.789 zeta = 0.0267 chi = -0.015 gamma = -0.0221 phi = 0.00332 Loss: 0.527 Simulating with theta = -0.786 zeta = 0.0207 chi = -0.0157 gamma = -0.0193 phi = -0.00313 Loss: 0.527 Simulating with theta = -0.787 zeta = 0.0228 chi = -0.0151 gamma = -0.0246 phi = 0.00721 Loss: 0.527

```
characterization_result.final_params
```

{(cirq.GridQubit(4, 4), cirq.GridQubit(4, 5)): {'theta': -0.7875996188349357, 'zeta': 0.024337612097931387, 'chi': -0.015137949185273395, 'gamma': -0.02279913410514104, 'phi': 0.004192234177185471} }

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