Skip to content

[wip] Feature/measure v2 scan nd#8117

Open
jenshnielsen wants to merge 5 commits into
microsoft:mainfrom
jenshnielsen:feature/measure-v2-scan-nd
Open

[wip] Feature/measure v2 scan nd#8117
jenshnielsen wants to merge 5 commits into
microsoft:mainfrom
jenshnielsen:feature/measure-v2-scan-nd

Conversation

@jenshnielsen
Copy link
Copy Markdown
Collaborator

No description provided.

jenshnielsen and others added 4 commits May 12, 2026 09:45
Introduce a parallel, async-capable measurement API for QCoDeS that
decouples plan description from execution. Plans are Python generators
yielding typed messages (Set, Read, Sleep, Emit, OpenRun, CloseRun); a
MeasurementEngine drives them on a dedicated worker thread and publishes
events (RunStarted, RowEmitted, RunStopped) to pluggable sinks via a
publisher thread with a bounded queue.

Key properties of the tracer-bullet implementation:

- Non-blocking submission via engine.submit(...) returning a RunHandle
  with futures for the dataset and final result.
- Cancellation via it.throw(CancelRequested) at the next yield point;
  plan try/finally blocks always run for safe cleanup. scan_1d
  guarantees a ramp-to-zero of the swept parameter on any exit path.
- Cancel flag re-checked between .send() and dispatch to prevent one
  extra Set/Read leaking to hardware after cancellation.
- handle.wait() returning guarantees data is durably committed: the
  publisher thread completes futures only after all sinks have
  processed RunStopped.
- SqliteSink reuses existing DataSaver/Measurement infrastructure;
  opens its own SQLite connection on the publisher thread to satisfy
  check_same_thread=True. ~50 lines of integration code.
- MemorySink for tests and lightweight in-memory consumption.

Public surface:

  import qcodes as qc
  ds = qc.measure_v2.scan(LinSweep(g, 0, 1, 11), measure=[i])

Advanced users construct an explicit MeasurementEngine and submit plans
built via run(...) + plan-builders (scan_1d, or custom generators).

Tracer scope is deliberately small: 1D scans of scalar parameters with
the default SQLite sink. Multi-dim scans, Call/Describe messages,
parallel reads via underlying_instrument grouping, queueing, pause/resume,
live-plot/progress sinks, and a dond shim are P0-P2 for the v1 release.
See src/qcodes/measure_v2/DESIGN.md for the full architecture.

50 tests across L1 (message-stream), L2 (engine + MemorySink), and
L3 (engine + SqliteSink) levels. All three end-to-end acceptance
scenarios pass.

Co-authored-by: Copilot <[email protected]>
Introduces scan_nd(*sweeps, measured=[...]) for nested multi-dimensional
scans, the highest-value gap vs dond/do2d/doNd. Sweeps are outermost-first
ordered; at each innermost setpoint the plan reads all measured parameters
and emits one dataset row. Per-sweep delay and get_after_set are honored.

Cleanup contract extends scan_1d's: on exit (success, error, or cancel)
every swept parameter is set to 0.0, in outer-to-inner order, with the
engine dispatching each cleanup Set before publishing RunStopped.

scan_1d is refactored as a thin delegate to scan_nd (single-sweep case).
get_after_set support — deferred from the tracer bullet — is now honored
in both, inserting a Read of the swept parameter immediately after Set so
the dataset row reflects the actual instrument readback.

The convenience entrypoint qc.measure_v2.scan(*sweeps, measure=[...]) now
accepts any number of sweeps and dispatches accordingly. The previous
NotImplementedError for multi-sweep usage is removed.

20 new tests across L1 (message-stream), L2 (engine + MemorySink), and
L3 (engine + SqliteSink) levels. All 70 measure_v2 tests pass.

Co-authored-by: Copilot <[email protected]>
Walks through qcodes.measure_v2 end-to-end with executable examples:

1. Setup (temp database, software-only parameters)
2. Simple 1D blocking scan via qc.measure_v2.scan()
3. Non-blocking submission with wait=False; peeking partial data
4. Cancellation with verified cleanup (g ramps back to 0)
5. 2D scan via scan_nd
6. Writing a custom plan-builder (bisection search) with try/finally
7. Custom sinks: MemorySink for inspection, plain-function sink for
   live progress
8. Unit-testing plan-builders without an engine (drive_plan helper)
9. Caveats and v1 backlog

All cells executed; the cancel-safety guarantee and bisection
convergence are demonstrated with real output values in the saved
notebook.

Co-authored-by: Copilot <[email protected]>
@jenshnielsen jenshnielsen requested a review from a team as a code owner May 12, 2026 08:24
@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

❌ Patch coverage is 91.24579% with 52 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.42%. Comparing base (0a13dca) to head (175b20c).
⚠️ Report is 66 commits behind head on main.

Files with missing lines Patch % Lines
src/qcodes/measure_v2/engine.py 88.59% 35 Missing ⚠️
src/qcodes/measure_v2/sinks/sqlite.py 87.14% 9 Missing ⚠️
src/qcodes/measure_v2/convenience.py 76.47% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8117      +/-   ##
==========================================
- Coverage   70.80%   70.42%   -0.38%     
==========================================
  Files         302      312      +10     
  Lines       31911    32490     +579     
==========================================
+ Hits        22593    22882     +289     
- Misses       9318     9608     +290     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Add @overload signatures to qc.measure_v2.scan() so wait=True returns
  DataSetProtocol | None and wait=False returns RunHandle.
- Narrow Future[Any] -> Future[DataSetProtocol | None] for RunHandle.dataset
  and _Submission.dataset_future. Callers no longer need to cast to access
  .cache.
- Type all test fixtures as Generator[T, None, None] (they yield, not return).
- Move type-only application imports (e.g., ParameterBase) into
  TYPE_CHECKING blocks in test files.
- Tutorial notebook: add type annotations throughout the code cells
  (function signatures, key variable annotations), and place type-only
  imports under TYPE_CHECKING. Notebook still executes end-to-end.

pyright clean on src/qcodes/measure_v2 + tests/measure_v2 (0 errors).
All 70 tests pass. prek hooks pass.

Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant