def test_apply_constraints(self): # Get norm of original operator. original_norm = 0. for term, coefficient in self.fermion_hamiltonian.terms.items(): if term != (): original_norm += abs(coefficient) # Get modified operator. modified_operator = apply_constraints(self.fermion_hamiltonian, self.n_fermions) modified_operator.compress() # Get norm of modified operator. modified_norm = 0. for term, coefficient in modified_operator.terms.items(): if term != (): modified_norm += abs(coefficient) self.assertTrue(modified_norm < original_norm) # Map both to sparse matrix under Jordan-Wigner. sparse_original = get_sparse_operator(self.fermion_hamiltonian) sparse_modified = get_sparse_operator(modified_operator) # Check spectra. sparse_original = jw_number_restrict_operator(sparse_original, self.n_fermions) sparse_modified = jw_number_restrict_operator(sparse_modified, self.n_fermions) original_spectrum = sparse_eigenspectrum(sparse_original) modified_spectrum = sparse_eigenspectrum(sparse_modified) spectral_deviation = numpy.amax( numpy.absolute(original_spectrum - modified_spectrum)) self.assertAlmostEqual(spectral_deviation, 0.) # Check expectation value. energy, wavefunction = get_ground_state(sparse_original) modified_energy = expectation(sparse_modified, wavefunction) self.assertAlmostEqual(modified_energy, energy) # Test consistency with cvxopt. scipy_operator = apply_constraints(self.fermion_hamiltonian, self.n_fermions, use_scipy=False) # Get norm. scipy_norm = 0. for term, coefficient in scipy_operator.terms.items(): if term != (): scipy_norm += abs(coefficient) self.assertAlmostEqual(scipy_norm, modified_norm)
def test_number_restricted_spectra_match_molecule(self): hamiltonian_fop = get_fermion_operator(self.molecular_hamiltonian) sparse_ham_number_preserving = get_number_preserving_sparse_operator( hamiltonian_fop, self.molecule.n_qubits, self.molecule.n_electrons, spin_preserving=False) sparse_ham = get_sparse_operator(hamiltonian_fop, self.molecule.n_qubits) sparse_ham_restricted_number_preserving = jw_number_restrict_operator( sparse_ham, n_electrons=self.molecule.n_electrons, n_qubits=self.molecule.n_qubits) spectrum_from_new_sparse_method = sparse_eigenspectrum( sparse_ham_number_preserving) spectrum_from_old_sparse_method = sparse_eigenspectrum( sparse_ham_restricted_number_preserving) spectral_deviation = numpy.amax( numpy.absolute(spectrum_from_new_sparse_method - spectrum_from_old_sparse_method)) self.assertAlmostEqual(spectral_deviation, 0.)
def test_hf_state_energy_close_to_ground_energy_at_high_density(self): grid_length = 8 dimension = 1 spinless = True n_particles = grid_length**dimension // 2 # High density -> small length_scale. length_scale = 0.25 grid = Grid(dimension, grid_length, length_scale) hamiltonian = jellium_model(grid, spinless) hamiltonian_sparse = get_sparse_operator(hamiltonian) hf_state = hartree_fock_state_jellium(grid, n_particles, spinless, plane_wave=True) restricted_hamiltonian = jw_number_restrict_operator( hamiltonian_sparse, n_particles) E_g = get_ground_state(restricted_hamiltonian)[0] E_HF_plane_wave = expectation(hamiltonian_sparse, hf_state) self.assertAlmostEqual(E_g, E_HF_plane_wave, places=5)
def test_number_restricted_spectra_match_hubbard(self): hamiltonian_fop = self.hubbard_hamiltonian sparse_ham_number_preserving = get_number_preserving_sparse_operator( hamiltonian_fop, 8, 4, spin_preserving=False) sparse_ham = get_sparse_operator(hamiltonian_fop, 8) sparse_ham_restricted_number_preserving = jw_number_restrict_operator( sparse_ham, n_electrons=4, n_qubits=8) spectrum_from_new_sparse_method = sparse_eigenspectrum( sparse_ham_number_preserving) spectrum_from_old_sparse_method = sparse_eigenspectrum( sparse_ham_restricted_number_preserving) spectral_deviation = numpy.amax(numpy.absolute( spectrum_from_new_sparse_method - spectrum_from_old_sparse_method)) self.assertAlmostEqual(spectral_deviation, 0.)