Пример #1
0
    def test_rhs_shape_is_maintained(self):
        A = scipy.sparse.csr_matrix(
            np.array([
                [1, 0, 2],
                [0, 0, 3],
                [-4, 5, 6],
            ], dtype=np.complex128))
        b = np.array([0, 2, 0], dtype=np.complex128)
        out = mkl_spsolve(A, b, verbose=True)
        assert b.shape == out.shape

        b = np.array([0, 2, 0], dtype=np.complex128).reshape((3, 1))
        out = mkl_spsolve(A, b, verbose=True)
        assert b.shape == out.shape
Пример #2
0
def test_mkl_spsolve7():
    """
    MKL spsolve : Solution shape same as input RHS vec
    """
    row = np.array([0,0,1,2,2,2])
    col = np.array([0,2,2,0,1,2])
    data = np.array([1,2,3,-4,5,6], dtype=complex)
    A = sp.csr_matrix((data,(row,col)), shape=(3,3), dtype=complex)

    b = np.array([0,2,0],dtype=complex)
    out = mkl_spsolve(A,b)
    assert_(b.shape==out.shape)
    
    b = np.array([0,2,0],dtype=complex).reshape((3,1))
    out = mkl_spsolve(A,b)
    assert_(b.shape==out.shape)
Пример #3
0
def test_mkl_spsolve7():
    """
    MKL spsolve : Solution shape same as input RHS vec
    """
    row = np.array([0, 0, 1, 2, 2, 2])
    col = np.array([0, 2, 2, 0, 1, 2])
    data = np.array([1, 2, 3, -4, 5, 6], dtype=complex)
    A = sp.csr_matrix((data, (row, col)), shape=(3, 3), dtype=complex)

    b = np.array([0, 2, 0], dtype=complex)
    out = mkl_spsolve(A, b)
    assert_(b.shape == out.shape)

    b = np.array([0, 2, 0], dtype=complex).reshape((3, 1))
    out = mkl_spsolve(A, b)
    assert_(b.shape == out.shape)
Пример #4
0
 def test_single_rhs_vector_real(self):
     Adense = np.array([[0, 1, 1], [1, 0, 1], [0, 0, 1]])
     As = scipy.sparse.csr_matrix(Adense)
     np.random.seed(1234)
     x = np.random.randn(3)
     b = As * x
     x2 = mkl_spsolve(As, b, verbose=True)
     np.testing.assert_allclose(x, x2)
Пример #5
0
 def test_symmetric_solver(self, dtype):
     A = qutip.rand_herm(np.arange(1, 11)).data
     if dtype == np.float64:
         A = A.real
     x = np.ones(10, dtype=dtype)
     b = A.dot(x)
     y = mkl_spsolve(A, b, hermitian=1, verbose=True)
     np.testing.assert_allclose(x, y)
Пример #6
0
def test_mklspsolve2():
    """
    MKL spsolve : Single RHS vector (Complex)
    """
    A = rand_herm(10)
    x = rand_ket(10).full()
    b = A * x
    y = mkl_spsolve(A.data,b)
    assert_array_almost_equal(x, y)
Пример #7
0
def test_mkl_spsolve9():
    """
    MKL spsolve : Hermitian (complex) solver
    """
    A = rand_herm(np.arange(1,11)).data
    x = np.ones(10,dtype=complex)
    b = A.dot(x)
    y = mkl_spsolve(A, b, hermitian=1)
    assert_array_almost_equal(x, y)
Пример #8
0
def test_mklspsolve2():
    """
    MKL spsolve : Single RHS vector (Complex)
    """
    A = rand_herm(10)
    x = rand_ket(10).full()
    b = A * x
    y = mkl_spsolve(A.data, b)
    assert_array_almost_equal(x, y)
Пример #9
0
def test_mkl_spsolve9():
    """
    MKL spsolve : Hermitian (complex) solver
    """
    A = rand_herm(np.arange(1, 11)).data
    x = np.ones(10, dtype=complex)
    b = A.dot(x)
    y = mkl_spsolve(A, b, hermitian=1)
    assert_array_almost_equal(x, y)
Пример #10
0
def test_mkl_spsolve10():
    """
    MKL spsolve : Hermitian (real) solver
    """
    A = rand_herm(np.arange(1, 11)).data
    A = sp.csr_matrix((np.real(A.data), A.indices, A.indptr), dtype=float)
    x = np.ones(10, dtype=float)
    b = A.dot(x)
    y = mkl_spsolve(A, b, hermitian=1)
    assert_array_almost_equal(x, y)
Пример #11
0
def test_mkl_spsolve10():
    """
    MKL spsolve : Hermitian (real) solver
    """
    A = rand_herm(np.arange(1,11)).data
    A = sp.csr_matrix((np.real(A.data), A.indices, A.indptr), dtype=float)
    x = np.ones(10, dtype=float)
    b = A.dot(x)
    y = mkl_spsolve(A, b, hermitian=1)
    assert_array_almost_equal(x, y)
Пример #12
0
def test_mkl_spsolve8():
    """
    MKL spsolve : Sparse RHS matrix
    """
    A = sp.csr_matrix([[1, 2, 0], [0, 3, 0], [0, 0, 5]])
    b = sp.csr_matrix([[0, 1], [1, 0], [0, 0]])

    x = mkl_spsolve(A, b)
    ans = np.array([[-0.66666667, 1], [0.33333333, 0], [0, 0]])
    assert_array_almost_equal(x.toarray(), ans)
Пример #13
0
def test_mkl_spsolve1():
    """
    MKL spsolve : Single RHS vector (Real)
    """
    Adense = np.array([[0., 1., 1.], [1., 0., 1.], [0., 0., 1.]])
    As = sp.csr_matrix(Adense)
    np.random.seed(1234)
    x = np.random.randn(3)
    b = As * x
    x2 = mkl_spsolve(As, b)
    assert_array_almost_equal(x, x2)
Пример #14
0
def test_mkl_spsolve1():
    """
    MKL spsolve : Single RHS vector (Real)
    """
    Adense = np.array([[0., 1., 1.],
                    [1., 0., 1.],
                    [0., 0., 1.]])
    As = sp.csr_matrix(Adense)
    np.random.seed(1234)
    x = np.random.randn(3)
    b = As * x
    x2 = mkl_spsolve(As, b)
    assert_array_almost_equal(x, x2)
Пример #15
0
def _pseudo_inverse_sparse(L, rhoss, method='splu', **pseudo_args):
    """
    Internal function for computing the pseudo inverse of an Liouvillian using
    sparse matrix methods. See pseudo_inverse for details.
    """

    N = np.prod(L.dims[0][0])

    rhoss_vec = operator_to_vector(rhoss)

    tr_op = tensor([identity(n) for n in L.dims[0][0]])
    tr_op_vec = operator_to_vector(tr_op)

    P = sp.kron(rhoss_vec.data, tr_op_vec.data.T, format='csr')
    I = sp.eye(N*N, N*N, format='csr')
    Q = I - P

    if pseudo_args['use_rcm']:
        perm = reverse_cuthill_mckee(L.data)
        A = sp_permute(L.data, perm, perm, 'csr')
        Q = sp_permute(Q, perm, perm, 'csr')
    else:
        if not settings.has_mkl:
            A = L.data.tocsc()
        A.sort_indices()
    
    if method == 'splu':
        if settings.has_mkl:
            LIQ = mkl_spsolve(A,Q.toarray())
        else:
            lu = sp.linalg.splu(A, permc_spec=pseudo_args['permc_spec'],
                            diag_pivot_thresh=pseudo_args['diag_pivot_thresh'],
                            options=dict(ILU_MILU=pseudo_args['ILU_MILU']))
            LIQ = lu.solve(Q.toarray())

    elif method == 'spilu':
        lu = sp.linalg.spilu(A, permc_spec=pseudo_args['permc_spec'],
                             fill_factor=pseudo_args['fill_factor'], 
                             drop_tol=pseudo_args['drop_tol'])
        LIQ = lu.solve(Q.toarray())

    else:
        raise ValueError("unsupported method '%s'" % method)

    R = sp.csr_matrix(Q * LIQ)

    if pseudo_args['use_rcm']:
        rev_perm = np.argsort(perm)
        R = sp_permute(R, rev_perm, rev_perm, 'csr')

    return Qobj(R, dims=L.dims)
Пример #16
0
 def test_sparse_rhs(self):
     A = scipy.sparse.csr_matrix([
         [1, 2, 0],
         [0, 3, 0],
         [0, 0, 5],
     ])
     b = scipy.sparse.csr_matrix([
         [0, 1],
         [1, 0],
         [0, 0],
     ])
     x = mkl_spsolve(A, b, verbose=True)
     ans = np.array([[-0.66666667, 1], [0.33333333, 0], [0, 0]])
     np.testing.assert_allclose(x.toarray(), ans)
Пример #17
0
 def test_multi_rhs_vector(self, dtype):
     M = np.array([
         [1, 0, 2],
         [0, 0, 3],
         [-4, 5, 6],
     ], dtype=dtype)
     sM = scipy.sparse.csr_matrix(M)
     N = np.array([
         [3, 0, 1],
         [0, 2, 0],
         [0, 0, 0],
     ], dtype=dtype)
     sX = mkl_spsolve(sM, N, verbose=True)
     X = scipy.linalg.solve(M, N)
     np.testing.assert_allclose(X, sX)
Пример #18
0
def test_mkl_spsolve4():
    """
    MKL spsolve : Multi RHS vector (Complex)
    """
    row = np.array([0, 0, 1, 2, 2, 2])
    col = np.array([0, 2, 2, 0, 1, 2])
    data = np.array([1, 2, 3, -4, 5, 6], dtype=complex)
    sM = sp.csr_matrix((data, (row, col)), shape=(3, 3), dtype=complex)
    M = sM.toarray()
    row = np.array([0, 0, 1, 1, 0, 0])
    col = np.array([0, 2, 1, 1, 0, 0])
    data = np.array([1, 1, 1, 1, 1, 1], dtype=complex)
    sN = sp.csr_matrix((data, (row, col)), shape=(3, 3), dtype=complex)
    N = sN.toarray()
    sX = mkl_spsolve(sM, N)
    X = la.solve(M, N)
    assert_array_almost_equal(X, sX)
Пример #19
0
def test_mkl_spsolve4():
    """
    MKL spsolve : Multi RHS vector (Complex)
    """
    row = np.array([0,0,1,2,2,2])
    col = np.array([0,2,2,0,1,2])
    data = np.array([1,2,3,-4,5,6],dtype=complex)
    sM = sp.csr_matrix((data,(row,col)), shape=(3,3), dtype=complex)
    M = sM.toarray()
    row = np.array([0,0,1,1,0,0])
    col = np.array([0,2,1,1,0,0])
    data = np.array([1,1,1,1,1,1],dtype=complex)
    sN = sp.csr_matrix((data, (row,col)), shape=(3,3), dtype=complex)
    N = sN.toarray()
    sX = mkl_spsolve(sM, N)
    X = la.solve(M, N)
    assert_array_almost_equal(X, sX)
Пример #20
0
def test_mkl_spsolve8():
    """
    MKL spsolve : Sparse RHS matrix
    """
    A = sp.csr_matrix([
                [1, 2, 0],
                [0, 3, 0],
                [0, 0, 5]])
    b = sp.csr_matrix([
        [0, 1],
        [1, 0],
        [0, 0]])

    x = mkl_spsolve(A, b)
    ans = np.array([[-0.66666667, 1],
                    [0.33333333, 0],
                    [0, 0]])
    assert_array_almost_equal(x.toarray(), ans)
Пример #21
0
def _steadystate_direct_sparse(L, ss_args):
    """
    Direct solver that uses scipy sparse matrices
    """
    if settings.debug:
        logger.debug('Starting direct LU solver.')

    dims = L.dims[0]
    n = int(np.sqrt(L.shape[0]))
    b = np.zeros(n**2, dtype=complex)
    b[0] = ss_args['weight']

    if ss_args['solver'] == 'mkl':
        has_mkl = 1
    else:
        has_mkl = 0

    ss_lu_liouv_list = _steadystate_LU_liouvillian(L, ss_args, has_mkl)
    L, perm, perm2, rev_perm, ss_args = ss_lu_liouv_list
    if np.any(perm):
        b = b[np.ix_(perm, )]
    if np.any(perm2):
        b = b[np.ix_(perm2, )]

    if ss_args['solver'] == 'scipy':
        ss_args['info']['permc_spec'] = ss_args['permc_spec']
        ss_args['info']['drop_tol'] = ss_args['drop_tol']
        ss_args['info']['diag_pivot_thresh'] = ss_args['diag_pivot_thresh']
        ss_args['info']['fill_factor'] = ss_args['fill_factor']
        ss_args['info']['ILU_MILU'] = ss_args['ILU_MILU']

    if not ss_args['solver'] == 'mkl':
        # Use superLU solver
        orig_nnz = L.nnz
        _direct_start = time.time()
        lu = splu(L,
                  permc_spec=ss_args['permc_spec'],
                  diag_pivot_thresh=ss_args['diag_pivot_thresh'],
                  options=dict(ILU_MILU=ss_args['ILU_MILU']))
        v = lu.solve(b)
        _direct_end = time.time()
        ss_args['info']['solution_time'] = _direct_end - _direct_start
        if (settings.debug or ss_args['return_info']) and _scipy_check:
            L_nnz = lu.L.nnz
            U_nnz = lu.U.nnz
            ss_args['info']['l_nnz'] = L_nnz
            ss_args['info']['u_nnz'] = U_nnz
            ss_args['info']['lu_fill_factor'] = (L_nnz + U_nnz) / L.nnz
            if settings.debug:
                logger.debug('L NNZ: %i ; U NNZ: %i' % (L_nnz, U_nnz))
                logger.debug('Fill factor: %f' % ((L_nnz + U_nnz) / orig_nnz))

    else:  # Use MKL solver
        if len(ss_args['info']['perm']) != 0:
            in_perm = np.arange(n**2, dtype=np.int32)
        else:
            in_perm = None
        _direct_start = time.time()
        v = mkl_spsolve(L,
                        b,
                        perm=in_perm,
                        verbose=ss_args['verbose'],
                        max_iter_refine=ss_args['max_iter_refine'],
                        scaling_vectors=ss_args['scaling_vectors'],
                        weighted_matching=ss_args['weighted_matching'])
        _direct_end = time.time()
        ss_args['info']['solution_time'] = _direct_end - _direct_start

    if ss_args['return_info']:
        ss_args['info']['residual_norm'] = la.norm(b - L * v, np.inf)
        ss_args['info']['max_iter_refine'] = ss_args['max_iter_refine']
        ss_args['info']['scaling_vectors'] = ss_args['scaling_vectors']
        ss_args['info']['weighted_matching'] = ss_args['weighted_matching']

    if ss_args['use_rcm']:
        v = v[np.ix_(rev_perm, )]

    data = dense2D_to_fastcsr_fmode(vec2mat(v), n, n)
    data = 0.5 * (data + data.H)
    if ss_args['return_info']:
        return Qobj(data, dims=dims, isherm=True), ss_args['info']
    else:
        return Qobj(data, dims=dims, isherm=True)
