def test_ham_j1j2_4_bz(self): h = qu.ham_j1j2(4, j2=0.5, cyclic=True, bz=0) lk = qu.eigvalsh(h, k=11) assert_allclose(lk, [-1.5, -1.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5]) h = qu.ham_j1j2(4, j2=0.5, cyclic=True, bz=0.05) lk = qu.eigvalsh(h, k=11) assert_allclose(lk, [-1.5, -1.5, -0.55, -0.55, -0.55, -0.5, -0.5, -0.5, -0.45, -0.45, -0.45])
def test_mpo_site_ham_heis(self, cyclic, j, bz, n): hh_mpo = MPO_ham_heis(n, tags=['foo'], cyclic=cyclic, j=j, bz=bz) assert hh_mpo[0].tags == {'I0', 'foo'} assert hh_mpo[1].tags == {'I1', 'foo'} assert hh_mpo[-1].tags == {'I{}'.format(n - 1), 'foo'} assert hh_mpo.shape == (2, ) * 2 * n hh_ex = qu.ham_heis(n, cyclic=cyclic, j=j, b=bz) assert_allclose(qu.eigvalsh(hh_ex), qu.eigvalsh(hh_mpo.to_dense()), atol=1e-13)
def test_pauli_dim3(self): for dir in (1, 'x', 'X', 2, 'y', 'Y', 3, 'z', 'Z'): x = qu.pauli(dir, dim=3) assert_allclose(qu.eigvalsh(x), [-1, 0, 1], atol=1e-15)
def test_rank(self, bond_dim, cyclic): psi = qu.rand_matrix_product_state( 10, bond_dim, cyclic=cyclic) rhoa = qu.ptr(psi, [2] * 10, [0, 1, 2, 3]) el = qu.eigvalsh(rhoa) # bond_dim squared as cyclic mps is generated assert sum(el > 1e-12) == bond_dim ** (2 if cyclic else 1)
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_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_approx_spectral_function(self, fn_matrix_rtol, bsz, mpi): fn, matrix, rtol = fn_matrix_rtol a = matrix(2**7) pos = fn == np.sqrt actual_x = sum(fn(eigvalsh(a))) approx_x = approx_spectral_function(a, fn, mpi=mpi, pos=pos, bsz=bsz, verbosity=2) assert_allclose(actual_x, approx_x, rtol=rtol)
def test_rand_pos_sparse(self, dtype): a = qu.rand_pos(3, sparse=True, density=0.3, dtype=dtype) assert a.shape == (3, 3) assert type(a) == sp.csr_matrix assert a.dtype == dtype evals = qu.eigvalsh(a.A) assert_allclose(evals.imag, [0, 0, 0], atol=1e-7) assert np.all(evals.real >= -1e-15)
def test_approx_spectral_function_ptr_ppt_lin_op(self, fn_approx_rtol, psi_abc, psi_ab, bsz): fn, approx, rtol = fn_approx_rtol rho_ab_ppt = partial_transpose(psi_abc.ptr(DIMS, [0, 1]), DIMS[:-1], 0) actual_x = sum(fn(eigvalsh(rho_ab_ppt))) lo = lazy_ptr_ppt_linop(psi_abc, DIMS, sysa=0, sysb=1) approx_x = approx_spectral_function(lo, fn, K=20, R=20, bsz=bsz) assert_allclose(actual_x, approx_x, rtol=rtol)
def test_rand_pos(self, dtype): a = qu.rand_pos(3, dtype=dtype) assert qu.ispos(a) assert a.shape == (3, 3) assert type(a) == qu.qarray assert a.dtype == dtype evals = qu.eigvalsh(a) assert_allclose(evals.imag, [0, 0, 0], atol=1e-7) assert np.all(evals.real >= 0)
def test_approx_spectral_function_ptr_lin_op(self, fn_approx_rtol, psi_abc, psi_ab, bsz): fn, approx, rtol = fn_approx_rtol sysa = [0, 1] rho_ab = psi_abc.ptr(DIMS, sysa) actual_x = sum(fn(eigvalsh(rho_ab))) lo = lazy_ptr_linop(psi_abc, DIMS, sysa) approx_x = approx(lo, R=50, bsz=bsz, verbosity=2) assert_allclose(actual_x, approx_x, rtol=rtol)
def test_approx_spectral_function_with_v0(self, fn_matrix_rtol, bsz): fn, matrix, rtol = fn_matrix_rtol a = matrix(2**7) actual_x = sum(fn(eigvalsh(a))) # check un-normalized state work properly v0 = (neel_state(7) + neel_state(7, down_first=True)) v0 = v0.A.reshape(-1) pos = fn == np.sqrt approx_x = approx_spectral_function(a, fn, K=20, v0=v0, pos=pos, bsz=bsz, verbosity=2) assert_allclose(actual_x, approx_x, rtol=rtol)
def test_entanglement(self): rho = qu.rand_seperable([2, 3, 2], 10) assert_allclose(qu.tr(rho), 1.0) assert qu.isherm(rho) assert qu.logneg(rho, [2, 6]) < 1e-12 assert qu.logneg(rho, [6, 2]) < 1e-12 rho_a = qu.ptr(rho, [2, 3, 2], 1) el = qu.eigvalsh(rho_a) assert np.all(el < 1 - 1e-12) assert np.all(el > 1e-12)
def get_energies(self): omega_zero_all_energies = [] omega_non_zero_all_energies = [] state_tensors = [q.kron(*state) for state in self.states] for detuning in tqdm(self.Delta): H = self.get_hamiltonian(detuning) energies = [] for i, state in enumerate(self.states): energy = q.expec(H, state_tensors[i]).real energies.append(energy) omega_zero_all_energies.append(energies) if self.Omega != 0: eigenvalues = q.eigvalsh(H.toarray()).real omega_non_zero_all_energies.append(eigenvalues) self.Omega_zero_energies = np.array(omega_zero_all_energies) self.Omega_non_zero_energies = np.array(omega_non_zero_all_energies)
def test_pauli_dim2(self): for dir in (1, 'x', 'X', 2, 'y', 'Y', 3, 'z', 'Z'): x = qu.pauli(dir) assert_allclose(qu.eigvalsh(x), [-1, 1])
def test_spin_high(self, label, S): D = int(2 * S + 1) op = qu.spin_operator(label, S) assert_allclose(qu.eigvalsh(op), np.linspace(-S, S, D), atol=1e-13)
def test_ham_j1j2_6_sparse_cyc(self): h = qu.ham_j1j2(6, j2=0.5, sparse=True, cyclic=True) lk = qu.eigvalsh(h, k=5) assert_allclose(lk, [-9 / 4, -9 / 4, -7 / 4, -7 / 4, -7 / 4])
def test_ham_heis_bz(self): h = qu.ham_heis(2, cyclic=False, b=1) evals = qu.eigvalsh(h) assert_allclose(evals, [-3 / 4, -3 / 4, 1 / 4, 5 / 4])
def test_ham_heis_sparse_cyclic_4(self, parallel): h = qu.ham_heis(4, sparse=True, cyclic=True, parallel=parallel) lk = qu.eigvalsh(h, k=4) assert_allclose(lk, [-2, -1, -1, -1])
def test_approx_spectral_subspaces_with_heis_partition(self, bsz): h = ham_heis(10, sparse=True) beta = 0.01 actual_Z = sum(np.exp(-beta * eigvalsh(h.A))) approx_Z = tr_exp_approx(-beta * h, bsz=bsz) assert_allclose(actual_Z, approx_Z, rtol=3e-2)
def test_approx_fn(self, fn): A = MPO_rand_herm(10, 7, normalize=True) xe = sum(fn(eigvalsh(A.to_dense()))) xf = approx_spectral_function(A, fn, tol=0.1, verbosity=2) assert_allclose(xe, xf, rtol=0.5)
def test_eigvals(self, mat_herm_dense): _, a = mat_herm_dense evals = qu.eigvalsh(a) assert_allclose(evals, [-3, -1, 2, 4])
def test_ham_heis_2(self): h = qu.ham_heis(2, cyclic=False) evals = qu.eigvalsh(h) assert_allclose(evals, [-0.75, 0.25, 0.25, 0.25]) gs = qu.groundstate(h) assert_allclose(qu.expec(gs, qu.singlet()), 1.)
def test_eigs_small_sparse_novecs(self, mat_herm_sparse, backend): _, a = mat_herm_sparse assert qu.issparse(a) lk = qu.eigvalsh(a, k=2, backend=backend) assert_allclose(lk, (-3, -1))
def test_eigvals(self): H = qu.ham_hubbard_hardcore(4) a_el = qu.eigvalsh(H, autoblock=False) el = qu.eigvalsh(H, autoblock=True) assert_allclose(a_el, el, atol=1e-12)