Skip to content

Commit

Permalink
refactor: Reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-pasquale committed Dec 12, 2024
1 parent 6fcf9d6 commit 1a8f9f7
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 250 deletions.
88 changes: 4 additions & 84 deletions src/qibocal/protocols/coherence/cpmg.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
from dataclasses import dataclass, field
from typing import Optional
from dataclasses import dataclass

import numpy as np
import plotly.graph_objects as go
from qibolab import AcquisitionType, AveragingMode, Parameter, Sweeper

from qibocal.auto.operation import QubitId, Routine
from qibocal.calibration import CalibrationPlatform
from qibocal.result import probability

from ..utils import table_dict, table_html
from . import t1
from .spin_echo import SpinEchoParameters, SpinEchoResults, _update
from .utils import dynamical_decoupling_sequence, exp_decay, exponential_fit_probability
from .spin_echo import SpinEchoParameters, SpinEchoResults
from .utils import dynamical_decoupling_sequence, exponential_fit_probability, plot


@dataclass
Expand All @@ -27,11 +24,6 @@ class CpmgParameters(SpinEchoParameters):
class CpmgResults(SpinEchoResults):
"""SpinEcho outputs."""

chi2: Optional[dict[QubitId, tuple[float, Optional[float]]]] = field(
default_factory=dict
)
"""Chi squared estimate mean value and error."""


class CpmgData(t1.T1Data):
"""SpinEcho acquisition outputs."""
Expand Down Expand Up @@ -96,77 +88,5 @@ def _fit(data: CpmgData) -> CpmgResults:
return CpmgResults(t2Echos, fitted_parameters, pcovs, chi2)


def _plot(data: CpmgData, target: QubitId, fit: CpmgResults = None):
"""Plotting for Cpmg"""

figures = []
# iterate over multiple data folders
fitting_report = ""

qubit_data = data[target]
waits = qubit_data.wait
probs = qubit_data.prob
error_bars = qubit_data.error

fig = go.Figure(
[
go.Scatter(
x=waits,
y=probs,
opacity=1,
name="Probability of 1",
showlegend=True,
legendgroup="Probability of 1",
mode="lines",
),
go.Scatter(
x=np.concatenate((waits, waits[::-1])),
y=np.concatenate((probs + error_bars, (probs - error_bars)[::-1])),
fill="toself",
fillcolor=t1.COLORBAND,
line=dict(color=t1.COLORBAND_LINE),
showlegend=True,
name="Errors",
),
]
)

if fit is not None:
# add fitting trace
waitrange = np.linspace(
min(waits),
max(waits),
2 * len(qubit_data),
)
params = fit.fitted_parameters[target]

fig.add_trace(
go.Scatter(
x=waitrange,
y=exp_decay(waitrange, *params),
name="Fit",
line=go.scatter.Line(dash="dot"),
),
)
fitting_report = table_html(
table_dict(
target,
["T2", "chi2 reduced"],
[fit.t2_spin_echo[target], fit.chi2[target]],
display_error=True,
)
)

fig.update_layout(
showlegend=True,
xaxis_title="Time [ns]",
yaxis_title="Probability of State 1",
)

figures.append(fig)

return figures, fitting_report


cpmg = Routine(_acquisition, _fit, _plot, _update)
cpmg = Routine(_acquisition, _fit, plot)
"""Cpmg Routine object."""
78 changes: 2 additions & 76 deletions src/qibocal/protocols/coherence/spin_echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
from typing import Optional

import numpy as np
import plotly.graph_objects as go
from qibolab import AcquisitionType, AveragingMode, Parameter, Sweeper

from qibocal.auto.operation import QubitId, Routine
from qibocal.calibration import CalibrationPlatform
from qibocal.result import probability

from ..utils import table_dict, table_html
from . import t1
from .spin_echo_signal import SpinEchoSignalParameters, SpinEchoSignalResults, _update
from .utils import dynamical_decoupling_sequence, exp_decay, exponential_fit_probability
from .utils import dynamical_decoupling_sequence, exponential_fit_probability, plot


@dataclass
Expand Down Expand Up @@ -91,77 +89,5 @@ def _fit(data: SpinEchoData) -> SpinEchoResults:
return SpinEchoResults(t2Echos, fitted_parameters, pcovs, chi2)


def _plot(data: SpinEchoData, target: QubitId, fit: SpinEchoResults = None):
"""Plotting for SpinEcho"""

figures = []
# iterate over multiple data folders
fitting_report = ""

qubit_data = data[target]
waits = qubit_data.wait
probs = qubit_data.prob
error_bars = qubit_data.error

fig = go.Figure(
[
go.Scatter(
x=waits,
y=probs,
opacity=1,
name="Probability of 1",
showlegend=True,
legendgroup="Probability of 1",
mode="lines",
),
go.Scatter(
x=np.concatenate((waits, waits[::-1])),
y=np.concatenate((probs + error_bars, (probs - error_bars)[::-1])),
fill="toself",
fillcolor=t1.COLORBAND,
line=dict(color=t1.COLORBAND_LINE),
showlegend=True,
name="Errors",
),
]
)

if fit is not None:
# add fitting trace
waitrange = np.linspace(
min(waits),
max(waits),
2 * len(qubit_data),
)
params = fit.fitted_parameters[target]

