def test_recursive_min_eigen_optimizer(self):
        """Test the recursive minimum eigen optimizer."""
        filename = "op_ip1.lp"
        # get minimum eigen solver
        min_eigen_solver = NumPyMinimumEigensolver()

        # construct minimum eigen optimizer
        min_eigen_optimizer = MinimumEigenOptimizer(min_eigen_solver)
        recursive_min_eigen_optimizer = RecursiveMinimumEigenOptimizer(
            min_eigen_optimizer, min_num_vars=4)

        # load optimization problem
        problem = QuadraticProgram()
        lp_file = self.get_resource_path(filename, "algorithms/resources")
        problem.read_from_lp_file(lp_file)

        # solve problem with cplex
        cplex = CplexOptimizer()
        cplex_result = cplex.solve(problem)

        # solve problem
        result = recursive_min_eigen_optimizer.solve(problem)

        # analyze results
        np.testing.assert_array_almost_equal(cplex_result.x, result.x, 4)
        self.assertAlmostEqual(cplex_result.fval, result.fval)
    def test_recursive_min_eigen_optimizer(self):
        """Test the recursive minimum eigen optimizer."""
        try:
            filename = 'op_ip1.lp'
            # get minimum eigen solver
            min_eigen_solver = NumPyMinimumEigensolver()

            # construct minimum eigen optimizer
            min_eigen_optimizer = MinimumEigenOptimizer(min_eigen_solver)
            recursive_min_eigen_optimizer = RecursiveMinimumEigenOptimizer(
                min_eigen_optimizer, min_num_vars=4)

            # load optimization problem
            problem = QuadraticProgram()
            lp_file = self.get_resource_path(path.join('resources', filename))
            problem.read_from_lp_file(lp_file)

            # solve problem with cplex
            cplex = CplexOptimizer()
            cplex_result = cplex.solve(problem)

            # solve problem
            result = recursive_min_eigen_optimizer.solve(problem)

            # analyze results
            self.assertAlmostEqual(cplex_result.fval, result.fval)
        except MissingOptionalLibraryError as ex:
            self.skipTest(str(ex))
    def test_recursive_warm_qaoa(self):
        """Test the recursive optimizer with warm start qaoa."""
        algorithm_globals.random_seed = 12345
        backend = BasicAer.get_backend("statevector_simulator")
        qaoa = QAOA(quantum_instance=backend, reps=1)
        warm_qaoa = WarmStartQAOAOptimizer(pre_solver=SlsqpOptimizer(),
                                           relax_for_pre_solver=True,
                                           qaoa=qaoa)

        recursive_min_eigen_optimizer = RecursiveMinimumEigenOptimizer(
            warm_qaoa, min_num_vars=4)

        # load optimization problem
        problem = QuadraticProgram()
        lp_file = self.get_resource_path("op_ip1.lp", "algorithms/resources")
        problem.read_from_lp_file(lp_file)

        # solve problem with cplex
        cplex = CplexOptimizer(cplex_parameters={
            "threads": 1,
            "randomseed": 1
        })
        cplex_result = cplex.solve(problem)

        # solve problem
        result = recursive_min_eigen_optimizer.solve(problem)

        # analyze results
        np.testing.assert_array_almost_equal(cplex_result.x, result.x, 4)
        self.assertAlmostEqual(cplex_result.fval, result.fval)
Exemplo n.º 4
0
    def test_min_eigen_optimizer(self, config):
        """Min Eigen Optimizer Test"""
        try:
            # unpack configuration
            min_eigen_solver_name, backend, filename = config

            # get minimum eigen solver
            min_eigen_solver = self.min_eigen_solvers[min_eigen_solver_name]
            if backend:
                min_eigen_solver.quantum_instance = BasicAer.get_backend(backend)

            # construct minimum eigen optimizer
            min_eigen_optimizer = MinimumEigenOptimizer(min_eigen_solver)

            # load optimization problem
            problem = QuadraticProgram()
            lp_file = self.get_resource_path(filename, "algorithms/resources")
            problem.read_from_lp_file(lp_file)

            # solve problem with cplex
            cplex = CplexOptimizer()
            cplex_result = cplex.solve(problem)

            # solve problem
            result = min_eigen_optimizer.solve(problem)
            self.assertIsNotNone(result)

            # analyze results
            self.assertAlmostEqual(cplex_result.fval, result.fval)

            # check that eigensolver result is present
            self.assertIsNotNone(result.min_eigen_solver_result)
        except RuntimeError as ex:
            self.fail(str(ex))
Exemplo n.º 5
0
class TestCplexOptimizer(QiskitOptimizationTestCase):
    """Cplex Optimizer Tests."""

    def setUp(self):
        super().setUp()
        try:
            self.cplex_optimizer = CplexOptimizer(disp=False)
        except MissingOptionalLibraryError as ex:
            self.skipTest(str(ex))

    @data(
        ('op_ip1.lp', [0, 2], 6),
        ('op_mip1.lp', [1, 1, 0], 6),
        ('op_lp1.lp', [0.25, 1.75], 5.8750)
    )
    def test_cplex_optimizer(self, config):
        """ Cplex Optimizer Test """
        # unpack configuration
        filename, x, fval = config

        # load optimization problem
        problem = QuadraticProgram()
        lp_file = self.get_resource_path(path.join('resources', filename))
        problem.read_from_lp_file(lp_file)

        # solve problem with cplex
        result = self.cplex_optimizer.solve(problem)

        # analyze results
        self.assertAlmostEqual(result.fval, fval)
        for i in range(problem.get_num_vars()):
            self.assertAlmostEqual(result.x[i], x[i])
    def test_cplex_optimizer_no_solution(self, config):
        """Cplex Optimizer Test if no solution is found"""
        cplex_optimizer = CplexOptimizer(disp=False, cplex_parameters={"dettimelimit": 0})
        # unpack configuration
        filename, _, _ = config

        # load optimization problem
        problem = QuadraticProgram()
        lp_file = self.get_resource_path(filename, "algorithms/resources")
        problem.read_from_lp_file(lp_file)

        # solve problem with cplex
        result = cplex_optimizer.solve(problem)
        np.testing.assert_array_almost_equal(result.x, np.zeros(problem.get_num_vars()))
        self.assertEqual(result.status, OptimizationResultStatus.FAILURE)
        self.assertEqual(result.raw_results, None)
    def test_cplex_optimizer(self, config):
        """Cplex Optimizer Test"""
        cplex_optimizer = CplexOptimizer(disp=False)
        # unpack configuration
        filename, x, fval = config

        # load optimization problem
        problem = QuadraticProgram()
        lp_file = self.get_resource_path(filename, "algorithms/resources")
        problem.read_from_lp_file(lp_file)

        # solve problem with cplex
        result = cplex_optimizer.solve(problem)

        # analyze results
        self.assertAlmostEqual(result.fval, fval)
        for i in range(problem.get_num_vars()):
            self.assertAlmostEqual(result.x[i], x[i])