# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# 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
# ══════════════════════════════════════════════════════════════════════════
import difflib
import json
from pathlib import Path
from typing import Literal
from qcodes.instrument import Instrument
from bragi_quantum.datalayer.utils.logging.visuals import (
log_size_on_disk,
log_snapshot_size,
)
[docs]
def take_cluster_snapshots(
data_path: Path | None,
measurement: tuple[int, int],
cluster_names: list[str],
*,
label: Literal["before", "after"],
) -> None:
"""Save a JSON snapshot of every cluster under cluster_metadata/.
Each cluster gets its own subfolder inside the per-submeasurement directory::
cluster_metadata/measurement_<N>/<cluster_name>/before.json
cluster_metadata/measurement_<N>/<cluster_name>/after.json
cluster_metadata/measurement_<N>/<cluster_name>/snapshot.diff
"""
if data_path is None:
return
if label not in ("before", "after"):
raise ValueError("Label must be 'before' or 'after'")
metadata_dir = cluster_metadata_dir(data_path, measurement)
for cluster_name in cluster_names:
cluster_dir = metadata_dir / cluster_name
cluster_dir.mkdir(parents=True, exist_ok=True)
cluster_obj = Instrument.find_instrument(cluster_name)
# NOTE: update=False is a lot faster, but uses the QCoDeS cache
# instead of querying the cluster for all of the parameters.
# Typically the cache is in sync, but there
# are cases when the cache is outdated.
# If you are debugging cluster settings, change this to update=True
snap = cluster_obj.snapshot(update=False)
current_text = json.dumps(snap, indent=2, default=str)
file_path = cluster_dir / f"{label}.json"
file_path.write_text(current_text)
log_snapshot_size(file_path)
if label == "after":
before_path = cluster_dir / "before.json"
if before_path.exists():
before_text = before_path.read_text()
diff_lines = list(
difflib.unified_diff(
before_text.splitlines(keepends=True),
current_text.splitlines(keepends=True),
fromfile="before.json",
tofile="after.json",
)
)
diff_path = cluster_dir / "snapshot.diff"
diff_path.write_text(
"".join(diff_lines) if diff_lines else "(no changes)\n"
)
log_size_on_disk("Snapshot diff written:", diff_path)