![]() |
![]() |
![]() |
![]() |
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.50it/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 0x7f6621a3dc30>
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.533 Simulating with theta = -0.685 zeta = 0 chi = 0 gamma = 0 phi = 0 Loss: 0.588 Simulating with theta = -0.785 zeta = 0.1 chi = 0 gamma = 0 phi = 0 Loss: 0.55 Simulating with theta = -0.785 zeta = 0 chi = 0.1 gamma = 0 phi = 0 Loss: 0.565 Simulating with theta = -0.785 zeta = 0 chi = 0 gamma = 0.1 phi = 0 Loss: 0.605 Simulating with theta = -0.785 zeta = 0 chi = 0 gamma = 0 phi = 0.1 Loss: 0.57 Simulating with theta = -0.745 zeta = 0.04 chi = 0.04 gamma = -0.1 phi = 0.04 Loss: 0.581 Simulating with theta = -0.869 zeta = 0.056 chi = 0.056 gamma = -0.04 phi = 0.056 Loss: 0.584 Simulating with theta = -0.823 zeta = 0.042 chi = 0.042 gamma = -0.03 phi = 0.042 Loss: 0.551 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.548 Simulating with theta = -0.794 zeta = 0.0705 chi = 0.0705 gamma = -0.0332 phi = -0.0695 Loss: 0.591 Simulating with theta = -0.788 zeta = 0.0176 chi = 0.0176 gamma = -0.0083 phi = 0.0576 Loss: 0.539 Simulating with theta = -0.795 zeta = 0.0775 chi = -0.0625 gamma = -0.0365 phi = 0.0535 Loss: 0.557 Simulating with theta = -0.793 zeta = 0.0581 chi = -0.0219 gamma = -0.0274 phi = 0.0401 Loss: 0.54 Simulating with theta = -0.745 zeta = 0.042 chi = -0.03 gamma = -0.00548 phi = 0.0108 Loss: 0.545 Simulating with theta = -0.766 zeta = -0.0392 chi = -1.9e-05 gamma = -0.0377 phi = 0.0571 Loss: 0.542 Simulating with theta = -0.781 zeta = -0.00279 chi = -0.0479 gamma = 0.0215 phi = 0.0321 Loss: 0.561 Simulating with theta = -0.772 zeta = 0.025 chi = 0.0137 gamma = -0.0344 phi = 0.0337 Loss: 0.537 Simulating with theta = -0.817 zeta = -0.0174 chi = 0.0338 gamma = -0.0376 phi = 0.0646 Loss: 0.55 Simulating with theta = -0.763 zeta = 0.0271 chi = -0.0141 gamma = -0.0135 phi = 0.0242 Loss: 0.535 Simulating with theta = -0.794 zeta = 0.0904 chi = -0.00183 gamma = 0.00423 phi = 0.00517 Loss: 0.548 Simulating with theta = -0.773 zeta = -0.00682 chi = -0.000472 gamma = -0.0272 phi = 0.0441 Loss: 0.535 Simulating with theta = -0.76 zeta = -0.033 chi = 0.0286 gamma = -0.00596 phi = 0.0237 Loss: 0.542 Simulating with theta = -0.784 zeta = 0.0354 chi = -0.00925 gamma = -0.022 phi = 0.036 Loss: 0.534 Simulating with theta = -0.764 zeta = 0.0146 chi = -0.0217 gamma = -0.0305 phi = -0.00239 Loss: 0.547 Simulating with theta = -0.782 zeta = 0.0169 chi = 0.0078 gamma = -0.0139 phi = 0.0426 Loss: 0.534 Simulating with theta = -0.783 zeta = 0.00407 chi = -0.0201 gamma = 0.00374 phi = 0.0251 Loss: 0.538 Simulating with theta = -0.775 zeta = 0.0197 chi = 0.00524 gamma = -0.0249 phi = 0.0315 Loss: 0.534 Simulating with theta = -0.782 zeta = 0.0465 chi = -0.00364 gamma = -0.00251 phi = 0.00966 Loss: 0.534 Simulating with theta = -0.801 zeta = 0.0202 chi = 0.0141 gamma = -0.0118 phi = 0.0237 Loss: 0.537 Simulating with theta = -0.772 zeta = 0.0254 chi = -0.00702 gamma = -0.0131 phi = 0.0241 Loss: 0.533 Simulating with theta = -0.777 zeta = -0.00751 chi = 0.00235 gamma = -0.027 phi = 0.0441 Loss: 0.536 Simulating with theta = -0.781 zeta = 0.033 chi = -0.00214 gamma = -0.00864 phi = 0.0183 Loss: 0.533 Simulating with theta = -0.774 zeta = 0.00263 chi = 0.0108 gamma = -0.00214 phi = 0.0106 Loss: 0.533 Simulating with theta = -0.768 zeta = -0.0137 chi = 0.0208 gamma = 0.00781 phi = -0.00217 Loss: 0.536 Simulating with theta = -0.783 zeta = 0.0114 chi = -0.00146 gamma = 0.00976 phi = 0.00669 Loss: 0.533 Simulating with theta = -0.776 zeta = 0.0121 chi = -0.00773 gamma = 0.00822 phi = -0.0188 Loss: 0.534 Simulating with theta = -0.778 zeta = 0.0133 chi = -0.00385 gamma = 0.0027 phi = -0.00342 Loss: 0.533 Simulating with theta = -0.773 zeta = 0.0183 chi = 0.00058 gamma = -0.0182 phi = 0.0131 Loss: 0.534 Simulating with theta = -0.78 zeta = 0.0131 chi = -0.000952 gamma = 0.00277 phi = 0.00829 Loss: 0.532 Simulating with theta = -0.787 zeta = -0.000595 chi = 0.00856 gamma = 0.011 phi = -0.0106 Loss: 0.534 Simulating with theta = -0.776 zeta = 0.0189 chi = -0.00312 gamma = -0.00707 phi = 0.0154 Loss: 0.532 Simulating with theta = -0.77 zeta = 0.0324 chi = 0.000295 gamma = -0.00495 phi = 0.0196 Loss: 0.533 Simulating with theta = -0.782 zeta = 0.0081 chi = 7.38e-05 gamma = -0.00124 phi = 0.00491 Loss: 0.532 Simulating with theta = -0.775 zeta = -0.0105 chi = 0.00333 gamma = 0.00665 phi = -0.00395 Loss: 0.533 Simulating with theta = -0.776 zeta = 0.00034 chi = 0.00196 gamma = 0.00282 phi = 0.0016 Loss: 0.532 Simulating with theta = -0.783 zeta = 0.0189 chi = -0.0132 gamma = 0.00213 phi = 0.000156 Loss: 0.533 Simulating with theta = -0.776 zeta = 0.0067 chi = 0.00481 gamma = -0.00107 phi = 0.00797 Loss: 0.532 Simulating with theta = -0.778 zeta = 0.00558 chi = 0.00495 gamma = -0.00422 phi = 0.0187 Loss: 0.533 Simulating with theta = -0.778 zeta = 0.0114 chi = -0.00165 gamma = 0.000971 phi = 0.00211 Loss: 0.532 Simulating with theta = -0.775 zeta = 0.00502 chi = 0.00178 gamma = -0.005 phi = 0.00451 Loss: 0.533 Simulating with theta = -0.779 zeta = 0.0111 chi = -0.000268 gamma = 0.000824 phi = 0.00735 Loss: 0.532 Simulating with theta = -0.772 zeta = 0.0113 chi = 0.000619 gamma = -0.000171 phi = 0.00887 Loss: 0.532 Simulating with theta = -0.777 zeta = -0.00259 chi = 0.00531 gamma = 0.00842 phi = -0.00427 Loss: 0.533 Simulating with theta = -0.776 zeta = 0.0135 chi = -0.00101 gamma = -0.0032 phi = 0.0105 Loss: 0.532 Simulating with theta = -0.776 zeta = 0.0213 chi = -0.000957 gamma = -0.00388 phi = 0.0131 Loss: 0.532 Simulating with theta = -0.782 zeta = 0.0143 chi = -0.000249 gamma = -0.00237 phi = 0.00755 Loss: 0.532 Simulating with theta = -0.775 zeta = 0.012 chi = 0.000402 gamma = -0.000721 phi = 0.00854 Loss: 0.532 Simulating with theta = -0.777 zeta = 0.000643 chi = 0.00187 gamma = 0.0026 phi = 0.00147 Loss: 0.532 Simulating with theta = -0.777 zeta = 0.0161 chi = -0.00025 gamma = -0.00226 phi = 0.0102 Loss: 0.532 Simulating with theta = -0.778 zeta = 0.019 chi = -0.00592 gamma = -0.000682 phi = 0.00751 Loss: 0.532 Simulating with theta = -0.776 zeta = 0.00976 chi = 0.00213 gamma = -0.000975 phi = 0.00785 Loss: 0.532 Simulating with theta = -0.775 zeta = 0.0137 chi = 0.00205 gamma = -0.0035 phi = 0.0157 Loss: 0.532 Simulating with theta = -0.776 zeta = 0.0131 chi = 0.00112 gamma = -0.00239 phi = 0.0123 Loss: 0.532 Simulating with theta = -0.773 zeta = 0.0147 chi = 0.00122 gamma = -0.00464 phi = 0.0124 Loss: 0.532 Simulating with theta = -0.777 zeta = 0.012 chi = 0.000105 gamma = -0.000542 phi = 0.00861 Loss: 0.532
characterization_result.final_params
{(cirq.GridQubit(4, 4), cirq.GridQubit(4, 5)): {'theta': -0.7774323324979654, 'zeta': 0.01200888296804746, 'chi': 0.00010475490628246274, 'gamma': -0.0005421275850693121, 'phi': 0.008611520855789399} }
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()