-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
base: main
Are you sure you want to change the base?
Conversation
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 12635149746Warning: 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
💛 - Coveralls |
There was a problem hiding this 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 thinkpub(crate)
just indicates that a function is defined in the wrong file. In many others, since this PR is looking to a future whenSparseObservable
is consumable by non-Qiskit crates directly from Rust, I suspect that anything that becamepub(crate)
should be either private or fullypub
. If it's useful for the Python wrapper, feels highly likely it ought to be a proper public interface.
@@ -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> { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🙂
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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)
#[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 | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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__
😄
releasenotes/notes/update-sparse-observable-error-2bb4b9e678675eee.yaml
Outdated
Show resolved
Hide resolved
Thanks for the comments! I think they all make sense but I'll read them more carefully next year as well 🙂 Regarding
To me, having a separate crate (I assume into |
btw should Max be a co-author, or is this all you so far? |
- avoid using &Vec - add unsafe to mutable indices/boundaries - rm upgrade reno
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)
This was my bit so far, since I was reviewing the |
Summary
Closes #13594 to prepare for
SparseObservable
's C API. This change has been tested with our basic C API forSparseObservable
, 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 aPySparseObservable
, which serves as Python interface. As suggested in #13391, the Python interface keeps anArc
to a read-write-lockedSparseObservable
. TheAPI Change
label is only due to some minuscule change in an error message, the Python interface remains unchanged.The implementation is based on
and methods on
PySparseObservable
first acquire the read- or write-lock to perform actions on theinner
data. For example, implementingtranspose
becomesSome notes/questions:
SparseTerm
we analogously split offPySparseTerm
, since it can be returned to Python. The view/mutable view versions are not returned to Python and don't need a specific interface.IntoPy
toPoisonError
(coming from RwLock::read/write), so as solution we introduced customInnerReadError
s andInnerWriteError
s.pymethods
into the core Rust object and restricted direct access to the inner data, in favor of using public getters/methods.SparseObservable
docstring is moved to the Python interface for now, though we might want to add a bit more Rust-specific info.