Ejemplo n.º 1
0
class TestNumPyEigensolverFactory(QiskitNatureTestCase):
    """Test NumPyMinimumEigensovler Factory"""

    # NOTE: The actual usage of this class is mostly tested in combination with the ground-state
    # eigensolvers (one module above).

    @requires_extra_library
    def setUp(self):
        super().setUp()

        self.driver = PySCFDriver(
            atom="H .0 .0 .0; H .0 .0 0.75",
            unit=UnitsType.ANGSTROM,
            charge=0,
            spin=0,
            basis="sto3g",
        )

        self.electronic_structure_problem = ElectronicStructureProblem(
            self.driver)

        # pylint: disable=unused-argument
        def filter_criterion(eigenstate, eigenvalue, aux_values):
            return np.isclose(aux_values[0][0], 2.0)

        self.k = 99
        self._numpy_eigensolver_factory = NumPyEigensolverFactory(
            filter_criterion=filter_criterion, k=self.k)

    def test_setters_getters(self):
        """Test Getter/Setter"""

        # filter_criterion
        self.assertIsNotNone(self._numpy_eigensolver_factory.filter_criterion)

        # pylint: disable=unused-argument
        def filter_criterion(eigenstate, eigenvalue, aux_values):
            return np.isclose(aux_values[0][0], 3.0)

        self._numpy_eigensolver_factory.filter_criterion = filter_criterion
        self.assertEqual(self._numpy_eigensolver_factory.filter_criterion,
                         filter_criterion)

        # k
        self.assertEqual(self._numpy_eigensolver_factory.k, self.k)
        self._numpy_eigensolver_factory.k = 100
        self.assertEqual(self._numpy_eigensolver_factory.k, 100)

        # use_default_filter_criterion
        self.assertFalse(
            self._numpy_eigensolver_factory.use_default_filter_criterion)
        self._numpy_eigensolver_factory.use_default_filter_criterion = True
        self.assertTrue(
            self._numpy_eigensolver_factory.use_default_filter_criterion)
        # get_solver
        solver = self._numpy_eigensolver_factory.get_solver(
            self.electronic_structure_problem)
        self.assertIsInstance(solver, NumPyEigensolver)
        self.assertEqual(solver.k, 100)
        self.assertEqual(solver.filter_criterion, filter_criterion)
    def test_numpy_factory(self):
        """ Test with NumPyEigensolver """
        solver = NumPyEigensolverFactory(use_default_filter_criterion=True)
        esc = ExcitedStatesEigensolver(self.qubit_converter, solver)
        results = esc.solve(self.vibrational_problem)

        for idx in range(len(self.reference_energies)):
            self.assertAlmostEqual(results.computed_vibrational_energies[idx],
                                   self.reference_energies[idx],
                                   places=4)
Ejemplo n.º 3
0
    def setUp(self):
        super().setUp()

        self.driver = PySCFDriver(
            atom="H .0 .0 .0; H .0 .0 0.75",
            unit=UnitsType.ANGSTROM,
            charge=0,
            spin=0,
            basis="sto3g",
        )

        self.electronic_structure_problem = ElectronicStructureProblem(
            self.driver)

        # pylint: disable=unused-argument
        def filter_criterion(eigenstate, eigenvalue, aux_values):
            return np.isclose(aux_values[0][0], 2.0)

        self.k = 99
        self._numpy_eigensolver_factory = NumPyEigensolverFactory(
            filter_criterion=filter_criterion, k=self.k)
    def setUp(self):
        super().setUp()

        try:
            self.driver = PySCFDriver(atom='H .0 .0 .0; H .0 .0 0.75',
                                      unit=UnitsType.ANGSTROM,
                                      charge=0,
                                      spin=0,
                                      basis='sto3g')
        except QiskitNatureError:
            self.skipTest('PYSCF driver does not appear to be installed')

        self.electronic_structure_problem = ElectronicStructureProblem(
            self.driver)

        # pylint: disable=unused-argument
        def filter_criterion(eigenstate, eigenvalue, aux_values):
            return np.isclose(aux_values[0][0], 2.)

        self.k = 99
        self._numpy_eigensolver_factory = NumPyEigensolverFactory(
            filter_criterion=filter_criterion, k=self.k)
Ejemplo n.º 5
0
    def test_h2_bopes_sampler_excited_eigensolver(self):
        """Test BOPES Sampler on H2"""
        # Molecule
        dof = partial(Molecule.absolute_distance, atom_pair=(1, 0))
        m = Molecule(
            geometry=[["H", [0.0, 0.0, 1.0]], ["H", [0.0, 0.45, 1.0]]],
            degrees_of_freedom=[dof],
        )

        mapper = ParityMapper()
        converter = QubitConverter(mapper=mapper)

        driver = ElectronicStructureMoleculeDriver(
            m, driver_type=ElectronicStructureDriverType.PYSCF)
        problem = ElectronicStructureProblem(driver)

        # pylint: disable=unused-argument
        def filter_criterion(eigenstate, eigenvalue, aux_values):
            particle_number_filter = np.isclose(
                aux_values["ParticleNumber"][0], 2.0)
            magnetization_filter = np.isclose(aux_values["Magnetization"][0],
                                              0.0)
            return particle_number_filter and magnetization_filter

        solver = NumPyEigensolverFactory(filter_criterion=filter_criterion)
        np_excited_solver = ExcitedStatesEigensolver(converter, solver)

        # BOPES sampler
        sampler = BOPESSampler(np_excited_solver)

        # absolute internuclear distance in Angstrom
        points = [0.7, 1.0, 1.3]
        results = sampler.sample(problem, points)

        points_run = results.points
        energies = results.energies

        np.testing.assert_array_almost_equal(points_run, [0.7, 1.0, 1.3])
        np.testing.assert_array_almost_equal(
            energies,
            [
                [-1.13618945, -0.47845306, -0.1204519, 0.5833141],
                [-1.10115033, -0.74587179, -0.35229063, 0.03904763],
                [-1.03518627, -0.85523694, -0.42240202, -0.21860355],
            ],
            decimal=2,
        )
    def test_numpy_factory(self):
        """Test NumPyEigenSolverFactory with ExcitedStatesEigensolver"""

        # pylint: disable=unused-argument
        def filter_criterion(eigenstate, eigenvalue, aux_values):
            return np.isclose(aux_values[0][0], 2.0)

        solver = NumPyEigensolverFactory(filter_criterion=filter_criterion)
        esc = ExcitedStatesEigensolver(self.qubit_converter, solver)
        results = esc.solve(self.electronic_structure_problem)

        # filter duplicates from list
        computed_energies = [results.computed_energies[0]]
        for comp_energy in results.computed_energies[1:]:
            if not np.isclose(comp_energy, computed_energies[-1]):
                computed_energies.append(comp_energy)

        for idx, energy in enumerate(self.reference_energies):
            self.assertAlmostEqual(computed_energies[idx], energy, places=4)