예제 #1
0
def test_model2():
    J, Jz = -3., 2
    N1, N2 = 2, 3
    scfg = SpinSpaceConfig([N1 * N2, 2])
    config = array([1, 1, 0, 1, 1, 0])
    #config=array([1,1,0,0,1,0,1,1,0])
    #construct true H
    for periodic in [True, False]:
        h = HeisenbergH2D(N1, N2, J=J, Jz=Jz, periodic=periodic)
        H = FakeVMC(h).get_H()
        print 'Testing rmatmul of Hamiltonian(%s)' % ('PBC'
                                                      if periodic else 'OBC')
        wl, flips = h._rmatmul(1 - 2 * config)
        configs = []
        for flip in flips:
            nc = copy(1 - 2 * config)
            nc[asarray(flip)] *= -1
            configs.append(nc)
        ss = SparseState(wl, configs)

        vec = ss.tovec(scfg)
        v0 = zeros(scfg.hndim)
        v0[scfg.config2ind(config)] = 1
        v_true = v0.dot(H)
        assert_allclose(vec, v_true)
예제 #2
0
class TestLinop(object):
    def __init__(self):
        J = 1.
        nsite = 4
        self.scfg = SpinSpaceConfig([nsite, 2])

        #construct true H
        I2, I4 = eye(2), eye(4)
        h2 = J / 4. * (kron(sx, sx) + kron(sz, sz) + kron(sy, sy))

        #construct operator H act on config
        self.h1 = TFI(nsite=4)
        self.h2 = HeisenbergH(nsite=4)
        self.toy1, self.toy2 = FakeVMC(self.h1), FakeVMC(self.h2)
        self.H1, self.H2 = self.toy1.get_H(), self.toy2.get_H()
        self.pW = PartialW()

        #generate a random rbm and the corresponding vector v
        self.rbm = random_rbm(nin=nsite, nhid=nsite)
        self.v = self.rbm.tovec(self.scfg)

        self.rbm_g = random_rbm(nin=nsite, nhid=nsite, group=TIGroup(4))
        self.v_g = self.rbm_g.tovec(self.scfg)

    def test_sandwichh(self):
        for h, H in [(self.h1, self.H1), (self.h2, self.H2)]:
            print 'Testing sandwich %s.' % h.__class__
            config = random.randint(0, 2, 4)
            ket = zeros(self.scfg.hndim)
            ket[self.scfg.config2ind(config)] = 1

            H, v, v_g = self.H2, self.v, self.v_g
            print 'Testing the non-group version'
            O_true = ket.conj().dot(H).dot(v) / sum(ket.conj() * v)
            cg = RBMConfigGenerator(nflip=2, initial_config=1 - 2 * config)
            cg.set_state(self.rbm)

            O_s = c_sandwich(self.h2, cg)
            assert_almost_equal(O_s, O_true, decimal=8)

            print 'Testing the group version'
            cg.set_state(self.rbm_g)
            O_sg = c_sandwich(self.h2, cg)
            O_trueg = ket.conj().dot(H).dot(v_g) / sum(ket.conj() * v_g)

            assert_almost_equal(O_sg, O_trueg, decimal=8)

    def test_sandwichpw(self):
        print 'Testing sandwich PartialW.'
        config = random.randint(0, 2, 4)
        ket = zeros(self.scfg.hndim)
        ket[self.scfg.config2ind(config)] = 1
        H, v, v_g = self.H, self.v, self.v_g

        cg = RBMConfigGenerator(nflip=2, initial_config=1 - 2 * config)
        cg.set_state(self.rbm)
        O_true = ket.conj().dot(Wmat).dot(v) / sum(ket.conj() * v)
        O_s = c_sandwich(self.pW, cg)

        O_trueg = ket.conj().dot(Wmat).dot(v_g) / sum(ket.conj() * v_g)
        cg.set_state(self.rbm_g)
        O_sg = c_sandwich(self.pW, cg)

        assert_almost_equal(O_s, O_true, decimal=8)
        assert_almost_equal(O_sg, O_trueg, decimal=8)