#
# Copyright 2021 Keysight Technologies Inc.
#
"""
Comparing Infidelities with SRB and XRB
=======================================
"""
#%%
# This example demonstrates how to estimate the process infidelities of specified
# systems using :tqdoc:`SRB`\, and how this quantity can be divided into coherent and
# stochastic infidelity by the addition of :tqdoc:`XRB` circuits. While this example
# uses a :doc:`simulator<../../guides/run/simulator>` to execute the circuits, the same
# procedure can be followed for hardware applications.
import trueq as tq
# generate SRB circuits to simultaneously characterize a single qubit [0],
# a pair of qubits [1, 2], and another single qubit [3]
circuits = tq.make_srb([[0], [1, 2], [3]], [2, 4, 16], 30)
# generate XRB circuits using the same arguments as SRB and combine them
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)
# print the fit summary
circuits.fit()
#%%
# Notice that the :py:meth:`~trueq.CircuitCollection.fit` output contains an estimate
# of process infidelity for the specified systems from :tqdoc:`SRB`\, the stochastic
# infidelity from :tqdoc:`XRB`\, and the unitary infidelity which would be absent if the
# circuit collection did not contain both :tqdoc:`SRB` and :tqdoc:`XRB` circuits. We
# can visualize the different process infidelities as follows.
# plot a comparison of process infidelities
circuits.plot.compare_rb()