fig.add_trace(
go.Scatter(
x=waitrange,
y=exp_decay(waitrange, *params),
name="Fit",
line=go.scatter.Line(dash="dot"),
),
)
fitting_report = table_html(
table_dict(
target,
["T2 Spin Echo [ns]", "chi2 reduced"],
[fit.t2_spin_echo[target], fit.chi2[target]],
display_error=True,
)
)

fig.update_layout(
showlegend=True,
xaxis_title="Time [ns]",
yaxis_title="Probability of State 1",
)

figures.append(fig)

return figures, fitting_report


spin_echo = Routine(_acquisition, _fit, _plot, _update)
spin_echo = Routine(_acquisition, _fit, plot, _update)
"""SpinEcho Routine object."""
6 changes: 3 additions & 3 deletions src/qibocal/protocols/coherence/spin_echo_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SpinEchoSignalParameters(Parameters):
class SpinEchoSignalResults(Results):
"""SpinEchoSignal outputs."""

t2_spin_echo: dict[QubitId, Union[float, list[float]]]
t2: dict[QubitId, Union[float, list[float]]]
"""T2 echo for each qubit."""
fitted_parameters: dict[QubitId, dict[str, float]]
"""Raw fitting output."""
Expand Down Expand Up @@ -160,7 +160,7 @@ def _plot(data: SpinEchoSignalData, target: QubitId, fit: SpinEchoSignalResults
table_dict(
target,
["T2 Spin Echo [ns]"],
[np.round(fit.t2_spin_echo[target])],
[np.round(fit.t2[target])],
display_error=True,
)
)
Expand All @@ -179,7 +179,7 @@ def _plot(data: SpinEchoSignalData, target: QubitId, fit: SpinEchoSignalResults
def _update(
results: SpinEchoSignalResults, platform: CalibrationPlatform, target: QubitId
):
update.t2_spin_echo(results.t2_spin_echo[target], platform, target)
update.t2_spin_echo(results.t2[target], platform, target)


spin_echo_signal = Routine(_acquisition, _fit, _plot, _update)
Expand Down
7 changes: 2 additions & 5 deletions src/qibocal/protocols/coherence/t1.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
from ..utils import table_dict, table_html
from . import t1_signal, utils

COLORBAND = "rgba(0,100,80,0.2)"
COLORBAND_LINE = "rgba(255,255,255,0)"


@dataclass
class T1Parameters(t1_signal.T1SignalParameters):
Expand Down Expand Up @@ -125,8 +122,8 @@ def _plot(data: T1Data, target: QubitId, fit: T1Results = None):
x=np.concatenate((waits, waits[::-1])),
y=np.concatenate((probs + error_bars, (probs - error_bars)[::-1])),
fill="toself",
fillcolor=COLORBAND,
line=dict(color=COLORBAND_LINE),
fillcolor=utils.COLORBAND,
line=dict(color=utils.COLORBAND_LINE),
showlegend=True,
name="Errors",
),
Expand Down
79 changes: 1 addition & 78 deletions src/qibocal/protocols/coherence/t2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
from typing import Optional

import numpy as np
import plotly.graph_objects as go
from qibolab import AcquisitionType, AveragingMode, Parameter, Sweeper

from qibocal.auto.operation import QubitId, Routine
from qibocal.calibration import CalibrationPlatform

from ...result import probability
from ..ramsey.utils import ramsey_sequence
from ..utils import table_dict, table_html
from . import t1, t2_signal, utils


Expand Down Expand Up @@ -93,80 +91,5 @@ def _fit(data: T2Data) -> T2Results:
return T2Results(t2s, fitted_parameters, pcovs, chi2)


def _plot(data: T2Data, target: QubitId, fit: T2Results = None):
"""Plotting function for Ramsey Experiment."""

figures = []
fitting_report = ""
qubit_data = data[target]
waits = qubit_data.wait
probs = qubit_data.prob
error_bars = qubit_data.error

fig = go.Figure(
[
go.Scatter(
x=waits,
y=probs,
opacity=1,
name="Probability of 1",
showlegend=True,
legendgroup="Probability of 1",
mode="lines",
),
go.Scatter(
x=np.concatenate((waits, waits[::-1])),
y=np.concatenate((probs + error_bars, (probs - error_bars)[::-1])),
fill="toself",
fillcolor=t1.COLORBAND,
line=dict(color=t1.COLORBAND_LINE),
showlegend=True,
name="Errors",
),
]
)

if fit is not None:
# add fitting trace
waitrange = np.linspace(
min(qubit_data.wait),
max(qubit_data.wait),
2 * len(qubit_data),
)

params = fit.fitted_parameters[target]
fig.add_trace(
go.Scatter(
x=waitrange,
y=utils.exp_decay(
waitrange,
*params,
),
name="Fit",
line=go.scatter.Line(dash="dot"),
)
)
fitting_report = table_html(
table_dict(
target,
[
"T2 [ns]",
"chi2 reduced",
],
[fit.t2[target], fit.chi2[target]],
display_error=True,
)
)
fig.update_layout(
showlegend=True,
xaxis_title="Time [ns]",
yaxis_title="Probability of State 1",
)

figures.append(fig)

return figures, fitting_report


t2 = Routine(_acquisition, _fit, _plot, t2_signal._update)
t2 = Routine(_acquisition, _fit, utils.plot, t2_signal._update)
"""T2 Routine object."""
Loading

0 comments on commit 1a8f9f7

Please sign in to comment.