Exemple #1
0
    def test_slsqp_optimizer(self):
        """Generic SLSQP Optimizer Test."""

        problem = QuadraticProgram()
        problem.continuous_var(upperbound=4)
        problem.continuous_var(upperbound=4)
        problem.linear_constraint(linear=[1, 1], sense="=", rhs=2)
        problem.minimize(linear=[2, 2], quadratic=[[2, 0.25], [0.25, 0.5]])

        # solve problem with SLSQP
        slsqp = SlsqpOptimizer(trials=3)
        result = slsqp.solve(problem)

        self.assertAlmostEqual(result.fval, 5.8750)
Exemple #2
0
    def test_slsqp_unbounded(self):
        """Unbounded test for optimization"""
        problem = QuadraticProgram()
        problem.continuous_var(name="x")
        problem.continuous_var(name="y")
        problem.maximize(linear=[2, 0], quadratic=[[-1, 2], [0, -2]])

        slsqp = SlsqpOptimizer()
        solution = slsqp.solve(problem)

        self.assertIsNotNone(solution)
        self.assertIsNotNone(solution.x)
        np.testing.assert_almost_equal([2.0, 1.0], solution.x, 3)
        self.assertIsNotNone(solution.fval)
        np.testing.assert_almost_equal(2.0, solution.fval, 3)
Exemple #3
0
    def test_slsqp_bounded(self):
        """Same as above, but a bounded test"""
        problem = QuadraticProgram()
        problem.continuous_var(name="x", lowerbound=2.5)
        problem.continuous_var(name="y", upperbound=0.5)
        problem.maximize(linear=[2, 0], quadratic=[[-1, 2], [0, -2]])

        slsqp = SlsqpOptimizer()
        solution = slsqp.solve(problem)

        self.assertIsNotNone(solution)
        self.assertIsNotNone(solution.x)
        np.testing.assert_almost_equal([2.5, 0.5], solution.x, 3)
        self.assertIsNotNone(solution.fval)
        np.testing.assert_almost_equal(0.75, solution.fval, 3)
    def test_simple_qubo(self):
        """Test on a simple QUBO problem."""
        model = Model()
        # pylint:disable=invalid-name
        u = model.binary_var(name="u")
        v = model.binary_var(name="v")

        model.minimize((u - v + 2)**2)
        problem = QuadraticProgram()
        problem.from_docplex(model)

        backend = BasicAer.get_backend("statevector_simulator")
        qaoa = QAOA(quantum_instance=backend, reps=1)
        optimizer = WarmStartQAOAOptimizer(
            pre_solver=SlsqpOptimizer(),
            relax_for_pre_solver=True,
            qaoa=qaoa,
            epsilon=0.25,
        )
        result_warm = optimizer.solve(problem)

        self.assertIsNotNone(result_warm)
        self.assertIsNotNone(result_warm.x)
        np.testing.assert_almost_equal([0, 1], result_warm.x, 3)
        self.assertIsNotNone(result_warm.fval)
        np.testing.assert_almost_equal(1, result_warm.fval, 3)
    def test_constrained_binary(self):
        """Constrained binary optimization problem."""
        model = Model()
        v = model.binary_var(name="v")
        w = model.binary_var(name="w")
        # pylint:disable=invalid-name
        t = model.binary_var(name="t")

        model.minimize(v + w + t)
        model.add_constraint(2 * v + 10 * w + t <= 3, "cons1")
        model.add_constraint(v + w + t >= 2, "cons2")

        problem = QuadraticProgram()
        problem.from_docplex(model)

        backend = BasicAer.get_backend("statevector_simulator")
        qaoa = QAOA(quantum_instance=backend, reps=1)
        aggregator = MeanAggregator()
        optimizer = WarmStartQAOAOptimizer(
            pre_solver=SlsqpOptimizer(),
            relax_for_pre_solver=True,
            qaoa=qaoa,
            epsilon=0.25,
            aggregator=aggregator,
        )
        result_warm = optimizer.solve(problem)

        self.assertIsNotNone(result_warm)
        self.assertIsNotNone(result_warm.x)
        np.testing.assert_almost_equal([1, 0, 1], result_warm.x, 3)
        self.assertIsNotNone(result_warm.fval)
        np.testing.assert_almost_equal(2, result_warm.fval, 3)
    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)
Exemple #7
0
    def test_slsqp_inequality(self):
        """A test with inequality constraint"""
        problem = QuadraticProgram()
        problem.continuous_var(name="x")
        problem.continuous_var(name="y")
        problem.linear_constraint(linear=[1, -1], sense=">=", rhs=1)
        problem.maximize(linear=[2, 0], quadratic=[[-1, 2], [0, -2]])

        slsqp = SlsqpOptimizer()
        solution = slsqp.solve(problem)

        self.assertIsNotNone(solution)
        self.assertIsNotNone(solution.x)
        np.testing.assert_almost_equal([2.0, 1.0], solution.x, 3)
        self.assertIsNotNone(solution.fval)
        np.testing.assert_almost_equal(2.0, solution.fval, 3)
Exemple #8
0
    def test_multistart_properties(self):
        """
        Tests properties of MultiStartOptimizer.
        Since it is an abstract class, the test is here.
        """
        trials = 5
        clip = 200.0

        slsqp = SlsqpOptimizer(trials=trials, clip=clip)
        self.assertEqual(trials, slsqp.trials)
        self.assertAlmostEqual(clip, slsqp.clip)

        trials = 6
        clip = 300.0
        slsqp.trials = trials
        slsqp.clip = clip
        self.assertEqual(trials, slsqp.trials)
        self.assertAlmostEqual(clip, slsqp.clip)
    def test_slsqp_unbounded_with_trials(self):
        """Unbounded test for optimization"""
        problem = QuadraticProgram()
        problem.continuous_var(name="x",
                               lowerbound=-INFINITY,
                               upperbound=INFINITY)
        problem.continuous_var(name="y",
                               lowerbound=-INFINITY,
                               upperbound=INFINITY)
        problem.maximize(linear=[2, 0], quadratic=[[-1, 2], [0, -2]])

        slsqp = SlsqpOptimizer(trials=3)
        solution = slsqp.solve(problem)

        self.assertIsNotNone(solution)
        self.assertIsNotNone(solution.x)
        np.testing.assert_almost_equal([2., 1.], solution.x, 3)
        self.assertIsNotNone(solution.fval)
        np.testing.assert_almost_equal(2., solution.fval, 3)
Exemple #10
0
    def test_slsqp_optimizer_with_quadratic_constraint(self):
        """A test with equality constraint"""
        problem = QuadraticProgram()
        problem.continuous_var(upperbound=1)
        problem.continuous_var(upperbound=1)

        problem.minimize(linear=[1, 1])

        linear = [-1, -1]
        quadratic = [[1, 0], [0, 1]]
        problem.quadratic_constraint(linear=linear,
                                     quadratic=quadratic,
                                     rhs=-1 / 2)

        slsqp = SlsqpOptimizer()
        solution = slsqp.solve(problem)

        self.assertIsNotNone(solution)
        self.assertIsNotNone(solution.x)
        np.testing.assert_almost_equal([0.5, 0.5], solution.x, 3)
        self.assertIsNotNone(solution.fval)
        np.testing.assert_almost_equal(1.0, solution.fval, 3)
Exemple #11
0
    def test_slsqp_optimizer_full_output(self):
        """Generic SLSQP Optimizer Test."""

        problem = QuadraticProgram()
        problem.continuous_var(upperbound=4)
        problem.continuous_var(upperbound=4)
        problem.linear_constraint(linear=[1, 1], sense="=", rhs=2)
        problem.minimize(linear=[2, 2], quadratic=[[2, 0.25], [0.25, 0.5]])

        # solve problem with SLSQP
        slsqp = SlsqpOptimizer(trials=3, full_output=True)
        result = slsqp.solve(problem)

        self.assertAlmostEqual(result.fval, 5.8750)

        self.assertAlmostEqual(result.fx, 5.8750)
        self.assertGreaterEqual(result.its, 1)
        self.assertEqual(result.imode, 0)
        self.assertIsNotNone(result.smode)
        self.assertEqual(len(result.samples), 1)
        self.assertAlmostEqual(result.fval, result.samples[0].fval)
        np.testing.assert_almost_equal(result.x, result.samples[0].x)
        self.assertEqual(result.status, result.samples[0].status)
        self.assertAlmostEqual(result.samples[0].probability, 1.0)