#
# Copyright 2021 Quantum Benchmark Inc.
#
"""
Extended Randomized Benchmarking (XRB)
======================================
"""
#%%
# This example illustrates how to generate extended randomized benchmarking
# (:tqdoc:`XRB`\) circuits and use them to estimate the probability of a stochastic
# error acting on the specified system(s) during a random gate. While this example uses
# a :doc:`simulator<../../guides/run/simulator>` to execute the circuits, the same
# procedure can be followed for hardware applications.
#%%
# Isolated XRB
# ------------
#
# This section illustrates how to generate :tqdoc:`XRB` circuits to characterize a pair
# of qubits in isolation. Here, we are performing two-qubit :tqdoc:`XRB` which learns
# the stochastic infidelity over the two-qubit Clifford gateset.
#%%
import trueq as tq
# generate XRB circuits to characterize a pair of qubits [0, 1]
# with 9 * 30 random circuits for each circuit depth [2, 4, 16]
circuits = tq.make_xrb([[0, 1]], [2, 4, 16], 30)
# initialize a noisy simulator with stochastic Pauli and overrotation
sim = tq.Simulator().add_stochastic_pauli(px=0.02).add_overrotation(0.04)
# run the circuits on the simulator to populate their results
sim.run(circuits, n_shots=1000)
# plot the exponential decay of the purities
circuits.plot.raw()
#%%
# print the fit summary
circuits.fit()
#%%
# Simultaneous XRB
# ----------------
#
# This section demonstrates how to generate :tqdoc:`XRB` circuits that characterize the
# amount of stochastic noise while gates are applied simultaneously on a device.
#%%
# generate XRB circuits to simultaneously characterize a single qubit [0],
# a pair of qubits [1, 2], and another single qubit [3] with 9 * 30 random circuits
# for each circuit depth [2, 4, 16]
circuits = tq.make_xrb([[0], [1, 2], [3]], [2, 4, 16], 30)
# initialize a noisy simulator with stochastic Pauli and overrotation
sim = tq.Simulator().add_stochastic_pauli(px=0.02).add_overrotation(0.04)
# run the circuits on the simulator to populate their results
sim.run(circuits, n_shots=1000)
# plot the exponential decay of the purities
circuits.plot.raw()
#%%
# print the fit summary
circuits.fit()