# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# 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:
#
# Axel Andersson, axean@chalmers.se
# Matteo Robbiati, robbiati@chalmers.se
# Yanchen Lin, linya@chalmers.se
# ══════════════════════════════════════════════════════════════════════════
# This code is part of Tergite
#
# (C) Copyright Eleftherios Moschandreou 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/schedule_execution.py
import threading
import time
from pathlib import Path
import tqdm
import xarray
from qblox_scheduler.qblox.hardware_agent import HardwareAgent
from qblox_scheduler.schedules.schedule import CompiledSchedule
from bragi_quantum.api.handlers import amc
from bragi_quantum.datalayer.managing.time_tracing import TimingTraceCategory
from bragi_quantum.datalayer.utils.logging import logger
from bragi_quantum.datalayer.utils.logging.visuals import log_timing
from bragi_quantum.datalayer.utils.mode import ExecutionMode
from bragi_quantum.experiments.utils.cluster_snapshots import take_cluster_snapshots
from bragi_quantum.experiments.utils.q1asm_dump import dump_q1asm_programs
from bragi_quantum.experiments.utils.timing import timed
[docs]
def execute_schedule(
compiled_schedule: CompiledSchedule,
hardware_agent: HardwareAgent,
*,
execution_mode: ExecutionMode,
measurement: tuple[int, int] = (0, 1),
data_path: Path | None = None,
cluster_names: list[str] | None = None,
save_cluster_metadata_to_disk: bool = False,
) -> xarray.Dataset:
logger.info("Starting measurement")
cluster_names = cluster_names or []
schedule_duration = compiled_schedule.get_schedule_duration()
def display_progress() -> None:
steps = int(schedule_duration * 5)
progress_sleep = 0.004 if execution_mode == ExecutionMode.dummy else 0.2
for _ in tqdm.tqdm(range(steps), desc=compiled_schedule.name, colour="blue"):
time.sleep(progress_sleep)
if save_cluster_metadata_to_disk:
with timed(
"Snapshotting clusters took: ", timing_category=TimingTraceCategory.LOGGING
):
take_cluster_snapshots(
data_path, measurement, cluster_names, label="before"
)
dump_q1asm_programs(data_path, compiled_schedule, measurement)
_measurement_msg = (
f". Measurement {measurement[0] + 1} of {measurement[1]}"
if measurement[1] > 1
else ""
)
log_timing("Schedule duration: ", schedule_duration, _measurement_msg)
instrument_coordinator = hardware_agent.instrument_coordinator
with timed("Prepare took: ", timing_category=TimingTraceCategory.PREPARATION):
instrument_coordinator.stop(allow_failure=True)
instrument_coordinator.prepare(compiled_schedule)
# AMC (mixer correction) is written onto the real sequencer parameters by
# prepare() above — check it here, before start() ever fires. Diagnostic
# output, not part of the measurement — but reading it back is a real
# per-sequencer hardware round trip (qcodes .get() per parameter), so it
# can take several seconds on a larger device; timed separately (as
# preparation, not measurement) so that cost is visible instead of
# silently padding the gap between prepare and measurement.
with timed(
"AMC state check took: ", timing_category=TimingTraceCategory.PREPARATION
):
clusters = list(hardware_agent.get_clusters().values())
amc.print_amc_state(clusters)
thread_tqdm = threading.Thread(target=display_progress)
thread_tqdm.start()
with timed(
"Measurement took: ",
suffix_fn=lambda d: f" ({d - schedule_duration:+.5g} sec)",
timing_category=TimingTraceCategory.MEASUREMENT,
):
instrument_coordinator.start()
instrument_coordinator.wait_done(timeout_sec=3600)
thread_tqdm.join()
with timed(
"Retrieve acquisition took: ", timing_category=TimingTraceCategory.MEASUREMENT
):
raw_dataset: xarray.Dataset = instrument_coordinator.retrieve_acquisition()
if save_cluster_metadata_to_disk:
with timed(
"Snapshotting clusters took: ", timing_category=TimingTraceCategory.LOGGING
):
take_cluster_snapshots(data_path, measurement, cluster_names, label="after")
return raw_dataset