def test_specials(self, var_one, var_two): K1 = qu.rand_herm(2**1) n = 10 HB = qtn.SpinHam(S=1 / 2) if var_two == 'some': HB += 1, K1, K1 HB[4, 5] += 1, K1, K1 HB[7, 8] += 1, K1, K1 elif var_two == 'only': for i in range(n - 1): HB[i, i + 1] += 1, K1, K1 else: HB += 1, K1, K1 if var_one == 'some': HB += 1, K1 HB[2] += 1, K1 HB[3] += 1, K1 elif var_one == 'only': for i in range(n - 1): HB[i] += 1, K1 elif var_one == 'only-some': HB[1] += 1, K1 elif var_one == 'def-only': HB += 1, K1 HB.build_nni(n) H_mpo = HB.build_mpo(n) H_sps = HB.build_sparse(n) assert_allclose(H_mpo.to_dense(), H_sps.A)
def test_rand_herm_sparse(self): a = rand_herm(3, sparse=True, density=0.3) assert a.shape == (3, 3) assert type(a) == sp.csr_matrix assert isherm(a) evals = np.linalg.eigvals(a.A) assert_allclose(evals.imag, [0, 0, 0], atol=1e-14)
def test_thermal_state_precomp(self): full = rand_herm(2**4) beta = 0.624 rhoth1 = thermal_state(full, beta) func = thermal_state(full, None, precomp_func=True) rhoth2 = func(beta) assert_allclose(rhoth1, rhoth2)
def test_specials(self, var_one, var_two): K1 = qu.rand_herm(2**1) n = 10 HB = qtn.SpinHam(S=1 / 2) if var_two == 'some': HB += 1, K1, K1 HB[4, 5] += 1, K1, K1 HB[7, 8] += 1, K1, K1 elif var_two == 'only': for i in range(n - 1): HB[i, i + 1] += 1, K1, K1 else: HB += 1, K1, K1 if var_one == 'some': HB += 1, K1 HB[2] += 1, K1 HB[3] += 1, K1 elif var_one == 'only': for i in range(n - 1): HB[i] += 1, K1 elif var_one == 'only-some': HB[1] += 1, K1 elif var_one == 'def-only': HB += 1, K1 HB.build_mpo(n) HB.build_nni(n)
def test_rand_herm(self): a = rand_herm(3) assert a.shape == (3, 3) assert type(a) == np.matrix assert_allclose(a, a.H) evals = np.linalg.eigvals(a) assert_allclose(evals.imag, [0, 0, 0], atol=1e-14)
def test_thermal_state_tuple(self): full = rand_herm(2**4) l, v = eigsys(full) for beta in (0, 0.5, 1, 10): rhoth1 = thermal_state(full, beta) rhoth2 = thermal_state((l, v), beta) assert_allclose(rhoth1, rhoth2)
def test_seigsys_slepc_eigvecs(self): h = rand_herm(100, sparse=True, density=0.2) lks, vks = seigsys_slepc(h, k=5) lka, vka = seigsys(h, k=5) assert vks.shape == vka.shape for ls, vs, la, va in zip(lks, vks.T, lka, vka.T): assert_allclose(ls, la) assert_allclose(expec(vs, va), 1.0)
def test_rand_herm(self, dtype): a = qu.rand_herm(3, dtype=dtype) assert a.shape == (3, 3) assert type(a) == qu.qarray assert a.dtype == dtype assert_allclose(a, a.H) evals = qu.eigvalsh(a) assert_allclose(evals.imag, [0, 0, 0], atol=1e-14)
def test_internal_interior_default(self): a = rand_herm(100, sparse=True) alo = sp.linalg.aslinearoperator(a) el, ev = eigs_slepc(alo, k=1, which='TR', sigma=0.0) el_s, ev_s = sp.linalg.eigsh(a.tocsc(), k=1, which='LM', sigma=0.0) assert_allclose(el_s, el, rtol=1e-5) assert_allclose(np.abs(ev_s.conj().T @ ev), 1.0)
def test_rand_herm_sparse(self, dtype): a = qu.rand_herm(3, sparse=True, density=0.3, dtype=dtype) assert a.shape == (3, 3) assert type(a) == sp.csr_matrix assert qu.isherm(a) assert a.dtype == dtype evals = qu.eigvalsh(a.A) assert_allclose(evals.imag, [0, 0, 0], atol=1e-14)
def test_construct_lanczos_tridiag_beta_breakdown(self, bsz): a = rand_herm(8) alpha, beta, scaling = last(construct_lanczos_tridiag(a, bsz=bsz, K=9)) assert alpha.shape == (8,) assert beta.shape == (8,) el, ev = lanczos_tridiag_eig(alpha, beta) assert el.shape == (8,) assert el.dtype == float assert ev.shape == (8, 8) assert ev.dtype == float
def test_exp_sparse(self): a = rand_herm(100, sparse=True, density=0.1) k = rand_ket(100) out = mfn_multiply_slepc(a, k) al, av = eigh(a.A) expected = av @ np.diag(np.exp(al)) @ av.conj().T @ k assert_allclose(out, expected)
def test_eigs_slepc_eigvecs(self, dtype): h = rand_herm(32, sparse=True, density=0.5) if dtype == 'real': h = h.real lks, vks = eigs_slepc(h, k=5) lka, vka = eigh(h, k=5, backend='scipy') assert vks.shape == vka.shape assert h.dtype == vks.dtype assert_allclose(lks, lka) assert_allclose(abs(vka.H @ vks), np.eye(5), atol=1e-8)
def srho_dot_ls(): rho = rand_rho(3) ham = rand_herm(3, sparse=True, density=0.5) gamma = 0.7 ls = [rand_matrix(3, sparse=True, density=0.5) for _ in range(3)] rhodl = -1.0j * (ham @ rho - rho @ ham) for l in ls: rhodl += gamma * (l @ rho @ l.H) rhodl -= gamma * 0.5 * (rho @ l.H @ l) rhodl -= gamma * 0.5 * (l.H @ l @ rho) return rho, ham, gamma, ls, rhodl
def test_construct_lanczos_tridiag(self, bsz): a = rand_herm(2**5) alpha, beta, scaling = last( construct_lanczos_tridiag(a, bsz=bsz, K=20)) assert alpha.shape == (20,) assert beta.shape == (20,) el, ev = lanczos_tridiag_eig(alpha, beta) assert el.shape == (20,) assert el.dtype == float assert ev.shape == (20, 20) assert ev.dtype == float
def rho_dot_ls(): np.random.seed(1) rho = rand_rho(3) ham = rand_herm(3) gamma = 0.7 ls = [rand_matrix(3) for _ in range(3)] rhodl = -1.0j * (ham @ rho - rho @ ham) for l in ls: rhodl += gamma * (l @ rho @ l.H) rhodl -= gamma * 0.5 * (rho @ l.H @ l) rhodl -= gamma * 0.5 * (l.H @ l @ rho) return rho, ham, gamma, ls, rhodl
def test_1(self): a = rand_herm(100, sparse=True) el, ev = eigh(a.A) which = abs(el) < 0.2 el, ev = el[which], ev[:, which] offset = norm(a, 'fro') a = a + offset * sp.eye(a.shape[0]) sl, sv = eigs_slepc(a, k=6, l_win=(-0.2 + offset, 0.2 + offset)) sl -= offset assert_allclose(el, sl) assert_allclose(np.abs(sv.H @ ev), np.eye(el.size), atol=1e-11)
def test_extermal_eigen(self): a = rand_herm(100, sparse=True) alo = sp.linalg.aslinearoperator(a) el_us, ev_us = sp.linalg.eigsh(alo, k=1, which='LA') el_u, ev_u = eigs_slepc(alo, k=1, which='LA') assert_allclose(el_us, el_u) assert_allclose(np.abs(ev_us.conj().T @ ev_u), 1.0) el_ls, ev_ls = sp.linalg.eigsh(alo, k=1, which='SA') el_l, ev_l = eigs_slepc(alo, k=1, which='SA') assert_allclose(el_ls, el_l) assert_allclose(np.abs(ev_ls.conj().T @ ev_l), 1.0)
def test_expm_krylov_expokit(self): ham = rand_herm(100, sparse=True, density=0.8) psi = rand_ket(100) evo_exact = QuEvo(psi, ham, method='solve') evo_krylov = QuEvo(psi, ham, method='expm', expm_backend='slepc-krylov') evo_expokit = QuEvo(psi, ham, method='expm', expm_backend='slepc-expokit') ts = np.linspace(0, 100, 21) for p1, p2, p3 in zip(evo_exact.at_times(ts), evo_krylov.at_times(ts), evo_expokit.at_times(ts)): assert abs(expec(p1, p2) - 1) < 1e-9 assert abs(expec(p1, p3) - 1) < 1e-9
def test_multisubsystem(self): qu.seed_rand(42) dims = [2, 2, 2] IIX = qu.ikron(qu.rand_herm(2), dims, 2) dcmp = qu.pauli_decomp(IIX, mode='c') for p, x in dcmp.items(): if x == 0j: assert (p[0] != 'I') or (p[1] != 'I') else: assert p[0] == p[1] == 'I' K = qu.rand_iso(3 * 4, 4).reshape(3, 4, 4) KIIXK = qu.kraus_op(IIX, K, dims=dims, where=[0, 2]) dcmp = qu.pauli_decomp(KIIXK, mode='c') for p, x in dcmp.items(): if x == 0j: assert p[1] != 'I' else: assert p[1] == 'I'
def test_internal_shift_invert_precond_jd(self): a = rand_herm(20, sparse=True, seed=42) alo = sp.linalg.aslinearoperator(a) st_opts = { 'STType': 'precond', 'KSPType': 'bcgs', # / 'gmres' 'PCType': 'none', } el, ev = eigs_slepc(alo, k=1, which='TR', sigma=0.0, st_opts=st_opts, EPSType='jd') el_s, ev_s = sp.linalg.eigsh(a.tocsc(), k=1, which='LM', sigma=0.0) assert_allclose(el_s, el, rtol=1e-6) assert_allclose(np.abs(ev_s.conj().T @ ev), 1.0)
def test_internal_shift_invert_linear_solver(self): a = rand_herm(100, sparse=True) alo = sp.linalg.aslinearoperator(a) st_opts = { 'STType': 'sinvert', 'KSPType': 'bcgs', # / 'gmres' 'PCType': 'none', } el, ev = seigsys_slepc(alo, k=1, which='TR', sigma=0.0, st_opts=st_opts, EPSType='krylovschur') el_s, ev_s = sp.linalg.eigsh(a.tocsc(), k=1, which='LM', sigma=0.0) assert_allclose(el_s, el, rtol=1e-5) assert_allclose(np.abs(ev_s.conj().T @ ev), 1.0)
def test_against_arpack(self): A = qu.rand_herm(32, dtype=float) lk, vk = qu.eigh(A, k=6, backend='lobpcg') slk, svk = qu.eigh(A, k=6, backend='scipy') assert_allclose(lk, slk) assert_allclose(np.eye(6), abs(vk.H @ svk), atol=1e-9, rtol=1e-9)
def test_sparse(self): a = qu.rand_herm(10, sparse=True, density=0.9) vt = qu.eigvecsh(a, sigma=0, k=1) assert qu.is_eigenvector(vt, a) vf = qu.rand_ket(10) assert not qu.is_eigenvector(vf, a)
def test_dense_false(self): a = qu.rand_herm(10) v = qu.rand_ket(10) assert not qu.is_eigenvector(v, a)
def test_dense_true(self): a = qu.rand_herm(10) v = qu.eigvecsh(a) for i in range(10): assert qu.is_eigenvector(v[:, [i]], a)
def test_simple_dense(self): a = rand_herm(2**4, sparse=True) y = rand_ket(2**4) x = ssolve_slepc(a, y) assert_allclose(a @ x, y, atol=1e-12, rtol=1e-6)
def test_thermal_state_hot(self): full = rand_herm(2**4) rhoth = chop(thermal_state(full, 0.0)) assert_allclose(rhoth, eye(2**4) / 2**4)
def test_norm_fro_approx(self, bsz): A = rand_herm(2**5) actual_norm_fro = norm_fro(A) approx_norm_fro = norm_fro_approx(A, tol=1e-2, bsz=bsz) assert_allclose(actual_norm_fro, approx_norm_fro, rtol=1e-1)
def test_thermal_state_normalization(self): full = rand_herm(2**4) for beta in (0, 0.5, 1, 10): rhoth = thermal_state(full, beta) assert_allclose(tr(rhoth), 1)