qutip / qutip-tensorflow

TensorFlow linear-algebra backend for QuTiP
BSD 3-Clause "New" or "Revised" License
15 stars 9 forks source link

QObj Backwards Compatibility Issues #37

Closed HarshBabla99 closed 2 years ago

HarshBabla99 commented 2 years ago

Some issues with using some basic regular qutip commands within a QObj defined with qutip_tensorflow.

Example 1: QObj.unit()

import tensorflow as tf
import qutip as qp
import numpy as np
import qutip_tensorflow as qtf

g = qp.basis(2,0).to('TfTensor128')
e = qp.basis(2,1).to('TfTensor128')

plus = (g+e).unit()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
----> 9: plus = (g+e).unit()
File ....\lib\site-packages\qutip\core\qobj.py:1062, in Qobj.unit(self, inplace, norm, kwargs)
-> 1062     out = self / norm
File ...\lib\site-packages\tensorflow\python\util\traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
--> 153   raise e.with_traceback(filtered_tb) from None
File ...\lib\site-packages\tensorflow\python\framework\constant_op.py:102, in convert_to_eager_tensor(value, ctx, dtype)
--> 102 return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: TypeError: object of type 'Qobj' has no len()

Example 2: Trace QObj.tr()

import tensorflow as tf
import qutip as qp
import numpy as np
import qutip_tensorflow as qtf

g = qp.basis(2,0).to('TfTensor128')
e = qp.basis(2,1).to('TfTensor128')

plus = (g+e)/np.sqrt(2)

plus_dm = qp.ket2dm(plus)

print(plus_dm.tr())
 ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
---> 13 print(plus_dm.tr())
File ...\lib\site-packages\qutip\core\qobj.py:788, in Qobj.tr(self)
--> 788 return out.real if self.isherm else out
File ...\lib\site-packages\tensorflow\python\framework\ops.py:446, in Tensor.__getattr__(self, name)
--> 446 self.__getattribute__(name)
AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'real'

_Versions:

Paths from the traceback data has been redacted for privacy

AGaliciaMartinez commented 2 years ago

Thanks @HarshBabla99 for reporting this! qutip-tensorflow is still in a very experimental stage so I would expect more of this in the future. I will make a PR solving this issue soon though.

AGaliciaMartinez commented 2 years ago

This should be entirely fixed now. Thanks again for filling the issue!