Implement SparseObservable.to_matrix - #16809
Open
davidfcohen wants to merge 35 commits into
Open
Conversation
SparseObservable::to_matrix.SparseObservable::to_matrix
SparseObservable::to_matrixSparseObservable::to_matrix
davidfcohen
marked this pull request as ready for review
August 21, 2026 18:08
Collaborator
|
One or more of the following people are relevant to this code:
|
SparseObservable::to_matrixSparseObservable::to_matrix
Coverage Report for CI Build 33183909265Warning Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes. Coverage increased (+0.009%) to 87.76%Details
Uncovered Changes
Coverage Regressions40 previously-covered lines in 3 files lost coverage.
Coverage Stats
💛 - Coveralls |
3 tasks
SparseObservable::to_matrixSparseObservable::to_matrix with Python API
SparseObservable::to_matrix with Python APISparseObservable.to_matrix backed by Rust
SparseObservable.to_matrix backed by RustSparseObservable.to_matrix backed by Rust
SparseObservable.to_matrix backed by RustSparseObservable.to_matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The
SparseObservable.to_matrixmethod expands the observable into its dense matrix representation. The Rust counterpart isSparseObservable::to_matrix.There are 3 primary motivations for this feature:
SparseObservableought to have ato_matrixmethod that handles projectors directly, without expanding into Pauli terms.SparseObservable.to_matrixandSparsePauliOp.to_matrixshould share the same implementation.SparseObservablewill be the base for some custom operations. This function is useful for those implementingCustomOperation::to_matrix.An observable is a linear combination of multi-qubit terms. If the term is composed of Pauli-only operators, its coefficient is added once per row. We take advantage of this property to fill the matrix more efficiently. This part of the solution is derived from the internals of
SparsePauliOp.to_matrix.We decreased the cost of filling the matrix using some clever tricks. Thank you @jakelishman and @Cryoris 😄 However, it's clear that the total time is dominated by the O(4n) matrix allocation.
closes #13389
AI/LLM disclosure
The
add_term_kronfunction is derived from an AI generated algorithm suggested by @Cryoris. The Rust derivative was generated using Claude Sonnet 5. The rest of the PR is hand-written.