Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix probems for fitting #55

Merged
merged 7 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/qcvv/calibrations/qubit_spectroscopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def qubit_spectroscopy(
if count % points == 0 and count > 0:
yield prec_data
yield lorentzian_fit(
prec_data,
data + prec_data,
x="frequency[GHz]",
y="MSR[uV]",
qubit=qubit,
Expand Down
2 changes: 1 addition & 1 deletion src/qcvv/calibrations/resonator_spectroscopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def resonator_spectroscopy(
if count % points == 0 and count > 0:
yield precision_sweep__data
yield lorentzian_fit(
precision_sweep__data,
fast_sweep_data + precision_sweep__data,
x="frequency[GHz]",
y="MSR[uV]",
qubit=qubit,
Expand Down
8 changes: 4 additions & 4 deletions src/qcvv/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def __init__(self, name=None):

self.df = pd.DataFrame()

# def __add__(self, data):
# self.df = pd.concat([self.df, data.df)
# return self
def __add__(self, data):
self.df = pd.concat([self.df, data.df], ignore_index=True)
return self

@abstractmethod
def add(self, data):
Expand Down Expand Up @@ -165,7 +165,7 @@ def __init__(self, name=None, quantities=None):
if quantities is not None:
self.quantities = quantities
for name in quantities:
self.df.insert(0, name, pd.Series())
self.df.insert(0, name, pd.Series(dtype=object))

def add(self, data):
"""Add a row to dataset.
Expand Down
38 changes: 23 additions & 15 deletions src/qcvv/fitting/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@
"""Routine-specific method for post-processing data acquired."""
import lmfit
import numpy as np
import yaml

from qcvv.config import log
from qcvv.data import Data
from qcvv.fitting.utils import lorenzian, parse


def lorentzian_fit(data, x, y, qubit, nqubits, labels):
"""Fitting routine for resonator spectroscopy"""

data_fit = Data(
name=f"fit_q{qubit}",
quantities=[
"fit_amplitude",
"fit_center",
"fit_sigma",
"fit_offset",
labels[1],
labels[0],
],
)

frequencies = data.get_values(*parse(x))
voltages = data.get_values(*parse(y))

# Create a lmfit model for fitting equation defined in resonator_peak
model_Q = lmfit.Model(lorenzian)

# Guess parameters for Lorentzian max or min
if nqubits == 1 and labels[0] == "resonator_freq":
if (nqubits == 1 and labels[0] == "resonator_freq") or (
nqubits != 1 and labels[0] == "qubit_freq"
):
guess_center = frequencies[
np.argmax(voltages)
] # Argmax = Returns the indices of the maximum values along an axis.
Expand Down Expand Up @@ -46,7 +60,13 @@ def lorentzian_fit(data, x, y, qubit, nqubits, labels):
guess_parameters = model_Q.make_params()

# fit the model with the data and guessed parameters
fit_res = model_Q.fit(data=voltages, frequency=frequencies, params=guess_parameters)
try:
fit_res = model_Q.fit(
data=voltages, frequency=frequencies, params=guess_parameters
)
except:
log.warning("The fitting was not successful")
return data_fit

# get the values for postprocessing and for legend.
f0 = fit_res.best_values["center"]
Expand All @@ -57,18 +77,6 @@ def lorentzian_fit(data, x, y, qubit, nqubits, labels):
+ fit_res.best_values["offset"]
)

data_fit = Data(
name=f"fit_q{qubit}",
quantities=[
"fit_amplitude",
"fit_center",
"fit_sigma",
"fit_offset",
labels[1],
labels[0],
],
)

freq = f0 * 1e6

data_fit.add(
Expand Down
13 changes: 11 additions & 2 deletions src/qcvv/plots/scatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ def frequency_msr_phase__fast_precision(folder, routine, qubit, format):
try:
data_fit = Data.load_data(folder, routine, format, f"fit_q{qubit}")
except:
data_fit = Data()
data_fit = Data(
quantities=[
"fit_amplitude",
"fit_center",
"fit_sigma",
"fit_offset",
"label1",
"label2",
]
)

fig = make_subplots(
rows=1,
Expand Down Expand Up @@ -70,7 +79,7 @@ def frequency_msr_phase__fast_precision(folder, routine, qubit, format):
row=1,
col=2,
)
if len(data_fast) > 0:
if len(data_fast) > 0 and len(data_fit) > 0:
freqrange = np.linspace(
min(data_fast.get_values("frequency", "GHz")),
max(data_fast.get_values("frequency", "GHz")),
Expand Down