You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.]]
`
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 expressiona+1
is run, but it is. That is to say,a+1
has the same effect asa+=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
Code Output
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 ofa
is modified without the code ever explicitly reassigninga
. 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
Additional Context
No response
The text was updated successfully, but these errors were encountered: