Example #1
0
 def test_seed(self, dtype, sparse):
     a = qu.rand_matrix(10, sparse=sparse, dtype=dtype, seed=42)
     b = qu.rand_matrix(10, sparse=sparse, dtype=dtype, seed=42)
     if sparse:
         assert_allclose(a.data, b.data)
     else:
         assert_allclose(a, b)
Example #2
0
 def test_2d_simple(self):
     a = (qu.rand_matrix(2), qu.rand_matrix(2))
     dims = ((2, 3), (3, 2))
     inds = ((0, 0), (1, 1))
     b = qu.ikron(a, dims, inds)
     assert b.shape == (36, 36)
     assert_allclose(b, a[0] & qu.eye(9) & a[1])
Example #3
0
 def test_2d_simple(self):
     a = (rand_matrix(2), rand_matrix(2))
     dims = ((2, 3), (3, 2))
     inds = ((0, 0), (1, 1))
     b = eyepad(a, dims, inds)
     assert b.shape == (36, 36)
     assert_allclose(b, a[0] & eye(9) & a[1])
Example #4
0
 def test_ikron_ownership(self, sparse, ri, rf):
     dims = [7, 2, 4, 3]
     X = qu.rand_matrix(2, sparse=sparse)
     Y = qu.rand_matrix(3, sparse=sparse)
     X1 = qu.ikron((X, Y), dims, (1, 3))[ri:rf, :]
     X2 = qu.ikron((X, Y), dims, (1, 3), ownership=(ri, rf))
     assert_allclose(X1.A, X2.A)
Example #5
0
 def test_sparse(self):
     i = qu.eye(2, sparse=True)
     a = qu.qu(qu.rand_matrix(2), sparse=True)
     b = qu.ikron(a, [2, 2, 2], 1)  # infer sparse
     assert (qu.issparse(b))
     assert_allclose(b.A, (i & a & i).A)
     a = qu.rand_matrix(2)
     b = qu.ikron(a, [2, 2, 2], 1, sparse=True)  # explicit sparse
     assert (qu.issparse(b))
     assert_allclose(b.A, (i & a & i).A)
Example #6
0
 def test_sparse(self):
     i = eye(2, sparse=True)
     a = qu(rand_matrix(2), sparse=True)
     b = eyepad(a, [2, 2, 2], 1)  # infer sparse
     assert(issparse(b))
     assert_allclose(b.A, (i & a & i).A)
     a = rand_matrix(2)
     b = eyepad(a, [2, 2, 2], 1, sparse=True)  # explicit sparse
     assert(issparse(b))
     assert_allclose(b.A, (i & a & i).A)
Example #7
0
 def test_ndarrays(self):
     a = rand_matrix(2)
     i = eye(2)
     b = eyepad([a], np.array([2, 2, 2]), [0, 2])
     assert_allclose(b, a & i & a)
     b = eyepad([a], [2, 2, 2], np.array([0, 2]))
     assert_allclose(b, a & i & a)
Example #8
0
 def test_ndarrays(self):
     a = qu.rand_matrix(2)
     i = qu.eye(2)
     b = qu.ikron([a], np.array([2, 2, 2]), [0, 2])
     assert_allclose(b, a & i & a)
     b = qu.ikron([a], [2, 2, 2], np.array([0, 2]))
     assert_allclose(b, a & i & a)
Example #9
0
 def test_ldmul_large(self):
     vec = np.random.randn(501)
     mat = rand_matrix(501)
     a = ldmul(vec, mat)
     b = np.diag(vec) @ mat
     assert isinstance(a, np.matrix)
     assert_allclose(a, b)
Example #10
0
 def test_rdmul_large(self):
     vec = np.random.randn(501)
     mat = rand_matrix(501)
     a = rdmul(mat, vec)
     b = mat @ np.diag(vec)
     assert isinstance(a, np.matrix)
     assert_allclose(a, b)
Example #11
0
 def test_mid_multi_reverse(self):
     a = [qu.rand_matrix(2) for i in range(3)]
     i = qu.eye(2)
     dims = [2, 2, 2, 2, 2, 2]
     inds = [5, 4, 1]
     b = qu.ikron(a, dims, inds)
     assert_allclose(b, i & a[2] & i & i & a[1] & a[0])
