def smepdpsolve_generic(ssdata, options, progress_bar): """ For internal use. .. note:: Experimental. """ if debug: print(inspect.stack()[0][3]) N_store = len(ssdata.tlist) N_substeps = ssdata.nsubsteps N = N_store * N_substeps dt = (ssdata.tlist[1] - ssdata.tlist[0]) / N_substeps NT = ssdata.ntraj data = Odedata() data.solver = "smepdpsolve" data.times = ssdata.tlist data.expect = np.zeros((len(ssdata.e_ops), N_store), dtype=complex) data.jump_times = [] data.jump_op_idx = [] # Liouvillian for the deterministic part. # needs to be modified for TD systems L = liouvillian_fast(ssdata.H, ssdata.c_ops) progress_bar.start(ssdata.ntraj) for n in range(ssdata.ntraj): progress_bar.update(n) rho_t = mat2vec(ssdata.rho0.full()).ravel() states_list, jump_times, jump_op_idx = \ _smepdpsolve_single_trajectory(data, L, dt, ssdata.tlist, N_store, N_substeps, rho_t, ssdata.c_ops, ssdata.e_ops) data.states.append(states_list) data.jump_times.append(jump_times) data.jump_op_idx.append(jump_op_idx) progress_bar.finished() # average density matrices if options.average_states and np.any(data.states): data.states = [sum(state_list).unit() for state_list in data.states] # average data.expect = data.expect / ssdata.ntraj # standard error if NT > 1: data.se = (data.ss - NT * (data.expect ** 2)) / (NT * (NT - 1)) else: data.se = None return data
def smesolve_generic(H, rho0, tlist, c_ops, sc_ops, e_ops, rhs, d1, d2, d2_len, ntraj, nsubsteps, options, progress_bar): """ internal .. note:: Experimental. """ if debug: print(inspect.stack()[0][3]) N_store = len(tlist) N_substeps = nsubsteps N = N_store * N_substeps dt = (tlist[1] - tlist[0]) / N_substeps data = Odedata() data.solver = "smesolve" data.times = tlist data.expect = np.zeros((len(e_ops), N_store), dtype=complex) # pre-compute collapse operator combinations that are commonly needed # when evaluating the RHS of stochastic master equations A_ops = [] for c_idx, c in enumerate(sc_ops): # xxx: precompute useful operator expressions... cdc = c.dag() * c Ldt = spre(c) * spost(c.dag()) - 0.5 * spre(cdc) - 0.5 * spost(cdc) LdW = spre(c) + spost(c.dag()) Lm = spre(c) + spost(c.dag()) # currently same as LdW A_ops.append([Ldt.data, LdW.data, Lm.data]) # Liouvillian for the deterministic part L = liouvillian_fast(H, c_ops) # needs to be modified for TD systems progress_bar.start(ntraj) for n in range(ntraj): progress_bar.update(n) rho_t = mat2vec(rho0.full()) states_list = _smesolve_single_trajectory( L, dt, tlist, N_store, N_substeps, rho_t, A_ops, e_ops, data, rhs, d1, d2, d2_len) # if average -> average... data.states.append(states_list) progress_bar.finished() # average data.expect = data.expect / ntraj return data
def _mesolve_const(H, rho0, tlist, c_op_list, e_ops, args, opt, progress_bar): """! Evolve the density matrix using an ODE solver, for constant hamiltonian and collapse operators. """ if debug: print(inspect.stack()[0][3]) # # check initial state # if isket(rho0): # if initial state is a ket and no collapse operator where given, # fall back on the unitary schrodinger equation solver if len(c_op_list) == 0 and isoper(H): return _sesolve_const(H, rho0, tlist, e_ops, args, opt) # Got a wave function as initial state: convert to density matrix. rho0 = rho0 * rho0.dag() # # construct liouvillian # if opt.tidy: H = H.tidyup(opt.atol) L = liouvillian_fast(H, c_op_list) # # setup integrator # initial_vector = mat2vec(rho0.full()).ravel() r = scipy.integrate.ode(cy_ode_rhs) r.set_f_params(L.data.data, L.data.indices, L.data.indptr) r.set_integrator('zvode', method=opt.method, order=opt.order, atol=opt.atol, rtol=opt.rtol, nsteps=opt.nsteps, first_step=opt.first_step, min_step=opt.min_step, max_step=opt.max_step) r.set_initial_value(initial_vector, tlist[0]) # # call generic ODE code # return _generic_ode_solve(r, rho0, tlist, e_ops, opt, progress_bar)
def _mesolve_func_td(L_func, rho0, tlist, c_op_list, e_ops, args, opt, progress_bar): """! Evolve the density matrix using an ODE solver with time dependent Hamiltonian. """ if debug: print(inspect.stack()[0][3]) # # check initial state # if isket(rho0): rho0 = ket2dm(rho0) # # construct liouvillian # new_args = None if len(c_op_list) > 0: L_data = liouvillian_fast(None, c_op_list).data else: n, m = rho0.shape L_data = sp.csr_matrix((n ** 2, m ** 2), dtype=complex) if type(args) is dict: new_args = {} for key in args: if isinstance(args[key], Qobj): if isoper(args[key]): new_args[key] = ( -1j * (spre(args[key]) - spost(args[key]))).data else: new_args[key] = args[key].data else: new_args[key] = args[key] elif type(args) is list: new_args = [] for arg in args: if isinstance(arg, Qobj): if isoper(arg): new_args.append((-1j * (spre(arg) - spost(arg))).data) else: new_args.append(arg.data) else: new_args.append(arg) else: if isinstance(args, Qobj): if isoper(args): new_args = (-1j * (spre(args) - spost(args))).data else: new_args = args.data else: new_args = args # # setup integrator # initial_vector = mat2vec(rho0.full()).ravel() if not opt.rhs_with_state: r = scipy.integrate.ode(cy_ode_rho_func_td) else: r = scipy.integrate.ode(_ode_rho_func_td_with_state) r.set_integrator('zvode', method=opt.method, order=opt.order, atol=opt.atol, rtol=opt.rtol, nsteps=opt.nsteps, first_step=opt.first_step, min_step=opt.min_step, max_step=opt.max_step) r.set_initial_value(initial_vector, tlist[0]) r.set_f_params(L_data, L_func, new_args) # # call generic ODE code # return _generic_ode_solve(r, rho0, tlist, e_ops, opt, progress_bar)
def _mesolve_list_td(H_func, rho0, tlist, c_op_list, e_ops, args, opt, progress_bar): """! Evolve the density matrix using an ODE solver with time dependent Hamiltonian. """ if debug: print(inspect.stack()[0][3]) # # check initial state # if isket(rho0): # if initial state is a ket and no collapse operator where given, # fall back on the unitary schrodinger equation solver if len(c_op_list) == 0: return _sesolve_list_td(H_func, rho0, tlist, e_ops, args, opt) # Got a wave function as initial state: convert to density matrix. rho0 = ket2dm(rho0) # # construct liouvillian # if len(H_func) != 2: raise TypeError('Time-dependent Hamiltonian list must have two terms.') if not isinstance(H_func[0], (list, np.ndarray)) or len(H_func[0]) <= 1: raise TypeError('Time-dependent Hamiltonians must be a list ' + 'with two or more terms') if (not isinstance(H_func[1], (list, np.ndarray))) or \ (len(H_func[1]) != (len(H_func[0]) - 1)): raise TypeError('Time-dependent coefficients must be list with ' + 'length N-1 where N is the number of ' + 'Hamiltonian terms.') if opt.rhs_reuse and odeconfig.tdfunc is None: rhs_generate(H_func, args) lenh = len(H_func[0]) if opt.tidy: H_func[0] = [(H_func[0][k]).tidyup() for k in range(lenh)] L_func = [[liouvillian_fast(H_func[0][0], c_op_list)], H_func[1]] for m in range(1, lenh): L_func[0].append(liouvillian_fast(H_func[0][m], [])) # create data arrays for time-dependent RHS function Ldata = [L_func[0][k].data.data for k in range(lenh)] Linds = [L_func[0][k].data.indices for k in range(lenh)] Lptrs = [L_func[0][k].data.indptr for k in range(lenh)] # setup ode args string string = "" for k in range(lenh): string += ("Ldata[%d], Linds[%d], Lptrs[%d]," % (k, k, k)) if args: td_consts = args.items() for elem in td_consts: string += str(elem[1]) if elem != td_consts[-1]: string += (",") # run code generator if not opt.rhs_reuse or odeconfig.tdfunc is None: if opt.rhs_filename is None: odeconfig.tdname = "rhs" + str(odeconfig.cgen_num) else: odeconfig.tdname = opt.rhs_filename cgen = Codegen(h_terms=n_L_terms, h_tdterms=Lcoeff, args=args, odeconfig=odeconfig) cgen.generate(odeconfig.tdname + ".pyx") code = compile('from ' + odeconfig.tdname + ' import cyq_td_ode_rhs', '<string>', 'exec') exec(code, globals()) odeconfig.tdfunc = cyq_td_ode_rhs # # setup integrator # initial_vector = mat2vec(rho0.full()).ravel() r = scipy.integrate.ode(odeconfig.tdfunc) r.set_integrator('zvode', method=opt.method, order=opt.order, atol=opt.atol, rtol=opt.rtol, nsteps=opt.nsteps, first_step=opt.first_step, min_step=opt.min_step, max_step=opt.max_step) r.set_initial_value(initial_vector, tlist[0]) code = compile('r.set_f_params(' + string + ')', '<string>', 'exec') exec(code) # # call generic ODE code # return _generic_ode_solve(r, rho0, tlist, e_ops, opt, progress_bar)
def _mesolve_list_func_td(H_list, rho0, tlist, c_list, e_ops, args, opt, progress_bar): """ Internal function for solving the master equation. See mesolve for usage. """ if debug: print(inspect.stack()[0][3]) # # check initial state # if isket(rho0): rho0 = rho0 * rho0.dag() # # construct liouvillian in list-function format # L_list = [] if opt.rhs_with_state: constant_func = lambda x, y, z: 1.0 else: constant_func = lambda x, y: 1.0 # add all hamitonian terms to the lagrangian list for h_spec in H_list: if isinstance(h_spec, Qobj): h = h_spec h_coeff = constant_func elif isinstance(h_spec, list) and isinstance(h_spec[0], Qobj): h = h_spec[0] h_coeff = h_spec[1] else: raise TypeError("Incorrect specification of time-dependent " + "Hamiltonian (expected callback function)") if isoper(h): L_list.append([(-1j * (spre(h) - spost(h))).data, h_coeff, False]) elif issuper(h): L_list.append([h.data, h_coeff, False]) else: raise TypeError("Incorrect specification of time-dependent " + "Hamiltonian (expected operator or superoperator)") # add all collapse operators to the lagrangian list for c_spec in c_list: if isinstance(c_spec, Qobj): c = c_spec c_coeff = constant_func c_square = False elif isinstance(c_spec, list) and isinstance(c_spec[0], Qobj): c = c_spec[0] c_coeff = c_spec[1] c_square = True else: raise TypeError("Incorrect specification of time-dependent " + "collapse operators (expected callback function)") if isoper(c): cdc = c.dag() * c L_list.append([liouvillian_fast(None, [c], data_only=True), c_coeff, c_square]) elif issuper(c): L_list.append([c.data, c_coeff, c_square]) else: raise TypeError("Incorrect specification of time-dependent " + "collapse operators (expected operator or " + "superoperator)") # # setup integrator # initial_vector = mat2vec(rho0.full()).ravel() if opt.rhs_with_state: r = scipy.integrate.ode(drho_list_td_with_state) else: r = scipy.integrate.ode(drho_list_td) r.set_integrator('zvode', method=opt.method, order=opt.order, atol=opt.atol, rtol=opt.rtol, nsteps=opt.nsteps, first_step=opt.first_step, min_step=opt.min_step, max_step=opt.max_step) r.set_initial_value(initial_vector, tlist[0]) r.set_f_params(L_list, args) # # call generic ODE code # return _generic_ode_solve(r, rho0, tlist, e_ops, opt, progress_bar)
def _mesolve_func_td(L_func, rho0, tlist, c_op_list, e_ops, args, opt, progress_bar): """! Evolve the density matrix using an ODE solver with time dependent Hamiltonian. """ if debug: print(inspect.stack()[0][3]) # # check initial state # if isket(rho0): rho0 = ket2dm(rho0) # # construct liouvillian # new_args = None if len(c_op_list) > 0: L_data = liouvillian_fast(None, c_op_list).data else: n, m = rho0.shape L_data = sp.csr_matrix((n**2, m**2), dtype=complex) if type(args) is dict: new_args = {} for key in args: if isinstance(args[key], Qobj): if isoper(args[key]): new_args[key] = (-1j * (spre(args[key]) - spost(args[key]))).data else: new_args[key] = args[key].data else: new_args[key] = args[key] elif type(args) is list: new_args = [] for arg in args: if isinstance(arg, Qobj): if isoper(arg): new_args.append((-1j * (spre(arg) - spost(arg))).data) else: new_args.append(arg.data) else: new_args.append(arg) else: if isinstance(args, Qobj): if isoper(args): new_args = (-1j * (spre(args) - spost(args))).data else: new_args = args.data else: new_args = args # # setup integrator # initial_vector = mat2vec(rho0.full()).ravel() if not opt.rhs_with_state: r = scipy.integrate.ode(cy_ode_rho_func_td) else: r = scipy.integrate.ode(_ode_rho_func_td_with_state) r.set_integrator('zvode', method=opt.method, order=opt.order, atol=opt.atol, rtol=opt.rtol, nsteps=opt.nsteps, first_step=opt.first_step, min_step=opt.min_step, max_step=opt.max_step) r.set_initial_value(initial_vector, tlist[0]) r.set_f_params(L_data, L_func, new_args) # # call generic ODE code # return _generic_ode_solve(r, rho0, tlist, e_ops, opt, progress_bar)
def _mesolve_list_func_td(H_list, rho0, tlist, c_list, e_ops, args, opt, progress_bar): """ Internal function for solving the master equation. See mesolve for usage. """ if debug: print(inspect.stack()[0][3]) # # check initial state # if isket(rho0): rho0 = rho0 * rho0.dag() # # construct liouvillian in list-function format # L_list = [] if opt.rhs_with_state: constant_func = lambda x, y, z: 1.0 else: constant_func = lambda x, y: 1.0 # add all hamitonian terms to the lagrangian list for h_spec in H_list: if isinstance(h_spec, Qobj): h = h_spec h_coeff = constant_func elif isinstance(h_spec, list) and isinstance(h_spec[0], Qobj): h = h_spec[0] h_coeff = h_spec[1] else: raise TypeError("Incorrect specification of time-dependent " + "Hamiltonian (expected callback function)") if isoper(h): L_list.append([(-1j * (spre(h) - spost(h))).data, h_coeff, False]) elif issuper(h): L_list.append([h.data, h_coeff, False]) else: raise TypeError("Incorrect specification of time-dependent " + "Hamiltonian (expected operator or superoperator)") # add all collapse operators to the lagrangian list for c_spec in c_list: if isinstance(c_spec, Qobj): c = c_spec c_coeff = constant_func c_square = False elif isinstance(c_spec, list) and isinstance(c_spec[0], Qobj): c = c_spec[0] c_coeff = c_spec[1] c_square = True else: raise TypeError("Incorrect specification of time-dependent " + "collapse operators (expected callback function)") if isoper(c): cdc = c.dag() * c L_list.append([ liouvillian_fast(None, [c], data_only=True), c_coeff, c_square ]) elif issuper(c): L_list.append([c.data, c_coeff, c_square]) else: raise TypeError("Incorrect specification of time-dependent " + "collapse operators (expected operator or " + "superoperator)") # # setup integrator # initial_vector = mat2vec(rho0.full()).ravel() if opt.rhs_with_state: r = scipy.integrate.ode(drho_list_td_with_state) else: r = scipy.integrate.ode(drho_list_td) r.set_integrator('zvode', method=opt.method, order=opt.order, atol=opt.atol, rtol=opt.rtol, nsteps=opt.nsteps, first_step=opt.first_step, min_step=opt.min_step, max_step=opt.max_step) r.set_initial_value(initial_vector, tlist[0]) r.set_f_params(L_list, args) # # call generic ODE code # return _generic_ode_solve(r, rho0, tlist, e_ops, opt, progress_bar)
def smesolve_generic(ssdata, options, progress_bar): """ internal .. note:: Experimental. """ if debug: print(inspect.stack()[0][3]) N_store = len(ssdata.tlist) N_substeps = ssdata.nsubsteps N = N_store * N_substeps dt = (ssdata.tlist[1] - ssdata.tlist[0]) / N_substeps NT = ssdata.ntraj data = Odedata() data.solver = "smesolve" data.times = ssdata.tlist data.expect = np.zeros((len(ssdata.e_ops), N_store), dtype=complex) data.ss = np.zeros((len(ssdata.e_ops), N_store), dtype=complex) data.noise = [] data.measurement = [] # pre-compute suporoperator operator combinations that are commonly needed # when evaluating the RHS of stochastic master equations A_ops = [] for c_idx, c in enumerate(ssdata.sc_ops): n = c.dag() * c A_ops.append([spre(c).data, spost(c).data, spre(c.dag()).data, spost(c.dag()).data, spre(n).data, spost(n).data, (spre(c) * spost(c.dag())).data, lindblad_dissipator(c, data_only=True)]) s_e_ops = [spre(e) for e in ssdata.e_ops] # Liouvillian for the deterministic part. # needs to be modified for TD systems L = liouvillian_fast(ssdata.H, ssdata.c_ops) progress_bar.start(ssdata.ntraj) for n in range(ssdata.ntraj): progress_bar.update(n) rho_t = mat2vec(ssdata.state0.full()).ravel() noise = ssdata.noise[n] if ssdata.noise else None states_list, dW, m = _smesolve_single_trajectory( L, dt, ssdata.tlist, N_store, N_substeps, rho_t, A_ops, s_e_ops, data, ssdata.rhs, ssdata.d1, ssdata.d2, ssdata.d2_len, ssdata.homogeneous, ssdata.distribution, ssdata.args, store_measurement=ssdata.store_measurement, store_states=ssdata.store_states, noise=noise) data.states.append(states_list) data.noise.append(dW) data.measurement.append(m) progress_bar.finished() # average density matrices if options.average_states and np.any(data.states): data.states = [sum(state_list).unit() for state_list in data.states] # average data.expect = data.expect / NT # standard error if NT > 1: data.se = (data.ss - NT * (data.expect ** 2)) / (NT * (NT - 1)) else: data.se = None # convert complex data to real if hermitian data.expect = [np.real(data.expect[n,:]) if e.isherm else data.expect[n,:] for n, e in enumerate(ssdata.e_ops)] return data
def smepdpsolve_generic(ssdata, options, progress_bar): """ For internal use. .. note:: Experimental. """ if debug: print(inspect.stack()[0][3]) N_store = len(ssdata.tlist) N_substeps = ssdata.nsubsteps N = N_store * N_substeps dt = (ssdata.tlist[1] - ssdata.tlist[0]) / N_substeps NT = ssdata.ntraj data = Odedata() data.solver = "smepdpsolve" data.times = ssdata.tlist data.expect = np.zeros((len(ssdata.e_ops), N_store), dtype=complex) data.jump_times = [] data.jump_op_idx = [] # Liouvillian for the deterministic part. # needs to be modified for TD systems L = liouvillian_fast(ssdata.H, ssdata.c_ops) progress_bar.start(ssdata.ntraj) for n in range(ssdata.ntraj): progress_bar.update(n) rho_t = mat2vec(ssdata.rho0.full()).ravel() states_list, jump_times, jump_op_idx = \ _smepdpsolve_single_trajectory(L, dt, ssdata.tlist, N_store, N_substeps, rho_t, ssdata.c_ops, ssdata.e_ops, data) data.states.append(states_list) data.jump_times.append(jump_times) data.jump_op_idx.append(jump_op_idx) progress_bar.finished() # average density matrices if options.average_states and np.any(data.states): data.states = [sum(state_list).unit() for state_list in data.states] # average data.expect = data.expect / ssdata.ntraj # standard error if NT > 1: data.se = (data.ss - NT * (data.expect ** 2)) / (NT * (NT - 1)) else: data.se = None return data