![]() |
![]() |
![]() |
![]() |
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:23<00:00, 4.59it/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 0x7f974baf6370>
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.585 Simulating with theta = -0.785 zeta = 0.1 chi = 0 gamma = 0 phi = 0 Loss: 0.553 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.586 Simulating with theta = -0.785 zeta = 0 chi = 0 gamma = 0 phi = 0.1 Loss: 0.548 Simulating with theta = -0.745 zeta = 0.04 chi = 0.04 gamma = -0.1 phi = 0.04 Loss: 0.573 Simulating with theta = -0.869 zeta = 0.056 chi = 0.056 gamma = -0.04 phi = 0.056 Loss: 0.581 Simulating with theta = -0.823 zeta = 0.042 chi = 0.042 gamma = -0.03 phi = 0.042 Loss: 0.546 Simulating with theta = -0.841 zeta = 0.0168 chi = 0.0168 gamma = 0.088 phi = 0.0168 Loss: 0.604 Simulating with theta = -0.769 zeta = 0.0342 chi = 0.0342 gamma = -0.053 phi = 0.0342 Loss: 0.542 Simulating with theta = -0.794 zeta = 0.0705 chi = -0.0695 gamma = -0.0332 phi = 0.0705 Loss: 0.549 Simulating with theta = -0.798 zeta = -0.0413 chi = 0.00267 gamma = -0.0465 phi = 0.0987 Loss: 0.536 Simulating with theta = -0.79 zeta = -0.0565 chi = 0.101 gamma = -0.0186 phi = 0.0395 Loss: 0.548 Simulating with theta = -0.791 zeta = -0.0248 chi = 0.0584 gamma = -0.0222 phi = 0.0472 Loss: 0.537 Simulating with theta = -0.801 zeta = 0.00404 chi = 0.0549 gamma = -0.0607 phi = -0.0112 Loss: 0.555 Simulating with theta = -0.789 zeta = 0.00101 chi = 0.0137 gamma = -0.0152 phi = 0.0722 Loss: 0.532 Simulating with theta = -0.75 zeta = -0.0544 chi = 0.00161 gamma = -0.0248 phi = 0.0589 Loss: 0.546 Simulating with theta = -0.768 zeta = -0.0303 chi = 0.0117 gamma = -0.0261 phi = 0.0547 Loss: 0.533 Simulating with theta = -0.804 zeta = -0.0723 chi = 0.000412 gamma = 0.00901 phi = 0.0749 Loss: 0.57 Simulating with theta = -0.778 zeta = 0.00756 chi = 0.0258 gamma = -0.0375 phi = 0.0444 Loss: 0.531 Simulating with theta = -0.776 zeta = -0.000431 chi = -0.0369 gamma = -0.0278 phi = 0.0608 Loss: 0.533 Simulating with theta = -0.761 zeta = 0.0325 chi = 0.00305 gamma = 0.00385 phi = -0.00586 Loss: 0.538 Simulating with theta = -0.788 zeta = -0.0229 chi = 0.00277 gamma = -0.0339 phi = 0.0725 Loss: 0.529 Simulating with theta = -0.788 zeta = -0.0174 chi = 0.0585 gamma = -0.0172 phi = 0.0368 Loss: 0.537 Simulating with theta = -0.779 zeta = -0.00467 chi = -0.013 gamma = -0.0252 phi = 0.0548 Loss: 0.528 Simulating with theta = -0.8 zeta = 0.0227 chi = -2.42e-05 gamma = -0.0186 phi = 0.0429 Loss: 0.53 Simulating with theta = -0.783 zeta = 6.74e-05 chi = -0.00755 gamma = -0.0309 phi = 0.0136 Loss: 0.533 Simulating with theta = -0.788 zeta = 0.000774 chi = 0.00841 gamma = -0.0191 phi = 0.0576 Loss: 0.529 Simulating with theta = -0.798 zeta = -0.0092 chi = -0.0265 gamma = -0.00123 phi = 0.0467 Loss: 0.536 Simulating with theta = -0.783 zeta = 0.00337 chi = 0.0127 gamma = -0.0284 phi = 0.045 Loss: 0.528 Simulating with theta = -0.79 zeta = -0.00029 chi = 0.00432 gamma = -0.0501 phi = 0.109 Loss: 0.527 Simulating with theta = -0.792 zeta = -0.000435 chi = 0.00648 gamma = -0.0752 phi = 0.164 Loss: 0.531 Simulating with theta = -0.771 zeta = -0.0322 chi = 0.00608 gamma = -0.0441 phi = 0.0927 Loss: 0.532 Simulating with theta = -0.793 zeta = 0.00897 chi = 0.0015 gamma = -0.025 phi = 0.0553 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0261 chi = 0.00278 gamma = -0.0252 phi = 0.0561 Loss: 0.529 Simulating with theta = -0.785 zeta = 0.0139 chi = 0.00278 gamma = -0.0274 phi = 0.0602 Loss: 0.528 Simulating with theta = -0.784 zeta = 0.00773 chi = -0.00511 gamma = -0.0433 phi = 0.0722 Loss: 0.527 Simulating with theta = -0.782 zeta = 0.0112 chi = -0.0119 gamma = -0.0554 phi = 0.0795 Loss: 0.528 Simulating with theta = -0.789 zeta = 0.00688 chi = -0.0165 gamma = -0.04 phi = 0.0957 Loss: 0.529 Simulating with theta = -0.785 zeta = 0.00425 chi = 0.00539 gamma = -0.0313 phi = 0.0576 Loss: 0.527 Simulating with theta = -0.796 zeta = 0.0185 chi = 0.0166 gamma = -0.0457 phi = 0.087 Loss: 0.528 Simulating with theta = -0.792 zeta = 0.0127 chi = 0.00918 gamma = -0.0405 phi = 0.079 Loss: 0.527 Simulating with theta = -0.792 zeta = -0.000541 chi = 0.00333 gamma = -0.0487 phi = 0.089 Loss: 0.526 Simulating with theta = -0.795 zeta = -0.00775 chi = 0.00361 gamma = -0.0594 phi = 0.103 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.000569 chi = 0.00534 gamma = -0.0606 phi = 0.107 Loss: 0.526 Simulating with theta = -0.792 zeta = 0.00382 chi = 0.00144 gamma = -0.066 phi = 0.125 Loss: 0.527 Simulating with theta = -0.788 zeta = 0.01 chi = 0.00136 gamma = -0.0536 phi = 0.08 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.00836 chi = 0.0042 gamma = -0.0327 phi = 0.046 Loss: 0.528 Simulating with theta = -0.79 zeta = 0.00496 chi = 0.00213 gamma = -0.0577 phi = 0.105 Loss: 0.526 Simulating with theta = -0.783 zeta = -0.00361 chi = -0.00636 gamma = -0.065 phi = 0.103 Loss: 0.528 Simulating with theta = -0.789 zeta = 0.00862 chi = 0.0053 gamma = -0.0467 phi = 0.0849 Loss: 0.526 Simulating with theta = -0.788 zeta = -0.00147 chi = 0.00304 gamma = -0.0492 phi = 0.104 Loss: 0.527 Simulating with theta = -0.788 zeta = 0.0014 chi = 0.00262 gamma = -0.0503 phi = 0.0976 Loss: 0.526 Simulating with theta = -0.793 zeta = -0.00173 chi = 0.0126 gamma = -0.0623 phi = 0.122 Loss: 0.527 Simulating with theta = -0.786 zeta = 0.00537 chi = -0.000684 gamma = -0.0481 phi = 0.0845 Loss: 0.526 Simulating with theta = -0.794 zeta = 0.00735 chi = -0.000265 gamma = -0.04 phi = 0.0771 Loss: 0.527 Simulating with theta = -0.787 zeta = 0.00226 chi = 0.00394 gamma = -0.0555 phi = 0.0999 Loss: 0.526 Simulating with theta = -0.784 zeta = 0.00958 chi = 0.00199 gamma = -0.0546 phi = 0.0998 Loss: 0.526 Simulating with theta = -0.79 zeta = 0.00199 chi = 0.003 gamma = -0.0502 phi = 0.0917 Loss: 0.526 Simulating with theta = -0.787 zeta = -0.00223 chi = -0.000896 gamma = -0.058 phi = 0.107 Loss: 0.526 Simulating with theta = -0.789 zeta = 0.00591 chi = 0.00375 gamma = -0.0495 phi = 0.0903 Loss: 0.526 Simulating with theta = -0.789 zeta = 0.00679 chi = 0.00223 gamma = -0.054 phi = 0.091 Loss: 0.526 Simulating with theta = -0.791 zeta = 0.0034 chi = 0.0067 gamma = -0.0587 phi = 0.107 Loss: 0.526 Simulating with theta = -0.788 zeta = 0.00487 chi = 0.00116 gamma = -0.0507 phi = 0.0901 Loss: 0.526 Simulating with theta = -0.787 zeta = 0.00378 chi = 0.0035 gamma = -0.0463 phi = 0.0799 Loss: 0.526 Simulating with theta = -0.789 zeta = 0.00466 chi = 0.00247 gamma = -0.0548 phi = 0.0989 Loss: 0.526 Simulating with theta = -0.788 zeta = 0.00109 chi = 0.0035 gamma = -0.0502 phi = 0.0973 Loss: 0.526 Simulating with theta = -0.788 zeta = 0.00537 chi = 0.00255 gamma = -0.0531 phi = 0.0926 Loss: 0.526 Simulating with theta = -0.786 zeta = 0.00724 chi = 0.00255 gamma = -0.0553 phi = 0.097 Loss: 0.526 Simulating with theta = -0.787 zeta = 0.00593 chi = 0.00266 gamma = -0.054 phi = 0.0957 Loss: 0.526 Simulating with theta = -0.79 zeta = 0.00843 chi = 0.0011 gamma = -0.0494 phi = 0.0872 Loss: 0.526 Simulating with theta = -0.787 zeta = 0.00381 chi = 0.00323 gamma = -0.0539 phi = 0.0967 Loss: 0.526 Simulating with theta = -0.787 zeta = 0.00395 chi = 0.00108 gamma = -0.0571 phi = 0.0993 Loss: 0.526 Simulating with theta = -0.788 zeta = 0.00542 chi = 0.00308 gamma = -0.0514 phi = 0.0926 Loss: 0.526 Simulating with theta = -0.789 zeta = 0.0052 chi = 0.00444 gamma = -0.0562 phi = 0.101 Loss: 0.526 Simulating with theta = -0.788 zeta = 0.00512 chi = 0.00362 gamma = -0.0548 phi = 0.0979 Loss: 0.526 Simulating with theta = -0.789 zeta = 0.00382 chi = 0.00332 gamma = -0.0532 phi = 0.0958 Loss: 0.526 Simulating with theta = -0.789 zeta = 0.00376 chi = 0.00374 gamma = -0.0542 phi = 0.1 Loss: 0.526 Simulating with theta = -0.789 zeta = 0.00497 chi = 0.00285 gamma = -0.0534 phi = 0.0945 Loss: 0.526
characterization_result.final_params
{(cirq.GridQubit(4, 4), cirq.GridQubit(4, 5)): {'theta': -0.7885023518989509, 'zeta': 0.004965639473578381, 'chi': 0.002846967041608324, 'gamma': -0.05337043633170291, 'phi': 0.09449869704440125} }
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()