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

Adding number to ket unexpectedly modifies ket data #2208

Closed
vikramkashyap opened this issue Jul 29, 2023 · 2 comments
Closed

Adding number to ket unexpectedly modifies ket data #2208

vikramkashyap opened this issue Jul 29, 2023 · 2 comments

Comments

@vikramkashyap
Copy link

Bug Description

Adding (or subtracting) a number from a ket modifies the data of the ket even without an assignment. In the code below, I would expect that a is not modified when the expression a+1 is run, but it is. That is to say, a+1 has the same effect as a+=1. In contrast, the data of the QObj does not change when adding a number to an operator or when multiplying a ket by a number.

Code to Reproduce the Bug

from qutip import basis
a = basis(2)
print(a)
print()
print(a+1)
print()
print(a)

Code Output

Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket
Qobj data =
[[1.]
 [0.]]

Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket
Qobj data =
[[2.]
 [0.]]

Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket
Qobj data =
[[2.]
 [0.]]

Expected Behaviour

First of all, adding a number to a ket is not a well-defined operation, so perhaps it should throw an error. However, I think that the value of a+1 is not entirely unexpected, so it could be kept. The real issue is that the data of a is modified without the code ever explicitly reassigning a. This is definitely unexpected. I would expect the output to be:

`Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket
Qobj data =
[[1.]
[0.]]

Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket
Qobj data =
[[2.]
[0.]]

Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket
Qobj data =
[[1.]
[0.]]
`

Your Environment

QuTiP Version:      4.7.2
Numpy Version:      1.23.5
Scipy Version:      1.10.0
Cython Version:     0.29.33
Matplotlib Version: 3.6.2
Python Version:     3.10.9
Number of CPUs:     4
BLAS Info:          OPENBLAS
OPENMP Installed:   False

Additional Context

No response

@Ericgig
Copy link
Member

Ericgig commented Jul 31, 2023

Thank you for reporting.

I will fix by having ket + scalar raising an error.

@AGaliciaMartinez
Copy link
Member

Closing the issue, it was solved in #2209. Thanks for reporting the issue @vikramkashyap and for solving it @Ericgig !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants