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

Split PySparseObservable off SparseObservable #13595

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Cryoris
Copy link
Contributor

@Cryoris Cryoris commented Dec 23, 2024

Summary

Closes #13594 to prepare for SparseObservable's C API. This change has been tested with our basic C API for SparseObservable, which will come in a separate PR to keep the review load in balance 🙂

Details and comments

This PR splits the sparse observable class into a Rust-only SparseObservable struct and a PySparseObservable, which serves as Python interface. As suggested in #13391, the Python interface keeps an Arc to a read-write-locked SparseObservable. The API Change label is only due to some minuscule change in an error message, the Python interface remains unchanged.

The implementation is based on

#[pyclass(name = "SparseObservable", ...)]  // exposed as qiskit.quantum_info.SparseObservable, as before
struct PySparseObservable {
    // This class keeps a pointer to a pure Rust-SparseTerm and serves as interface from Python.
    inner: Arc<RwLock<SparseObservable>>,
}

and methods on PySparseObservable first acquire the read- or write-lock to perform actions on the inner data. For example, implementing transpose becomes

    fn transpose(&self) -> PyResult<Self> {
        // acquire the read lock, mapping the PoisonError into our own error that can be cast to a PyErr
        let inner = self.inner.read().map_err(|_| InnerReadError)?;

        // perform the action
        let result = inner.transpose();  
        
        // return a new Arc<RwLock> (if we did an inplace operation, we would just return nothing)
        Ok(Self { inner: Arc::new(RwLock::new(result)) })
    }

Some notes/questions:

  • For SparseTerm we analogously split off PySparseTerm, since it can be returned to Python. The view/mutable view versions are not returned to Python and don't need a specific interface.
  • We couldn't implement IntoPy to PoisonError (coming from RwLock::read/write), so as solution we introduced custom InnerReadErrors and InnerWriteErrors.
  • We moved some methods from the pymethods into the core Rust object and restricted direct access to the inner data, in favor of using public getters/methods.
  • The SparseObservable docstring is moved to the Python interface for now, though we might want to add a bit more Rust-specific info.

@Cryoris Cryoris added Changelog: API Change Include in the "Changed" section of the changelog Rust This PR or issue is related to Rust code in the repository labels Dec 23, 2024
@Cryoris Cryoris added this to the 2.0.0 milestone Dec 23, 2024
@Cryoris Cryoris requested a review from a team as a code owner December 23, 2024 10:50
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@coveralls
Copy link

coveralls commented Dec 23, 2024

Pull Request Test Coverage Report for Build 12635149746

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 1144 of 1204 (95.02%) changed or added relevant lines in 3 files are covered.
  • 34 unchanged lines in 5 files lost coverage.
  • Overall coverage increased (+0.009%) to 88.96%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/sparse_observable.rs 1142 1202 95.01%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/two_qubit_decompose.rs 1 92.13%
crates/accelerate/src/unitary_synthesis.rs 1 92.2%
crates/qasm2/src/lex.rs 2 92.73%
crates/accelerate/src/sparse_observable.rs 10 94.41%
qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py 20 94.58%
Totals Coverage Status
Change from base Build 12420636821: 0.009%
Covered Lines: 79625
Relevant Lines: 89507

💛 - Coveralls

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Thanks for doing this.

This is just a quick high-level overview - I'll look more in detail in the new year, especially since I'll have to use a lot more local tools to do a good comparison - with the file move and changes to the code, it's hard to see what's gone on here.

Top level questions:

  • Why split py_sparse_observable into a separate flat file? I'd have expected any of:

    • keep both in the same file
    • make a sparse_observable module to put them in
    • make a separate crate that contains only the C component

    with a rough preference to just keeping everything in the same file for now. This form to me has meant that a lot of logically private functions have had to become pub(crate), and now there's more places to look to understand the code.

  • For everything that's become pub(crate): in some cases, I think pub(crate) just indicates that a function is defined in the wrong file. In many others, since this PR is looking to a future when SparseObservable is consumable by non-Qiskit crates directly from Rust, I suspect that anything that became pub(crate) should be either private or fully pub. If it's useful for the Python wrapper, feels highly likely it ought to be a proper public interface.

crates/accelerate/src/py_sparse_observable.rs Outdated Show resolved Hide resolved
crates/accelerate/src/py_sparse_observable.rs Outdated Show resolved Hide resolved
crates/accelerate/src/py_sparse_observable.rs Outdated Show resolved Hide resolved
crates/accelerate/src/py_sparse_observable.rs Outdated Show resolved Hide resolved
crates/accelerate/src/py_sparse_observable.rs Outdated Show resolved Hide resolved
@@ -146,7 +126,7 @@ impl BitTerm {
/// returning `Ok(None)` for it. All other letters outside the alphabet return the complete
/// error condition.
#[inline]
fn try_from_u8(value: u8) -> Result<Option<Self>, BitTermFromU8Error> {
pub(crate) fn try_from_u8(value: u8) -> Result<Option<Self>, BitTermFromU8Error> {
Copy link
Member

Choose a reason for hiding this comment

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

If this needs to be pub beyond this module, I don't see much reason to make it pub(crate) and not just pub, looking to a future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We might want to make it pub with some of the C functionality, but I wanted to be as cautious as possible -- we can just make it pub pending the structural question you raised above 🙂

Copy link
Member

Choose a reason for hiding this comment

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

Personally I've seen very few reasons for pub(crate) so far - most of them have either been that the function is misplaced, an API boundary has got very muddled, or they should just be pub. We've had quite a lot of churn turning pub(crate) into pub.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

3a4b5f0 merged the py_sparse_observable.rs file back into sparse_observable.rs so we don't need to touch the visibility of these methods anymore (though we likely will for the C API later)

crates/accelerate/src/sparse_observable.rs Show resolved Hide resolved
crates/accelerate/src/sparse_observable.rs Outdated Show resolved Hide resolved
Comment on lines 385 to 423
#[inline]
pub fn coeffs(&self) -> &Vec<Complex64> {
&self.coeffs
}

#[inline]
pub fn coeffs_mut(&mut self) -> &mut Vec<Complex64> {
&mut self.coeffs
}

#[inline]
pub fn indices(&self) -> &Vec<u32> {
&self.indices
}

#[inline]
pub fn indices_mut(&mut self) -> &mut Vec<u32> {
&mut self.indices
}

#[inline]
pub fn boundaries(&self) -> &Vec<usize> {
&self.boundaries
}

#[inline]
pub fn boundaries_mut(&mut self) -> &mut Vec<usize> {
&mut self.boundaries
}

#[inline]
pub fn bit_terms(&self) -> &Vec<BitTerm> {
&self.bit_terms
}

#[inline]
pub fn bit_terms_mut(&mut self) -> &mut Vec<BitTerm> {
&mut self.bit_terms
}
Copy link
Member

@jakelishman jakelishman Dec 23, 2024

Choose a reason for hiding this comment

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

Returning &Vec<T> is almost never correct; you almost invariably mean &[T] (and you do, in every case here). &mut Vec can be correct in some circumstances, but in this case is very not - all these methods allow the Vec-resizing methods to be called, which easily makes the data incoherent. For ones we definitely need to expose, it should be &mut [T].

boundaries_mut and indices_mut might very well be considered unsafe, since you can easily break data coherence by writing bad values to them. I can't remember what the old code did around this, but if they are actually needed and used, then they may well want to be unsafe in Rust interfaces.

Copy link
Contributor Author

@Cryoris Cryoris Jan 6, 2025

Choose a reason for hiding this comment

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

Oh right, it should definitely not be Vec here -- I'll fix that 🙂

Regarding necessity of these methods: this mutable access is used in ArrayView::__setitem__, which previously had a SparseObservable as base and simply used mutable access to the internals. But now PySparseObservable it the base and we don't have this access anymore. Using unsafe seems fine, but maybe we should add some validation in __setitem__ 😄

@Cryoris
Copy link
Contributor Author

Cryoris commented Dec 23, 2024

Thanks for the comments! I think they all make sense but I'll read them more carefully next year as well 🙂 Regarding

Why split py_sparse_observable into a separate flat file? I'd have expected any of:

  • keep both in the same file
  • make a sparse_observable module to put them in
  • make a separate crate that contains only the C components

with a rough preference to just keeping everything in the same file for now. This form to me has meant that a lot of logically private functions have had to become pub(crate), and now there's more places to look to understand the code.

To me, having a separate crate (I assume into py_ext?) sounds the cleanest, but I didn't want just move it w/o discussion, so I moved it into a separate file to facilitate that process 😛 I'm fine with keeping it in the same file as well for now too, though.

@jakelishman
Copy link
Member

btw should Max be a co-author, or is this all you so far?

@Cryoris
Copy link
Contributor Author

Cryoris commented Jan 6, 2025

With 3a4b5f0, all code is in a single file for now, but it should be easy to separate if we want to later on (basically the lower half is the Python wrapper)

btw should Max be a co-author, or is this all you so far?

This was my bit so far, since I was reviewing the SparseObservable PRs initially I volunteered to do the split 🙂

@Cryoris Cryoris removed the Changelog: API Change Include in the "Changed" section of the changelog label Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust This PR or issue is related to Rust code in the repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Separate SparseObservable into a Rust-only core and Python interface
4 participants