Skip to content

Commit

Permalink
Merge pull request #588 from qiboteam/fix_hpars
Browse files Browse the repository at this point in the history
Fix hpars
  • Loading branch information
Edoardo-Pedicillo authored Oct 27, 2023
2 parents 6fa349d + aaf4360 commit 7098692
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 38 deletions.
9 changes: 3 additions & 6 deletions src/qibocal/fitting/classifier/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,9 @@ def train_qubit(
classifier = Classifier(mod, qubit_dir)
classifier.savedir.mkdir(exist_ok=True)
logging.info(f"Training quibt with classifier: {pretty_name(classifier.name)}")
if classifier.name not in cls_data.classifiers_hpars[qubit]:
hyperpars = classifier.hyperopt(
x_train, y_train.astype(np.int64), classifier.savedir
)
else:
hyperpars = cls_data.classifiers_hpars[qubit][classifier.name]
hyperpars = classifier.hyperopt(
x_train, y_train.astype(np.int64), classifier.savedir
)
hpars_list.append(hyperpars)
classifier.dump_hyper(hyperpars)
model = classifier.create_model(hyperpars)
Expand Down
51 changes: 23 additions & 28 deletions src/qibocal/protocols/characterization/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ class SingleShotClassificationParameters(Parameters):
class SingleShotClassificationData(Data):
nshots: int
"""Number of shots."""
classifiers_hpars: dict[QubitId, dict]
"""Models' hyperparameters"""
savedir: str
"""Dumping folder of the classification results"""
data: dict[QubitId, npt.NDArray] = field(default_factory=dict)
Expand Down Expand Up @@ -173,7 +171,6 @@ def _acquisition(

RX_pulses = {}
ro_pulses = {}
hpars = {}
for qubit in qubits:
RX_pulses[qubit] = platform.create_RX_pulse(qubit, start=0)
ro_pulses[qubit] = platform.create_qubit_readout_pulse(
Expand All @@ -183,12 +180,10 @@ def _acquisition(
state0_sequence.add(ro_pulses[qubit])
state1_sequence.add(RX_pulses[qubit])
state1_sequence.add(ro_pulses[qubit])
hpars[qubit] = qubits[qubit].classifiers_hpars
# create a DataUnits object to store the results

data = SingleShotClassificationData(
nshots=params.nshots,
classifiers_list=params.classifiers_list,
classifiers_hpars=hpars,
savedir=params.savedir,
)

Expand Down Expand Up @@ -303,7 +298,7 @@ def _fit(data: SingleShotClassificationData) -> SingleShotClassificationResults:
def _plot(
data: SingleShotClassificationData, qubit, fit: SingleShotClassificationResults
):
fitting_report = None
fitting_report = ""
models_name = data.classifiers_list
figures = plot_results(data, qubit, 2, fit)
if fit is not None:
Expand Down Expand Up @@ -346,28 +341,28 @@ def _plot(
)
figures.append(fig_roc)

if models_name[i] == "qubit_fit":
fitting_report = table_html(
table_dict(
qubit,
[
"Average State 0",
"Average State 1",
"Rotational Angle",
"Threshold",
"Readout Fidelity",
"Assignment Fidelity",
],
[
np.round(fit.mean_gnd_states[qubit], 3),
np.round(fit.mean_exc_states[qubit], 3),
np.round(fit.rotation_angle[qubit], 3),
np.round(fit.threshold[qubit], 6),
np.round(fit.fidelity[qubit], 3),
np.round(fit.assignment_fidelity[qubit], 3),
],
)
if "qubit_fit" in models_name:
fitting_report = table_html(
table_dict(
qubit,
[
"Average State 0",
"Average State 1",
"Rotational Angle",
"Threshold",
"Readout Fidelity",
"Assignment Fidelity",
],
[
np.round(fit.mean_gnd_states[qubit], 3),
np.round(fit.mean_exc_states[qubit], 3),
np.round(fit.rotation_angle[qubit], 3),
np.round(fit.threshold[qubit], 6),
np.round(fit.fidelity[qubit], 3),
np.round(fit.assignment_fidelity[qubit], 3),
],
)
)

return figures, fitting_report

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ def _acquisition(
# taking advantage of multiplexing, apply the same set of gates to all qubits in parallel
states_sequences = [PulseSequence() for _ in range(3)]
ro_pulses = {}
hpars = {}
for qubit in qubits:
rx_pulse = platform.create_RX_pulse(qubit, start=0)
rx12_pulse = platform.create_RX12_pulse(qubit, start=rx_pulse.finish)
drive_pulses = [rx_pulse, rx12_pulse]
hpars[qubit] = qubits[qubit].classifiers_hpars
ro_pulses[qubit] = []
for i, sequence in enumerate(states_sequences):
sequence.add(*drive_pulses[:i])
Expand All @@ -88,7 +86,6 @@ def _acquisition(
data = QutritClassificationData(
nshots=params.nshots,
classifiers_list=params.classifiers_list,
classifiers_hpars=hpars,
savedir=params.savedir,
)
states_results = []
Expand Down
11 changes: 10 additions & 1 deletion src/qibocal/protocols/characterization/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,16 @@ def evaluate_grid(
return np.vstack([i_values.ravel(), q_values.ravel()]).T


def plot_results(data: Data, qubit, qubit_states, fit: Results):
def plot_results(data: Data, qubit: QubitId, qubit_states: list, fit: Results):
"""
Plots for the qubit and qutrit classification.
Args:
data (Data): acquisition data
qubit (QubitID): qubit
qubit_states (list): list of qubit states available.
fit (Results): fit results
"""
figures = []
models_name = data.classifiers_list
qubit_data = data.data[qubit]
Expand Down

0 comments on commit 7098692

Please sign in to comment.