def to_quadratic_problem(self): mdl = Model() x = { i: mdl.binary_var(name='x_{0}'.format(i)) for i in range(self.g.number_of_nodes()) } for u, v in self.g.edges: self.g.edges[u, v].setdefault('weight', 1) objective = mdl.sum(self.g.edges[i, j]['weight'] * x[i] * (1 - x[j]) for i, j in self.g.edges) mdl.maximize(objective) # print(mdl.export_as_lp_string()) qp = QuadraticProgram() qp.from_docplex(mdl) #print(qp.export_as_lp_string()) self.qp = qp return self.qp
def to_quadratic_program(self): num_assets = len(self._mu) mdl = AdvModel(name='portfolio') x = [mdl.binary_var(name='x_{0}'.format(i)) for i in range(num_assets)] quad = mdl.quad_matrix_sum(self._sigma, x) linear = np.dot(self._mu, x) mdl.minimize(quad + linear) mdl.add_constraint(mdl.sum(x[i] for i in range(num_assets)) == self._budget) qp = QuadraticProgram() qp.from_docplex(mdl) return qp
def to_quadratic_program(self) -> QuadraticProgram: """Convert a vehicle routing problem instance into a :class:`~qiskit_optimization.problems.QuadraticProgram` Returns: The :class:`~qiskit_optimization.problems.QuadraticProgram` created from the vehicle routing problem instance. """ mdl = Model(name='Vehicle routing') n = self._graph.number_of_nodes() x = {} for i in range(n): for j in range(n): if i != j: x[(i, j)] = mdl.binary_var(name='x_{0}_{1}'.format(i, j)) mdl.minimize( mdl.sum(self._graph.edges[i, j]['weight'] * x[(i, j)] for i in range(n) for j in range(n) if i != j)) # Only 1 edge goes out from each node for i in range(n): if i != self.depot: mdl.add_constraint( mdl.sum(x[i, j] for j in range(n) if i != j) == 1) # Only 1 edge comes into each node for j in range(n): if j != self.depot: mdl.add_constraint( mdl.sum(x[i, j] for i in range(n) if i != j) == 1) # For the depot node mdl.add_constraint( mdl.sum(x[i, self.depot] for i in range(n) if i != self.depot) == self.num_vehicles) mdl.add_constraint( mdl.sum(x[self.depot, j] for j in range(n) if j != self.depot) == self.num_vehicles) # To eliminate sub-routes node_list = [i for i in range(n) if i != self.depot] clique_set = [] for i in range(2, len(node_list) + 1): for comb in itertools.combinations(node_list, i): clique_set.append(list(comb)) for clique in clique_set: mdl.add_constraint( mdl.sum(x[(i, j)] for i in clique for j in clique if i != j) <= len(clique) - 1) op = QuadraticProgram() op.from_docplex(mdl) return op
def to_quadratic_program(self) -> QuadraticProgram: """Convert a vertex cover instance into a :class:`~qiskit_optimization.problems.QuadraticProgram` Returns: The :class:`~qiskit_optimization.problems.QuadraticProgram` created from the vertex cover instance. """ mdl = Model(name='Vertex cover') n = self._graph.number_of_nodes() x = {i: mdl.binary_var(name='x_{0}'.format(i)) for i in range(n)} objective = mdl.sum(x[i] for i in x) for w, v in self._graph.edges: mdl.add_constraint(x[w] + x[v] >= 1) mdl.minimize(objective) op = QuadraticProgram() op.from_docplex(mdl) return op
def to_quadratic_program(self) -> QuadraticProgram: """Convert a knapsack problem instance into a :class:`~qiskit_optimization.problems.QuadraticProgram` Returns: The :class:`~qiskit_optimization.problems.QuadraticProgram` created from the knapsack problem instance. """ mdl = Model(name="Knapsack") x = { i: mdl.binary_var(name="x_{0}".format(i)) for i in range(len(self._values)) } mdl.maximize(mdl.sum(self._values[i] * x[i] for i in x)) mdl.add_constraint( mdl.sum(self._weights[i] * x[i] for i in x) <= self._max_weight) op = QuadraticProgram() op.from_docplex(mdl) return op
def to_quadratic_program(self) -> QuadraticProgram: """Convert a number partitioning problem instance into a :class:`~qiskit_optimization.problems.QuadraticProgram` Returns: The :class:`~qiskit_optimization.problems.QuadraticProgram` created from the number partitioning problem instance. """ mdl = Model(name='Number partitioning') x = { i: mdl.binary_var(name='x_{0}'.format(i)) for i in range(len(self._number_set)) } mdl.add_constraint( mdl.sum(num * (-2 * x[i] + 1) for i, num in enumerate(self._number_set)) == 0) op = QuadraticProgram() op.from_docplex(mdl) return op
def to_quadratic_program(self) -> QuadraticProgram: """Convert a Max-cut problem instance into a :class:`~qiskit_optimization.problems.QuadraticProgram` Returns: The :class:`~qiskit_optimization.problems.QuadraticProgram` created from the Max-cut problem instance. """ mdl = Model(name='Max-cut') x = {i: mdl.binary_var(name='x_{0}'.format(i)) for i in range(self._graph.number_of_nodes())} for w, v in self._graph.edges: self._graph.edges[w, v].setdefault('weight', 1) objective = mdl.sum(self._graph.edges[i, j]['weight'] * x[i] * (1 - x[j]) + self._graph.edges[i, j]['weight'] * x[j] * (1 - x[i]) for i, j in self._graph.edges) mdl.maximize(objective) op = QuadraticProgram() op.from_docplex(mdl) return op
def to_quadratic_program(self) -> QuadraticProgram: """Convert a graph partition instance into a :class:`~qiskit_optimization.problems.QuadraticProgram` Returns: The :class:`~qiskit_optimization.problems.QuadraticProgram` created from the graph partition instance. """ mdl = Model(name='Graph partition') n = self._graph.number_of_nodes() x = {i: mdl.binary_var(name='x_{0}'.format(i)) for i in range(n)} for w, v in self._graph.edges: self._graph.edges[w, v].setdefault('weight', 1) objective = mdl.sum(self._graph.edges[i, j]['weight'] * (x[i] + x[j] - 2*x[i]*x[j]) for i, j in self._graph.edges) mdl.minimize(objective) mdl.add_constraint(mdl.sum([x[i] for i in x]) == n//2) op = QuadraticProgram() op.from_docplex(mdl) return op
def to_quadratic_program(self) -> QuadraticProgram: """Convert a stable set instance into a :class:`~qiskit_optimization.problems.QuadraticProgram` Returns: The :class:`~qiskit_optimization.problems.QuadraticProgram` created from the stable set instance. """ mdl = Model(name='Stable set') n = self._graph.number_of_nodes() x = {i: mdl.binary_var(name='x_{0}'.format(i)) for i in range(n)} for w, v in self._graph.edges: self._graph.edges[w, v].setdefault('weight', 1) objective = mdl.sum(x[i] for i in x) for w, v in self._graph.edges: mdl.add_constraint(x[w] + x[v] <= 1) mdl.maximize(objective) op = QuadraticProgram() op.from_docplex(mdl) return op
def to_quadratic_program(self) -> QuadraticProgram: """Convert a set packing instance into a :class:`~qiskit_optimization.problems.QuadraticProgram` Returns: The :class:`~qiskit_optimization.problems.QuadraticProgram` created from the set packing instance. """ mdl = Model(name='Set packing') x = { i: mdl.binary_var(name='x_{0}'.format(i)) for i in range(len(self._subsets)) } mdl.maximize(mdl.sum(x[i] for i in x)) for element in self._set: mdl.add_constraint( mdl.sum(x[i] for i, sub in enumerate(self._subsets) if element in sub) <= 1) op = QuadraticProgram() op.from_docplex(mdl) return op
def to_quadratic_program(self) -> QuadraticProgram: """Convert a clique problem instance into a :class:`~qiskit_optimization.problems.QuadraticProgram`. When "size" is None, this makes an optimization model for a maximal clique instead of the specified size of a clique. Returns: The :class:`~qiskit_optimization.problems.QuadraticProgram` created from the clique problem instance. """ complement_g = nx.complement(self._graph) mdl = Model(name='Clique') n = self._graph.number_of_nodes() x = {i: mdl.binary_var(name='x_{0}'.format(i)) for i in range(n)} for w, v in complement_g.edges: mdl.add_constraint(x[w] + x[v] <= 1) if self.size is None: mdl.maximize(mdl.sum(x[i] for i in x)) else: mdl.add_constraint(mdl.sum(x[i] for i in x) == self.size) op = QuadraticProgram() op.from_docplex(mdl) return op