Пример #22
0
    def steady_state(self,
                     use_mkl=True,
                     mkl_max_iter_refine=100,
                     mkl_weighted_matching=False):
        """
        Compute the steady state of the system.

        Parameters
        ----------
        use_mkl : bool, default=False
            Whether to use mkl or not. If mkl is not installed or if
            this is false, use the scipy splu solver instead.

        mkl_max_iter_refine : int
            Specifies the the maximum number of iterative refinement steps that
            the MKL PARDISO solver performs.

            For a complete description, see iparm(8) in
            http://cali2.unilim.fr/intel-xe/mkl/mklman/GUID-264E311E-ACED-4D56-AC31-E9D3B11D1CBF.htm.

        mkl_weighted_matching : bool
            MKL PARDISO can use a maximum weighted matching algorithm to
            permute large elements close the diagonal. This strategy adds an
            additional level of reliability to the factorization methods.

            For a complete description, see iparm(13) in
            http://cali2.unilim.fr/intel-xe/mkl/mklman/GUID-264E311E-ACED-4D56-AC31-E9D3B11D1CBF.htm.

        Returns
        -------
        steady_state : Qobj
            The steady state density matrix of the system.

        steady_ados : :class:`HierarchyADOsState`
            The steady state of the full ADO hierarchy. A particular ADO may be
            extracted from the full state by calling :meth:`.extract`.
        """
        n = self._sys_shape

        b_mat = np.zeros(n**2 * self._n_ados, dtype=complex)
        b_mat[0] = 1.0

        L = deepcopy(self.RHSmat)
        L = L.tolil()
        L[0, 0:n**2 * self._n_ados] = 0.0
        L = L.tocsr()
        L += sp.csr_matrix(
            (np.ones(n), (np.zeros(n), [num * (n + 1) for num in range(n)])),
            shape=(n**2 * self._n_ados, n**2 * self._n_ados))

        if mkl_spsolve is not None and use_mkl:
            L.sort_indices()
            solution = mkl_spsolve(
                L,
                b_mat,
                perm=None,
                verbose=False,
                max_iter_refine=mkl_max_iter_refine,
                scaling_vectors=True,
                weighted_matching=mkl_weighted_matching,
            )
        else:
            L = L.tocsc()
            solution = spsolve(L, b_mat)

        data = dense2D_to_fastcsr_fmode(vec2mat(solution[:n**2]), n, n)
        data = 0.5 * (data + data.H)
        steady_state = Qobj(data, dims=self._sys_dims)

        solution = solution.reshape((self._n_ados, n, n))
        steady_ados = HierarchyADOsState(steady_state, self.ados, solution)

        return steady_state, steady_ados
Пример #23
0
def _pseudo_inverse_sparse(L, rhoss, w=None, **pseudo_args):
    """
    Internal function for computing the pseudo inverse of an Liouvillian using
    sparse matrix methods. See pseudo_inverse for details.
    """

    N = np.prod(L.dims[0][0])

    rhoss_vec = operator_to_vector(rhoss)

    tr_op = tensor([identity(n) for n in L.dims[0][0]])
    tr_op_vec = operator_to_vector(tr_op)

    P = zcsr_kron(rhoss_vec.data, tr_op_vec.data.T)
    I = sp.eye(N * N, N * N, format='csr')
    Q = I - P

    if w is None:
        L = 1.0j * (1e-15) * spre(tr_op) + L
    else:
        if w != 0.0:
            L = 1.0j * w * spre(tr_op) + L
        else:
            L = 1.0j * (1e-15) * spre(tr_op) + L

    if pseudo_args['use_rcm']:
        perm = sp.csgraph.reverse_cuthill_mckee(L.data)
        A = sp_permute(L.data, perm, perm)
        Q = sp_permute(Q, perm, perm)
    else:
        if ss_args['solver'] == 'scipy':
            A = L.data.tocsc()
            A.sort_indices()

    if pseudo_args['method'] == 'splu':
        if settings.has_mkl:
            A = L.data.tocsr()
            A.sort_indices()
            LIQ = mkl_spsolve(A, Q.toarray())
        else:
            pspec = pseudo_args['permc_spec']
            diag_p_thresh = pseudo_args['diag_pivot_thresh']
            pseudo_args = pseudo_args['ILU_MILU']
            lu = sp.linalg.splu(A,
                                permc_spec=pspec,
                                diag_pivot_thresh=diag_p_thresh,
                                options=dict(ILU_MILU=pseudo_args))
            LIQ = lu.solve(Q.toarray())

    elif pseudo_args['method'] == 'spilu':
        lu = sp.linalg.spilu(A,
                             permc_spec=pseudo_args['permc_spec'],
                             fill_factor=pseudo_args['fill_factor'],
                             drop_tol=pseudo_args['drop_tol'])
        LIQ = lu.solve(Q.toarray())

    else:
        raise ValueError("unsupported method '%s'" % method)

    R = sp.csr_matrix(Q * LIQ)

    if pseudo_args['use_rcm']:
        rev_perm = np.argsort(perm)
        R = sp_permute(R, rev_perm, rev_perm, 'csr')

    return Qobj(R, dims=L.dims)
