def _steadystate_direct_sparse(L, use_rcm=True, use_umfpack=False): """ Direct solver that uses scipy sparse matrices """ if settings.debug: print('Starting direct solver...') dims=L.dims[0] weight=np.abs(L.data.max()) n = prod(L.dims[0][0]) b = np.zeros((n ** 2, 1), dtype=complex) b[0,0] = weight L = L.data + sp.csr_matrix((weight*np.ones(n), (np.zeros(n), [nn * (n + 1) for nn in range(n)])), shape=(n ** 2, n ** 2)) L.sort_indices() use_solver(assumeSortedIndices=True, useUmfpack=use_umfpack) if use_rcm: perm = symrcm(L) L = sparse_permute(L,perm,perm) b = b[np.ix_(perm,)] v = spsolve(L, b) if use_rcm: rev_perm = np.argsort(perm) v = v[np.ix_(rev_perm,)] data = vec2mat(v) data = 0.5 * (data + data.conj().T) return Qobj(data, dims=dims, isherm=True)
def _steadystate_lu(L, use_rcm=True, use_umfpack=False): """ Find the steady state(s) of an open quantum system by computing the LU decomposition of the underlying matrix. """ if settings.debug: print('Starting LU solver...') dims=L.dims[0] weight=np.abs(L.data.max()) n = prod(L.dims[0][0]) b = np.zeros(n ** 2, dtype=complex) b[0] = weight L = L.data.tocsc() + sp.csc_matrix((weight*np.ones(n), (np.zeros(n), [nn * (n + 1) for nn in range(n)])), shape=(n ** 2, n ** 2)) L.sort_indices() use_solver(assumeSortedIndices=True, useUmfpack=use_umfpack) if use_rcm: perm = symrcm(L) L = sparse_permute(L,perm,perm) b = b[np.ix_(perm,)] solve = factorized(L) v = solve(b) if use_rcm: rev_perm = np.argsort(perm) v = v[np.ix_(rev_perm,)] data = vec2mat(v) data = 0.5 * (data + data.conj().T) return Qobj(data, dims=dims, isherm=True)
def _steadystate_lu(L, use_rcm=True, use_umfpack=False): """ Find the steady state(s) of an open quantum system by computing the LU decomposition of the underlying matrix. """ if settings.debug: print('Starting LU solver...') dims = L.dims[0] weight = np.abs(L.data.max()) n = prod(L.dims[0][0]) b = np.zeros(n**2, dtype=complex) b[0] = weight L = L.data.tocsc() + sp.csc_matrix( (weight * np.ones(n), (np.zeros(n), [nn * (n + 1) for nn in range(n)])), shape=(n**2, n**2)) L.sort_indices() use_solver(assumeSortedIndices=True, useUmfpack=use_umfpack) if use_rcm: perm = symrcm(L) L = sparse_permute(L, perm, perm) b = b[np.ix_(perm, )] solve = factorized(L) v = solve(b) if use_rcm: rev_perm = np.argsort(perm) v = v[np.ix_(rev_perm, )] data = vec2mat(v) data = 0.5 * (data + data.conj().T) return Qobj(data, dims=dims, isherm=True)
def _steadystate_direct_sparse(L, use_rcm=True, use_umfpack=False): """ Direct solver that uses scipy sparse matrices """ if settings.debug: print('Starting direct solver...') dims = L.dims[0] weight = np.abs(L.data.max()) n = prod(L.dims[0][0]) b = np.zeros((n**2, 1), dtype=complex) b[0, 0] = weight L = L.data + sp.csr_matrix( (weight * np.ones(n), (np.zeros(n), [nn * (n + 1) for nn in range(n)])), shape=(n**2, n**2)) L.sort_indices() use_solver(assumeSortedIndices=True, useUmfpack=use_umfpack) if use_rcm: perm = symrcm(L) L = sparse_permute(L, perm, perm) b = b[np.ix_(perm, )] v = spsolve(L, b) if use_rcm: rev_perm = np.argsort(perm) v = v[np.ix_(rev_perm, )] data = vec2mat(v) data = 0.5 * (data + data.conj().T) return Qobj(data, dims=dims, isherm=True)
def test_sp_bandwidth(): "Sparse: Bandwidth" # Bandwidth test 1 A = create(25)+destroy(25)+qeye(25) band = sp_bandwidth(A.data) assert_equal(band[0], 3) assert_equal(band[1] == band[2] == 1, 1) # Bandwidth test 2 A = np.array([[1, 0, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0, 1, 1, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 1, 0], [1, 0, 1, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 1, 0, 1], [0, 0, 0, 1, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0, 1]], dtype=np.int32) A = sp.csr_matrix(A) out1 = sp_bandwidth(A) assert_equal(out1[0], 13) assert_equal(out1[1] == out1[2] == 6, 1) # Bandwidth test 3 perm = symrcm(A) B = sp_permute(A, perm, perm) out2 = sp_bandwidth(B) assert_equal(out2[0], 5) assert_equal(out2[1] == out2[2] == 2, 1) # Asymmetric bandwidth test A = destroy(25)+qeye(25) out1 = sp_bandwidth(A.data) assert_equal(out1[0], 2) assert_equal(out1[1], 0) assert_equal(out1[2], 1)
def _steadystate_iterative(L, tol=1e-5, use_precond=True, M=None, use_rcm=True, sym=False, fill_factor=12, maxiter=1000, drop_tol=1e-3, diag_pivot_thresh=None, use_umfpack=False): """ Iterative steady state solver using the LGMRES algorithm and a sparse incomplete LU preconditioner. """ if settings.debug: print('Starting GMRES solver...') dims = L.dims[0] n = prod(L.dims[0][0]) b = np.zeros(n**2) b[0] = 1.0 L = L.data.tocsc() + sp.csc_matrix( (1e-1 * np.ones(n), (np.zeros(n), [nn * (n + 1) for nn in range(n)])), shape=(n**2, n**2)) if use_rcm: if settings.debug: print('Original bandwidth ', sparse_bandwidth(L)) perm = symrcm(L) rev_perm = np.argsort(perm) L = sparse_permute(L, perm, perm, 'csc') b = b[np.ix_(perm, )] if settings.debug: print('RCM bandwidth ', sparse_bandwidth(L)) use_solver(assumeSortedIndices=True, useUmfpack=use_umfpack) L.sort_indices() if M is None and use_precond: M = _iterative_precondition(L, n, drop_tol, diag_pivot_thresh, fill_factor) v, check = gmres(L, b, tol=tol, M=M, maxiter=maxiter) if check > 0: raise Exception("Steadystate solver did not reach tolerance after " + str(check) + " steps.") elif check < 0: raise Exception("Steadystate solver failed with fatal error: " + str(check) + ".") if use_rcm: v = v[np.ix_(rev_perm, )] data = vec2mat(v) data = 0.5 * (data + data.conj().T) return Qobj(data, dims=dims, isherm=True)
def _steadystate_iterative_bicg(L, tol=1e-5, use_precond=True, use_rcm=True, M=None, maxiter=1000, drop_tol=1e-3, diag_pivot_thresh=None, fill_factor=12, verbose=False): """ Iterative steady state solver using the BICG algorithm and a sparse incomplete LU preconditioner. """ if verbose: print('Starting BICG solver...') use_solver(assumeSortedIndices=True, useUmfpack=False) dims=L.dims[0] n = prod(L.dims[0][0]) b = np.zeros(n ** 2) b[0] = 1.0 L = L.data.tocsc() + sp.csc_matrix((np.ones(n), (np.zeros(n), [nn * (n + 1) for nn in range(n)])), shape=(n ** 2, n ** 2)) L.sort_indices() if use_rcm: if verbose: print('Original bandwidth ', sparse_bandwidth(L)) perm=symrcm(L) rev_perm=np.argsort(perm) L=sparse_permute(L,perm,perm,'csc') b = b[np.ix_(perm,)] if verbose: print('RCM bandwidth ', sparse_bandwidth(L)) if M is None and use_precond: M = _iterative_precondition(L, n, drop_tol, diag_pivot_thresh, fill_factor, verbose) if verbose: start_time = time.time() v, check = bicgstab(L, b, tol=tol, M=M) if use_rcm: v = v[np.ix_(rev_perm,)] if check > 0: raise Exception("Steadystate solver did not reach tolerance after " + str(check) + " steps.") elif check < 0: raise Exception( "Steadystate solver failed with fatal error: " + str(check) + ".") if verbose: print('BICG solver time: ', time.time() - start_time) data = vec2mat(v) data = 0.5 * (data + data.conj().T) return Qobj(data, dims=dims, isherm=True)
def _steadystate_iterative(L, ss_args): """ Iterative steady state solver using the LGMRES algorithm and a sparse incomplete LU preconditioner. """ if settings.debug: print('Starting GMRES solver...') dims = L.dims[0] n = prod(L.dims[0][0]) b = np.zeros(n ** 2) b[0] = 1.0 L = L.data.tocsc() + sp.csc_matrix( (1e-1 * np.ones(n), (np.zeros(n), [nn * (n + 1) for nn in range(n)])), shape=(n ** 2, n ** 2)) if ss_args['use_rcm']: if settings.debug: print('Original bandwidth ', sp_bandwidth(L)) perm = symrcm(L) rev_perm = np.argsort(perm) L = sp_permute(L, perm, perm, 'csc') b = b[np.ix_(perm,)] if settings.debug: print('RCM bandwidth ', sp_bandwidth(L)) L.sort_indices() if ss_args['M'] is None and ss_args['use_precond']: M = _iterative_precondition(L, n, ss_args['drop_tol'], ss_args['diag_pivot_thresh'], ss_args['fill_factor']) v, check = gmres(L, b, tol=ss_args['tol'], M=ss_args['M'], maxiter=ss_args['maxiter']) if check > 0: raise Exception("Steadystate solver did not reach tolerance after " + str(check) + " steps.") elif check < 0: raise Exception( "Steadystate solver failed with fatal error: " + str(check) + ".") if ss_args['use_rcm']: v = v[np.ix_(rev_perm,)] data = vec2mat(v) data = 0.5 * (data + data.conj().T) return Qobj(data, dims=dims, isherm=True)