# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# 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
# Yanchen Lin, linya@chalmers.se
# Matteo Robbiati, robbiati@chalmers.se
# ══════════════════════════════════════════════════════════════════════════
r"""
Shared CZ transition frequency logic for cz_parametrization and cz_chevron.
The CZ transition frequency (Hz) is the detuning between the :math:`|11\rangle` state and
the targeted :math:`|20\rangle` / :math:`|02\rangle` state, computed from the qubits' calibrated 01/12
clock frequencies. cz_currents_from_spectroscopies computes and stores this
value on the coupler's Redis hash (fields ``cz_transition_frequency_20`` /
``cz_transition_frequency_02``); consumers should prefer that stored value
and only fall back to recomputing it live from the device when Redis has
nothing stored yet (e.g. cz_currents_from_spectroscopies has never run for
that coupler).
"""
from typing import Any
import numpy as np
from bragi_quantum.datalayer.utils.logging import logger
REDIS_KEY_BY_TRANSITION = {
"20": "cz_transition_frequency_20",
"02": "cz_transition_frequency_02",
}
[docs]
def compute_transition_frequency(device, coupler: str, cz_transition: str) -> float:
"""
Raw (unrounded) CZ transition frequency (Hz) for ``coupler``, computed
from the qubits' current clock frequencies on ``device``.
"""
if cz_transition not in REDIS_KEY_BY_TRANSITION:
raise ValueError(
f"Unknown cz_transition {cz_transition!r} for {coupler}; "
"expected '20' or '02'."
)
q1_name, q2_name = coupler.split("_")
q1 = device.elements[q1_name]
q2 = device.elements[q2_name]
q1_f01 = float(q1.clock_freqs.f01)
q1_f12 = float(q1.clock_freqs.f12)
q2_f01 = float(q2.clock_freqs.f01)
q2_f12 = float(q2.clock_freqs.f12)
if cz_transition == "20":
return np.abs(q1_f01 + q2_f01 - (q1_f01 + q1_f12))
return np.abs(q1_f01 + q2_f01 - (q2_f01 + q2_f12))
def _from_redis(
redis_connection: Any | None, coupler: str, cz_transition: str
) -> float | None:
if redis_connection is None:
return None
key = REDIS_KEY_BY_TRANSITION[cz_transition]
stored = redis_connection.hget(f"couplers:{coupler}", key)
if not stored:
return None
try:
return float(stored)
except (TypeError, ValueError):
logger.warning(f"Could not parse stored {key} for {coupler}: {stored!r}")
return None
[docs]
def get_cz_transition_frequency(
device,
redis_connection: Any | None,
coupler: str,
cz_transition: str,
cz_half_frequency_wp: bool = False,
) -> float:
"""
CZ transition frequency (Hz) for ``coupler``, preferring the value last
saved to Redis by cz_currents_from_spectroscopies and falling back to
computing it live from the device's clock frequencies if Redis has
nothing stored for ``cz_transition``.
"""
ac_freq = _from_redis(redis_connection, coupler, cz_transition)
if ac_freq is None:
ac_freq = compute_transition_frequency(device, coupler, cz_transition)
ac_freq = int(ac_freq / 1e4) * 1e4
if cz_half_frequency_wp:
ac_freq /= 2
logger.info(
f"cz_half_frequency_wp enabled for {coupler}: centering the CZ "
f"pulse frequency sweep at {ac_freq / 1e6:.3f} MHz."
)
logger.info(f"CZ transition frequency for {coupler}: {ac_freq / 1e6:.3f} MHz")
return ac_freq