Source code for bragi_quantum.experiments.utils.classification_functions

# ══════════════════════════════════════════════════════════════════════════
#     ___   ___ ___   _      _   ___
#    / _ \ / __|_  ) | |    /_\ | _ )
#   | (_) | (__ / /  | |__ / _ \| _ \
#    \__\_\\___/___| |____/_/ \_\___/
#
#   QC2 @ CHALMERS CALIBRATION TOOLKIT
#   (c) 2026 Chalmers University of Technology and Project Contributors
# ──────────────────────────────────────────────────────────────────────────
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       https://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
# ──────────────────────────────────────────────────────────────────────────
#   CONTRIBUTORS list:
#
#   Joel Sandås, joel.sandas@chalmers.se
#   Axel Andersson, axean@chalmers.se
#   Matteo Robbiati, robbiati@chalmers.se
# ══════════════════════════════════════════════════════════════════════════


"""
Ported from tergite-autocalibration (``lib/utils/classification_functions``).

Classifies raw IQ points into the |0>, |1>, |2> states using the three
angular boundaries (omega_01, omega_12, omega_20) and centroid produced by a
3-state readout calibration (see
``bragi_quantum.experiments.fitting_tools.models.classification.ThreeClassBoundary``). Unlike the tergite
original, the boundary values are passed in explicitly rather than read from
Redis inside this function, so callers control where those values come from
(Redis, the device model, or a test fixture) and the classifier itself stays
pure and easily testable.
"""

import numpy as np
import xarray as xr


[docs] def assign_state( iq_values: "np.ndarray | xr.DataArray", *, centroid_i: float, centroid_q: float, omega_01: float, omega_12: float, omega_20: float, ) -> xr.DataArray: """ Classify each point in ``iq_values`` into state 0, 1, or 2. The three angular boundaries divide the IQ plane (relative to the centroid) into three sectors, one per state; ``iq_values`` is translated so the centroid sits at the origin, converted to an angle, and each angle is assigned to the sector it falls into. """ state_boundaries = {"01": omega_01, "12": omega_12, "20": omega_20} sorted_state_boundaries_dict = { k: v for k, v in sorted(state_boundaries.items(), key=lambda item: item[1]) } sorted_state_boundaries = list(sorted_state_boundaries_dict.keys()) # translate iq_values so their origin is the centroid iq_values_translated = iq_values - (centroid_i + 1j * centroid_q) # convert the translated iq to an array of angles with respect to the centroid iq_values_angles = xr.apply_ufunc( lambda x: (np.angle(x, deg=True) + 360) % 360, iq_values_translated ) def state_filter(angles_array: xr.DataArray) -> xr.DataArray: boundary_1 = sorted_state_boundaries[0] # eg '01' boundary_2 = sorted_state_boundaries[1] # eg '12' boundary_3 = sorted_state_boundaries[2] # eg '20' angle_1 = state_boundaries[boundary_1] angle_2 = state_boundaries[boundary_2] angle_3 = state_boundaries[boundary_3] # find the common state between the boundaries. eg: '01' & '12' -> 1 state_between_1_and_2 = int(next(iter(set(boundary_1) & set(boundary_2)))) state_between_2_and_3 = int(next(iter(set(boundary_2) & set(boundary_3)))) state_between_3_and_1 = int(next(iter(set(boundary_3) & set(boundary_1)))) condition_1 = ( (angles_array > angle_1) & (angles_array < angle_2) ) * state_between_1_and_2 condition_2 = ( (angles_array > angle_2) & (angles_array < angle_3) ) * state_between_2_and_3 condition_3 = ( (angles_array < angle_1) | (angles_array > angle_3) ) * state_between_3_and_1 return condition_1 + condition_2 + condition_3 assigned_states = xr.apply_ufunc(state_filter, iq_values_angles) return assigned_states