示例#1
0
def test_add():
    dps = (2,2,2,2)
    #np.random.seed(417)
    mps_obc = dMPX.rand(dps, D=2, bc="obc")
    mps_obc2 = dMPX.rand(dps, D=2, bc="obc")
    mps_pbc = dMPX.rand(dps, D=2, bc="pbc")
    mps_pbc2 = dMPX.rand(dps, D=2, bc="pbc")
    
    assert np.allclose(np.linalg.norm(MPX.asfull(mps_obc)+MPX.asfull(mps_obc2)),
                       MPX.norm(dMPX.add(mps_obc,mps_obc2)))
    assert np.allclose(np.linalg.norm(MPX.asfull(mps_pbc)+MPX.asfull(mps_pbc2)),
                       MPX.norm(dMPX.add(mps_pbc,mps_pbc2)))
    assert np.allclose(np.linalg.norm(MPX.asfull(mps_obc)+MPX.asfull(mps_pbc2)),
                       MPX.norm(dMPX.add(mps_obc,mps_pbc2)))
示例#2
0
def test_norm():
    dp = (1,3,2,2)
    np.random.seed(417)
    mps_obc = dMPX.rand(dp, D=7, bc="obc")
    mps_pbc = dMPX.zeros(dp, D=7, bc="pbc")
    dMPX.overwrite(mps_obc, out=mps_pbc)
    
    norm_o = np.linalg.norm(MPX.asfull(mps_obc))
    norm_p = np.linalg.norm(MPX.asfull(mps_pbc))

    norm_o1 = MPX.norm(mps_obc)
    norm_p1 = MPX.norm(mps_pbc)
    assert np.allclose(norm_o, norm_o1)
    assert np.allclose(norm_p, norm_p1)
示例#3
0
def test_dotcompress():
    dps = [1,2,3,4,2]
    dpo = [(d,d) for d in dps]
    mps = dMPX.rand(dps)
    mpo = dMPX.rand(dpo)
 
    mps2 = MPX.dot(mpo,mps)
    mpsc, errc = MPX.compress(mps2,4)

    mpsdc, errdc = MPX.dot_compress(mpo,mps,4)

    print errc, errdc, errdc-errc

    diff_mps = dMPX.add(MPX.mul(-1,mpsc),mpsdc)
    assert(MPX.norm(diff_mps) < 1.0e-8), MPX.norm(diff_mps)
示例#4
0
def initialize_heisenberg(N, h, J, M):
    """
    Initialize the MPS, MPO, lopr and ropr.
    """
    # MPS
    mpss = dMPX.rand([2] * N, D=M, bc='obc', seed=0)
    normalize_factor = 1.0 / MPSblas.norm(mpss)
    mpss = MPSblas.mul(normalize_factor, mpss)

    # make MPS right canonical
    for i in xrange(N - 1, 0, -1):
        mpss[i], gaug = canonicalize(0, mpss[i], M=M)
        mpss[i - 1] = einsum("ijk, kl -> ijl", mpss[i - 1], gaug)

    # MPO
    mpos = np.asarray(heisenberg_mpo(N, h, J))

    # lopr
    loprs = [np.array([[[1.0]]])]
    # ropr
    roprs = [np.array([[[1.0]]])]
    for i in xrange(N - 1, 0, -1):
        roprs.append(
            renormalize(0, mpos[i], roprs[-1], mpss[i].conj(), mpss[i]))

    # NOTE the loprs and roprs should be list currently to support pop()!
    return mpss, mpos, loprs, roprs
示例#5
0
def test_mul():
    dps = [1, 5, 4]
    mps1 = dMPX.rand(dps, 4, bc="obc")

    alpha = -1
    mps2 = MPX.mul(alpha, mps1)
  
    norm1 = MPX.norm(mps1)
    norm2 = MPX.norm(mps2)
    ovlp  = MPX.dot(mps1.conj(),mps2)

    print 'scaling used: ', np.abs(alpha), np.angle(alpha)
    print 'ratio of norms: ', norm2 / norm1
    print 'scaling', ovlp / (norm1**2)
    assert(np.allclose(norm1, norm2))
    assert(np.allclose(ovlp / (norm1 ** 2), alpha))
示例#6
0
def test_norm():
    dp = (2, 3, 2)
    np.random.seed(417)
    mps_obc = sMPX.rand(dp, D=3, bc="obc")
    print mps_obc[0]
    mps_full = MPX.asfull(mps_obc)
    print np.linalg.norm(mps_full)
    print "norm", MPX.norm(mps_obc)

    mps_pbc = sMPX.zeros(dp, D=7, bc="pbc")
    sMPX.overwrite(mps_obc, out=mps_pbc)

    norm_o = np.linalg.norm(MPX.asfull(mps_obc))
    norm_p = np.linalg.norm(MPX.asfull(mps_pbc))

    norm_o1 = MPX.norm(mps_obc)
    norm_p1 = MPX.norm(mps_pbc)
    print norm_o, norm_o1, norm_p, norm_p1
    assert np.allclose(norm_o, norm_o1)
    assert np.allclose(norm_p, norm_p1)
示例#7
0
def test_add():
    dps = (2, 2, 2, 2)
    #np.random.seed(417)
    mps_obc = sMPX.rand(dps, D=4, bc="obc")
    mps_obc2 = sMPX.rand(dps, D=4, bc="obc")
    mps_pbc = sMPX.rand(dps, D=4, bc="pbc")
    mps_pbc2 = sMPX.rand(dps, D=4, bc="pbc")

    print mps_obc[0]
    print mps_obc[0].shape
    print mps_obc[0].coords
    print mps_obc[0].data

    assert np.allclose(
        np.linalg.norm(MPX.asfull(mps_obc) + MPX.asfull(mps_obc2)),
        MPX.norm(sMPX.add(mps_obc, mps_obc2)))
    assert np.allclose(
        np.linalg.norm(MPX.asfull(mps_pbc) + MPX.asfull(mps_pbc2)),
        MPX.norm(sMPX.add(mps_pbc, mps_pbc2)))
    assert np.allclose(
        np.linalg.norm(MPX.asfull(mps_obc) + MPX.asfull(mps_pbc2)),
        MPX.norm(sMPX.add(mps_obc, mps_pbc2)))
示例#8
0
def test_compress():
    #np.random.seed(417)
    dps = [2] * 10
    mps0 = dMPX.rand(dps, D=7, bc="obc")

    smps1 = sMPX.from_dense(mps0)
    print "Check sparse", [m.__class__.__name__ for m in smps1]
    # check dimension preserving
    mps00 = dMPX.add(mps0, mps0)
    smps11 = sMPX.add(smps1, smps1)

    print MPX.norm(mps00), MPX.norm(smps11)

    print "Initial dimensions", [m.shape for m in mps00]
    # compress
    for D in (2, 3, 7):
        mps00c = MPX.compress(mps00, D, preserve_dim="false")
        print "After DENSE compress", D, MPX.norm(mps00c)
        #print "Compressed dimensions", [m.shape for m in mps00c]

    print "IN SPARSE part"
    for D in (2, 3, 7):
        smps11c = MPX.compress(smps11, D, preserve_dim="false")
        print "After SPARSE compress", D, MPX.norm(smps11c)
示例#9
0
def test_compress_pbc():
    np.random.seed(417)
    
    dps = [1,3,2,2]
    mps_obc = dMPX.rand(dps, D=7, bc="obc")
    print "full dim", MPX.obc_dim(dps)
    mps1 = dMPX.zeros(dps, D=7, bc="pbc")
    dMPX.overwrite(mps_obc, out=mps1)
    print MPX.norm(mps1), MPX.norm(mps_obc)
    
    mps2 = dMPX.add(mps1,mps1)
    mps11_0,dwt = MPX.compress(mps2,0,preserve_dim="true")
    mps11_1,dwt = MPX.compress(mps2,1,preserve_dim="true")
    mps11_2,dwt = MPX.compress(mps2,2,preserve_dim="true")
    mps11_2_,dwt = MPX.compress(mps2,2,preserve_dim="false")
    mps11_4,dwt = MPX.compress(mps2,4,preserve_dim="true")
    mps11_8,dwt = MPX.compress(mps2,8,preserve_dim="false")

    mps_diff0 = dMPX.add(MPX.mul(-1,mps11_0),mps2)
    mps_diff1 = dMPX.add(MPX.mul(-1,mps11_1),mps2)
    mps_diff2 = dMPX.add(MPX.mul(-1,mps11_2),mps2)
    mps_diff2_ = dMPX.add(MPX.mul(-1,mps11_2_),mps2)
    mps_diff4 = dMPX.add(MPX.mul(-1,mps11_4),mps2)
    mps_diff8 = dMPX.add(MPX.mul(-1,mps11_8),mps2)

    mps4o,dwt = MPX.compress(dMPX.add(mps1,mps1), 4)
    mps_diff4o = dMPX.add(MPX.mul(-1,mps4o),mps2)
    
    print "D full", abs(MPX.norm(mps_diff0))
    print "D=1", abs(MPX.norm(mps_diff1))
    print "D=2", abs(MPX.norm(mps_diff2))
    print "D=2 (trunc)", abs(MPX.norm(mps_diff2_))
    print "D=4", abs(MPX.norm(mps_diff4))
    print "D=4 obc", abs(MPX.norm(mps_diff4o))
    print "D=8", abs(MPX.norm(mps_diff8))

    assert(abs(MPX.norm(mps_diff0))<1.0e-7)
示例#10
0
def test_compress_obc():
    dps = [1,3,2,2]
    mps1 = dMPX.rand(dps, D=7, bc="obc")

    print [m.shape for m in mps1]
    
    mps_diff1 = dMPX.add(MPX.mul(-1,mps1), mps1)
    assert not(np.isnan(MPX.norm(mps1))), MPX.norm(mps1)
    assert MPX.norm(mps_diff1)<1.0e-12, MPX.norm(mps_diff1)

    # check dimension preserving
    mps11 = dMPX.add(mps1,mps1)
    mps11,dwt = MPX.compress(mps11,0,preserve_dim="true")
    mps11_,dwt = MPX.compress(mps11,0,preserve_dim="false")
    print [m.shape for m in mps11]
    print [m.shape for m in mps11_]
    print [m.shape for m in mps1]
    
    print MPX.norm(mps11)
    assert(dwt == 0), dwt
    
    mps_diff = dMPX.add(MPX.mul(-2,mps1),mps11)
    print abs(MPX.norm(mps_diff)/MPX.norm(mps11))<1.0e-7, MPX.norm(mps_diff)

    mps_diff = dMPX.add(MPX.mul(-2,mps1),mps11_)
    print abs(MPX.norm(mps_diff)/MPX.norm(mps11_))<1.0e-7, MPX.norm(mps_diff)