Exemplo n.º 1
0
def test_csr_kron():
    "Sparse: Test CSR Kron"
    for kk in range(10):
        ra = np.random.randint(2,100)
        rb = np.random.randint(2,100)
        A = rand_herm(ra,0.5).data
        B = rand_herm(rb,0.5).data
        C = sp.kron(A,B, format='csr')
        D = _csr_kron(A.data,A.indices,A.indptr, A.shape[0], A.shape[1],
                    B.data,B.indices,B.indptr, B.shape[0], B.shape[1])
        assert_equal(C.data, D.data)
        assert_equal(C.indices, D.indices)
        assert_equal(C.indptr, D.indptr)
        
    for kk in range(10):
        ra = np.random.randint(2,100)
        rb = np.random.randint(2,100)
        A = rand_ket(ra,0.5).data
        B = rand_herm(rb,0.5).data
        C = sp.kron(A,B, format='csr')
        D = _csr_kron(A.data,A.indices,A.indptr, A.shape[0], A.shape[1],
                    B.data,B.indices,B.indptr, B.shape[0], B.shape[1])
        assert_equal(C.data, D.data)
        assert_equal(C.indices, D.indices)
        assert_equal(C.indptr, D.indptr)
    
    for kk in range(10):
        ra = np.random.randint(2,100)
        rb = np.random.randint(2,100)
        A = rand_dm(ra,0.5).data
        B = rand_herm(rb,0.5).data
        C = sp.kron(A,B, format='csr')
        D = _csr_kron(A.data,A.indices,A.indptr, A.shape[0], A.shape[1],
                    B.data,B.indices,B.indptr, B.shape[0], B.shape[1])
        assert_equal(C.data, D.data)
        assert_equal(C.indices, D.indices)
        assert_equal(C.indptr, D.indptr)
        
    for kk in range(10):
        ra = np.random.randint(2,100)
        rb = np.random.randint(2,100)
        A = rand_ket(ra,0.5).data
        B = rand_ket(rb,0.5).data
        C = sp.kron(A,B, format='csr')
        D = _csr_kron(A.data,A.indices,A.indptr, A.shape[0], A.shape[1],
                    B.data,B.indices,B.indptr, B.shape[0], B.shape[1])
        assert_equal(C.data, D.data)
        assert_equal(C.indices, D.indices)
        assert_equal(C.indptr, D.indptr)
Exemplo n.º 2
0
def test_csr_kron():
    "Sparse: Test CSR Kron"
    for kk in range(10):
        ra = np.random.randint(2, 100)
        rb = np.random.randint(2, 100)
        A = rand_herm(ra, 0.5).data
        B = rand_herm(rb, 0.5).data
        C = sp.kron(A, B, format='csr')
        D = _csr_kron(A.data, A.indices, A.indptr, A.shape[0], A.shape[1],
                      B.data, B.indices, B.indptr, B.shape[0], B.shape[1])
        assert_almost_equal(C.data, D.data)
        assert_equal(C.indices, D.indices)
        assert_equal(C.indptr, D.indptr)

    for kk in range(10):
        ra = np.random.randint(2, 100)
        rb = np.random.randint(2, 100)
        A = rand_ket(ra, 0.5).data
        B = rand_herm(rb, 0.5).data
        C = sp.kron(A, B, format='csr')
        D = _csr_kron(A.data, A.indices, A.indptr, A.shape[0], A.shape[1],
                      B.data, B.indices, B.indptr, B.shape[0], B.shape[1])
        assert_almost_equal(C.data, D.data)
        assert_equal(C.indices, D.indices)
        assert_equal(C.indptr, D.indptr)

    for kk in range(10):
        ra = np.random.randint(2, 100)
        rb = np.random.randint(2, 100)
        A = rand_dm(ra, 0.5).data
        B = rand_herm(rb, 0.5).data
        C = sp.kron(A, B, format='csr')
        D = _csr_kron(A.data, A.indices, A.indptr, A.shape[0], A.shape[1],
                      B.data, B.indices, B.indptr, B.shape[0], B.shape[1])
        assert_almost_equal(C.data, D.data)
        assert_equal(C.indices, D.indices)
        assert_equal(C.indptr, D.indptr)

    for kk in range(10):
        ra = np.random.randint(2, 100)
        rb = np.random.randint(2, 100)
        A = rand_ket(ra, 0.5).data
        B = rand_ket(rb, 0.5).data
        C = sp.kron(A, B, format='csr')
        D = _csr_kron(A.data, A.indices, A.indptr, A.shape[0], A.shape[1],
                      B.data, B.indices, B.indptr, B.shape[0], B.shape[1])
        assert_almost_equal(C.data, D.data)
        assert_equal(C.indices, D.indices)
        assert_equal(C.indptr, D.indptr)
