Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Produces a plot to see how well the true gate fidelity is estimated, for different sample sizes
import progressbar
import numpy as np
import matplotlib.pyplot as plt
import qutip
def average_fidelity(U, V, sample_size):
total = 0
for idx in range(sample_size):
psi = qutip.rand_ket_haar(8)
total += np.abs((psi.dag() * U.dag() * V * psi)[0, 0])**2
return total / sample_size
def exact_average_fidelity_unitaryVSunitary(U, V):
if isinstance(U, qutip.Qobj):
U = U.full()
if isinstance(V, qutip.Qobj):
V = V.full()
D = U.shape[0]
expval = np.einsum('ij,ij,kl,kl', U.conj(), V, U, V.conj()).real
return 1 / (D + 1) * (1 + expval / D)
U = qutip.rand_unitary_haar(8)
V = qutip.rand_unitary_haar(8)
# compute average fidelities over samples of various sizes
bar = progressbar.ProgressBar()
sample_sizes = range(100, 5000, 100)
avgs = []
for sample_size in bar(sample_sizes):
avgs.append(average_fidelity(U, V, sample_size))
# compute exact average fidelity
exact_avg_fid = exact_average_fidelity_unitaryVSunitary(U, V)
# plot results
plt.plot(sample_sizes, avgs, 'o--')
plt.hlines(y=exact_avg_fid, xmin=0, xmax=sample_sizes[-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment