def test_mul2(): dp = [1, 5, 4, 2, 4] mps = dMPX.rand(dp, 7, bc="obc") alpha1 = np.random.random() - 0.5 res = MPX.mul(alpha1, mps) mps_f = MPX.asfull(mps) res_from_mps_f = mps_f * alpha1 res_f = MPX.asfull(res) assert(np.allclose(res_f, res_from_mps_f)) dp = [1, 2] mps = dMPX.rand(dp, 2, bc="obc", dtype=np.complex) alpha1 = 1j print mps[0].dtype res = MPX.mul(alpha1, mps) mps_f = MPX.asfull(mps) res_from_mps_f = mps_f * alpha1 res_f = MPX.asfull(res) dp = [1, 2] mps = dMPX.rand(dp, 2, bc="pbc", dtype=np.complex) alpha1 = 1j res = MPX.mul(alpha1, mps) mps_f = MPX.asfull(mps) res_from_mps_f = mps_f * alpha1 res_f = MPX.asfull(res) assert(np.allclose(res_f, res_from_mps_f))
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)
def test_mul2(): dp = [1, 5, 4, 2, 4] mps = sMPX.rand(dp, 7, bc="obc") alpha1 = np.random.random() - 0.5 res = MPX.mul(alpha1, mps) assert ((mps[0] * alpha1).__class__.__name__ == "COO") print res[1] mps_f = MPX.asfull(mps) res_from_mps_f = mps_f * alpha1 res_f = MPX.asfull(res) assert (np.allclose(res_f, res_from_mps_f)) dp = [1, 2] mps = sMPX.rand(dp, 2, bc="obc") alpha1 = 1j res = MPX.mul(alpha1, mps) mps_f = MPX.asfull(mps) res_from_mps_f = mps_f * alpha1 res_f = MPX.asfull(res) dp = [1, 2] mps = sMPX.rand(dp, 2, bc="pbc") alpha1 = 1j + 3 res = MPX.mul(alpha1, mps) print res[0] mps_f = MPX.asfull(mps) res_from_mps_f = mps_f * alpha1 res_f = MPX.asfull(res) assert (np.allclose(res_f, res_from_mps_f))
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
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)
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))
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)