def test_element(): dp = (4, 5, 3, 3, 3) mps = dMPX.product_state(dp, [0, 1, 2, 1, 1], bc="obc") vec = MPX.asfull(mps) for i, occ in enumerate(np.ndindex(dp)): assert abs(MPX.element(mps, occ) - vec[i]) < 1.e-10 mps = dMPX.product_state(dp, [0, 1, 2, 2, 2], D=4, bc="pbc") vec = MPX.asfull(mps) for i, occ in enumerate(np.ndindex(dp)): assert abs(np.linalg.norm(MPX.element(mps, occ) - vec[i])) < 1.e-10
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)
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)
def test_element(): dp = (2, 2, 3, 3, 3) mps = dMPX.product_state(dp, [0, 1, 2, 2, 2], D=2, bc="obc") smps = sMPX.from_dense(mps) vec = MPX.asfull(mps) assert (MPX.element(smps, [0, 1, 2, 2, 2]) == MPX.element(mps, [0, 1, 2, 2, 2])) for i, occ in enumerate(np.ndindex(dp)): print occ, MPX.element(smps, occ), vec[i] assert abs(np.linalg.norm(MPX.element(smps, occ) - vec[i])) < 1.e-10
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_dot(): dp = (4, 5, 3) # test mpo x mpo mpo = dMPX.rand(zip(dp, dp), D=3, bc="obc") mat = MPX.asfull(mpo) mpo2 = MPX.dot(mpo, mpo) mat2 = np.dot(mat, mat) print np.linalg.norm(mat2) print np.linalg.norm(MPX.asfull(mpo2)) assert np.linalg.norm(mat2 - MPX.asfull(mpo2)) < 1.e-9 # test mpo x mps mps = dMPX.rand(dp, D=3, bc="pbc") vec = MPX.asfull(mps) matvec = np.dot(mat, vec) mps1 = MPX.dot(mpo, mps) assert np.linalg.norm(matvec - MPX.asfull(mps1)) < 1.e-9 # test mps x mpo mps1 = MPX.dot(mps, mpo) vecmat = np.dot(vec, mat) assert np.linalg.norm(vecmat - MPX.asfull(mps1)) < 1.e-9 # test mps x mps norm1 = MPX.dot(mps, mps) norm = np.dot(vec, vec) assert abs(norm - norm1) < 1.e-9
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 test_asfull(): dp = [1, 5, 4, 2, 4] smps = sMPX.rand(dp, 7, bc="pbc") mps = sMPX.todense(smps) print np.linalg.norm(MPX.asfull(mps)), np.linalg.norm(MPX.asfull(smps)) assert np.allclose(np.linalg.norm(MPX.asfull(mps) - MPX.asfull(smps)), 0.) mps = sMPX.rand(dp, 7, bc="obc") smps = sMPX.from_dense(mps) assert np.allclose(np.linalg.norm(MPX.asfull(mps) - MPX.asfull(smps)), 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)))
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)))
def test_dot2(): dps = [1,4,4,2] mps1 = dMPX.rand(dps,4,bc="obc") mps2 = dMPX.rand(dps,3,bc="pbc") dps_o = [(d,d) for d in dps] mpo1 = dMPX.rand(dps_o,4,bc="pbc") mpo2 = dMPX.rand(dps_o,3,bc="obc") mpo3 = dMPX.rand(dps_o,3,bc="pbc") ## <mps1|mps2> ss11 = MPX.dot(mps1,mps1) ss12 = MPX.dot(mps1,mps2) ss22 = MPX.dot(mps2,mps2) assert np.allclose(ss11, np.dot(MPX.asfull(mps1), MPX.asfull(mps1))) assert np.allclose(ss12, np.dot(MPX.asfull(mps1), MPX.asfull(mps2))) assert np.allclose(ss22, np.dot(MPX.asfull(mps2), MPX.asfull(mps2))) ## mpo1|mps1> os11 = MPX.dot(mpo1,mps1) assert np.allclose(MPX.asfull(os11), np.dot(MPX.asfull(mpo1), MPX.asfull(mps1))) ## mpo2*mpo1 oo21 = MPX.dot(mpo2,mpo1) assert np.allclose(MPX.asfull(oo21), np.dot(MPX.asfull(mpo2), MPX.asfull(mpo1))) ## <mps1|mpo1 so11 = MPX.dot(mps1,mpo1) os32 = MPX.dot(mpo3,mps2) so23 = MPX.dot(mps2,mpo3) assert np.allclose(MPX.asfull(so11), np.dot(MPX.asfull(mps1), MPX.asfull(mpo1))) assert np.allclose(MPX.asfull(os32), np.dot(MPX.asfull(mpo3), MPX.asfull(mps2))) assert np.allclose(MPX.asfull(so23), np.dot(MPX.asfull(mps2), MPX.asfull(mpo3)))