~jrjohansson/qutip/master

« back to all changes in this revision

Viewing changes to qutip/wf_ode_evolve.py

  • Committer: Paul Nation
  • Date: 2011-04-21 04:46:56 UTC
  • Revision ID: git-v1:dd4c966b490aa468dfbd28cef66694df4bf235c8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#This file is part of QuTIP.
 
2
#
 
3
#    QuTIP is free software: you can redistribute it and/or modify
 
4
#    it under the terms of the GNU General Public License as published by
 
5
#    the Free Software Foundation, either version 3 of the License, or
 
6
#   (at your option) any later version.
 
7
#
 
8
#    QuTIP is distributed in the hope that it will be useful,
 
9
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
#    GNU General Public License for more details.
 
12
#
 
13
#    You should have received a copy of the GNU General Public License
 
14
#    along with QuTIP.  If not, see <http://www.gnu.org/licenses/>.
 
15
#
 
16
# Copyright (C) 2011, Paul D. Nation & Robert J. Johansson
 
17
#
 
18
###########################################################################
 
19
 
 
20
from qobj import *
 
21
from scipy.integrate import *
 
22
 
 
23
import random
 
24
 
 
25
# ------------------------------------------------------------------------------
 
26
# Wave function evolution using a ODE solver (unitary quantum evolution)
 
27
 
28
def wf_ode_evolve(tlist, H, psi0, expt_op_list):
 
29
    """!
 
30
    @brief Evolve the wave function using an ODE solver
 
31
    """
 
32
    n_tsteps  = len(tlist)
 
33
    n_expt_op = len(expt_op_list)
 
34
    expt_list = zeros([n_expt_op, n_tsteps])
 
35
    dt        = tlist[1]-tlist[0]
 
36
 
 
37
    r = scipy.integrate.ode(mcwf_ode_func)
 
38
 
 
39
    initial_vector = psi0.full() 
 
40
    #print "initial vector = ", initial_vector
 
41
 
 
42
    r.set_integrator('zvode').set_initial_value(initial_vector, 0.0).set_f_params(H)
 
43
 
 
44
    t_idx = 0
 
45
    for m in range(0, n_expt_op):
 
46
        expt_list[m,t_idx] += expect(expt_op_list[m], psi0)
 
47
    
 
48
    while r.successful() and r.t < tlist[-1]:
 
49
        t_idx += 1
 
50
 
 
51
        psi = qobj(r.y)
 
52
        psi.dims = psi0.dims
 
53
 
 
54
        # calculate all the expectation values: contribution from single trajectory
 
55
        for m in range(0, n_expt_op):
 
56
            expt_list[m,t_idx] += expect(expt_op_list[m], psi)
 
57
 
 
58
        r.integrate(r.t + dt)
 
59
           
 
60
    return expt_list
 
61
 
 
62
#
 
63
# evaluate dpsi(t)/dt
 
64
#
 
65
def mcwf_ode_func(t, psi, H):
 
66
    dpsi = -1j * (H.data * psi)
 
67
    return dpsi
 
68
 
 
69
 
 
70
# ------------------------------------------------------------------------------
 
71
# Master equation solver
 
72
 
73
def me_ode_evolve(tlist, H, rho0, c_op_list, expt_op_list):
 
74
    """!
 
75
    @brief Evolve the denstiy matrix using an ODE solver
 
76
    """
 
77
    n_tsteps  = len(tlist)
 
78
    n_expt_op = len(expt_op_list)
 
79
    n_op      = len(c_op_list)
 
80
    expt_list = zeros([n_expt_op, n_tsteps], dtype=complex)
 
81
    dt        = tlist[1]-tlist[0]
 
82
 
 
83
    if isket(rho0):
 
84
        # Got a wave function as initial state: convert to density matrix.
 
85
        rho0 = rho0 * trans(rho0)
 
86
 
 
87
    #
 
88
    # construct liouvillian
 
89
    #
 
90
    L = -1j*(spre(H) - spost(H))
 
91
    for m in range(0, n_op):
 
92
        cdc = c_op_list[m].dag() * c_op_list[m]
 
93
        L += spre(c_op_list[m])*spost(c_op_list[m].dag())-0.5*spre(cdc)-0.5*spost(cdc)
 
94
 
 
95
    #
 
96
    # setup integrator
 
97
    #
 
98
    initial_vector = rho0.full().reshape(prod(rho0.shape),1)
 
99
    r = scipy.integrate.ode(me_ode_func).set_integrator('zvode').set_initial_value(initial_vector, tlist[0]).set_f_params(L.data)
 
100
 
 
101
    #r.set_integrator('zvode').set_initial_value(initial_vector, 0.0).set_f_params(L)
 
102
 
 
103
    #
 
104
    # start evolution
 
105
    #
 
106
    rho = qobj(rho0)
 
107
 
 
108
    t_idx = 0
 
109
    # while t_idx <= n_tsteps:
 
110
    for t in tlist:
 
111
        if not r.successful():
 
112
            break;
 
113
 
 
114
        rho.data = r.y.reshape(rho0.shape)
 
115
 
 
116
        # calculate all the expectation values
 
117
        for m in range(0, n_expt_op):
 
118
            expt_list[m,t_idx] = expect(expt_op_list[m], rho)
 
119
 
 
120
        r.integrate(r.t + dt)
 
121
        t_idx += 1
 
122
          
 
123
    return expt_list
 
124
 
 
125
 
 
126
#
 
127
# evaluate drho(t)/dt according to the master eqaution
 
128
#
 
129
def me_ode_func(t, rho, L):
 
130
    drho = (L * rho)
 
131
    return drho
 
132
 
 
133
 
 
134
 
 
135
 
 
136
 
 
137
 
 
138
 
 
139
 
 
140
 
 
141
 
 
142
 
 
143
 
 
144
 
 
145
 
 
146
 
 
147
 
 
148