def test_cobyla_optimizer_with_variable_bounds(self): """ Cobyla Optimizer Test With Variable Bounds. """ # initialize optimizer cobyla = CobylaOptimizer() # initialize problem problem = QuadraticProgram() # set variables and bounds problem.continuous_var(lowerbound=-1, upperbound=1) problem.continuous_var(lowerbound=-2, upperbound=2) # set objective and minimize problem.minimize(linear=[1, 1]) # solve problem with cobyla result = cobyla.solve(problem) # analyze results self.assertAlmostEqual(result.x[0], -1.0, places=6) self.assertAlmostEqual(result.x[1], -2.0, places=6) # set objective and minimize problem.maximize(linear=[1, 1]) # solve problem with cobyla result = cobyla.solve(problem) # analyze results self.assertAlmostEqual(result.x[0], 1.0, places=6) self.assertAlmostEqual(result.x[1], 2.0, places=6)
def test_admm_maximization(self): """Tests a simple maximization problem using ADMM optimizer""" 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() solver = ADMMOptimizer(params=admm_params, continuous_optimizer=CobylaOptimizer()) 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) self.assertEqual(len(solution.samples), 1) self.assertAlmostEqual(solution.fval, solution.samples[0].fval) np.testing.assert_almost_equal(solution.x, solution.samples[0].x) self.assertEqual(solution.status, solution.samples[0].status) self.assertAlmostEqual(solution.samples[0].probability, 1.0)
def test_cobyla_optimizer_with_trials(self): """ Cobyla Optimizer Test. """ # load optimization problem 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 cobyla cobyla = CobylaOptimizer(trials=3) result = cobyla.solve(problem) # analyze results self.assertAlmostEqual(result.fval, 5.8750)
class TestCobylaOptimizer(QiskitOptimizationTestCase): """Cobyla Optimizer Tests.""" def setUp(self): super().setUp() self.resource_path = './test/optimization/resources/' self.cobyla_optimizer = CobylaOptimizer() @data( ('op_lp1.lp', 5.8750) ) def test_cobyla_optimizer(self, config): """ Cobyla Optimizer Test """ # unpack configuration filename, fval = config # load optimization problem problem = OptimizationProblem() problem.read(self.resource_path + filename) # solve problem with cobyla result = self.cobyla_optimizer.solve(problem) # analyze results self.assertAlmostEqual(result.fval, fval) def test_cobyla_optimizer_with_quadratic_constraint(self): """ Cobyla Optimizer Test """ # load optimization problem problem = OptimizationProblem() problem.variables.add(lb=[0, 0], ub=[1, 1], types='CC') problem.objective.set_linear([(0, 1), (1, 1)]) qc = problem.quadratic_constraints linear = SparsePair(ind=[0, 1], val=[-1, -1]) quadratic = SparseTriple(ind1=[0, 1], ind2=[0, 1], val=[1, 1]) qc.add(name='qc', lin_expr=linear, quad_expr=quadratic, rhs=-1/2) # solve problem with cobyla result = self.cobyla_optimizer.solve(problem) # analyze results self.assertAlmostEqual(result.fval, 1.0, places=2)
def test_cobyla_optimizer_with_quadratic_constraint(self): """ Cobyla Optimizer Test With Quadratic Constraints. """ # load optimization problem 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) # solve problem with cobyla cobyla = CobylaOptimizer() result = cobyla.solve(problem) # analyze results self.assertAlmostEqual(result.fval, 1.0, places=2)
def calculate(self, G, cost_matrix, starting_node = 0): # Create nodes array for the TSP solver in Qiskit coords = [] for node in G.nodes: coords.append(G.nodes[node]['pos']) tsp_instance = tsp.TspData(name = "TSP", dim = len(G.nodes), coord = coords, w = cost_matrix) qubitOp, offset = tsp.get_operator(tsp_instance) print("Qubits needed: ", qubitOp.num_qubits) #print(qubitOp.print_details()) #backend = Aer.get_backend('statevector_simulator') backend = Aer.get_backend('qasm_simulator') # Create QUBO based on qubitOp from the TSP qp = QuadraticProgram() qp.from_ising(qubitOp, offset, linear=True) admm_params = ADMMParameters( rho_initial=1001, beta=1000, factor_c=900, maxiter=100, three_block=True, tol=1.e-6) qubo_optimizer = MinimumEigenOptimizer(QAOA(quantum_instance=backend)) convex_optimizer = CobylaOptimizer() admm = ADMMOptimizer(params=admm_params, qubo_optimizer=qubo_optimizer, continuous_optimizer=convex_optimizer) quantum_instance = QuantumInstance(backend) result = admm.solve(qp) print(result)
def setUp(self): super().setUp() self.resource_path = './test/optimization/resources/' self.cobyla_optimizer = CobylaOptimizer()