Speeding up tensor and ptrace

63 views
Skip to first unread message

spac...@googlemail.com

unread,
Mar 14, 2017, 6:28:09 AM3/14/17
to QuTiP: Quantum Toolbox in Python
Hi,
I need to integrate a nonlinear equation (a mean field master equation) and use the scipy.integrate library. In this nonlinear equation I use qutip functions like
q.tensor([rho0,rho1])
and
q.ptrace
repeatedly with density matrices around (100x100).

My question is: How can I speed these function calls (tensor and ptrace) up?

Timing results:
For the linear part of the master equation I use
dotrho = cy_ode_rhs(0,initial_vector, L.data.data, L.data.indices, L.data.indptr  )
which takes less than a millisecond for me.
Whereas building the liouvillian beforehand takes soemthing like 80ms.
L = liouvillian(fulldict["H"], fulldict["c_ops"])
The tensor and ptrace commands take each around 2ms, which makes my whole integration very slow. Can I somehow speed these functions up?



Paul Nation

unread,
Mar 14, 2017, 6:49:16 AM3/14/17
to QuTiP Group
Tensor an ptrace are mostly c++ code, so not much to do there.

-P


--
You received this message because you are subscribed to the Google Groups "QuTiP: Quantum Toolbox in Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qutip+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex Pitchford

unread,
Mar 14, 2017, 6:56:38 AM3/14/17
to qu...@googlegroups.com
Firstly, can I check are you using the latest release 4.1.0? There have been many speed ups (inc ptrace) in this release.

Second, are the dimensions and sel the same for each ptrace? I made a dense ptrace function a while ago due to similar issues, in which the permutation array is reused. That is the perm is only calculated once, and this is the typically the most expensive bit. I have not got round to adding this to qutip yet, but this could be the motivation I need.

If your matrices are dense, then probably you would be better to maintain them as such, i.e. as ndarray. Use the scipy kron for the tensor. Then create the Qobj when you need it for cy_ode_rhs. There is also a discussion about how much we could do with a dense Qobj. QuTiP is optimised for sparse matrix operations.

spac...@googlemail.com

unread,
Mar 14, 2017, 7:11:54 AM3/14/17
to QuTiP: Quantum Toolbox in Python
Sadly I cannot upgrade the qutip version which is installed on our computer cluster. Currently the version is qutip/3.1.0-Python-2.7.11. But I can copy the latest qutip into my directory and try importing qutip from there.

Yes, same dimensions of all matrices. (For now.. later it may be different).

I currently use qutip to create my operators and density matrices (it's so incredibly convenient). I didn't know scipy's kron function. I'll check it out.
I just played around with numpy and noticed it isn't much faster than qutip (my local machine qutip 4.0.1). So there is probably really nothing to gain....  but I will compare the speed to qutip 3.1.0

def nptensor2matrix(t1,t2):
   
return np.tensordot(t1.full(),t2.full(),axes=0).swapaxes(1,2).reshape([t1.shape[0]*t2.shape[0],-1])


1.0159999999999059 ms for Timer: qutip 
0.6139999999996704 ms for Timer: np 
Message has been deleted

spac...@googlemail.com

unread,
Mar 14, 2017, 7:15:26 AM3/14/17
to QuTiP: Quantum Toolbox in Python
Scipy is slower:
sM = scipy.kron(t1.full(),t2.full())
1.5479999999996608 ms for Timer: scipy 

Paul Nation

unread,
Mar 14, 2017, 7:29:11 AM3/14/17
to QuTiP Group
Yes, dense kron is slow.  You really need to update to 4.1.  Both tensor and ptrace are much faster.

spac...@googlemail.com

unread,
Mar 14, 2017, 7:39:33 AM3/14/17
to QuTiP: Quantum Toolbox in Python
I'll do that. Thanks!
Message has been deleted
Message has been deleted

spac...@googlemail.com

unread,
Mar 14, 2017, 8:31:37 AM3/14/17
to QuTiP: Quantum Toolbox in Python
I'm trying to import qutip (git) from the local directory (i do not have root rights) with
import sys
sys
.path.append('qutip')
sys
.path.append('qutip/qutip/cy')
import qutip  
but I  get the error
ImportError: No module named spmatfuncs
and after running
import qutip  
again I get
 import qutip 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "qutip/qutip/__init__.py", line 49, in <module>
    qutip.settings.ipython = False
AttributeError: 'module' object has no attribute 'settings'
I'm at a loss how to fix this....

Alex Pitchford

unread,
Mar 14, 2017, 8:53:23 AM3/14/17
to qu...@googlegroups.com
We would recommend running qutip under anaconda or miniconda. This can be installed in your user area - no sudo required.  see:
You then have full control over your Python environment.

On 14 March 2017 at 12:07, space2178 via QuTiP: Quantum Toolbox in Python <qu...@googlegroups.com> wrote:
hm... I downloaded qutip 4.1 and build it in qutip/build/lib.linux-x86_64-3.5/qutip/. However I'm at a loss on how to import qutip located in a folder in python... Do you have any idea?  I do not have root rights on that machine... otherwise I could install it.

spac...@googlemail.com

unread,
Mar 14, 2017, 9:21:21 AM3/14/17
to QuTiP: Quantum Toolbox in Python
great!. Thx
Reply all
Reply to author
Forward
0 new messages