Skip to content

Implement SparseObservable.to_matrix - #16809

Open
davidfcohen wants to merge 35 commits into
Qiskit:mainfrom
davidfcohen:sparse-observable-matrix
Open

Implement SparseObservable.to_matrix#16809
davidfcohen wants to merge 35 commits into
Qiskit:mainfrom
davidfcohen:sparse-observable-matrix

Conversation

@davidfcohen

@davidfcohen davidfcohen commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

The SparseObservable.to_matrix method expands the observable into its dense matrix representation. The Rust counterpart is SparseObservable::to_matrix.

There are 3 primary motivations for this feature:

  1. SparseObservable ought to have a to_matrix method that handles projectors directly, without expanding into Pauli terms.
  2. In the future, SparseObservable.to_matrix and SparsePauliOp.to_matrix should share the same implementation.
  3. In the future, SparseObservable will be the base for some custom operations. This function is useful for those implementing CustomOperation::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

  • I didn't use LLM tooling, or only used it privately.
  • I used the following tool to help write this PR description:
  • I used the following tool to generate or modify code:

The add_term_kron function 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.

@davidfcohen davidfcohen self-assigned this Aug 20, 2026
@davidfcohen davidfcohen added mod: quantum info Related to the Quantum Info module (States & Operators) Changelog: None Do not include in the GitHub Release changelog. Rust This PR or issue is related to Rust code in the repository labels Aug 20, 2026
@davidfcohen davidfcohen changed the title [WIP] Implement SparseObservable::to_matrix. [WIP] Implement SparseObservable::to_matrix Aug 20, 2026
@davidfcohen davidfcohen changed the title [WIP] Implement SparseObservable::to_matrix Implement SparseObservable::to_matrix Aug 21, 2026
@davidfcohen
davidfcohen marked this pull request as ready for review August 21, 2026 18:08
@davidfcohen
davidfcohen requested a review from a team as a code owner August 21, 2026 18:08
@qiskit-bot

Copy link
Copy Markdown
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@davidfcohen davidfcohen added this to the 2.6.0 milestone Aug 21, 2026
@davidfcohen davidfcohen removed their assignment Aug 21, 2026
@davidfcohen davidfcohen changed the title Implement SparseObservable::to_matrix [WIP] Implement SparseObservable::to_matrix Aug 24, 2026
@coveralls

coveralls commented Aug 25, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 33183909265

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage increased (+0.009%) to 87.76%

Details

  • Coverage increased (+0.009%) from the base build.
  • Patch coverage: 6 uncovered changes across 1 file (211 of 217 lines covered, 97.24%).
  • 40 coverage regressions across 3 files.

Uncovered Changes

File Changed Covered %
crates/quantum_info/src/sparse_observable/matrix.rs 203 197 97.04%
Total (2 files) 217 211 97.24%

Coverage Regressions

40 previously-covered lines in 3 files lost coverage.

File Lines Losing Coverage Coverage
crates/providers/src/tensor.rs 30 94.93%
crates/providers/src/program_node.rs 7 67.86%
crates/qasm2/src/lex.rs 3 92.29%

Coverage Stats

Coverage Status
Relevant Lines: 131296
Covered Lines: 115226
Line Coverage: 87.76%
Coverage Strength: 982449.7 hits per line

💛 - Coveralls

@davidfcohen davidfcohen changed the title [WIP] Implement SparseObservable::to_matrix [WIP] SparseObservable::to_matrix with Python API Aug 26, 2026
@davidfcohen davidfcohen added Changelog: Added Add an "Added" entry in the GitHub Release changelog. and removed Changelog: None Do not include in the GitHub Release changelog. labels Aug 26, 2026
@davidfcohen davidfcohen changed the title [WIP] SparseObservable::to_matrix with Python API [WIP] SparseObservable.to_matrix backed by Rust Aug 26, 2026
@davidfcohen davidfcohen changed the title [WIP] SparseObservable.to_matrix backed by Rust SparseObservable.to_matrix backed by Rust Aug 26, 2026
@davidfcohen davidfcohen changed the title SparseObservable.to_matrix backed by Rust Implement SparseObservable.to_matrix Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: Added Add an "Added" entry in the GitHub Release changelog. mod: quantum info Related to the Quantum Info module (States & Operators) Rust This PR or issue is related to Rust code in the repository

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

Implement SparseObservable.to_matrix

3 participants