GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Compensated summation for cpu_statevec_anyCtrlAnyTargDenseMatr_sub (#598) - [unitaryHACK] by mk0dz · Pull Request #791 · QuEST-Kit/QuEST

Conversation

@mk0dz

@mk0dz mk0dz commented Jun 12, 2026

Copy link
Copy Markdown

Summary

Adds an optional compensated-summation path to improve numerical accuracy in CPU dense-matrix target evolution.

The implementation introduces a thinner reduction in cpu_statevec_anyCtrlAnyTargDenseMat_sub(), controlled by the compile-time flag:

-DQUEST_COMPENSATED_DENSEMAT_SUM=ON

The feature is disabled by default to preserve existing performance characteristics.

Implementation Notes

  • Accumulates the Kahan compensation term into a local cpu_qcomp variable and writes it back once after the reduction.
  • Applies only to the CPU dense-matrix path.
  • This source file is not built with fast-math, so the compensation term is preserved by the compiler.
  • Replaces the previous TODO comment with documentation describing the behaviour and trade-offs.

Files Changed

  • quest/src/cpu/cpu_subroutines.cpp
  • quest/src/cpu/CMakeLists.txt

Accuracy & Performance

Benchmarked against a __float128 reference using the naive vs. compensated Kahan implementation across all three floating-point precisions.

Precision Accuracy @ 12 Targets Runtime Cost
fp32 ~58× lower error ~3.3×
fp64 ~29× lower error ~2.2× (~1.76× for 10 targets on a 24-qubit state)
fp80 ~25× lower error ~2.1×

Observations

  • Accuracy improvements grow with target count.
  • Benefits become negligible below roughly 7 targets.
  • Runtime overhead remains greater than 1× because the kernel is compute-bound on matrix multiplication rather than memory-bandwidth limited.
  • The Kahan recurrence prevents efficient vectorization.
accuracy bandwidth_regime runtime tradeoff

Validation

All existing dense CompMatr tests pass for both compensated and uncompensated builds, including:

  • applyCompMatr
  • Controlled variants

Test results:

  • 93,791 assertions
  • 4 test cases

Rationale for Default-Off

The improvement is primarily beneficial for large or ill-conditioned matrices, while introducing a consistent ~2–3× runtime overhead.

Keeping the feature disabled by default preserves current performance expectations while allowing users to opt in when higher numerical accuracy is required.

Future Work

A possible alternative is pairwise summation, which offers:

  • Error scaling of approximately O(log N)
  • Better vectorization opportunities
  • Potentially lower runtime overhead

This can be evaluated separately in a future PR.

I also acknowledge claude for guiding me thorough it.

/claim #598

Closes #598.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mk0dz
Add opt-in compensated (Kahan) summation to applyCompMatr CPU path (Q…
d73dfc6
…uEST-Kit#598)

The dense-matrix subroutine's inner reduction is liable to catastrophic
cancellation for many target qubits. This adds an opt-in compensated path
(-DQUEST_COMPENSATE_DENSEMATR_SUM=ON); base_qcomp's operators are plain IEEE
arithmetic so the compensation is honoured directly.

Single-CPU benchmarks (fp32/fp64/fp80): relative error improves ~25-58x at 12
targets, the benefit growing with target count; runtime cost is ~2-3.3x
(compute-bound) falling to ~1.8x in the large-statevector regime. Left opt-in
(off by default) per that trade-off.
@TysonRayJones

TysonRayJones commented Jun 15, 2026

Copy link
Copy Markdown
Member

Hi Mukul,

Beautiful diff, and very useful and thorough benchmarking!

Super interesting that quadrupling the flops per outer iteration (by the Kahan summation) still measurably impacts runtime at 24 qubits (at which the state is ~120 - 500 MiB). You have demonstrated compensation is only worthwhile for many target qubits (otherwise of course, the sum is too small to need compensation), and the more target qubits, the exponentially greater the total number of flops per iteration. So, there might not exist a regime where compensation of the inner loop has an accuracy benefit, where the outer loop is simultaneously memory bandwidth bound to hide the slowdown. Quite a shock to me!

We will explore further to decide if exposing this hyperparameter (whether or not to compensate) is worthwhile. In any case, you have satisfied the requirements of the unitaryHACK challenge! 🎉 Please comment on issue #598 so I can assign it to you, and award the prize.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mk0dz mk0dz mentioned this pull request Jun 15, 2026
Improve applyCompMatr accuracy with compensated summation #598
Closed
This was referenced Jun 22, 2026
Improve accuracy with compensated summation #640
Open
v4.3 release notes #774
Closed

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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@mk0dz @TysonRayJones
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.