Пример #24
0
def _steadystate_direct_sparse(L, ss_args):
    """
    Direct solver that uses scipy sparse matrices
    """
    if settings.debug:
        logger.debug('Starting direct LU solver.')

    dims = L.dims[0]
    n = int(np.sqrt(L.shape[0]))
    b = np.zeros(n ** 2, dtype=complex)
    b[0] = ss_args['weight']

    if ss_args['solver'] == 'mkl':
        has_mkl = 1
    else:
        has_mkl = 0

    ss_lu_liouv_list = _steadystate_LU_liouvillian(L, ss_args, has_mkl)
    L, perm, perm2, rev_perm, ss_args = ss_lu_liouv_list
    if np.any(perm):
        b = b[np.ix_(perm,)]
    if np.any(perm2):
        b = b[np.ix_(perm2,)]

    if ss_args['solver'] == 'scipy':
        ss_args['info']['permc_spec'] = ss_args['permc_spec']
        ss_args['info']['drop_tol'] = ss_args['drop_tol']
        ss_args['info']['diag_pivot_thresh'] = ss_args['diag_pivot_thresh']
        ss_args['info']['fill_factor'] = ss_args['fill_factor']
        ss_args['info']['ILU_MILU'] = ss_args['ILU_MILU']

    if not ss_args['solver'] == 'mkl':
        # Use superLU solver
        orig_nnz = L.nnz
        _direct_start = time.time()
        lu = splu(L, permc_spec=ss_args['permc_spec'],
                  diag_pivot_thresh=ss_args['diag_pivot_thresh'],
                  options=dict(ILU_MILU=ss_args['ILU_MILU']))
        v = lu.solve(b)
        _direct_end = time.time()
        ss_args['info']['solution_time'] = _direct_end - _direct_start
        if (settings.debug or ss_args['return_info']) and _scipy_check:
            L_nnz = lu.L.nnz
            U_nnz = lu.U.nnz
            ss_args['info']['l_nnz'] = L_nnz
            ss_args['info']['u_nnz'] = U_nnz
            ss_args['info']['lu_fill_factor'] = (L_nnz + U_nnz)/L.nnz
            if settings.debug:
                logger.debug('L NNZ: %i ; U NNZ: %i' % (L_nnz, U_nnz))
                logger.debug('Fill factor: %f' % ((L_nnz + U_nnz)/orig_nnz))

    else:  # Use MKL solver
        if len(ss_args['info']['perm']) != 0:
            in_perm = np.arange(n**2, dtype=np.int32)
        else:
            in_perm = None
        _direct_start = time.time()
        v = mkl_spsolve(L, b, perm=in_perm, verbose=ss_args['verbose'],
                        max_iter_refine=ss_args['max_iter_refine'],
                        scaling_vectors=ss_args['scaling_vectors'],
                        weighted_matching=ss_args['weighted_matching'])
        _direct_end = time.time()
        ss_args['info']['solution_time'] = _direct_end-_direct_start

    if ss_args['return_info']:
        ss_args['info']['residual_norm'] = la.norm(b - L*v, np.inf)
        ss_args['info']['max_iter_refine'] = ss_args['max_iter_refine']
        ss_args['info']['scaling_vectors'] = ss_args['scaling_vectors']
        ss_args['info']['weighted_matching'] = ss_args['weighted_matching']

    if ss_args['use_rcm']:
        v = v[np.ix_(rev_perm,)]

    data = dense2D_to_fastcsr_fmode(vec2mat(v), n, n)
    data = 0.5 * (data + data.H)
    if ss_args['return_info']:
        return Qobj(data, dims=dims, isherm=True), ss_args['info']
    else:
        return Qobj(data, dims=dims, isherm=True)
Пример #25
0
 def test_single_rhs_vector_complex(self):
     A = qutip.rand_herm(10)
     x = qutip.rand_ket(10).full()
     b = A.full() @ x
     y = mkl_spsolve(A.data, b, verbose=True)
     np.testing.assert_allclose(x, y)
