def test_mpo_identity(self): k = MPS_rand_state(13, 7) b = MPS_rand_state(13, 7) o1 = k @ b i = MPO_identity(13) k, i, b = align_TN_1D(k, i, b) o2 = (k & i & b) ^ ... assert_allclose(o1, o2)
def rand_tn1d_sect(n, bd, dtype=complex): mps = qtn.MPS_rand_state(n + 2, bd, dtype=dtype) mpo = qtn.MPO_rand_herm(n + 2, 5, dtype=dtype) norm = qtn.TensorNetwork(qtn.align_TN_1D(mps.H, mpo, mps)) lix = qtn.bonds(norm[0], norm[1]) rix = qtn.bonds(norm[n], norm[n + 1]) to = norm[1:n + 1] return qtn.TNLinearOperator1D(to, lix, rix, 1, n + 1)
def test_single_explicit_sweep(self): h = MPO_ham_heis(5) dmrg = DMRG1(h, bond_dims=3) assert dmrg._k[0].dtype == float energy_tn = (dmrg._b | dmrg.ham | dmrg._k) e0 = energy_tn ^ ... assert abs(e0.imag) < 1e-13 de1 = dmrg.sweep_right() e1 = energy_tn ^ ... assert_allclose(de1, e1) assert abs(e1.imag) < 1e-13 de2 = dmrg.sweep_right() e2 = energy_tn ^ ... assert_allclose(de2, e2) assert abs(e2.imag) < 1e-13 # state is already left canonized after right sweep de3 = dmrg.sweep_left(canonize=False) e3 = energy_tn ^ ... assert_allclose(de3, e3) assert abs(e2.imag) < 1e-13 de4 = dmrg.sweep_left() e4 = energy_tn ^ ... assert_allclose(de4, e4) assert abs(e2.imag) < 1e-13 # test still normalized assert dmrg._k[0].dtype == float align_TN_1D(dmrg._k, dmrg._b, inplace=True) assert_allclose(abs(dmrg._b @ dmrg._k), 1) assert e1.real < e0.real assert e2.real < e1.real assert e3.real < e2.real assert e4.real < e3.real
def test_explicit_sweeps(self): # import pdb; pdb.set_trace() n = 8 chi = 16 ham = MPO_ham_mbl(n, dh=5, run=42) p0 = MPS_neel_state(n).expand_bond_dimension(chi) b0 = p0.H align_TN_1D(p0, ham, b0, inplace=True) en0 = np.asscalar(p0 & ham & b0 ^ ...) dmrgx = DMRGX(ham, p0, chi) dmrgx.sweep_right() en1 = dmrgx.sweep_left(canonize=False) assert en0 != en1 dmrgx.sweep_right(canonize=False) en = dmrgx.sweep_right(canonize=True) # check normalized assert_allclose(dmrgx._k.H @ dmrgx._k, 1.0) k = dmrgx._k.to_dense() h = ham.to_dense() el, ev = eigsys(h) # check variance very low assert np.abs((k.H @ h @ h @ k) - (k.H @ h @ k)**2) < 1e-12 # check exactly one eigenvalue matched well assert np.sum(np.abs(el - en) < 1e-12) == 1 # check exactly one eigenvector is matched with high fidelity ovlps = (ev.H @ k).A**2 big_ovlps = ovlps[ovlps > 1e-12] assert_allclose(big_ovlps, [1]) # check fully assert is_eigenvector(k, h)
def rand_tn1d_sect(n, bd, dtype=complex): mps = qtn.MPS_rand_state(n + 2, bd, dtype=dtype) mpo = qtn.MPO_rand_herm(n + 2, 5, dtype=dtype) norm = qtn.TensorNetwork(qtn.align_TN_1D(mps.H, mpo, mps)) # greedy not good and contracting with large bsz norm.structure_bsz = 2 lix = qtn.bonds(norm[0], norm[1]) rix = qtn.bonds(norm[n], norm[n + 1]) to = norm[1:n + 1] return qtn.TNLinearOperator1D(to, lix, rix, 1, n + 1)
def test_sites_mpo_mps_product(self, cyclic): k = MPS_rand_state(13, 7, cyclic=cyclic) X = MPO_rand_herm(3, 5, sites=[3, 6, 7], nsites=13, cyclic=cyclic) b = k.H align_TN_1D(k, X, b, inplace=True) assert (k & X & b) ^ ...
def loss_fn(psi, H): k, H, b = qtn.align_TN_1D(psi, H, psi.H) energy = (k & H & b).contract(all, optimize='random-greedy') return real(energy)