def setUp(self): super().setUp() try: self.resource_path = './test/optimization/resources/' self.cplex_optimizer = CplexOptimizer(disp=False) except NameError as ex: self.skipTest(str(ex))
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() problem.read_from_lp_file(self.resource_path + filename) # 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 RuntimeError as ex: msg = str(ex) if 'CPLEX' in msg: self.skipTest(msg) else: self.fail(msg)
def test_min_eigen_optimizer(self, config): """ Min Eigen Optimizer Test """ # 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 = OptimizationProblem() problem.read(self.resource_path + filename) # solve problem with cplex cplex = CplexOptimizer() cplex_result = cplex.solve(problem) # solve problem result = min_eigen_optimizer.solve(problem) # analyze results 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_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() problem.read_from_lp_file(self.resource_path + filename) # solve problem with cplex cplex = CplexOptimizer() cplex_result = cplex.solve(problem) # solve problem result = min_eigen_optimizer.solve(problem) # analyze results self.assertAlmostEqual(cplex_result.fval, result.fval) except RuntimeError as ex: msg = str(ex) if 'CPLEX' in msg: self.skipTest(msg) else: self.fail(msg)
def test_admm_ex4_no_bin_var_in_objective(self): """Modified Example 4 as a unit test. See the description of the previous test. This test differs from the previous in the objective: one binary variable is omitted in objective to test a problem when a binary variable defined but is used only in constraints. """ try: mdl = Model('ex4') v = mdl.binary_var(name='v') w = mdl.binary_var(name='w') # pylint:disable=invalid-name t = mdl.binary_var(name='t') # b = 1 b = 2 mdl.minimize(v + t) mdl.add_constraint(2 * v + 10 * w + t <= 3, "cons1") mdl.add_constraint(v + w + t >= b, "cons2") op = QuadraticProgram() op.from_docplex(mdl) # qubo_optimizer = MinimumEigenOptimizer(NumPyMinimumEigensolver()) qubo_optimizer = CplexOptimizer() continuous_optimizer = CplexOptimizer() admm_params = ADMMParameters(rho_initial=1001, beta=1000, factor_c=900, max_iter=100, three_block=False) solver = ADMMOptimizer( params=admm_params, qubo_optimizer=qubo_optimizer, continuous_optimizer=continuous_optimizer, ) solution = solver.solve(op) self.assertIsNotNone(solution) self.assertIsInstance(solution, ADMMOptimizationResult) self.assertIsNotNone(solution.x) np.testing.assert_almost_equal([1., 0., 1.], solution.x, 3) self.assertIsNotNone(solution.fval) np.testing.assert_almost_equal(2., solution.fval, 3) self.assertIsNotNone(solution.state) self.assertIsInstance(solution.state, ADMMState) except NameError as ex: self.skipTest(str(ex))
def test_admm_ex4(self): """Example 4 as a unit test. Example 4 is reported in: Gambella, C., & Simonetto, A. (2020). Multi-block ADMM Heuristics for Mixed-Binary Optimization on Classical and Quantum Computers. arXiv preprint arXiv:2001.02069.""" try: mdl = Model('ex4') v = mdl.binary_var(name='v') w = mdl.binary_var(name='w') # pylint:disable=invalid-name t = mdl.binary_var(name='t') # b = 1 b = 2 mdl.minimize(v + w + t) mdl.add_constraint(2 * v + 10 * w + t <= 3, "cons1") mdl.add_constraint(v + w + t >= b, "cons2") op = QuadraticProgram() op.from_docplex(mdl) # qubo_optimizer = MinimumEigenOptimizer(NumPyMinimumEigensolver()) qubo_optimizer = CplexOptimizer() continuous_optimizer = CplexOptimizer() admm_params = ADMMParameters(rho_initial=1001, beta=1000, factor_c=900, max_iter=100, three_block=False) solver = ADMMOptimizer( params=admm_params, qubo_optimizer=qubo_optimizer, continuous_optimizer=continuous_optimizer, ) solution = solver.solve(op) self.assertIsNotNone(solution) self.assertIsInstance(solution, ADMMOptimizationResult) self.assertIsNotNone(solution.x) np.testing.assert_almost_equal([1., 0., 1.], solution.x, 3) self.assertIsNotNone(solution.fval) np.testing.assert_almost_equal(2., solution.fval, 3) self.assertIsNotNone(solution.state) self.assertIsInstance(solution.state, ADMMState) except NameError as ex: self.skipTest(str(ex))
def test_admm_ex6(self): """Example 6 as a unit test. Example 6 is reported in: Gambella, C., & Simonetto, A. (2020). Multi-block ADMM Heuristics for Mixed-Binary Optimization on Classical and Quantum Computers. arXiv preprint arXiv:2001.02069.""" try: mdl = Model('ex6') # pylint:disable=invalid-name v = mdl.binary_var(name='v') w = mdl.binary_var(name='w') t = mdl.binary_var(name='t') u = mdl.continuous_var(name='u') mdl.minimize(v + w + t + 5 * (u - 2)**2) mdl.add_constraint(v + 2 * w + t + u <= 3, "cons1") mdl.add_constraint(v + w + t >= 1, "cons2") mdl.add_constraint(v + w == 1, "cons3") op = QuadraticProgram() op.from_docplex(mdl) qubo_optimizer = CplexOptimizer() continuous_optimizer = CplexOptimizer() admm_params = ADMMParameters(rho_initial=1001, beta=1000, factor_c=900, max_iter=100, three_block=True, tol=1.e-6) solver = ADMMOptimizer(params=admm_params, qubo_optimizer=qubo_optimizer, continuous_optimizer=continuous_optimizer) solution = solver.solve(op) self.assertIsNotNone(solution) self.assertIsInstance(solution, ADMMOptimizationResult) self.assertIsNotNone(solution.x) np.testing.assert_almost_equal([1., 0., 0., 2.], solution.x, 3) self.assertIsNotNone(solution.fval) np.testing.assert_almost_equal(1., solution.fval, 3) self.assertIsNotNone(solution.state) self.assertIsInstance(solution.state, ADMMState) except NameError as ex: self.skipTest(str(ex))
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_admm_maximization(self): """Tests a simple maximization problem using ADMM optimizer""" mdl = Model('test') c = mdl.continuous_var(lb=0, ub=10, name='c') x = mdl.binary_var(name='x') mdl.maximize(c + x * x) op = OptimizationProblem() op.from_docplex(mdl) self.assertIsNotNone(op) admm_params = ADMMParameters() qubo_optimizer = MinimumEigenOptimizer(NumPyMinimumEigensolver()) continuous_optimizer = CplexOptimizer() solver = ADMMOptimizer(qubo_optimizer=qubo_optimizer, continuous_optimizer=continuous_optimizer, params=admm_params) solution: ADMMOptimizerResult = solver.solve(op) self.assertIsNotNone(solution) self.assertIsInstance(solution, ADMMOptimizerResult) self.assertIsNotNone(solution.x) np.testing.assert_almost_equal([10, 0], solution.x, 3) self.assertIsNotNone(solution.fval) np.testing.assert_almost_equal(10, solution.fval, 3) self.assertIsNotNone(solution.state) self.assertIsInstance(solution.state, ADMMState)
def test_admm_miskp_eigen(self): """ADMM Optimizer Test based on Mixed-Integer Setup Knapsack Problem using NumPy eigen optimizer""" miskp = Miskp(*self._get_miskp_problem_params()) op: OptimizationProblem = miskp.create_problem() self.assertIsNotNone(op) admm_params = ADMMParameters() # use numpy exact diagonalization qubo_optimizer = MinimumEigenOptimizer(NumPyMinimumEigensolver()) continuous_optimizer = CplexOptimizer() solver = ADMMOptimizer(qubo_optimizer=qubo_optimizer, continuous_optimizer=continuous_optimizer, params=admm_params) solution: ADMMOptimizerResult = solver.solve(op) self.assertIsNotNone(solution) self.assertIsInstance(solution, ADMMOptimizerResult) correct_solution = [ 0.009127, 0.009127, 0.009127, 0.009127, 0.009127, 0.009127, 0.009127, 0.009127, 0.009127, 0.009127, 0.006151, 0.006151, 0.006151, 0.006151, 0.006151, 0.006151, 0.006151, 0.006151, 0.006151, 0.006151, 0., 0. ] correct_objective = -1.2113693 self.assertIsNotNone(solution.x) np.testing.assert_almost_equal(correct_solution, solution.x, 3) self.assertIsNotNone(solution.fval) np.testing.assert_almost_equal(correct_objective, solution.fval, 3) self.assertIsNotNone(solution.state) self.assertIsInstance(solution.state, ADMMState)
def test_continuous_variable_decode(self): """ Test decode func of IntegerToBinaryConverter for continuous variables""" try: mdl = Model('test_continuous_varable_decode') c = mdl.continuous_var(lb=0, ub=10.9, name='c') x = mdl.binary_var(name='x') mdl.maximize(c + x * x) op = QuadraticProgram() op.from_docplex(mdl) converter = IntegerToBinary() op = converter.convert(op) admm_params = ADMMParameters() qubo_optimizer = MinimumEigenOptimizer(NumPyMinimumEigensolver()) continuous_optimizer = CplexOptimizer() solver = ADMMOptimizer( qubo_optimizer=qubo_optimizer, continuous_optimizer=continuous_optimizer, params=admm_params, ) result = solver.solve(op) result = converter.interpret(result) self.assertEqual(result.x[0], 10.9) self.assertListEqual(result.variable_names, ['c', 'x']) self.assertDictEqual(result.variables_dict, {'c': 10.9, 'x': 0}) except NameError as ex: self.skipTest(str(ex))
class TestCplexOptimizer(QiskitOptimizationTestCase): """Cplex Optimizer Tests.""" def setUp(self): super().setUp() self.resource_path = './test/optimization/resources/' self.cplex_optimizer = CplexOptimizer() @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 = OptimizationProblem() problem.read(self.resource_path + filename) # solve problem with cplex result = self.cplex_optimizer.solve(problem) # analyze results self.assertAlmostEqual(result.fval, fval) self.assertAlmostEqual(result.x, x)
def test_admm_maximization(self): """Tests a simple maximization problem using ADMM optimizer""" try: mdl = Model('simple-max') c = mdl.continuous_var(lb=0, ub=10, name='c') x = mdl.binary_var(name='x') mdl.maximize(c + x * x) op = QuadraticProgram() op.from_docplex(mdl) admm_params = ADMMParameters() qubo_optimizer = MinimumEigenOptimizer(NumPyMinimumEigensolver()) # qubo_optimizer = CplexOptimizer() continuous_optimizer = CplexOptimizer() solver = ADMMOptimizer(qubo_optimizer=qubo_optimizer, continuous_optimizer=continuous_optimizer, params=admm_params) solution = solver.solve(op) self.assertIsNotNone(solution) self.assertIsInstance(solution, ADMMOptimizationResult) self.assertIsNotNone(solution.x) np.testing.assert_almost_equal([10, 0], solution.x, 3) self.assertIsNotNone(solution.fval) np.testing.assert_almost_equal(10, solution.fval, 3) self.assertIsNotNone(solution.state) self.assertIsInstance(solution.state, ADMMState) except NameError as ex: self.skipTest(str(ex))
def test_quad_constraints(self): """Simple example to test quadratic constraints.""" try: mdl = Model('quad-constraints') v = mdl.binary_var(name='v') w = mdl.continuous_var(name='w', lb=0.) mdl.minimize(v + w) mdl.add_constraint(v + w >= 1, "cons2") mdl.add_constraint(v**2 + w**2 <= 1, "cons2") op = QuadraticProgram() op.from_docplex(mdl) # qubo_optimizer = MinimumEigenOptimizer(NumPyMinimumEigensolver()) qubo_optimizer = CplexOptimizer() continuous_optimizer = CplexOptimizer() admm_params = ADMMParameters( rho_initial=1001, beta=1000, factor_c=900, max_iter=100, three_block=True, ) solver = ADMMOptimizer( params=admm_params, qubo_optimizer=qubo_optimizer, continuous_optimizer=continuous_optimizer, ) solution = solver.solve(op) self.assertIsNotNone(solution) self.assertIsInstance(solution, ADMMOptimizationResult) self.assertIsNotNone(solution.x) np.testing.assert_almost_equal([0., 1.], solution.x, 3) self.assertIsNotNone(solution.fval) np.testing.assert_almost_equal(1., solution.fval, 3) self.assertIsNotNone(solution.state) self.assertIsInstance(solution.state, ADMMState) except NameError as ex: self.skipTest(str(ex))
def test_equality_constraints_with_continuous_variables(self): """Simple example to test equality constraints with continuous variables.""" try: mdl = Model("eq-constraints-cts-vars") # pylint:disable=invalid-name v = mdl.binary_var(name='v') w = mdl.continuous_var(name='w', lb=0.) t = mdl.continuous_var(name='t', lb=0.) mdl.minimize(v + w + t) mdl.add_constraint(2 * v + w >= 2, "cons1") mdl.add_constraint(w + t == 1, "cons2") op = QuadraticProgram() op.from_docplex(mdl) # qubo_optimizer = MinimumEigenOptimizer(NumPyMinimumEigensolver()) qubo_optimizer = CplexOptimizer() continuous_optimizer = CplexOptimizer() admm_params = ADMMParameters( rho_initial=1001, beta=1000, factor_c=900, max_iter=100, three_block=True, ) solver = ADMMOptimizer( params=admm_params, qubo_optimizer=qubo_optimizer, continuous_optimizer=continuous_optimizer, ) solution = solver.solve(op) self.assertIsNotNone(solution) self.assertIsInstance(solution, ADMMOptimizationResult) self.assertIsNotNone(solution.x) np.testing.assert_almost_equal([0., 1., 0.], solution.x, 3) self.assertIsNotNone(solution.fval) np.testing.assert_almost_equal(1., solution.fval, 3) self.assertIsNotNone(solution.state) self.assertIsInstance(solution.state, ADMMState) except NameError as ex: self.skipTest(str(ex))
def test_admm_ex6_max(self): """Example 6 as maximization""" try: mdl = Model('ex6-max') # pylint:disable=invalid-name v = mdl.binary_var(name='v') w = mdl.binary_var(name='w') t = mdl.binary_var(name='t') u = mdl.continuous_var(name='u') # mdl.minimize(v + w + t + 5 * (u - 2) ** 2) mdl.maximize(-v - w - t - 5 * (u - 2)**2) mdl.add_constraint(v + 2 * w + t + u <= 3, "cons1") mdl.add_constraint(v + w + t >= 1, "cons2") mdl.add_constraint(v + w == 1, "cons3") op = QuadraticProgram() op.from_docplex(mdl) qubo_optimizer = CplexOptimizer() continuous_optimizer = CplexOptimizer() admm_params = ADMMParameters(rho_initial=1001, beta=1000, factor_c=900, max_iter=100, three_block=True, tol=1.e-6) solver = ADMMOptimizer(params=admm_params, qubo_optimizer=qubo_optimizer, continuous_optimizer=continuous_optimizer) solution = solver.solve(op) self.assertIsNotNone(solution) self.assertIsInstance(solution, ADMMOptimizationResult) self.assertIsNotNone(solution.x) np.testing.assert_almost_equal([1., 0., 0., 2.], solution.x, 3) self.assertIsNotNone(solution.fval) np.testing.assert_almost_equal(-1., solution.fval, 3) self.assertIsNotNone(solution.state) self.assertIsInstance(solution.state, ADMMState) except NameError as ex: self.skipTest(str(ex))
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(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 = 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 MissingOptionalLibraryError as ex: self.skipTest(str(ex)) except RuntimeError as ex: self.fail(str(ex))
def test_continuous_variable_decode(self): """ Test decode func of IntegerToBinaryConverter for continuous variables""" try: mdl = Model('test_continuous_varable_decode') c = mdl.continuous_var(lb=0, ub=10.9, name='c') x = mdl.binary_var(name='x') mdl.maximize(c + x * x) op = QuadraticProgram() op.from_docplex(mdl) converter = IntegerToBinary() op = converter.encode(op) admm_params = ADMMParameters() qubo_optimizer = MinimumEigenOptimizer(NumPyMinimumEigensolver()) continuous_optimizer = CplexOptimizer() solver = ADMMOptimizer( qubo_optimizer=qubo_optimizer, continuous_optimizer=continuous_optimizer, params=admm_params, ) solution = solver.solve(op) solution = converter.decode(solution) self.assertEqual(solution.x[0], 10.9) except NameError as ex: self.skipTest(str(ex))
def setUp(self): super().setUp() try: self.cplex_optimizer = CplexOptimizer(disp=False) except NameError as ex: self.skipTest(str(ex))
def setUp(self): super().setUp() try: self.cplex_optimizer = CplexOptimizer(disp=False) except MissingOptionalLibraryError as ex: self.skipTest(str(ex))
def setUp(self): super().setUp() self.resource_path = './test/optimization/resources/' self.cplex_optimizer = CplexOptimizer()