# Test and benchmarks of expectation value calculation in QuTiP¶

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

In [1]:
from qutip import *

In [2]:
from qutip.cyQ.spmatfuncs import cy_expect

In [3]:
N = 50

In [4]:
op = rand_herm(N)
ket = rand_ket(N)
rho = ket2dm(ket)

ket_data = ket.full()
op_data = op.data

In [5]:
sop = spre(op)
sop_data = sop.data

In [6]:
rho_vec = operator_to_vector(rho)
rho_vec_data = rho_vec.full()


## Test for consistent results¶

Test various ways, from high level to low level, of calculating expectation values in qutip.

In [7]:
expect(op, ket)

Out[7]:
-7.758978511084069
In [8]:
expect(op, rho)

Out[8]:
-7.75897851108407
In [9]:
cy_expect(op_data.data, op_data.indices, op_data.indptr, ket_data, 1)

Out[9]:
-7.758978511084069
In [10]:
expect_psi(op_data, ket_data, 1)

Out[10]:
-7.758978511084069
In [11]:
expect_rho_vec(sop_data, rho_vec_data, 1)

Out[11]:
-7.75897851108407

## Benchmarks¶

### wavefunction¶

In [12]:
%%timeit

expect(op, ket)

1000 loops, best of 3: 688 µs per loop

In [13]:
%%timeit

cy_expect(op_data.data, op_data.indices, op_data.indptr, ket_data, 1)

10000 loops, best of 3: 34 µs per loop

In [14]:
%%timeit

expect_psi(op_data, ket_data, 1)

10000 loops, best of 3: 30.2 µs per loop


### density matrix¶

In [15]:
%%timeit

expect(op, rho)

1000 loops, best of 3: 802 µs per loop

In [16]:
%%timeit

expect_rho_vec(sop_data, rho_vec_data)

1000 loops, best of 3: 640 µs per loop


### Software versions¶

In [17]:
from qutip.ipynbtools import version_table

version_table()

Out[17]:
SoftwareVersion
matplotlib1.4.x
Numpy1.8.0.dev-75cdf3d
Python3.3.1 (default, Apr 17 2013, 22:30:32) [GCC 4.7.3]
IPython1.0.dev
OSposix [linux]
SciPy0.13.0.dev-d74fd00
QuTiP2.3.0.dev-7f0ed30
Cython0.19.1
Thu Jul 04 15:50:58 2013 JST
In [ ]: