-
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 12465601613Details
💛 - 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 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 becomepub(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.
/// A single term from a complete :class:`SparseObservable`. | ||
/// | ||
/// These are typically created by indexing into or iterating through a :class:`SparseObservable`. | ||
#[pyclass(name = "Term", frozen, module = "qiskit.quantum_info")] | ||
#[derive(Clone, Debug)] | ||
struct PySparseTerm { | ||
// This class keeps a pointer to a pure Rust-SparseTerm and serves as interface from Python. | ||
// Note that we can use the standard clone, since the PySparseTerm is immutable from Python | ||
// space, hence we can just clone the pointer. Otherwise we'd have to impl Clone manually, | ||
// like for the PySparseObservable. | ||
inner: Arc<RwLock<SparseTerm>>, | ||
} |
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.
Why does PySparseTerm
need to include an Arc<RwLock<SparseTerm>>
, rather than just being a wrapper around SparseTerm
?
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.
You mean because it's immutable anyways?
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.
I can't imagine a situation where there'd be shared ownership of an underlying SparseTerm
- the only reason the Arc<RwLock<T>>
is useful for SparseObservable
is because of the shallow-copy qargs
-after-__call__
behaviour of the QI classes (so strictly it's not even needed for this PR).
let inner = SparseObservable::zero(num_qubits); | ||
Self { | ||
inner: Arc::new(RwLock::new(inner)), | ||
} |
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.
In normal situations, this kind of thing is what the IntoPy
/IntoPyObject
trait is for. I think we can do that in a coherent manner, even with a clean crate structure, since IntoPyObject
takes a type argument that will be Py<PySparseObservable>
.
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.
Ah yes, this is actually one of the to-dos we didn't try in the end, that would be much nicer 🙂
// get the number of qubits in the layout and map the layout to Vec<u32> to | ||
// call SparseObservable.apply_layout | ||
let (num_qubits, layout): (u32, Option<Vec<u32>>) = if layout.is_none() { | ||
// if the layout is none, | ||
(num_qubits.unwrap_or(inner.num_qubits()), None) |
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.
Some of the comments left around the file seem like they're old helper reminders? Might be good to run through and clean them up.
// we acquire the read lock once here and use the internal methods | ||
// to avoid checking and acquiring the lock in every method call | ||
let inner = self.inner.read().map_err(|_| InnerReadError)?; |
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 comment is helpful to you, feel free to leave it, but this is really the base expectation for how to handle the RwLock
- it would be weird to keep locking and unlocking.
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.
I'll remove it, it was mainly helpful for my own sanity starting to use these objects 😜
|
||
impl Clone for PySparseObservable { | ||
// we need to implement clone specifically to ensure the underlying inner data | ||
// is cloned, not the reference to it | ||
fn clone(&self) -> Self { | ||
let inner = self.inner.read().unwrap(); | ||
Self { | ||
inner: Arc::new(RwLock::new(inner.clone())), | ||
} | ||
} | ||
} |
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.
What's the reason for this implementation? In what situations are you thinking that this will be called from Rust space, and how sure are you that these semantics are always what would be expected? I'm pretty uncomfortable with a Clone
implementation that can panic.
Since this is object intended for consumption only from Python space, perhaps it would be cleaner to override __copy__
and __deepcopy__
, if copy.copy
and copy.deepcopy
aren't doing what you expect (though I think they probably should already work).
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.
The reason is that we didn't think of putting this into __copy__
and we wanted to ensure we clone the object, not the ref 🙂 You're right, it's not really intended to be called from Rust space
(though I think they probably should already work).
I.e. that Python's native __copy__
would already copy the inner
data instead of the reference to it?
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.
copy.copy
and copy.deepcopy
don't need __copy__
etc to be defined - they'll use the pickle-protocol methods (__reduce__
or __getstate__
/__statestate__
) if they're not, and that causes SparseObservable
to integrate with them already. I'm relatively sure I wrote a test.
@@ -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
.
#[error("cannot shrink the qubit count in an observable from {current} to {target}")] | ||
NotEnoughQubits { current: usize, target: usize }, |
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.
I think this is just a straight-up code change? Might be better to do that in a separate PR, if you think it's necessary.
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.
Hmmm I think we added this because it comes up in apply_layout
, which was previously only a Python method which directly triggered a PyErr
. Now that apply_layout
is moved into the Rust methods, we needed a new error -- so it's linked to this PR due to the move of the apply_layout
logic 🙂 (which could ofc also be separated out if you prefer)
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.
Ok sure, if it's actually an important part of the split, it's fine to do here. (Part of the difficulty in reviewing this kind of PR!)
/// costs. If this does not fit your use cases, you can either periodically call :meth:`simplify`, | ||
/// or discuss further APIs with us for better building of observables. | ||
#[pyclass(module = "qiskit.quantum_info", sequence)] | ||
/// See PySparseObservable in crates/accelerate/src/py_sparse_observable for detailed docs. |
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.
Possibley
See
[PySparseObservable]
for detailed docs.
might be clearer that it's just implied you should go find that struct, and less likely for the file to get out of sync.
#[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.
--- | ||
upgrade: | ||
- | | ||
Binary arithmetic operations on :class:`.SparseObservable` have a slightly updated | ||
error message, if the two observables in the operation have incompatible numberss of qubits. | ||
Previously, the resulting ``ValueError`` contained the message | ||
``incompatible numbers of qubits: A and B``, which now is | ||
``mismatched numbers of qubits: A, B``. |
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.
This is good thinking, but I don't think we really need to consider this an interface break.
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.
Ah even better -- I was just being double safe 😉
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? |
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.