示例#1
0
    def test_from_integrals_to_qubit(self):
        hamiltonian = jordan_wigner(self.molecule.get_molecular_hamiltonian())
        doci_hamiltonian = DOCIHamiltonian.from_integrals(
            constant=self.molecule.nuclear_repulsion,
            one_body_integrals=self.molecule.one_body_integrals,
            two_body_integrals=self.molecule.two_body_integrals).qubit_operator

        hamiltonian_matrix = get_sparse_operator(hamiltonian).toarray()
        doci_hamiltonian_matrix = get_sparse_operator(
            doci_hamiltonian).toarray()
        diagonal = numpy.real(numpy.diag(hamiltonian_matrix))
        doci_diagonal = numpy.real(numpy.diag(doci_hamiltonian_matrix))
        position_of_doci_diag_in_h = [0] * len(doci_diagonal)
        for idx, doci_eigval in enumerate(doci_diagonal):
            closest_in_diagonal = None
            for idx2, eig in enumerate(diagonal):
                if closest_in_diagonal is None or abs(eig - doci_eigval) < abs(
                        closest_in_diagonal - doci_eigval):
                    closest_in_diagonal = eig
                    position_of_doci_diag_in_h[idx] = idx2
            assert abs(closest_in_diagonal - doci_eigval) < EQ_TOLERANCE, (
                "Value " + str(doci_eigval) + " of the DOCI Hamiltonian " +
                "diagonal did not appear in the diagonal of the full " +
                "Hamiltonian. The closest value was " +
                str(closest_in_diagonal))

        sub_matrix = hamiltonian_matrix[numpy.ix_(position_of_doci_diag_in_h,
                                                  position_of_doci_diag_in_h)]
        assert numpy.allclose(doci_hamiltonian_matrix, sub_matrix), (
            "The coupling between the DOCI states in the DOCI Hamiltonian " +
            "should be identical to that between these states in the full " +
            "Hamiltonian but the DOCI hamiltonian matrix\n" +
            str(doci_hamiltonian_matrix) +
            "\ndoes not match the corresponding sub-matrix of the full " +
            "Hamiltonian\n" + str(sub_matrix))
示例#2
0
 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)
示例#3
0
 def test_error(self):
     doci_hamiltonian = DOCIHamiltonian.from_integrals(
         constant=self.molecule.nuclear_repulsion,
         one_body_integrals=self.molecule.one_body_integrals,
         two_body_integrals=self.molecule.two_body_integrals)
     with self.assertRaises(TypeError):
         doci_hamiltonian[((1, 0), (0, 1))] = 1
     with self.assertRaises(IndexError):
         _ = doci_hamiltonian[((1, 0), )]
     with self.assertRaises(IndexError):
         _ = doci_hamiltonian[((1, 1), (0, 0))]
     with self.assertRaises(IndexError):
         _ = doci_hamiltonian[((0, 1), (1, 1), (0, 0), (2, 0))]