Пример #26
0
def countstat_current_noise(L,
                            c_ops,
                            wlist=None,
                            rhoss=None,
                            J_ops=None,
                            sparse=True,
                            method='direct'):
    """
    Compute the cross-current noise spectrum for a list of collapse operators
    `c_ops` corresponding to monitored currents, given the system
    Liouvillian `L`. The current collapse operators `c_ops` should be part
    of the dissipative processes in `L`, but the `c_ops` given here does not
    necessarily need to be all collapse operators contributing to dissipation
    in the Liouvillian. Optionally, the steadystate density matrix `rhoss`
    and the current operators `J_ops` correpsonding to the current collapse 
    operators `c_ops` can also be specified. If either of
    `rhoss` and `J_ops` are omitted, they will be computed internally.
    'wlist' is an optional list of frequencies at which to evaluate the noise 
    spectrum.  
    
    Note:
    The default method is a direct solution using dense matrices, as sparse 
    matrix methods fail for some examples of small systems.
    For larger systems it is reccomended to use the sparse solver
    with the direct method, as it avoids explicit calculation of the
    pseudo-inverse, as described in page 67 of "Electrons in nanostructures"
    C. Flindt, PhD Thesis, available online:
    http://orbit.dtu.dk/fedora/objects/orbit:82314/datastreams/file_4732600/content
    
    Parameters
    ----------

    L : :class:`qutip.Qobj`
        Qobj representing the system Liouvillian.

    c_ops : array / list
        List of current collapse operators.

    rhoss : :class:`qutip.Qobj` (optional)
        The steadystate density matrix corresponding the system Liouvillian
        `L`.
        
    wlist : array / list (optional)
        List of frequencies at which to evaluate (if none are given, evaluates 
        at zero frequency)

    J_ops : array / list (optional)
        List of current superoperators.

    sparse : bool
        Flag that indicates whether to use sparse or dense matrix methods when
        computing the pseudo inverse. Default is false, as sparse solvers
        can fail for small systems. For larger systems the sparse solvers
        are reccomended. 
        
        
    Returns
    --------
    I, S : tuple of arrays
        The currents `I` corresponding to each current collapse operator
        `c_ops` (or, equivalently, each current superopeator `J_ops`) and the
        zero-frequency cross-current correlation `S`.
    """

    if rhoss is None:
        rhoss = steadystate(L, c_ops)

    if J_ops is None:
        J_ops = [sprepost(c, c.dag()) for c in c_ops]

    N = len(J_ops)
    I = np.zeros(N)

    if wlist is None:
        S = np.zeros((N, N, 1))
        wlist = [0.]
    else:
        S = np.zeros((N, N, len(wlist)))

    if sparse == False:
        rhoss_vec = mat2vec(rhoss.full()).ravel()
        for k, w in enumerate(wlist):
            R = pseudo_inverse(L,
                               rhoss=rhoss,
                               w=w,
                               sparse=sparse,
                               method=method)
            for i, Ji in enumerate(J_ops):
                for j, Jj in enumerate(J_ops):
                    if i == j:
                        I[i] = expect_rho_vec(Ji.data, rhoss_vec, 1)
                        S[i, j, k] = I[i]
                    S[i, j, k] -= expect_rho_vec(
                        (Ji * R * Jj + Jj * R * Ji).data, rhoss_vec, 1)
    else:
        if method == "direct":
            N = np.prod(L.dims[0][0])

            rhoss_vec = operator_to_vector(rhoss)

            tr_op = tensor([identity(n) for n in L.dims[0][0]])
            tr_op_vec = operator_to_vector(tr_op)

            Pop = sp.kron(rhoss_vec.data, tr_op_vec.data.T, format='csr')
            Iop = sp.eye(N * N, N * N, format='csr')
            Q = Iop - Pop

            for k, w in enumerate(wlist):

                if w != 0.0:
                    L_temp = 1.0j * w * spre(tr_op) + L
                else:  #At zero frequency some solvers fail for small systems.
                    #Adding a small finite frequency of order 1e-15
                    #helps prevent the solvers from throwing an exception.
                    L_temp = 1.0j * (1e-15) * spre(tr_op) + L

                if not settings.has_mkl:
                    A = L_temp.data.tocsc()
                else:
                    A = L_temp.data.tocsr()
                    A.sort_indices()

                rhoss_vec = mat2vec(rhoss.full()).ravel()

                for j, Jj in enumerate(J_ops):
                    Qj = Q.dot(Jj.data.dot(rhoss_vec))
                    try:
                        if settings.has_mkl:
                            X_rho_vec_j = mkl_spsolve(A, Qj)
                        else:
                            X_rho_vec_j = sp.linalg.splu(
                                A, permc_spec='COLAMD').solve(Qj)
                    except:
                        X_rho_vec_j = sp.linalg.lsqr(A, Qj)[0]
                    for i, Ji in enumerate(J_ops):
                        Qi = Q.dot(Ji.data.dot(rhoss_vec))
                        try:
                            if settings.has_mkl:
                                X_rho_vec_i = mkl_spsolve(A, Qi)
                            else:
                                X_rho_vec_i = sp.linalg.splu(
                                    A, permc_spec='COLAMD').solve(Qi)
                        except:
                            X_rho_vec_i = sp.linalg.lsqr(A, Qi)[0]
                        if i == j:
                            I[i] = expect_rho_vec(Ji.data, rhoss_vec, 1)
                            S[j, i, k] = I[i]

                        S[j, i,
                          k] -= (expect_rho_vec(Jj.data * Q, X_rho_vec_i, 1) +
                                 expect_rho_vec(Ji.data * Q, X_rho_vec_j, 1))

        else:
            rhoss_vec = mat2vec(rhoss.full()).ravel()
            for k, w in enumerate(wlist):

                R = pseudo_inverse(L,
                                   rhoss=rhoss,
                                   w=w,
                                   sparse=sparse,
                                   method=method)

                for i, Ji in enumerate(J_ops):
                    for j, Jj in enumerate(J_ops):
                        if i == j:
                            I[i] = expect_rho_vec(Ji.data, rhoss_vec, 1)
                            S[i, j, k] = I[i]
                        S[i, j, k] -= expect_rho_vec(
                            (Ji * R * Jj + Jj * R * Ji).data, rhoss_vec, 1)
    return I, S
