def test_all_gate_methods(self, Circ): rots = ['rx', 'ry', 'rz'] g1s = ['x', 'y', 'z', 's', 't', 'h', 'iden'] g2s = ['cx', 'cy', 'cz', 'cnot', 'swap'] g_rand = np.random.permutation(rots + g1s + g2s + ['u3']) psi0 = qtn.MPS_rand_state(2, 2) circ = Circ(2, psi0) for g in g_rand: if g == 'u3': angles = np.random.uniform(0, 2 * np.pi, size=3) i = np.random.choice([0, 1]) args = (*angles, i) elif g in rots: theta = np.random.uniform(0, 2 * np.pi) i = np.random.choice([0, 1]) args = (theta, i) elif g in g1s: i = np.random.choice([0, 1]) args = (i, ) elif g in g2s: i, j = np.random.permutation([0, 1]) args = (i, j) getattr(circ, g)(*args) assert circ.psi.H @ circ.psi == pytest.approx(1.0) assert abs((circ.psi.H & psi0) ^ all) < 0.99999999
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 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_non_trans_invar(self): n = 10 tf = 1.0 p0 = qtn.MPS_rand_state(n, bond_dim=1) H = qtn.NNI_ham_mbl(n, dh=1.7, cyclic=False, seed=42) print(H) assert H.special_sites == {(i, i + 1) for i in range(n)} tebd = qtn.TEBD(p0, H) tebd.update_to(tf, tol=1e-3) p0d = p0.to_dense() Hd = qu.ham_mbl(n, dh=1.7, cyclic=False, seed=42, sparse=True) evo = qu.Evolution(p0d, Hd) evo.update_to(tf) assert qu.expec(tebd.pt.to_dense(), evo.pt) == pytest.approx(1.0)
def heis_pbc(): L = 10 chi = 8 dtype = 'float32' psi0 = qtn.MPS_rand_state(L, chi, cyclic=True, seed=42).astype(dtype) H = qtn.MPO_ham_heis(L, cyclic=True).astype(dtype) def norm_fn(psi): factor = (psi & psi).contract(all, optimize='random-greedy') return psi / factor**0.5 def loss_fn(psi, H): k, H, b = qtn.align_TN_1D(psi, H, psi) energy = (k & H & b).contract(all, optimize='random-greedy') return energy en_ex = qu.groundenergy(qu.ham_heis(L, cyclic=True, sparse=True)) return psi0, H, norm_fn, loss_fn, en_ex
def ham_mbl_pbc_complex(): L = 10 chi = 8 dtype = 'complex64' psi0 = qtn.MPS_rand_state(L, chi, cyclic=True, seed=42).astype(dtype) ham_opts = {'cyclic': True, 'dh': 0.7, 'dh_dim': 3, 'seed': 42} H = qtn.MPO_ham_mbl(L, **ham_opts).astype(dtype) def norm_fn(psi): factor = (psi.H & psi).contract(all, optimize='random-greedy') return psi * factor**-0.5 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) en_ex = qu.groundenergy(qu.ham_mbl(L, sparse=True, **ham_opts)) return psi0, H, norm_fn, loss_fn, en_ex
def rand_mps(num, rank, size=2): """ Generate random MPS. """ mps = qtn.MPS_rand_state(num, rank, phys_dim=size) return load_quimb_tensors(mps)