Skip to content

Accumulate ParallelLinearQubitOperator matvecs in-place (#1410) - #1450

Open
rosspeili wants to merge 1 commit into
quantumlib:mainfrom
rosspeili:fix/issue-1410-matvec-inplace-accumulate
Open

Accumulate ParallelLinearQubitOperator matvecs in-place (#1410)#1450
rosspeili wants to merge 1 commit into
quantumlib:mainfrom
rosspeili:fix/issue-1410-matvec-inplace-accumulate

Conversation

@rosspeili

Copy link
Copy Markdown
Contributor

Implements the suggestion from #1410: avoid functools.reduce(numpy.add, …) when combining partial matvecs, which was allocating a new full-sized array for every partial sum.

Used the approach from GCA and went a bit further:

  • Shared in-place += accumulation for both the single-process and multiprocess paths (same cost when combining worker results).
  • Only set forkserver when processes > 1, so the single-process path works on Windows.

There is one nuance with processes=1, where operator grouping usually yields a single group so the biggest + is on the multiprocess reduce. Happy to trim or adjust if its better to keep the change scoped only to the single-process path.

Tests passed, and will wait for CI to see if I missed anything.

Fixes #1410

…antumlib#1410)

Accumulate partial matvecs into one buffer with in-place += instead of functools.reduce(numpy.add), for both the single-process and multiprocess paths. Defer forkserver setup until processes > 1 so the single-process path works on Windows.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request optimizes memory usage and improves platform compatibility for ParallelLinearQubitOperator. It replaces functools.reduce(numpy.add, ...) with a new helper function _accumulate_vectors that performs in-place vector accumulation to avoid intermediate allocations. Additionally, it ensures that the 'forkserver' multiprocessing start method is only set when spawning multiple processes, allowing the single-process path to run on platforms like Windows. Unit tests have been added to cover the new helper function and the single-process execution path. There are no review comments, so I have no feedback to provide.

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.

Possible micro-optimization in linear_qubit_operator.py

1 participant