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.80it/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 0x7f51c0ffe2f0>
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.586 Simulating with theta = -0.785 zeta = 0.1 chi = 0 gamma = 0 phi = 0 Loss: 0.551 Simulating with theta = -0.785 zeta = 0 chi = 0.1 gamma = 0 phi = 0 Loss: 0.572 Simulating with theta = -0.785 zeta = 0 chi = 0 gamma = 0.1 phi = 0 Loss: 0.584 Simulating with theta = -0.785 zeta = 0 chi = 0 gamma = 0 phi = 0.1 Loss: 0.559 Simulating with theta = -0.885 zeta = 0.04 chi = 0.04 gamma = 0.04 phi = 0.04 Loss: 0.626 Simulating with theta = -0.735 zeta = 0.01 chi = 0.01 gamma = 0.01 phi = 0.01 Loss: 0.546 Simulating with theta = -0.765 zeta = 0.044 chi = 0.044 gamma = -0.096 phi = 0.044 Loss: 0.567 Simulating with theta = -0.757 zeta = 0.0616 chi = -0.0784 gamma = -0.0344 phi = 0.0616 Loss: 0.568 Simulating with theta = -0.764 zeta = 0.0462 chi = -0.0338 gamma = -0.0258 phi = 0.0462 Loss: 0.541 Simulating with theta = -0.777 zeta = 0.0185 chi = -0.0535 gamma = 0.0897 phi = 0.0185 Loss: 0.594 Simulating with theta = -0.768 zeta = 0.0376 chi = 0.0196 gamma = -0.0496 phi = 0.0376 Loss: 0.543 Simulating with theta = -0.75 zeta = 0.0775 chi = -0.00167 gamma = -0.0262 phi = -0.0625 Loss: 0.554 Simulating with theta = -0.759 zeta = 0.0581 chi = -0.00125 gamma = -0.0196 phi = -0.0219 Loss: 0.538 Simulating with theta = -0.74 zeta = -0.0392 chi = -0.00217 gamma = -0.034 phi = 0.0288 Loss: 0.553 Simulating with theta = -0.774 zeta = 0.0652 chi = -0.000543 gamma = -0.0085 phi = 0.0072 Loss: 0.535 Simulating with theta = -0.805 zeta = 0.0729 chi = -0.0164 gamma = -0.0514 phi = 0.0177 Loss: 0.553 Simulating with theta = -0.753 zeta = 0.0257 chi = 0.0034 gamma = -0.00535 phi = 0.0119 Loss: 0.535 Simulating with theta = -0.766 zeta = 0.0405 chi = -0.0325 gamma = 0.0259 phi = -0.0202 Loss: 0.537 Simulating with theta = -0.77 zeta = 0.0296 chi = 0.0214 gamma = 0.0228 phi = -0.0554 Loss: 0.53 Simulating with theta = -0.773 zeta = 0.0213 chi = 0.0491 gamma = 0.047 phi = -0.106 Loss: 0.54 Simulating with theta = -0.78 zeta = 0.00626 chi = -0.00202 gamma = 0.0335 phi = -0.000752 Loss: 0.537 Simulating with theta = -0.775 zeta = 0.0192 chi = -0.00183 gamma = 0.0202 phi = -0.00603 Loss: 0.53 Simulating with theta = -0.777 zeta = 0.0154 chi = 0.0415 gamma = -0.0142 phi = 0.00331 Loss: 0.538 Simulating with theta = -0.769 zeta = 0.0342 chi = -0.014 gamma = 0.0159 phi = -0.0143 Loss: 0.531 Simulating with theta = -0.767 zeta = -0.0217 chi = 0.00415 gamma = 0.0299 phi = -0.0327 Loss: 0.535 Simulating with theta = -0.794 zeta = -0.00116 chi = 0.0005 gamma = 0.0409 phi = -0.0553 Loss: 0.53 Simulating with theta = -0.79 zeta = 0.0544 chi = -0.0017 gamma = 0.00998 phi = -0.0197 Loss: 0.532 Simulating with theta = -0.785 zeta = 0.0354 chi = -0.000241 gamma = 0.015 phi = -0.023 Loss: 0.528 Simulating with theta = -0.795 zeta = -0.000979 chi = 0.022 gamma = 0.0237 phi = -0.0415 Loss: 0.529 Simulating with theta = -0.782 zeta = 0.0328 chi = 0.0167 gamma = 0.049 phi = -0.0725 Loss: 0.528 Simulating with theta = -0.78 zeta = 0.0493 chi = 0.0251 gamma = 0.0735 phi = -0.109 Loss: 0.535 Simulating with theta = -0.795 zeta = 0.0191 chi = 0.026 gamma = 0.0403 phi = -0.093 Loss: 0.535 Simulating with theta = -0.78 zeta = 0.0192 chi = 0.00512 gamma = 0.0252 phi = -0.0278 Loss: 0.528 Simulating with theta = -0.771 zeta = 0.0476 chi = 0.0255 gamma = 0.0134 phi = -0.0327 Loss: 0.532 Simulating with theta = -0.788 zeta = 0.011 chi = 0.00675 gamma = 0.034 phi = -0.0497 Loss: 0.527 Simulating with theta = -0.802 zeta = 0.00938 chi = -0.00132 gamma = 0.036 phi = -0.0304 Loss: 0.532 Simulating with theta = -0.778 zeta = 0.0246 chi = 0.0158 gamma = 0.0261 phi = -0.0491 Loss: 0.528 Simulating with theta = -0.77 zeta = 0.0502 chi = -0.00431 gamma = 0.036 phi = -0.0473 Loss: 0.531 Simulating with theta = -0.789 zeta = 0.0118 chi = 0.0154 gamma = 0.0268 phi = -0.043 Loss: 0.527 Simulating with theta = -0.782 zeta = 0.00436 chi = 0.0241 gamma = 0.0495 phi = -0.0739 Loss: 0.528 Simulating with theta = -0.785 zeta = -0.00447 chi = 0.0101 gamma = 0.0156 phi = -0.0249 Loss: 0.529 Simulating with theta = -0.783 zeta = 0.0235 chi = 0.0151 gamma = 0.0407 phi = -0.0606 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0317 chi = -0.000901 gamma = 0.0116 phi = -0.0182 Loss: 0.528 Simulating with theta = -0.783 zeta = 0.0112 chi = 0.0179 gamma = 0.04 phi = -0.0599 Loss: 0.527 Simulating with theta = -0.791 zeta = 0.00614 chi = 0.00833 gamma = 0.0406 phi = -0.0472 Loss: 0.528 Simulating with theta = -0.781 zeta = 0.02 chi = 0.0139 gamma = 0.0297 phi = -0.0487 Loss: 0.527 Simulating with theta = -0.79 zeta = 0.0118 chi = 0.0225 gamma = 0.0432 phi = -0.077 Loss: 0.529 Simulating with theta = -0.782 zeta = 0.0173 chi = 0.00946 gamma = 0.0297 phi = -0.0401 Loss: 0.527 Simulating with theta = -0.779 zeta = 0.0225 chi = 0.0219 gamma = 0.0328 phi = -0.0512 Loss: 0.528 Simulating with theta = -0.786 zeta = 0.0139 chi = 0.0105 gamma = 0.0337 phi = -0.0501 Loss: 0.527 Simulating with theta = -0.777 zeta = 0.0225 chi = 0.0114 gamma = 0.0427 phi = -0.0608 Loss: 0.528 Simulating with theta = -0.786 zeta = 0.0145 chi = 0.0144 gamma = 0.0308 phi = -0.0474 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.00723 chi = 0.0114 gamma = 0.0249 phi = -0.0379 Loss: 0.527 Simulating with theta = -0.783 zeta = 0.0194 chi = 0.0142 gamma = 0.0367 phi = -0.0549 Loss: 0.527 Simulating with theta = -0.787 zeta = 0.0106 chi = 0.0127 gamma = 0.0387 phi = -0.0523 Loss: 0.527 Simulating with theta = -0.787 zeta = 0.0191 chi = 0.00661 gamma = 0.0278 phi = -0.038 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0132 chi = 0.0151 gamma = 0.037 phi = -0.0544 Loss: 0.527 Simulating with theta = -0.783 zeta = 0.0161 chi = 0.0157 gamma = 0.0355 phi = -0.0496 Loss: 0.527 Simulating with theta = -0.782 zeta = 0.0172 chi = 0.0183 gamma = 0.0363 phi = -0.0494 Loss: 0.527 Simulating with theta = -0.781 zeta = 0.0216 chi = 0.0149 gamma = 0.0292 phi = -0.0463 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0134 chi = 0.0132 gamma = 0.0363 phi = -0.0508 Loss: 0.527 Simulating with theta = -0.781 zeta = 0.0173 chi = 0.0127 gamma = 0.0393 phi = -0.0525 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0152 chi = 0.014 gamma = 0.0329 phi = -0.0487 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0106 chi = 0.0128 gamma = 0.0318 phi = -0.0425 Loss: 0.527 Simulating with theta = -0.786 zeta = 0.0101 chi = 0.0189 gamma = 0.0396 phi = -0.0584 Loss: 0.527 Simulating with theta = -0.783 zeta = 0.0155 chi = 0.0118 gamma = 0.0322 phi = -0.0446 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0152 chi = 0.012 gamma = 0.0305 phi = -0.0401 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0162 chi = 0.0104 gamma = 0.0273 phi = -0.0329 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0195 chi = 0.0139 gamma = 0.0351 phi = -0.051 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0129 chi = 0.0131 gamma = 0.0326 phi = -0.0446 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.014 chi = 0.0124 gamma = 0.0339 phi = -0.0432 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0143 chi = 0.0128 gamma = 0.0337 phi = -0.0446 Loss: 0.527 Simulating with theta = -0.782 zeta = 0.0162 chi = 0.0129 gamma = 0.0295 phi = -0.0386 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0144 chi = 0.0148 gamma = 0.0325 phi = -0.0424 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0138 chi = 0.0163 gamma = 0.0327 phi = -0.0412 Loss: 0.527 Simulating with theta = -0.786 zeta = 0.0127 chi = 0.015 gamma = 0.0365 phi = -0.0494 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.016 chi = 0.0156 gamma = 0.0349 phi = -0.0453 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0152 chi = 0.0171 gamma = 0.0343 phi = -0.0457 Loss: 0.527 Simulating with theta = -0.786 zeta = 0.0129 chi = 0.0146 gamma = 0.0321 phi = -0.0391 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.013 chi = 0.0195 gamma = 0.0376 phi = -0.0482 Loss: 0.527 Simulating with theta = -0.785 zeta = 0.0136 chi = 0.0176 gamma = 0.0359 phi = -0.0462 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0159 chi = 0.0175 gamma = 0.0314 phi = -0.0375 Loss: 0.527 Simulating with theta = -0.784 zeta = 0.0151 chi = 0.0168 gamma = 0.0327 phi = -0.0405 Loss: 0.527
characterization_result.final_params
{(cirq.GridQubit(4, 4), cirq.GridQubit(4, 5)): {'theta': -0.78412228525473, 'zeta': 0.01376602370723885, 'chi': 0.01626017388052132, 'gamma': 0.032655490438939935, 'phi': -0.0412064179844695} }
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()