Example #12
0
 def test_mid_multi(self):
     a = [rand_matrix(2) for i in range(3)]
     i = eye(2)
     dims = [2, 2, 2, 2, 2, 2]
     inds = [1, 2, 4]
     b = eyepad(a, dims, inds)
     assert_allclose(b, i & a[0] & a[1] & i & a[2] & i)
Example #13
0
 def test_rand_matrix_bsr(self, dtype):
     a = qu.rand_matrix(10,
                        sparse=True,
                        density=0.2,
                        stype='bsr',
                        dtype=dtype)
     assert a.shape == (10, 10)
     assert type(a) == sp.bsr_matrix
     assert a.dtype == dtype
Example #14
0
 def test_overlap(self):
     a = [qu.rand_matrix(4) for i in range(2)]
     dims1 = [2, 2, 2, 2, 2, 2]
     dims2 = [2, 4, 4, 2]
     b = qu.ikron(a, dims1, [1, 2, 3, 4])
     c = qu.ikron(a, dims2, [1, 2])
     assert_allclose(c, b)
     dims2 = [4, 2, 2, 4]
     b = qu.ikron(a, dims1, [0, 1, 4, 5])
     c = qu.ikron(a, dims2, [0, 3])
     assert_allclose(c, b)
Example #15
0
def srho_dot_ls():
    rho = rand_rho(3)
    ham = rand_herm(3, sparse=True, density=0.5)
    gamma = 0.7
    ls = [rand_matrix(3, sparse=True, density=0.5) for _ in range(3)]
    rhodl = -1.0j * (ham @ rho - rho @ ham)
    for l in ls:
        rhodl += gamma * (l @ rho @ l.H)
        rhodl -= gamma * 0.5 * (rho @ l.H @ l)
        rhodl -= gamma * 0.5 * (l.H @ l @ rho)
    return rho, ham, gamma, ls, rhodl
Example #16
0
def rho_dot_ls():
    np.random.seed(1)
    rho = rand_rho(3)
    ham = rand_herm(3)
    gamma = 0.7
    ls = [rand_matrix(3) for _ in range(3)]
    rhodl = -1.0j * (ham @ rho - rho @ ham)
    for l in ls:
        rhodl += gamma * (l @ rho @ l.H)
        rhodl -= gamma * 0.5 * (rho @ l.H @ l)
        rhodl -= gamma * 0.5 * (l.H @ l @ rho)
    return rho, ham, gamma, ls, rhodl
Example #17
0
 def test_basic(self):
     a = qu.rand_matrix(2)
     i = qu.eye(2)
     dims = [2, 2, 2]
     b = qu.ikron([a], dims, [0])
     assert_allclose(b, a & i & i)
     b = qu.ikron([a], dims, [1])
     assert_allclose(b, i & a & i)
     b = qu.ikron([a], dims, [2])
     assert_allclose(b, i & i & a)
     b = qu.ikron([a], dims, [0, 2])
     assert_allclose(b, a & i & a)
     b = qu.ikron([a], dims, [0, 1, 2])
     assert_allclose(b, a & a & a)
Example #18
0
    def test_epeps_absorb_3body_tensor(self, Lx, Ly, method):
        '''Currently only tests 3-body ops!

        contract_methods = {0: False,  1: 'triangle_absorb'}
        '''

        # opts for splitting 'blob' in `triangle_absorb` method
        compress_opts = {'method': method}

        epeps = qnets.QubitEncodeVector.rand(Lx,
                                             Ly).convert_to_ePEPS(dummy_size=1)
        bra = epeps.H

        # compile (vertex, vertex, face) 'target' qubits
        LatticeCooHam = hams.SpinlessSimHam(Lx, Ly).\
            convert_to_coordinate_ham(lambda q: epeps.qubit_to_coo_map[q])

        for coos, _ in LatticeCooHam.gen_ham_terms():

            # SKIP 2-body interactions
            if len(coos) == 2:
                continue

            rand_gate = qu.rand_matrix(8)  #use a random 3-body gate

            G_ket_0 = epeps.gate(G=rand_gate, coos=coos, contract=False)
            G_ket_1 = epeps.gate(G=rand_gate,
                                 coos=coos,
                                 contract='triangle_absorb',
                                 **compress_opts)

            expec_0 = (bra & G_ket_0) ^ all  # `contract=False` reference
            expec_1 = (bra & G_ket_1) ^ all  # `triangle_absorb` reference

            assert expec_1 == pytest.approx(expec_0, rel=1e-2)

            coos = (coos[1], coos[0], coos[2]
                    )  # now try w/ vertex qubits swapped

            G_ket_0 = epeps.gate(G=rand_gate, coos=coos, contract=False)
            G_ket_2 = epeps.gate(G=rand_gate,
                                 coos=coos,
                                 contract='triangle_absorb',
                                 **compress_opts)

            expec_0 = (bra & G_ket_0) ^ all
            expec_2 = (bra & G_ket_2) ^ all
            assert expec_2 == pytest.approx(expec_0, rel=1e-2)
Example #19
0
 def test_holey_overlap(self):
     a = qu.rand_matrix(8)
     dims1 = (2, 2, 2, 2, 2)
     dims2 = (2, 8, 2)
     b = qu.ikron(a, dims1, (1, 3))
     c = qu.ikron(a, dims2, 1)
     assert_allclose(b, c)
     dims1 = (2, 2, 2, 2, 2)
     dims2 = (2, 2, 8)
     b = qu.ikron(a, dims1, (2, 4))
     c = qu.ikron(a, dims2, 2)
     assert_allclose(b, c)
     dims1 = (2, 2, 2, 2, 2)
     dims2 = (8, 2, 2)
     b = qu.ikron(a, dims1, (0, 2))
     c = qu.ikron(a, dims2, 0)
     assert_allclose(b, c)
Example #20
0
    def test_qev_triangle_absorb_method(self, Lx, Ly, method):
        '''Test 'triangle_absorb' method, which works for 
        `QubitEncodeVector`-type geometry without rotating the 
        face tensors (i.e. non-rectangular lattice geometry).
        '''
        # opts for splitting 'blob' in `triangle_absorb` method
        compress_opts = {'method': method}

        # without rotating face tensors!
        psi = qnets.QubitEncodeVector.rand(Lx, Ly, bond_dim=2)
        bra = psi.H
        # norm = (bra & psi) ^ all

        # compile all 3-tuples (vertex, vertex, face) qubits to act on
        LatticeHam = hams.SpinlessSimHam(Lx, Ly)
        for where, _ in LatticeHam.gen_ham_terms():

            # skip 2-body terms
            if len(where) == 2:
                continue

            rand_gate = qu.rand_matrix(8)

            # apply gate both 0) without contracting, and 1) with 'triangle-absorb'
            G_ket_0 = psi.apply_gate(G=rand_gate, where=where, contract=False)
            G_ket_1 = psi.apply_gate(G=rand_gate,
                                     where=where,
                                     contract='triangle_absorb',
                                     **compress_opts)

            expec_0 = (bra & G_ket_0) ^ all
            expec_1 = (bra & G_ket_1) ^ all
            assert expec_1 == pytest.approx(expec_0, rel=1e-2)

            ## also try with vertex qubits switched!
            where = (where[1], where[0], where[2])
            G_ket_0 = psi.apply_gate(G=rand_gate, where=where, contract=False)
            G_ket_2 = psi.apply_gate(G=rand_gate,
                                     where=where,
                                     contract='triangle_absorb',
                                     **compress_opts)

            expec_0 = (bra & G_ket_0) ^ all
            expec_2 = (bra & G_ket_2) ^ all
            assert expec_2 == pytest.approx(expec_0, rel=1e-2)
Example #21
0
    def test_epeps_absorb_3body_gate(self, Lx, Ly, method):
        '''Test absorbing a 3-body dense, random gate into the tn.
        '''

        # opts for splitting 'blob' in `triangle_absorb` method
        compress_opts = {'method': method}

        epeps = qnets.QubitEncodeVector.rand(Lx,
                                             Ly).convert_to_ePEPS(dummy_size=1)
        bra = epeps.H
        # norm = (bra & epeps) ^ all

        # use Ham to get (vertex, vertex, face) 'target' qubits
        LatticeCooHam = hams.SpinlessSimHam(Lx, Ly).\
            convert_to_coordinate_ham(lambda q: epeps.qubit_to_coo_map[q])

        for coos, _ in LatticeCooHam.gen_ham_terms():

            # skip 2-body interactions
            if len(coos) == 2:
                continue

            rand_gate = qu.rand_matrix(8)  #use a random 3-body gate

            G_ket_0 = epeps.gate(G=rand_gate, coos=coos, contract=False)
            G_ket_1 = epeps.absorb_three_body_gate(G=rand_gate,
                                                   coos=coos,
                                                   **compress_opts)

            expec_0 = (bra & G_ket_0) ^ all  # `contract=False` reference
            expec_1 = (bra & G_ket_1) ^ all  # `triangle_absorb` reference

            assert expec_1 == pytest.approx(expec_0, rel=1e-2)

            # now try with the vertex qubits swapped
            coos = (coos[1], coos[0], coos[2])
            G_ket_0 = epeps.gate(G=rand_gate, coos=coos, contract=False)
            G_ket_2 = epeps.absorb_three_body_gate(G=rand_gate,
                                                   coos=coos,
                                                   **compress_opts)

            expec_0 = (bra & G_ket_0) ^ all
            expec_2 = (bra & G_ket_2) ^ all
            assert expec_2 == pytest.approx(expec_0, rel=1e-2)
Example #22
0
    def test_epeps_2body_reduce_split(self, Lx, Ly, contract):

        epeps = qnets.QubitEncodeVector.rand(Lx,
                                             Ly).convert_to_ePEPS(dummy_size=1)
        bra = epeps.H

        # compile (vertex, vertex) 'target' qubit pairs
        LatticeCooHam = hams.SpinlessSimHam(Lx, Ly).\
            convert_to_coordinate_ham(lambda q: epeps.qubit_to_coo_map[q])

        for coos, _ in LatticeCooHam.gen_ham_terms():

            # skip 3-body terms here
            if len(coos) == 3:
                continue

            rand_gate = qu.rand_matrix(4)

            G_ket_0 = epeps.gate(G=rand_gate, coos=coos, contract=False)
            G_ket_1 = epeps.gate(G=rand_gate, coos=coos, contract=contract)

            assert (bra & G_ket_1) ^ all == pytest.approx(
                (bra & G_ket_0) ^ all, rel=1e-2)
Example #23
0
def rand_rect(m, n, sparse=False, dtype=complex):
    X = qu.rand_matrix(max(m, n), dtype=dtype, sparse=sparse)
    return X[:m, :n]
Example #24
0
def mat_s_nnz():
    return rand_matrix(_TEST_SZ, sparse=True, density=0.75)
Example #25
0
def mat_s2():
    return rand_matrix(_TEST_SZ, sparse=True, density=0.5)
Example #26
0
def mat_d3():
    return rand_matrix(_TEST_SZ)
Example #27
0
 def test_axes_types(self, axes):
     a = qu.rand_matrix(4)
     b = qu.itrace(a, axes)
     assert_allclose(b, np.trace(a))
Example #28
0
def os1():
    return qu.rand_matrix(3, sparse=True, density=0.5)
Example #29
0
 def test_kron_ownership(self, sparse, ri, rf):
     dims = [7, 2, 4, 3]
     ops = [qu.rand_matrix(d, sparse=sparse) for d in dims]
     X1 = qu.kron(*ops)[ri:rf, :]
     X2 = qu.kron(*ops, ownership=(ri, rf))
     assert_allclose(X1.A, X2.A)
Example #30
0
 def test_auto(self):
     a = qu.rand_matrix(2)
     i = qu.eye(2)
     b = qu.ikron([a], (2, -1, 2), [1])
     assert_allclose(b, i & a & i)