Skip to content

Qobj: Deprecate Qobj @ numpy.array#2829

Merged
Ericgig merged 5 commits into
qutip:masterfrom
mudit06mah:fix/__rmatmul()__
Apr 22, 2026
Merged

Qobj: Deprecate Qobj @ numpy.array#2829
Ericgig merged 5 commits into
qutip:masterfrom
mudit06mah:fix/__rmatmul()__

Conversation

@mudit06mah

@mudit06mah mudit06mah commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

Description
This PR deprecates Qobj @ numpy.array operation.

Related issues or PRs
fixes #2547

Steps to test
Code:

import qutip
import numpy as np

A = qutip.Qobj([[0, 1], [1, 0]])
C = np.array([[0, 1], [1, 0]])

print(A @ C)

Output:

/home/mochi/Projects/qutip/temp.py:7: DeprecationWarning: Support for Qobj @ numpy.array has been deprecated and will be removed in qutip 5.4. Please use Qobj(A) @ B instead.
  print(A @ C)
Quantum object: dims=[[2], [2]], shape=(2, 2), type='oper', dtype=Dense, isherm=True
Qobj data =
[[1. 0.]
 [0. 1.]]

@pmenczel pmenczel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for the contribution. Other than the comment below, this seems like a good addition. (@Ericgig is there any reason not to add this / why it hasn't been added yet?)

Note also, the changelog file should not have the ".rst" ending; that's why the documentation build is failing.

Comment thread qutip/core/qobj.py Outdated
)

def __rmatmul__(self, other) -> Qobj:
if not isinstance(other, Qobj):

@pmenczel pmenczel Mar 6, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This condition should always be true: if other were a Qobj, then it would have tried other.__matmul__(self) first. I would suggest to just remove the condition.

@Ericgig

Ericgig commented Mar 6, 2026

Copy link
Copy Markdown
Member

Do we want to encourage / officially support operations between Qobj and ndarray?

If we do, what about Qobj + ndarray, Qobj - ndarray, Qobj & ndarray, etc.
Also tests should be added for all new officially supported operations...

If not, should we remove it from __matmul__?

@mudit06mah mudit06mah requested a review from pmenczel March 8, 2026 19:30
@pmenczel

Copy link
Copy Markdown
Member

Do we want to encourage / officially support operations between Qobj and ndarray?

It's an a bit difficult question. In principle, I would be for removing it altogether, but I don't know if that could be a breaking change for users. However, having it only for the one order of operands is definitely weird. @Ericgig what do you think is best?

(And sorry @mudit06mah that #2547 was maybe somewhat misleading for an open issue.)

@mudit06mah

Copy link
Copy Markdown
Contributor Author

(And sorry @mudit06mah that #2547 was maybe somewhat misleading for an open issue.)

No worries, Since there is still need for changes (whether to add support or remove it completely) so I don't think it was invalid to keep it open.
Once a decision has been made I'll make changes accordingly 👾

@mudit06mah

Copy link
Copy Markdown
Contributor Author

@Ericgig @pmenczel Any Updates on this?

@Ericgig

Ericgig commented Mar 17, 2026

Copy link
Copy Markdown
Member

Personally I am for not supporting any operations with numpy array with the present Qobj.

But for this kind of design decision, it will be better to discuss with the other qutip developers, we will meet in a few weeks.

Comment thread doc/changes/2829.bugfix Outdated
@@ -0,0 +1 @@
Added a __rmatmul__() function to Qobj class to successfully handle numpy.ndarray @ Qobj operations

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it be considered a bug fix or a feature, though?

@veronikakurth veronikakurth added the core-design-review Affects core QuTiP design and needs prior maintainer discussion before work or merge label Apr 8, 2026
@veronikakurth

Copy link
Copy Markdown
Contributor

Personally I am for not supporting any operations with numpy array with the present Qobj.

But for this kind of design decision, it will be better to discuss with the other qutip developers, we will meet in a few weeks.

I added a new label (core-design-review) to this PR and a related issue for us to track such topics that require a core maintainers discussion before the actual work starts or changes get merged. So that we don't lose them out of sight.

@hodgestar

Copy link
Copy Markdown
Contributor

@mudit06mah: Thank you for raising this issue. As mentioned in #2547 we decided to deprecate support for numpy arrays in @ and ask users to do Qobj(A) @ B if A is a numpy array.

You're welcome to make a PR for the deprecation if you like, otherwise one of the admin team will get to it.

@mudit06mah

Copy link
Copy Markdown
Contributor Author

You're welcome to make a PR for the deprecation if you like, otherwise one of the admin team will get to it.

Sure, I'd change this PR only to deprecate it instead :)

@mudit06mah mudit06mah changed the title fix: qobj: Add __rmatmul__ function for consistency Qobj: Deprecate Qobj @ numpy.array Apr 15, 2026
@mudit06mah

Copy link
Copy Markdown
Contributor Author

@hodgestar @Ericgig @pmenczel @veronikakurth I have changed this PR to deprecate the operation instead :D PTAL

Comment thread qutip/core/qobj.py Outdated
if isinstance(other, np.ndarray):
warnings.warn(
"Support for Qobj @ numpy.array has been deprecated "
"and will be removed in qutip 5.4. "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It might be a bit of a short timeline for deprecation; @Ericgig would you say it would be better to communicate that the support will be removed in "qutip 5.5 or later"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have changed the warning message, Could you please review it again?

Comment thread qutip/core/qobj.py Outdated
"Support for Qobj @ numpy.array has been deprecated "
"and will be removed in qutip 5.5 or later. "
"Please use Qobj(A) @ B instead.",
DeprecationWarning,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's better to use FutureWarning, DeprecationWarning are hidden per default so the average user will miss them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see, I changed the stack level to 2 to make the DeprecationWarning visible earlier. I have changed it to FutureWarning now.

@Ericgig Ericgig added this to the QuTiP 5.3 milestone Apr 20, 2026
@Ericgig Ericgig merged commit f020adb into qutip:master Apr 22, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-design-review Affects core QuTiP design and needs prior maintainer discussion before work or merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Qobj multiplication overloading yields inconsistent results when combined with numpy objects

5 participants