Skip to content

feat(explorer): Implement c-optimal discretisation strategy to reduce gradient variance in error budgeting feature#266

Merged
rolandriver merged 26 commits into
Deltakit:mainfrom
adlantz:main
Jul 9, 2026
Merged

feat(explorer): Implement c-optimal discretisation strategy to reduce gradient variance in error budgeting feature#266
rolandriver merged 26 commits into
Deltakit:mainfrom
adlantz:main

Conversation

@adlantz

@adlantz adlantz commented May 29, 2026

Copy link
Copy Markdown
Contributor

🔗 Closed Issues

Closes #283


📝 Description

The functionget_error_budget() in deltakit-explorer/src/deltakit_explorer/analysis/error_budget/_budget.py takes a fitting_parameters parameter of type FittingParameters. This parameter has a discretisation_strategy property of type DiscretisationStrategy. In order to calculate the proportionate contribution of each error source to the error budget, we need to calculate the gradient for each error source, this requires sampling a set of points around the defined error rate, fitting the results to a polynomial, then calculating the gradient at that point. The discretisation_strategy property dictates which points should be sampled to calculate that gradient.

Currently we only give the users two strategies, DiscretisationStrategy.LINEAR and DiscretisationStrategy.LOGARITHMIC. These choose a linear and logarithmic spread of points respectively. Neither of these sampling strategies is optimal with respect to the variance of the final gradient calculation.

In this PR we introduce a third strategy DiscretisationStrategy.C_OPTIMAL which uses an optimization algorithm to find the best choice of points to sample in order to minimize the variance of the gradient estimate. This kind of optimization is known as a c-optimal experimental design (https://en.wikipedia.org/wiki/Optimal_experimental_design).

The DiscretisationStrategy.C_OPTIMAL approach consists of two main functions. The first isget_c_optimal_points() which sets up the optimization parameters and runs scipy.optimize.differential_evolution(). The second is _c_optimal_objective(), the objective function passed to the SciPy optimization algorithm. This calculates the variance of the gradient estimation given a set of sampling points.

There are a few things to note.

  1. In deltakit-explorer/src/deltakit_explorer/analysis/error_budget/_post_processing.py we update the _compute_logical_error_rate_per_round_from_results(). Instead of grabbing the firsts result from the pandas df query, we sum across all the results. This change is necessary because c-optimal can choose to sample single points multiple times. This is non-breaking for the existing use cases of this function, it still works if only 1 row is returned.
  2. The existing discretisation functions get_logarithmic_points() and get_linear_points() expect c, the central point to be a python float. However, currently generate_sweep_parameters() calls them with c as a size 1 numpy column vector. This was not breaking before because get_logarithmic_points() and get_linear_points(). We address this by explicitly flattening central_point in generate_sweep_parameters() before calling a discretisation strategy. We ran unit tests to confirm this didn't break the linear and logarithmic approaches.
  3. We hard coded the SciPy optimization algorithm's seed. This allows for a sense of reliability/consistency - a user may not want to see their error budget shifting when running the same code with the same parameters twice. However, it allowing for a random seed can produce better results. It might be worth considering a "random vs deterministic" parameter for the user to toggle.

Here is a run across 10 different seeds demonstrating the improvement of c-optimal over linear and logarithmic. We also considered a d-optimal approach but it was not as effective.
multi_seed_box_plot

Tests

We mimicked the existing pytest unit test infrastructure and added coverage for the new code in this feature.


🚦 Status

In review


🛠️ Future Work

This bounds issue #216 is a logical follow up.


➕️ Additional Information

This work was done by University of Washington students as part of a course in collaboration with @rolandriver


🧾 Release Note

Add c-optimal discretisation strategy to improve error budgeting feature

@CLAassistant

CLAassistant commented May 29, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@nelimee

nelimee commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

@adlantz let me know when I should start reviewing this PR. Ideally, it should be out of draft and with CI passing, but I am fine doing a pre-review if you need one :)

@adlantz adlantz changed the title [DRAFT] Implement C optimal discretisation strategy to reduce gradient variance in error budgeting feature [DRAFT] Implement c-optimal discretisation strategy to reduce gradient variance in error budgeting feature Jun 9, 2026
@rolandriver

Copy link
Copy Markdown
Collaborator

Hey @adlantz thank you for this. Yes, you can raise an issue with the description above. In the PR description you can reference the issue by adding "Closes #." and GitHub will create the link.

@adlantz
adlantz marked this pull request as ready for review June 12, 2026 06:26
@adlantz adlantz changed the title [DRAFT] Implement c-optimal discretisation strategy to reduce gradient variance in error budgeting feature Implement c-optimal discretisation strategy to reduce gradient variance in error budgeting feature Jun 12, 2026
@adlantz

adlantz commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

@nelimee @rolandriver CI checks are passing - PR is ready for review

@rolandriver rolandriver changed the title Implement c-optimal discretisation strategy to reduce gradient variance in error budgeting feature feat(explorer): Implement c-optimal discretisation strategy to reduce gradient variance in error budgeting feature Jun 15, 2026

@rolandriver rolandriver left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @adlantz for this really. This is looking quite nice indeed. Few comments from me and happy to approve soon after they've been addressed.

Comment thread deltakit-explorer/tests/analysis/budget/test_post_processing.py Outdated
Comment thread deltakit-explorer/tests/plotting/test_plotting.py Outdated
Comment thread deltakit-explorer/tests/utils/test_auth.py Outdated
@rolandriver

rolandriver commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Hey @adlantz would you have time to address review comments and finalise this PR? There had been quite few changes merged on main so please if you could rebase.

@nelimee

nelimee commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

I'll review that PR probably around Thursday, thanks for submitting it!

@rolandriver
rolandriver self-requested a review June 16, 2026 18:39
rolandriver
rolandriver previously approved these changes Jun 16, 2026

@rolandriver rolandriver left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for your contribution @adlantz. This is looking good to me. Approving now but merging upon @nelimee's review and green light.

@adlantz

adlantz commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

@rolandriver No problem, thank you for reviewing. I'll keep an eye on it until it's merged to see if there's any more updates to make.

@nelimee nelimee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I think that this brings a nice new way of choosing points for error-budgeting, but think this implements an approximation of what people usually think about when using "optimal design".

To be more explicit, your method starts by fixing a set of points that are sufficiently close to each-other (_C_OPTIMAL_CANDIDATE_GRID_SIZE which is 50 at the moment) and linearly spaced, and then optimises on which point should be evaluated and how many times it should be evaluated.

I think that this method tends to the original optimal-design idea I had when _C_OPTIMAL_CANDIDATE_GRID_SIZE tends to infinity, i.e., when there is no limit on which point (within the provided interval) can be picked.

That is perfectly fine to me, and implementing the continuous optimisation version will probably be a nice exercise for later.

I'll accept that PR once my comments are addressed.

Comment thread deltakit-explorer/tests/analysis/budget/test_discretisation.py Outdated
Comment thread deltakit-explorer/src/deltakit_explorer/analysis/error_budget/_discretisation.py Outdated
Comment thread deltakit-explorer/src/deltakit_explorer/analysis/error_budget/_discretisation.py Outdated
adlantz and others added 4 commits June 22, 2026 20:21
…_discretisation.py

Co-authored-by: Adrien Suau <12374487+nelimee@users.noreply.github.com>
@adlantz

adlantz commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

@nelimee I addressed your comments, thanks for your review. Let me know if there's anything else you want me to touch up!

nelimee
nelimee previously approved these changes Jul 1, 2026
Comment thread deltakit-explorer/src/deltakit_explorer/analysis/error_budget/_discretisation.py Outdated
Comment thread deltakit-explorer/tests/analysis/budget/test_discretisation.py Outdated
Comment thread deltakit-explorer/tests/analysis/budget/test_discretisation.py Outdated
@rolandriver

Copy link
Copy Markdown
Collaborator

Hey @adlantz would you have time to implement @nelimee comments? Just to finalise this PR. Thank you 🙏

@adlantz

adlantz commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

@rolandriver @nelimee apologies for being a little slow I was traveling without my laptop for the last 10 days. I will address the final comments and fix the merge conflicts tomorrow!

adlantz and others added 2 commits July 7, 2026 18:31
Co-authored-by: Adrien Suau <12374487+nelimee@users.noreply.github.com>
adlantz and others added 2 commits July 7, 2026 18:33
Co-authored-by: Adrien Suau <12374487+nelimee@users.noreply.github.com>
@adlantz

adlantz commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

@rolandriver @nelimee comments addressed and synced with main. I see a part of the CI failing but I don't think that's an issue I caused.

@nelimee

nelimee commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

@rolandriver @nelimee comments addressed and synced with main. I see a part of the CI failing but I don't think that's an issue I caused.

You are right, that's not due to this PR. I checked, none of the failures are due to this PR. Probably due to an update of pydoclint. We can merge as-is if you are ready and don't see anything else to change in this PR :)

@adlantz

adlantz commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

@rolandriver @nelimee Good to merge! I don't think I have the permissions to do it myself

@rolandriver

Copy link
Copy Markdown
Collaborator

Thank you @adlantz for this contribution!

@rolandriver
rolandriver merged commit f59bb4c into Deltakit:main Jul 9, 2026
34 of 47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

request: more efficient choice of sampling points for error budgeting feature

4 participants