Ejemplo n.º 1
0
 def test_gate_swap_and_split(self):
     n = 10
     p = MPS_computational_state('0' * n)
     assert p.bond_sizes() == [1] * (n - 1)
     G = qu.rand_uni(4)
     p.gate_(G, (1, n - 2), contract='swap+split')
     assert p.bond_sizes() == [1] + [2] * (n - 3) + [1]
Ejemplo n.º 2
0
def mat_herm_dense():
    np.random.seed(1)
    u = qu.rand_uni(4)
    a = u @ qu.ldmul(np.array([-1, 2, 4, -3]), u.H)
    #  |--|--|--|--|--|--|--|
    # -3    -1        2     4
    return u, a
Ejemplo n.º 3
0
 def test_rand_uni(self, dtype):
     u = qu.rand_uni(3, dtype=dtype)
     assert u.shape == (3, 3)
     assert type(u) == qu.qarray
     assert u.dtype == dtype
     # low tolerances for float32 etc
     assert_allclose(qu.eye(3), u @ u.H, atol=1e-7, rtol=1e-5)
     assert_allclose(qu.eye(3), u.H @ u, atol=1e-7, rtol=1e-5)
Ejemplo n.º 4
0
 def test_gate_no_contract(self, bsz, propagate_tags, contract):
     p = MPS_rand_state(5, 7)
     q = p.copy()
     G = qu.rand_uni(2**bsz)
     p = p.gate(G,
                where=[i for i in range(2, 2 + bsz)],
                tags='G',
                contract=contract)
     TG = p['G']
     if propagate_tags or contract:
         assert p.site_tag(2) in TG.tags
     assert p.H @ p == pytest.approx(1.0)
     assert abs(q.H @ p) < 1.0
     assert len(p.tensors) == 6 - int(contract) * bsz
     assert set(p.outer_inds()) == {'k{}'.format(i) for i in range(5)}
Ejemplo n.º 5
0
    def test_auto_split_detection(self):
        psi0 = MPS_computational_state('00')
        CNOT = qu.controlled('not')
        ISWAP = qu.iswap()
        G = qu.rand_uni(4)

        opts = {'contract': 'auto-split-gate', 'where': (0, 1)}

        psi_cnot = psi0.gate(CNOT, **opts)
        psi_iswap = psi0.gate(ISWAP, **opts)
        psi_G = psi0.gate(G, **opts)

        assert (psi_cnot.max_bond() == psi_iswap.max_bond() ==
                psi_G.max_bond() == 2)

        assert len(psi_cnot.tensors) == len(psi_iswap.tensors) == 4
        assert len(psi_G.tensors) == 3
Ejemplo n.º 6
0
 def test_gate_no_contract(self, bsz, propagate_tags, contract):
     p = MPS_rand_state(5, 7, tags={'PSI0'})
     q = p.copy()
     G = qu.rand_uni(2**bsz)
     p = p.gate_(G,
                 where=[i for i in range(2, 2 + bsz)],
                 tags='G',
                 contract=contract,
                 propagate_tags=propagate_tags)
     TG = p['G']
     if propagate_tags or contract:
         assert p.site_tag(2) in TG.tags
     assert ('PSI0' in TG.tags) == (propagate_tags is True) or contract
     assert (p.H & p) ^ all == pytest.approx(1.0)
     assert abs((q.H & p) ^ all) < 1.0
     assert len(p.tensors) == 6 - int(contract) * bsz
     assert set(p.outer_inds()) == {f'k{i}' for i in range(5)}