Exemplo n.º 3
0
def zcsr_kron(A, B):
    """Kronecker product between two CSR format sparse
    matrices with dtype = complex.
    
    Parameters
    ----------
    A : csr_matrix
        Input matrix
    B : csr_matrix
        Input matrix
        
    Returns
    -------
    C : csr_matrix
        Kronecker product of A & B
    
    """
    if (not sp.isspmatrix_csr(A)) or (not sp.isspmatrix_csr(B)):
        raise TypeError('Both input sparse matrices must be in CSR format.')

    if (not A.dtype == complex) or (not B.dtype == complex):
        raise TypeError('Both input sparse matrices must have dtype=complex.')

    C = _csr_kron(A.data, A.indices, A.indptr, A.shape[0], A.shape[1], B.data,
                  B.indices, B.indptr, B.shape[0], B.shape[1])
    return C
Exemplo n.º 4
0
def zcsr_kron(A,B):
    """Kronecker product between two CSR format sparse
    matrices with dtype = complex.
    
    Parameters
    ----------
    A : csr_matrix
        Input matrix
    B : csr_matrix
        Input matrix
        
    Returns
    -------
    C : csr_matrix
        Kronecker product of A & B
    
    """
    if (not sp.isspmatrix_csr(A)) or (not sp.isspmatrix_csr(B)):
        raise TypeError('Both input sparse matrices must be in CSR format.')
    
    if (not A.dtype == complex) or (not B.dtype == complex):
        raise TypeError('Both input sparse matrices must have dtype=complex.')
    
    C = _csr_kron(A.data, A.indices, A.indptr, A.shape[0], A.shape[1],
                B.data, B.indices, B.indptr, B.shape[0], B.shape[1])
    return C
Exemplo n.º 5
0
def liouvillian(H, c_ops=[], data_only=False, chi=None):
    """Assembles the Liouvillian superoperator from a Hamiltonian
    and a ``list`` of collapse operators. Like liouvillian, but with an
    experimental implementation which avoids creating extra Qobj instances,
    which can be advantageous for large systems.

    Parameters
    ----------
    H : qobj
        System Hamiltonian.

    c_ops : array_like
        A ``list`` or ``array`` of collapse operators.

    Returns
    -------
    L : qobj
        Liouvillian superoperator.

    """

    if chi and len(chi) != len(c_ops):
        raise ValueError('chi must be a list with same length as c_ops')

    if H is not None:
        if H.isoper:
            op_dims = H.dims
            op_shape = H.shape
        elif H.issuper:
            op_dims = H.dims[0]
            op_shape = [np.prod(op_dims[0]), np.prod(op_dims[0])]
        else:
            raise TypeError("Invalid type for Hamiltonian.")
    else:
        # no hamiltonian given, pick system size from a collapse operator
        if isinstance(c_ops, list) and len(c_ops) > 0:
            c = c_ops[0]
            if c.isoper:
                op_dims = c.dims
                op_shape = c.shape
            elif c.issuper:
                op_dims = c.dims[0]
                op_shape = [np.prod(op_dims[0]), np.prod(op_dims[0])]
            else:
                raise TypeError("Invalid type for collapse operator.")
        else:
            raise TypeError("Either H or c_ops must be given.")

    sop_dims = [[op_dims[0], op_dims[0]], [op_dims[1], op_dims[1]]]
    sop_shape = [np.prod(op_dims), np.prod(op_dims)]

    spI = sp.identity(op_shape[0], format='csr', dtype=complex)

    if H:
        if H.isoper:
            Ht = H.data.T.tocsr()
            data = -1j * zcsr_kron(spI, H.data)
            data += 1j * zcsr_kron(Ht, spI)
        else:
            data = H.data
    else:
        data = sp.csr_matrix((sop_shape[0], sop_shape[1]), dtype=complex)

    for idx, c_op in enumerate(c_ops):
        if c_op.issuper:
            data = data + c_op.data
        else:
            cd = c_op.data.T.conj().tocsr()
            c = c_op.data
            # Here we call _csr_kron directly as we can avoid creating
            # another sparse matrix by passing c.data.conj()
            if chi:
                data = data + np.exp(1j * chi[idx]) * \
                                _csr_kron(c.data.conj(), c.indices, c.indptr,
                                        c.shape[0], c.shape[1],
                                        c.data, c.indices, c.indptr,
                                        c.shape[0], c.shape[1])
            else:
                data = data + _csr_kron(c.data.conj(), c.indices, c.indptr,
                                        c.shape[0], c.shape[1],
                                        c.data, c.indices, c.indptr,
                                        c.shape[0], c.shape[1])
            cdc = cd * c
            cdct = cdc.T.tocsr()
            data = data - 0.5 * zcsr_kron(spI, cdc)
            data = data - 0.5 * zcsr_kron(cdct, spI)

    if data_only:
        return data
    else:
        L = Qobj()
        L.dims = sop_dims
        L.data = data
        L.isherm = False
        L.superrep = 'super'
        return L