QuTiP test notebook: Test sparse/dense eigenvalue/eigenvector solvers

Copyright (C) 2011 and later, Paul D. Nation & Robert J. Johansson


In [1]:
from qutip import *

In [2]:
N = 15
H = rand_dm(N) - rand_dm(N)

Test groundstate


In [3]:
H.isherm = False
e, eket1 = H.groundstate()

In [4]:
e


Out[4]:
(-0.18977307533043747-2.9509217660989655e-18j)

In [5]:
eket1


Out[5]:
\begin{equation}\text{Quantum object: dims = [[15], [1]], shape = [15, 1], type = ket}\\[1em]\begin{pmatrix}(0.191-0.082j)\\(0.073+0.066j)\\(-0.065+0.026j)\\(0.073+0.102j)\\(0.146-0.026j)\\\vdots\\(0.052+0.082j)\\(-0.050+0.026j)\\(0.065+0.072j)\\(0.036+0.046j)\\(0.230+0.069j)\\\end{pmatrix}\end{equation}

In [6]:
(H * eket1).full() / (eket1).full()


Out[6]:
array([[-0.18977308 +3.06343067e-17j],
       [-0.18977308 +3.51629507e-16j],
       [-0.18977308 -4.59495590e-16j],
       [-0.18977308 +1.12369136e-16j],
       [-0.18977308 +3.56273230e-16j],
       [-0.18977308 +9.82113592e-16j],
       [-0.18977308 -5.61321466e-17j],
       [-0.18977308 -4.87026157e-17j],
       [-0.18977308 -4.22344870e-18j],
       [-0.18977308 +8.26147250e-17j],
       [-0.18977308 +2.40104793e-16j],
       [-0.18977308 -6.85834186e-17j],
       [-0.18977308 +1.85102289e-16j],
       [-0.18977308 -1.04807234e-16j],
       [-0.18977308 +1.45586550e-16j]])

In [7]:
H.isherm = True
e, eket2 = H.groundstate()

In [8]:
e


Out[8]:
-0.18977307533043666

In [9]:
eket2


Out[9]:
\begin{equation}\text{Quantum object: dims = [[15], [1]], shape = [15, 1], type = ket}\\[1em]\begin{pmatrix}-0.208\\(-0.041-0.090j)\\(0.070+0.001j)\\(-0.027-0.122j)\\(-0.145-0.034j)\\\vdots\\(-0.016-0.096j)\\(0.056-0.004j)\\(-0.032-0.092j)\\(-0.015-0.056j)\\(-0.184-0.154j)\\\end{pmatrix}\end{equation}

In [10]:
(H * eket2).full() / (eket2).full()


Out[10]:
array([[-0.18977308 -3.90661525e-18j],
       [-0.18977308 +1.59521622e-17j],
       [-0.18977308 -2.28336412e-16j],
       [-0.18977308 -1.48759565e-16j],
       [-0.18977308 +7.39105708e-17j],
       [-0.18977308 -7.78740807e-17j],
       [-0.18977308 +9.45921075e-18j],
       [-0.18977308 -0.00000000e+00j],
       [-0.18977308 +3.01179206e-17j],
       [-0.18977308 +4.11468964e-17j],
       [-0.18977308 -3.07366693e-17j],
       [-0.18977308 +5.38194859e-17j],
       [-0.18977308 -8.44177005e-18j],
       [-0.18977308 -1.21824782e-16j],
       [-0.18977308 +6.66355559e-17j]])

In [11]:
eket1.full() / eket2.full()


Out[11]:
array([[-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j],
       [-0.91894328+0.39438973j]])

In [12]:
abs(eket1.full() / eket2.full()).T


Out[12]:
array([[ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.]])

Test eigenstates/eigenvectors


In [13]:
evals, evecs = la.eig(H.full())

In [14]:
evals


Out[14]:
array([ 0.20215382 -5.58621559e-18j, -0.18977308 -2.95092177e-18j,
       -0.10949756 -3.71245315e-18j,  0.09767662 -3.63361751e-18j,
       -0.08568833 +6.26299109e-19j,  0.07009392 -6.81681939e-19j,
        0.05621554 +4.39620683e-18j,  0.05059221 -1.25861075e-19j,
       -0.04620278 -3.34553216e-18j,  0.02336572 +2.85964480e-18j,
        0.01197180 -1.84446575e-18j, -0.03899531 +8.06584941e-18j,
       -0.00339483 -5.50447036e-18j, -0.01228244 +1.31656907e-18j,
       -0.02623532 -8.25657673e-18j])

Test eigenenergies with eigenvectors


In [15]:
H.eigenstates()[0]


Out[15]:
array([-0.18977308, -0.10949756, -0.08568833, -0.04620278, -0.03899531,
       -0.02623532, -0.01228244, -0.00339483,  0.0119718 ,  0.02336572,
        0.05059221,  0.05621554,  0.07009392,  0.09767662,  0.20215382])

hermitian


In [16]:
H.isherm = True

