# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# 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:
#
# Michele Faucci Giannelli, faucci@chalmers.se
# Abdullah-Al Amin, alamina@chalmers.se
# Matteo Robbiati, robbiati@chalmers.se
# Axel Andersson, axean@chalmers.se
# ══════════════════════════════════════════════════════════════════════════
# This code is part of Tergite
#
# (C) Copyright Eleftherios Moschandreou 2023, 2024
# (C) Copyright Liangyu Chen 2023, 2024
# (C) Copyright Michele Faucci Giannelli 2024
# (C) Copyright Stefan Hill 2024
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
#
# Modified from tergite-autocalibration
# (https://github.com/tergite/tergite-autocalibration), file:
# tergite_autocalibration/lib/utils/samplespace.py
"""
Utility functions for generating sample space arrays for spectroscopy measurements.
"""
import numpy as np
# bragi_quantum.datalayer Imports
from bragi_quantum.datalayer.managing.session import ActiveSession
from bragi_quantum.datalayer.utils.logging import logger
# TODO: understand where to put the central frequency
[docs]
def resonator_samples(
qubit: str,
state: int = 0,
samples: int | None = None,
half_width: float | None = None,
) -> np.ndarray:
"""
Dynamically generates the frequency sweep array for a resonator spectroscopy measurement.
Fetches the central readout frequency for the targeted qubit directly from the
active SessionContext's device, applying a state-dependent shift, and generating
a linear space around it.
Args:
qubit (str): The name of the target qubit (e.g., 'q16').
state (int): The target qubit state (0 for ground, 1 for first excited, etc.).
samples (Optional[int]): Number of points in the sweep. Defaults to 100 if None.
half_width (Optional[float]): The half-width of the sweep window in Hz. Defaults to 1 MHz if None.
Returns:
np.ndarray: A linear array of frequencies to sweep.
Raises:
KeyError: If the qubit is not found in the device.
"""
session = ActiveSession.get()
# Fetch the qubit from the session's device
try:
transmon = session.device.elements[qubit]
except (KeyError, AttributeError, TypeError):
raise KeyError(f"Qubit '{qubit}' not found in the device configuration.")
# Use provided samples/half_width or defaults
resolved_samples = samples if samples is not None else 100
resolved_half_width = half_width if half_width is not None else 1.0e6
# Base frequency (readout frequency)
base_frequency = float(transmon.clock_freqs.readout)
# Shift by 200 kHz per state based on legacy physics logic
frequency = base_frequency - 2e5 * state
min_freq = frequency - resolved_half_width
max_freq = frequency + resolved_half_width
return np.linspace(min_freq, max_freq, resolved_samples)
[docs]
def qubit_samples(
qubit: str,
transition: str = "01",
samples: int | None = None,
half_width: float | None = None,
use_freq_12_from_config: bool | None = None,
) -> np.ndarray:
"""
Dynamically generates the frequency sweep array for a qubit spectroscopy measurement.
Fetches the central transition frequency for the targeted qubit directly from the
active SessionContext's device, applying detuning logic for higher transitions.
Args:
qubit (str): The name of the target qubit (e.g., 'q16').
transition (str): The target transition ('01' or '12'). Defaults to '01'.
samples (Optional[int]): Number of points in the sweep. Defaults to 100 if None.
half_width (Optional[float]): The half-width of the sweep window in Hz. Defaults to 5 MHz if None.
use_freq_12_from_config (Optional[bool]): If True, attempts to use explicit f12 from config.
Returns:
np.ndarray: A linear array of frequencies to sweep.
Raises:
ValueError: If an unknown transition is provided.
KeyError: If the qubit is not found in the device.
"""
session = ActiveSession.get()
# Fetch the qubit from the session's device
try:
transmon = session.device.elements[qubit]
except (KeyError, AttributeError, TypeError):
raise KeyError(f"Qubit '{qubit}' not found in the device configuration.")
resolved_samples = samples if samples is not None else 100
if transition == "01":
resolved_half_width = half_width if half_width is not None else 5.0e6
frequency = float(transmon.clock_freqs.f01)
elif transition == "12":
resolved_half_width = half_width if half_width is not None else 12.0e6
# Determine detuning based on even/odd qubit index
try:
# Extract digits from qubit string (e.g., 'q16' -> 16)
qubit_index = int("".join(filter(str.isdigit, qubit)))
detuning = 150e6 if qubit_index % 2 == 0 else 250e6
except ValueError:
# Fallback if the qubit name does not contain an integer
logger.warning(
f"Could not parse integer from qubit name '{qubit}'. Defaulting to 200MHz detuning."
)
detuning = 200e6
frequency = float(transmon.clock_freqs.f01) - detuning
# Try to use f12 if available
if use_freq_12_from_config and hasattr(transmon.clock_freqs, "f12"):
try:
frequency = float(transmon.clock_freqs.f12)
except (ValueError, AttributeError):
pass # Fall back to calculated frequency
else:
raise ValueError(
f"Unknown transition '{transition}' provided to qubit_samples."
)
min_freq = frequency - resolved_half_width
max_freq = frequency + resolved_half_width
return np.linspace(min_freq, max_freq, resolved_samples)