def test_finite_DMRG_outstream(backend_dtype_values, capsys): np.random.seed(16) N = 6 backend = backend_dtype_values[0] dtype = backend_dtype_values[1] mpo = FiniteXXZ( Jz=np.ones(N - 1), Jxy=np.ones(N - 1), Bz=np.zeros(N), dtype=dtype, backend=backend) D = 32 mps = FiniteMPS.random([2] * N, [D] * (N - 1), dtype=dtype, backend=backend) dmrg = FiniteDMRG(mps, mpo) num_sweeps = 2 dmrg.run_one_site( num_sweeps=num_sweeps, num_krylov_vecs=10, verbose=2, precision=1E-100) out, _ = capsys.readouterr() out = out.split('\n') act = [o[:28] + '\n' for o in out] act = ''.join(act[0:num_sweeps * (2 * N - 2)]) exp = ''.join([ f"SS-DMRG sweep={n}/{num_sweeps}, site={m}/{N}:\n" for n in range(1, num_sweeps + 1) for m in [0, 1, 2, 3, 4, 5, 4, 3, 2, 1] ]) assert act == exp
def test_finite_DMRG_init(backend_dtype_values, N): np.random.seed(16) backend = backend_dtype_values[0] dtype = backend_dtype_values[1] H = get_XXZ_Hamiltonian(N, 1, 1, 1) eta, _ = np.linalg.eigh(H) mpo = FiniteXXZ( Jz=np.ones(N - 1), Jxy=np.ones(N - 1), Bz=np.zeros(N), dtype=dtype, backend=backend) D = 32 mps = FiniteMPS.random([2] * N, [D] * (N - 1), dtype=dtype, backend=backend) dmrg = FiniteDMRG(mps, mpo) energy = dmrg.run_one_site(num_sweeps=4, num_krylov_vecs=10) np.testing.assert_allclose(energy, eta[0])
def test_finiteFreeFermions2d(N1, N2, D): def adjacency(N1, N2): neighbors = {} mat = np.arange(N1 * N2).reshape(N1, N2) for n in range(N1 * N2): x, y = np.divmod(n, N2) if n not in neighbors: neighbors[n] = [] if y < N2 - 1: neighbors[n].append(mat[x, y + 1]) if x > 0: neighbors[n].append(mat[x - 1, y]) return neighbors adj = adjacency(N1, N2) tij = np.zeros((N1 * N2, N1 * N2)) t = -1 v = -1 for n, d in adj.items(): for ind in d: tij[n, ind] += t tij[ind, n] += t tij += np.diag(np.ones(N1 * N2) * v) eta, _ = np.linalg.eigh(tij) expected = min(np.cumsum(eta)) t1 = t t2 = t dtype = np.float64 mpo = FiniteFreeFermion2D(t1, t2, v, N1, N2, dtype) mps = FiniteMPS.random([2] * N1 * N2, [D] * (N1 * N2 - 1), dtype=np.float64) dmrg = FiniteDMRG(mps, mpo) actual = dmrg.run_one_site() np.testing.assert_allclose(actual, expected)
def test_finite_DMRG_init(N, config, D): backend = 'jax' dtype = 'float64' a = buildMPO() mpo = FiniteMPO(a, backend=backend) mps = FiniteMPS.random([4] * N, config, dtype=dtype, backend=backend) ans1 = 500 ans2 = 100 sumtime = 0 while abs(2 * (ans1 - ans2) / (ans1 + ans2)) > 0.1 / 100: print("Bond dim = {D}, Configuration = {c}".format(D=D, c=config)) bondarray.append(D) start_time = time.time() dmrg = FiniteDMRG(mps, mpo) [mps, energy] = run_one_site(dmrg, num_sweeps=1000, num_krylov_vecs=15, precision=prec, delta=prec, tol=prec, verbose=0, ndiag=1) timearray.append( jnp.around(float(time.time() - start_time) / 60, decimals=2)) print("{x} minutes".format(x=timearray[-1])) sumtime += timearray[-1] energy += 2 minarray.append(energy) print("Minimum: {x}".format(x=minarray[-1])) ans1 = ans2 ans2 = energy precisionarray.append( jnp.around(2 * (ans1 - ans2) / (ans1 + ans2) * 100, decimals=2)) print("Precision (%): {x}".format(x=precisionarray[-1])) print("Number of iterations: {x}\n".format(x=numofitarray[-1])) D *= 2 for i in range(N - 1): config[i] *= 2 A = mps.tensors B = jnp.zeros((1, 4, config[0]), dtype='float64') B = jax.ops.index_update(B, jax.ops.index[:, :, :A[0].shape[2]], A[0]) A[0] = B for i in range(1, N - 1): B = jnp.zeros((config[i - 1], 4, config[i]), dtype='float64') B = jax.ops.index_update( B, jax.ops.index[:A[i].shape[0], :, :A[i].shape[2]], A[i]) A[i] = B B = jnp.zeros((config[N - 2], 4, 1), dtype='float64') B = jax.ops.index_update(B, jax.ops.index[:A[N - 1].shape[0], :, :], A[N - 1]) A[N - 1] = B mps.tensors = A print("")