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

Qutrit classifier #543

Merged
merged 15 commits into from
Oct 25, 2023
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
7 changes: 3 additions & 4 deletions src/qibocal/fitting/classifier/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ def generate_models(data, qubit, test_size=0.25):
- x_test: Test inputs.
- y_test: Test outputs.
"""
data0 = data.data[qubit, 0].tolist()
data1 = data.data[qubit, 1].tolist()
qubit_data = data.data[qubit]
return train_test_split(
np.array(np.concatenate((data0, data1))),
np.array([0] * len(data0) + [1] * len(data1)),
np.array(qubit_data[["i", "q"]].tolist())[:, :],
np.array(qubit_data[["state"]].tolist())[:, 0],
test_size=test_size,
random_state=0,
shuffle=True,
Expand Down
40 changes: 40 additions & 0 deletions src/qibocal/fitting/classifier/decision_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from sklearn.model_selection import GridSearchCV, RepeatedStratifiedKFold
from sklearn.tree import DecisionTreeClassifier

from . import scikit_utils


def constructor(hyperpars):
r"""Return the model class.

Args:
hyperparams: Model hyperparameters.
"""
return DecisionTreeClassifier().set_params(**hyperpars)


def hyperopt(x_train, y_train, _path):
r"""Perform an hyperparameter optimization and return the hyperparameters.

Args:
x_train: Training inputs.
y_train: Training outputs.
_path (path): Model save path.

Returns:
Dictionary with model's hyperparameters.
"""
clf = DecisionTreeClassifier()
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
space = {}
space["criterion"] = ["gini", "entropy", "log_loss"]
space["splitter"] = ["best", "random"]
search = GridSearchCV(clf, space, scoring="accuracy", n_jobs=-1, cv=cv)
_ = search.fit(x_train, y_train)

return search.best_params_


normalize = scikit_utils.scikit_normalize
dump = scikit_utils.scikit_dump
predict_from_file = scikit_utils.scikit_predict
2 changes: 2 additions & 0 deletions src/qibocal/fitting/classifier/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"random_forest",
"rbf_svm",
"qblox_fit",
"decision_tree",
]

PRETTY_NAME = [
Expand All @@ -35,6 +36,7 @@
"Random Forest",
"RBF SVM",
"Qblox Fit",
"Decision Tree",
]


Expand Down
2 changes: 1 addition & 1 deletion src/qibocal/fitting/classifier/scikit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def scikit_normalize(constructor):

def scikit_dump(model, path: Path):
r"""Dumps scikit `model` in `path`"""
initial_type = [("float_input", FloatTensorType([1, 2]))]
initial_type = [("float_input", FloatTensorType([None, 2]))]
onx = to_onnx(model, initial_types=initial_type)
with open(path.with_suffix(".onnx"), "wb") as f:
f.write(onx.SerializeToString())
2 changes: 2 additions & 0 deletions src/qibocal/protocols/characterization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)
from .qubit_spectroscopy import qubit_spectroscopy
from .qubit_spectroscopy_ef import qubit_spectroscopy_ef
from .qutrit_classification import qutrit_classification
from .rabi.amplitude import rabi_amplitude
from .rabi.ef import rabi_amplitude_ef
from .rabi.length import rabi_length
Expand Down Expand Up @@ -92,4 +93,5 @@ class Operation(Enum):
twpa_power = twpa_power
rabi_amplitude_ef = rabi_amplitude_ef
qubit_spectroscopy_ef = qubit_spectroscopy_ef
qutrit_classification = qutrit_classification
resonator_amplitude = resonator_amplitude
Loading