Ejemplo n.º 7
0
def ham_rcr_psi():
    # Define a random hamiltonian with a known recurrence time
    d = 3
    np.random.seed(1)
    ems = np.random.randint(1, 6, d)
    ens = np.random.randint(1, 6, d)  # eigenvalues as rational numbers
    # numerator lowest common divisor
    LCD = reduce(gcd, ems)
    # denominator lowest common multiple
    LCM = reduce(lambda a, b: a * b // gcd(a, b), ens)
    trc = 2 * pi * LCM / LCD
    evals = np.array(ems) / np.array(ens)
    v = rand_uni(d)
    ham = v @ np.diag(evals) @ v.H
    p0 = rand_ket(d)
    tm = 0.573 * trc
    pm = v @ np.diag(np.exp(-1.0j * tm * evals)) @ v.H @ p0
    return ham, trc, p0, tm, pm
Ejemplo n.º 8
0
    def test_gate2split(self):
        psi = MPS_rand_state(10, 3)
        psi2 = psi.copy()
        G = qu.eye(2) & qu.eye(2)
        psi.gate2split(G, (2, 3), cutoff=0)
        assert psi.bond_size(2, 3) == 6
        assert psi.H @ psi2 == pytest.approx(1.0)

        # check a unitary application
        G = qu.rand_uni(2**2)
        psi.gate2split(G, (7, 8))
        psi.compress()
        assert psi.bond_size(2, 3) == 3
        assert psi.bond_size(7, 8) > 3
        assert psi.H @ psi == pytest.approx(1.0)
        assert abs(psi2.H @ psi) < 1.0

        # check matches dense application of gate
        psid = psi2.to_dense()
        Gd = qu.ikron(G, [2] * 10, (7, 8))
        assert psi.to_dense().H @ (Gd @ psid) == pytest.approx(1.0)
Ejemplo n.º 9
0
def mat_nherm_sparse():
    np.random.seed(1)
    u, v = rand_uni(5), rand_uni(5)
    a = u @ ldmul(np.array([1, 2, 4, 3, 0.1]), v.H)
    a = qu(a, sparse=True)
    return u, v, a
Ejemplo n.º 10
0
def ham2():
    u = qu.rand_uni(7)
    el = np.array([-3.72, 0, 1, 1.1, 2.1, 2.2, 6.28])
    return u @ qu.ldmul(el, u.H)
Ejemplo n.º 11
0
def ham1():
    evecs = rand_uni(5)
    evals = np.array([-5, -3, 0.1, 2, 4])
    return dot(evecs, ldmul(evals, evecs.H))
Ejemplo n.º 12
0
def mat_nherm_sparse():
    np.random.seed(1)
    u, v = qu.rand_uni(5), qu.rand_uni(5)
    a = u @ qu.ldmul(np.array([1, 2, 4, 3, 0.1]), v.H)
    a = qu.sparse(a)
    return u, v, a
Ejemplo n.º 13
0
def ham1():
    u = qu.rand_uni(7)
    el = np.array([-3, 0, 1, 2, 3, 4, 7])
    return u @ qu.ldmul(el, u.H)
Ejemplo n.º 14
0
def mat_herm_sparse():
    np.random.seed(1)
    u = qu.rand_uni(4)
    a = u @ qu.ldmul(np.array([-1, 2, 4, -3]), u.H)
    a = qu.sparse(a)
    return u, a
Ejemplo n.º 15
0
 def test_rand_uni(self):
     u = rand_uni(3)
     assert u.shape == (3, 3)
     assert type(u) == np.matrix
     assert_allclose(eye(3), chop(u @ u.H, inplace=False))
     assert_allclose(eye(3), chop(u.H @ u, inplace=False))
Ejemplo n.º 16
0
def prematsparse():
    u = rand_uni(4)
    a = u @ ldmul(np.array([-1, 2, 4, -3]), u.H)
    a = qu(a, sparse=True)
    return u, a
Ejemplo n.º 17
0
def mat_herm_sparse():
    np.random.seed(1)
    u = rand_uni(4)
    a = u @ ldmul(np.array([-1, 2, 4, -3]), u.H)
    a = qu(a, sparse=True)
    return u, a
Ejemplo n.º 18
0
def mat_nherm_dense():
    np.random.seed(1)
    u, v = rand_uni(5), rand_uni(5)
    a = u @ ldmul(np.array([1, 2, 4, 3, 0.1]), v.H)
    return u, v, a