In [17]:
H.eigenstates(sparse=False, eigvals=5, sort='low')[0]


Out[17]:
array([-0.18977308, -0.10949756, -0.08568833, -0.04620278, -0.03899531])

In [18]:
H.eigenstates(sparse=False, eigvals=5, sort='high')[0]


Out[18]:
array([ 0.20215382,  0.09767662,  0.07009392,  0.05621554,  0.05059221])

In [19]:
H.eigenstates(sparse=True, eigvals=5, sort='low')[0]


Out[19]:
array([-0.18977308, -0.10949756, -0.08568833, -0.04620278, -0.03899531])

In [20]:
H.eigenstates(sparse=True, eigvals=5, sort='high')[0]


Out[20]:
array([ 0.20215382,  0.09767662,  0.07009392,  0.05621554,  0.05059221])

nonhermitian


In [21]:
H.isherm = False

In [22]:
H.eigenstates(sparse=False, eigvals=5, sort='low')[0]


Out[22]:
array([-0.18977308 -2.95092177e-18j, -0.10949756 -3.71245315e-18j,
       -0.08568833 +6.26299109e-19j, -0.04620278 -3.34553216e-18j,
       -0.03899531 +8.06584941e-18j])

In [23]:
H.eigenstates(sparse=False, eigvals=5, sort='high')[0]


Out[23]:
array([ 0.20215382 -5.58621559e-18j,  0.09767662 -3.63361751e-18j,
        0.07009392 -6.81681939e-19j,  0.05621554 +4.39620683e-18j,
        0.05059221 -1.25861075e-19j])

In [24]:
H.eigenstates(sparse=True, eigvals=5, sort='low')[0]


Out[24]:
array([-0.18977308 -1.67208499e-18j, -0.10949756 +1.88908328e-18j,
       -0.08568833 +2.83895112e-18j, -0.04620278 -3.94181456e-18j,
       -0.03899531 +4.98626434e-19j])

In [25]:
H.eigenstates(sparse=True, eigvals=5, sort='high')[0]


Out[25]:
array([ 0.20215382 +3.19228800e-18j,  0.09767662 -2.00871820e-18j,
        0.07009392 -3.77697834e-18j,  0.05621554 -6.74360270e-19j,
        0.05059221 -4.09379031e-18j])

Test eigenenergies

hermitian


In [26]:
H.isherm = True

In [27]:
H.eigenenergies(sparse=False, eigvals=5, sort='low')


Out[27]:
array([-0.18977308, -0.10949756, -0.08568833, -0.04620278, -0.03899531])

In [28]:
H.eigenenergies(sparse=False, eigvals=5, sort='high')


Out[28]:
array([ 0.20215382,  0.09767662,  0.07009392,  0.05621554,  0.05059221])

In [29]:
H.eigenenergies(sparse=True, eigvals=5, sort='low')


Out[29]:
array([-0.18977308, -0.10949756, -0.08568833, -0.04620278, -0.03899531])

In [30]:
H.eigenenergies(sparse=True, eigvals=5, sort='high')


Out[30]:
array([ 0.20215382,  0.09767662,  0.07009392,  0.05621554,  0.05059221])

nonhermitian


In [31]:
H.isherm = False

In [32]:
H.eigenenergies(sparse=False, eigvals=5, sort='low')


Out[32]:
array([-0.18977308 -2.95092177e-18j, -0.10949756 -3.71245315e-18j,
       -0.08568833 +6.26299109e-19j, -0.04620278 -3.34553216e-18j,
       -0.03899531 +8.06584941e-18j])

In [33]:
H.eigenenergies(sparse=False, eigvals=5, sort='high')


Out[33]:
array([ 0.20215382 -5.58621559e-18j,  0.09767662 -3.63361751e-18j,
        0.07009392 -6.81681939e-19j,  0.05621554 +4.39620683e-18j,
        0.05059221 -1.25861075e-19j])

In [34]:
H.eigenenergies(sparse=True, eigvals=5, sort='low')


Out[34]:
array([-0.18977308 -9.46065488e-19j, -0.10949756 +1.24212913e-18j,
       -0.08568833 -1.67611217e-18j, -0.04620278 +1.65185784e-19j,
       -0.03899531 -1.30488484e-19j])

In [35]:
H.eigenenergies(sparse=True, eigvals=5, sort='high')


Out[35]:
array([ 0.20215382 -1.48162126e-18j,  0.09767662 -1.92787564e-18j,
        0.07009392 -3.92691747e-18j,  0.05621554 +2.39413867e-18j,
        0.05059221 +1.38973175e-18j])

Software versions


In [36]:
from qutip.ipynbtools import version_table

version_table()


Out[36]:
SoftwareVersion
Cython0.19-dev
SciPy0.13.0.dev-38ad5d2
QuTiP2.3.0.dev-75caa0a
Python2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2]
IPython0.13
OSposix [linux2]
Numpy1.8.0.dev-bd7104c
matplotlib1.3.x
Tue Apr 16 17:08:37 2013 JST