The Max K-Vertex Cover problem [1] is a classical problem in graph theory and computer science, where we aim to find a set of vertices such that each edge of the graph is incident to at least one vertex in the set, and the size of the set does not exceed a given number k.
The Max-k Vertex Cover problem can be formulated as an Integer Linear Program (ILP):Minimize:
∑(i,j)∈E(1−xi)(1−xj)Subject to:
∑i∈Vxi=kand
xi∈{0,1}∀i∈VWhere:
xi is a binary variable that equals 1 if node i is in the cover and 0 otherwise
E is the set of edges in the graph
V is the set of vertices in the graph
k is the maximum number of vertices allowed in the cover
We go through the steps of solving the problem with the Classiq platform, using QAOA algorithm [2].The solution is based on defining a Pyomo model for the optimization problem we would like to solve.
import networkx as nximport numpy as npimport pyomo.core as pyofrom IPython.display import Markdown, displayfrom matplotlib import pyplot as plt
In order to solve the Pyomo model defined above, we use the CombinatorialProblem python class.Under the hood it tranlates the Pyomo model to a quantum model of the QAOA algorithm, with cost hamiltonian translated from the Pyomo model. We can choose the number of layers for the QAOA ansatz using the argument num_layers, and the penalty_factor, which will be the coefficient of the constraints term in the cost hamiltonian.
We also set the quantum backend we want to execute on:
from classiq.execution import *execution_preferences = ExecutionPreferences( backend_preferences=ClassiqBackendPreferences(backend_name="simulator"),)
We now solve the problem by calling the optimize method of the CombinatorialProblem object.For the classical optimization part of the QAOA algorithm we define the maximum number of classical iterations (maxiter) and the α-parameter (quantile) for running CVaR-QAOA, an improved variation of the QAOA algorithm [3]:
def draw_solution(graph: nx.Graph, solution: list): solution_nodes = [v for v in graph.nodes if solution[v]] solution_edges = [ (u, v) for u, v in graph.edges if u in solution_nodes or v in solution_nodes ] nx.draw_kamada_kawai(graph, with_labels=True) nx.draw_kamada_kawai( graph, nodelist=solution_nodes, edgelist=solution_edges, node_color="r", edge_color="y", )draw_solution(graph, best_solution)
Lastly, we can compare to the classical solution of the problem:
from pyomo.opt import SolverFactorysolver = SolverFactory("couenne")solver.solve(mvc_model)classical_solution = [int(pyo.value(mvc_model.x[i])) for i in graph.nodes]