Пример #27
0
def countstat_current_noise(L, c_ops, wlist=None, rhoss=None, J_ops=None, 
                            sparse=True, method='direct'):
    """
    Compute the cross-current noise spectrum for a list of collapse operators
    `c_ops` corresponding to monitored currents, given the system
    Liouvillian `L`. The current collapse operators `c_ops` should be part
    of the dissipative processes in `L`, but the `c_ops` given here does not
    necessarily need to be all collapse operators contributing to dissipation
    in the Liouvillian. Optionally, the steadystate density matrix `rhoss`
    and the current operators `J_ops` correpsonding to the current collapse 
    operators `c_ops` can also be specified. If either of
    `rhoss` and `J_ops` are omitted, they will be computed internally.
    'wlist' is an optional list of frequencies at which to evaluate the noise 
    spectrum.  
    
    Note:
    The default method is a direct solution using dense matrices, as sparse 
    matrix methods fail for some examples of small systems.
    For larger systems it is reccomended to use the sparse solver
    with the direct method, as it avoids explicit calculation of the
    pseudo-inverse, as described in page 67 of "Electrons in nanostructures"
    C. Flindt, PhD Thesis, available online:
    http://orbit.dtu.dk/fedora/objects/orbit:82314/datastreams/file_4732600/content
    
    Parameters
    ----------

    L : :class:`qutip.Qobj`
        Qobj representing the system Liouvillian.

    c_ops : array / list
        List of current collapse operators.

    rhoss : :class:`qutip.Qobj` (optional)
        The steadystate density matrix corresponding the system Liouvillian
        `L`.
        
    wlist : array / list (optional)
        List of frequencies at which to evaluate (if none are given, evaluates 
        at zero frequency)

    J_ops : array / list (optional)
        List of current superoperators.

    sparse : bool
        Flag that indicates whether to use sparse or dense matrix methods when
        computing the pseudo inverse. Default is false, as sparse solvers
        can fail for small systems. For larger systems the sparse solvers
        are reccomended. 
        
        
    Returns
    --------
    I, S : tuple of arrays
        The currents `I` corresponding to each current collapse operator
        `c_ops` (or, equivalently, each current superopeator `J_ops`) and the
        zero-frequency cross-current correlation `S`.
    """

    if rhoss is None:
        rhoss = steadystate(L, c_ops)

    if J_ops is None:
        J_ops = [sprepost(c, c.dag()) for c in c_ops]

    

    N = len(J_ops)
    I = np.zeros(N)
    
    if wlist is None:
        S = np.zeros((N, N,1))
        wlist=[0.]
    else:
        S = np.zeros((N, N,len(wlist)))
        
    if sparse == False: 
        rhoss_vec = mat2vec(rhoss.full()).ravel()
        for k,w in enumerate(wlist):
            R = pseudo_inverse(L, rhoss=rhoss, w= w, sparse = sparse, method=method)
            for i, Ji in enumerate(J_ops):
                for j, Jj in enumerate(J_ops):
                    if i == j:
                        I[i] = expect_rho_vec(Ji.data, rhoss_vec, 1)
                        S[i, j,k] = I[i]
                    S[i, j,k] -= expect_rho_vec((Ji * R * Jj 
                                                + Jj * R * Ji).data,
                                                rhoss_vec, 1)
    else:
        if method == "direct":
            N = np.prod(L.dims[0][0])
            
            rhoss_vec = operator_to_vector(rhoss)
            
            tr_op = tensor([identity(n) for n in L.dims[0][0]])
            tr_op_vec = operator_to_vector(tr_op)
            
            Pop = sp.kron(rhoss_vec.data, tr_op_vec.data.T, format='csr')
            Iop = sp.eye(N*N, N*N, format='csr')
            Q = Iop - Pop
            
            for k,w in enumerate(wlist):
                
                if w != 0.0:    
                    L_temp = 1.0j*w*spre(tr_op) + L
                else: #At zero frequency some solvers fail for small systems.
                      #Adding a small finite frequency of order 1e-15
                      #helps prevent the solvers from throwing an exception.
                    L_temp =  1.0j*(1e-15)*spre(tr_op) + L
                    
                if not settings.has_mkl:
                    A = L_temp.data.tocsc()
                else:
                    A = L_temp.data.tocsr()
                    A.sort_indices()                      
                      
                rhoss_vec = mat2vec(rhoss.full()).ravel()               
                
                for j, Jj in enumerate(J_ops):
                    Qj = Q.dot( Jj.data.dot( rhoss_vec))
                    try:
                        if settings.has_mkl:
                            X_rho_vec_j = mkl_spsolve(A,Qj)                            
                        else:
                            X_rho_vec_j = sp.linalg.splu(A, permc_spec
                                                 ='COLAMD').solve(Qj)
                    except:
                        X_rho_vec_j = sp.linalg.lsqr(A,Qj)[0]
                    for i, Ji in enumerate(J_ops):
                        Qi = Q.dot( Ji.data.dot(rhoss_vec))
                        try:
                            if settings.has_mkl:                              
                                X_rho_vec_i = mkl_spsolve(A,Qi)  
                            else:
                                X_rho_vec_i = sp.linalg.splu(A, permc_spec
                                                     ='COLAMD').solve(Qi)
                        except:
                             X_rho_vec_i = sp.linalg.lsqr(A,Qi)[0]
                        if i == j:
                            I[i] = expect_rho_vec(Ji.data, 
                                                 rhoss_vec, 1)
                            S[j, i, k] = I[i]
                        
                        S[j, i, k] -= (expect_rho_vec(Jj.data * Q, 
                                        X_rho_vec_i, 1) 
                                        + expect_rho_vec(Ji.data * Q, 
                                        X_rho_vec_j, 1))

        else:
            rhoss_vec = mat2vec(rhoss.full()).ravel()
            for k,w in enumerate(wlist):

                R = pseudo_inverse(L,rhoss=rhoss, w= w, sparse = sparse, 
                                   method=method)
                                   
                for i, Ji in enumerate(J_ops):
                    for j, Jj in enumerate(J_ops):
                        if i == j:
                            I[i] = expect_rho_vec(Ji.data, rhoss_vec, 1)
                            S[i, j, k] = I[i]
                        S[i, j, k] -= expect_rho_vec((Ji * R * Jj 
                                                     + Jj * R * Ji).data,
                                                     rhoss_vec, 1)
    return I, S
