/
rbtools.py
70 lines (60 loc) · 2.12 KB
/
rbtools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import numpy as _np
from scipy.optimize import curve_fit as _curve_fit
from matplotlib import pyplot as _plt
def rb_decay(x,a,b):
"""
Return the Randomized Benchmarking (RB) decay function:
decay(x,a,b) = 1 + (2a-1) exp(-bx))/2
"""
return (1+(2*a-1)*_np.exp(-b * x))/2.
def rb_decay_rate(dataset,showPlot=False,xlim=None,ylim=None,saveFigPath=None):
"""
Compute the Randomized Benchmarking (RB) decay rate given an data set
containing counts for RB gate strings. Note: currently this function
only works for 1-qubit dataset having SPAM labels 'plus' and 'minus'.
Parameters
----------
dataset : DataSet
The RB data set.
showPlot : bool, optional
Whether to show a plot of the fit to the RB data.
xlim : (xmin,xmax), optional
Specify x-axis limits for plot
ylim : (ymin,ymax), optional
Specify y-axis limits for plot
saveFigPath : string, optional
Pathname to save a plot of the fit to the RB data.
Returns
-------
a,b : float
The best-fit decay curve parameters a and b, as defined in
the rb_decay function.
"""
RBlengths = []
RBsuccesses = []
for key in dataset.keys():
dataLine = dataset[key]
plus = dataLine['plus']
minus = dataLine['minus']
N = plus + minus
RBlengths.append(len(key))
RBsuccesses.append(1 - dataLine['plus']/float(N))
if dataLine['plus']/float(N) > 1:
print key
a,b = _curve_fit(rb_decay,RBlengths,RBsuccesses)[0]
if saveFigPath or showPlot:
newplot = _plt.figure()
newplotgca = newplot.gca()
newplotgca.plot(RBlengths,RBsuccesses,'.')
newplotgca.plot(xrange(max(RBlengths)),
rb_decay(_np.arange(max(RBlengths)),a,b),'+')
newplotgca.set_xlabel('RB sequence length (non-Clifford)')
newplotgca.set_ylabel('Success rate')
newplotgca.set_title('RB success')
if xlim:
_plt.xlim(xlim)
if ylim:
_plt.ylim(ylim)
if saveFigPath:
newplot.savefig(saveFigPath)
return a,b