Skip to content

Add parameter observers notified of every get and set - #8684

Open
Jens Hedegaard Nielsen (jenshnielsen) wants to merge 4 commits into
microsoft:mainfrom
jenshnielsen:jenshnielsen/parameter_observers
Open

Jens Hedegaard Nielsen (jenshnielsen) wants to merge 4 commits into
microsoft:mainfrom
jenshnielsen:jenshnielsen/parameter_observers

Conversation

@jenshnielsen

Copy link
Copy Markdown
Collaborator

Add qcodes.parameters.add_parameter_observer, remove_parameter_observer and the observe_parameters context manager. An observer is called with a ParameterAccess after every get and set of every parameter, describing the parameter, value, raw value, start time, duration and any exception. A set that ramps in steps is reported once per step. Any number of observers can be registered, independently of on_set_callback and global_on_set_callback.

This makes it possible to record all instrument communication made by a piece of code without changing it or the drivers it uses.

Add qcodes.parameters.add_parameter_observer, remove_parameter_observer and
the observe_parameters context manager. An observer is called with a
ParameterAccess after every get and set of every parameter, describing the
parameter, value, raw value, start time, duration and any exception. A set
that ramps in steps is reported once per step. Any number of observers can be
registered, independently of on_set_callback and global_on_set_callback.

This makes it possible to record all instrument communication made by a piece
of code without changing it or the drivers it uses.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.95833% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 75.12%. Comparing base (18fcd29) to head (fc2ea8d).
⚠️ Report is 36 commits behind head on main.

Files with missing lines Patch % Lines
src/qcodes/parameters/parameter_access.py 98.64% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8684      +/-   ##
==========================================
+ Coverage   75.02%   75.12%   +0.09%     
==========================================
  Files         307      308       +1     
  Lines       32511    32639     +128     
==========================================
+ Hits        24392    24519     +127     
- Misses       8119     8120       +1     

☔ View full report in Codecov by Harness.
📢 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.

Show how to record the parameter accesses of a block of code with
observe_parameters, what a ParameterAccess contains, that ramped sets are
reported per step and failed accesses with their exception, how to see what a
measurement did to the instruments, and how to register an observer for longer
with add_parameter_observer.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread docs/changes/newsfragments/+parameter_observers.new Outdated
@asmull

Copy link
Copy Markdown
Contributor

If you're worried about making this too general and abusable, one thing you could do is for now restrict the observer to be a concrete type that just appends to a list. Or maybe that and logging, to avoid the full general callback-like behavior.

Replace the parameter observer API (add_parameter_observer and friends) with
recording that can only store what happened, so that it cannot be used as a
callback, for example to set one parameter in reaction to another:

- record_parameter_accesses() appends each access to a plain list (subclasses
  are refused, so append cannot be overridden); log_parameter_accesses() logs
  them to the qcodes.parameters.parameter_access logger instead. No user code
  runs while a parameter is accessed.
- A ParameterAccess holds data only: the names of the parameter and its root
  instrument, the value, raw value, unit, timing and a description of any
  error, but not the parameter, instrument or exception.
- Recording is local to the context it was started in (thread or asyncio
  task), held in a ContextVar, and can be nested.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

2 participants