def test_zero_hamiltonian(self): potential_terms, kinetic_terms = ( diagonal_coulomb_potential_and_kinetic_terms_as_arrays( FermionOperator.zero())) self.assertListEqual(list(potential_terms), []) self.assertListEqual(list(kinetic_terms), [])
def test_jellium_hamiltonian_correctly_broken_up(self): grid = Grid(2, 3, 1.) hamiltonian = jellium_model(grid, spinless=True, plane_wave=False) potential_terms, kinetic_terms = ( diagonal_coulomb_potential_and_kinetic_terms_as_arrays(hamiltonian) ) potential = sum(potential_terms, FermionOperator.zero()) kinetic = sum(kinetic_terms, FermionOperator.zero()) true_potential = dual_basis_jellium_model(grid, spinless=True, kinetic=False) true_kinetic = dual_basis_jellium_model(grid, spinless=True, potential=False) for i in range(count_qubits(true_kinetic)): coeff = true_kinetic.terms.get(((i, 1), (i, 0))) if coeff: true_kinetic -= FermionOperator(((i, 1), (i, 0)), coeff) true_potential += FermionOperator(((i, 1), (i, 0)), coeff) self.assertEqual(potential, true_potential) self.assertEqual(kinetic, true_kinetic)
def test_identity_recognized_as_potential_term(self): potential_terms, kinetic_terms = ( diagonal_coulomb_potential_and_kinetic_terms_as_arrays( FermionOperator.identity())) self.assertListEqual(list(potential_terms), [FermionOperator.identity()]) self.assertListEqual(list(kinetic_terms), [])
def test_simple_hamiltonian(self): hamiltonian = (FermionOperator('3^ 1^ 3 1') + FermionOperator('1^ 1') - FermionOperator('1^ 2') - FermionOperator('2^ 1')) potential_terms, kinetic_terms = ( diagonal_coulomb_potential_and_kinetic_terms_as_arrays(hamiltonian) ) potential = sum(potential_terms, FermionOperator.zero()) kinetic = sum(kinetic_terms, FermionOperator.zero()) self.assertEqual( potential, (FermionOperator('1^ 1') + FermionOperator('3^ 1^ 3 1'))) self.assertEqual(kinetic, (-FermionOperator('1^ 2') - FermionOperator('2^ 1')))
def test_diagonal_coulomb_hamiltonian_class(self): hamiltonian = DiagonalCoulombHamiltonian(numpy.array([[1, 1], [1, 1]], dtype=float), numpy.array([[0, 1], [1, 0]], dtype=float), constant=2.3) potential_terms, kinetic_terms = ( diagonal_coulomb_potential_and_kinetic_terms_as_arrays(hamiltonian) ) potential = sum(potential_terms, FermionOperator.zero()) kinetic = sum(kinetic_terms, FermionOperator.zero()) expected_potential = (2.3 * FermionOperator.identity() + FermionOperator('0^ 0') + FermionOperator('1^ 1') - FermionOperator('1^ 0^ 1 0', 2.0)) expected_kinetic = FermionOperator('0^ 1') + FermionOperator('1^ 0') self.assertEqual(potential, expected_potential) self.assertEqual(kinetic, expected_kinetic)
def test_split_operator_error_operator_VT_order_against_definition(self): hamiltonian = (normal_ordered(fermi_hubbard(3, 3, 1., 4.0)) - 2.3 * FermionOperator.identity()) potential_terms, kinetic_terms = ( diagonal_coulomb_potential_and_kinetic_terms_as_arrays(hamiltonian) ) potential = sum(potential_terms, FermionOperator.zero()) kinetic = sum(kinetic_terms, FermionOperator.zero()) error_operator = ( split_operator_trotter_error_operator_diagonal_two_body( hamiltonian, order='V+T')) # V-then-T ordered double commutators: [V, [T, V]] + [T, [T, V]] / 2 inner_commutator = normal_ordered(commutator(kinetic, potential)) error_operator_definition = normal_ordered( commutator(potential, inner_commutator)) error_operator_definition += normal_ordered( commutator(kinetic, inner_commutator)) / 2.0 error_operator_definition /= 12.0 self.assertEqual(error_operator, error_operator_definition)
def test_type_error_on_bad_input_hamiltonian(self): with self.assertRaises(TypeError): diagonal_coulomb_potential_and_kinetic_terms_as_arrays('oops')