-
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
quantum_info.Statevector
methods do not return the expected custom type
#9513
Comments
In simple cases like this, it is possible to update, but I'm not super keen on opening the can of worms that is implicitly making Some of the problems that might well arise:
Those aren't necessarily unsolvable issues, I'm just mentioning them as reasons as to why I'm very nervous about trying to move towards a subclassable (For what it's worth, we actually do subclass |
Ok yes, thank you for the answer. Now I understand the sort of issues that can arise from making |
Introducing symbolic calculation with matrices is a whole other can of worms as well. Honestly, if you're having a play with that sort of thing, I'd probably try building them up as completely separate objects first, and only then maybe start to think about how interop might work - ergonomic and performant (well, as much as can be expected with symbols) can be tricky in their own right. It's quite difficult to correctly manage the multiple-dispatch needed to allow things like binary methods (e.g. |
Thanks again for your help! I'm having a try to make something like |
Environment
What is happening?
I'm working on the definition of a custom class
MyStatevector
which inherits fromqiskit.quantum_info.Statevector
and implements some further functionalities. The problem is that some of theStatevector
methods (e.g. staticmethod, classmethod, but not only) always return aStatevector
instance, no matter what type the original caller was.In general, I don't think this is the desirable and expected behaviour since, as soon as I call one of these methods on a
MyStatevector
instance, Qiskit silently "converts" it back to aStatevector
, preventing me from further using my custom object.How can we reproduce the issue?
As an example, let's consider the
Statevector.conjugate
method, implemented inqiskit.quantum_info
as follows:This is the definition of my custom class:
The following code raises an error:
AttributeError: 'Statevector' object has no attribute 'mymethod'
What should happen?
The code should run with no errors and print the string
'mymethod'
.The type of
conj
is always expected to be the same of the callerpsi
.Any suggestions?
In this simple case, it should be enough to fix the definition of
Statevector.conjugate
as follows:For static/class methods, fixing this could require a bit of work, but I think it could be very useful as well.
The text was updated successfully, but these errors were encountered: