def test_errors_operations(self): doci_hamiltonian = DOCIHamiltonian.zero(n_qubits=2) doci_hamiltonian2 = DOCIHamiltonian.zero(n_qubits=3) with self.assertRaises(TypeError): doci_hamiltonian += 'a' with self.assertRaises(TypeError): doci_hamiltonian -= 'a' with self.assertRaises(TypeError): doci_hamiltonian *= 'a' with self.assertRaises(TypeError): doci_hamiltonian /= 'a' with self.assertRaises(TypeError): doci_hamiltonian += doci_hamiltonian2 with self.assertRaises(TypeError): doci_hamiltonian -= doci_hamiltonian2
def test_getting_setting_1body(self): doci_hamiltonian = DOCIHamiltonian.zero(n_qubits=2) doci_hamiltonian.hc[0] = 2 doci_hamiltonian.hc[1] = 4 self.assertEqual(doci_hamiltonian[((0, 1), (0, 0))], 1) self.assertEqual(doci_hamiltonian[((1, 1), (1, 0))], 1) self.assertEqual(doci_hamiltonian[((2, 1), (2, 0))], 2) self.assertEqual(doci_hamiltonian[((3, 1), (3, 0))], 2)
def test_basic_operations(self): doci_hamiltonian1 = DOCIHamiltonian.zero(n_qubits=2) doci_hamiltonian2 = DOCIHamiltonian.from_integrals( constant=self.molecule.nuclear_repulsion, one_body_integrals=self.molecule.one_body_integrals, two_body_integrals=self.molecule.two_body_integrals) self.assertTrue(doci_hamiltonian2 == doci_hamiltonian1 + doci_hamiltonian2) self.assertTrue(doci_hamiltonian1 - doci_hamiltonian2 == doci_hamiltonian2 / -1) self.assertTrue(doci_hamiltonian2 * 0 == doci_hamiltonian1)
def test_n_body_tensor_errors(self): doci_hamiltonian = DOCIHamiltonian.zero(n_qubits=2) with self.assertRaises(TypeError): doci_hamiltonian.n_body_tensors = 0 with self.assertRaises(IndexError): _ = doci_hamiltonian[((0, 0), (0, 0))] with self.assertRaises(IndexError): _ = doci_hamiltonian[((0, 0), (0, 0), (0, 0), (0, 0))] with self.assertRaises(IndexError): _ = doci_hamiltonian[((1, 1), (0, 0))] with self.assertRaises(IndexError): _ = doci_hamiltonian[((0, 1), (2, 1), (3, 0), (8, 0))]
def test_getting_setting_hr1(self): doci_hamiltonian = DOCIHamiltonian.zero(n_qubits=2) doci_hamiltonian.hr1[0, 1] = 2 self.assertEqual(doci_hamiltonian[(0, 1), (1, 1), (3, 0), (2, 0)], 1) self.assertEqual(doci_hamiltonian[(1, 1), (0, 1), (2, 0), (3, 0)], 1)
def test_getting_setting_constant(self): doci_hamiltonian = DOCIHamiltonian.zero(n_qubits=2) doci_hamiltonian.constant = 1 self.assertEqual(doci_hamiltonian[()], 1)
def test_adding_constants(self): doci_hamiltonian = DOCIHamiltonian.zero(n_qubits=2) doci_hamiltonian += 2 self.assertAlmostEqual(doci_hamiltonian.constant, 2) doci_hamiltonian -= 3 self.assertAlmostEqual(doci_hamiltonian.constant, -1)