Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SparseObservable.compose #13766

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jakelishman
Copy link
Member

@jakelishman jakelishman commented Jan 30, 2025

Summary

This adds matrix multiplication between SparseObservables (or one SparseObservable and anything that can be coerced to an observable), using the same signature as similar methods in quantum_info.

The current implementation attempts to be relatively memory efficient, though there are specialisations that may be implemented if we wanted to be faster:

  • if all the contained terms are Paulis, so each pairwise multiplication produces either zero or one alphabet letters, we currently have a redundant copy-in and copy-out of the Cartesian-product generator that effectively doubles the overhead. We could check whether each term contains only Paulis once, and then avoid that overhead in those cases.

  • alternatively, the Cartesian-product iterator could be taught to use the buffers of the output SparseObservable directly as its output space, rather than requiring copy-out of its buffers, which would remove the overhead from all calls, not just the single-Pauli ones, but requires a bit more bookkeeping, since the references would keep need to be updated.

  • the case of qargs being not None and front=True currently involves an extra copy step to simplify the logic. If this is an important case, we could add a right-matmul specialisation of compose_map.

Details and comments

This commit currently does not contain tests, because we currently don't have suitable methods to test the validity of the output without coupling to the particular choice of factorisation of the matrix multiplication. We need either a way to convert to a Pauli-only representation (with the exponential explosion that entails) or to a matrix; either of these can be uniquely canonicalised.

WIP until it has tests.

Close #13390

This adds matrix multiplication between `SparseObservable`s (or one
`SparseObservable` and anything that can be coerced to an observable),
using the same signature as similar methods in `quantum_info`.

The current implementation attempts to be relatively memory efficient,
though there are specialisations that may be implemented if we wanted to
be faster:

- if all the contained terms are Paulis, so each pairwise multiplication
  produces either zero or one alphabet letters, we currently have a
  redundant copy-in and copy-out of the Cartesian-product generator that
  effectively doubles the overhead.  We could check whether each term
  contains only Paulis once, and then avoid that overhead in those
  cases.

- alternatively, the Cartesian-product iterator could be taught to use
  the buffers of the output `SparseObservable` directly as its output
  space, rather than requiring copy-out of its buffers, which would
  remove the overhead from all calls, not just the single-Pauli ones,
  but requires a bit more bookkeeping, since the references would keep
  need to be updated.

- the case of `qargs` being not `None` and `front=True` currently
  involves an extra copy step to simplify the logic.  If this is an
  important case, we could add a right-matmul specialisation of
  `compose_map`.

This commit currently does not contain tests, because we currently don't
have suitable methods to test the validity of the output without
coupling to the particular choice of factorisation of the matrix
multiplication.  We need either a way to convert to a Pauli-only
representation (with the exponential explosion that entails) or to a
matrix; either of these can be uniquely canonicalised.
@jakelishman jakelishman added Changelog: New Feature Include in the "Added" section of the changelog mod: quantum info Related to the Quantum Info module (States & Operators) labels Jan 30, 2025
@jakelishman jakelishman added this to the 2.0.0 milestone Jan 30, 2025
Before Rust 1.83, `const` functions can't borrow `static`s, even
immutably and without interior mutability.o

There's no need for `matmul` to be `const`; it's always inlined anyway,
and `matmul_generate` can be used in `const` contexts.
@coveralls
Copy link

Pull Request Test Coverage Report for Build 13058536869

Details

  • 0 of 424 (0.0%) changed or added relevant lines in 2 files are covered.
  • 31 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.4%) to 88.377%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/sparse_observable/lookup.rs 0 142 0.0%
crates/accelerate/src/sparse_observable/mod.rs 0 282 0.0%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 94.23%
crates/qasm2/src/lex.rs 6 91.48%
crates/qasm2/src/parse.rs 24 96.22%
Totals Coverage Status
Change from base Build 13055798857: -0.4%
Covered Lines: 79743
Relevant Lines: 90230

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog mod: quantum info Related to the Quantum Info module (States & Operators)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement SparseObservable.compose
3 participants