Пример #28
0
    def steady_state(self, max_iter_refine = 100, use_mkl = False, weighted_matching = False, series_method = False):
        """
        Computes steady state dynamics
        
        max_iter_refine : Int
            Parameter for the mkl LU solver. If pardiso errors are returned this should be increased.
        use_mkl : Boolean
            Optional override default use of mkl if mkl is installed.
        weighted_matching : Boolean
            Setting this true may increase run time, but reduce stability (pardisio may not converge).
        """
        
        
        
        nstates =  self._N_he
        sup_dim = self._sup_dim
        n = int(np.sqrt(sup_dim))
        unit_h_elems = sp.identity(nstates, format='csr')
        L = deepcopy(self.RHSmat)# + sp.kron(unit_h_elems, 
                        #liouvillian(H).data)

        b_mat = np.zeros(sup_dim*nstates, dtype=complex)
        b_mat[0] = 1.

        L = L.tolil()
        L[0, 0 : n**2*nstates] = 0.
        L = L.tocsr()
          
        if settings.has_mkl & use_mkl == True:
            print("Using Intel mkl solver")
            from qutip._mkl.spsolve import (mkl_splu, mkl_spsolve)
                  

            L = L.tocsr() + \
            sp.csr_matrix((np.ones(n), (np.zeros(n), 
                          [num*(n+1)for num in range(n)])),
                          shape=(n**2*nstates, n**2*nstates))

            L.sort_indices()
        

            
            solution = mkl_spsolve(L, b_mat, perm = None, verbose = True, \
                            max_iter_refine = max_iter_refine, \
                            scaling_vectors = True, \
                            weighted_matching = weighted_matching) 

        else:    
            if series_method == False:
                L = L.tocsc() + \
                    sp.csc_matrix((np.ones(n), (np.zeros(n), 
                                  [num*(n+1)for num in range(n)])),
                                  shape=(n**2*nstates, n**2*nstates))

                # Use superLU solver

                LU = splu(L)
                solution = LU.solve(b_mat)
            
            else:
                L = L.tocsc() + \
                    sp.csc_matrix((np.ones(n), (np.zeros(n), 
                                  [num*(n+1)for num in range(n)])),
                                  shape=(n**2*nstates, n**2*nstates))

                # Use series method
                L.sort_indices()
                solution,fidelity = lgmres(L, b_mat)
                
                
        dims = self.H_sys.dims
        data = dense2D_to_fastcsr_fmode(vec2mat(solution[:sup_dim]), n, n)
        data = 0.5*(data + data.H)

        solution = solution.reshape((nstates, self.H_sys.shape[0]**2))

        return Qobj(data, dims=dims), solution