Use this file to discover all available pages before exploring further.
View on GitHub
Open this notebook in GitHub to run it yourself
The Minimum Dominating Set problem [1] is a classical NP-hard problem in computer science and graph theory. In this problem, we are given a graph, and we aim to find the smallest subset of vertices such that every node in the graph is either in the subset or is a neighbor of a node in the subset.We represent the problem as a binary optimization problem.
Every node i is either in the dominating set or connected to a node in the dominating set:∀i∈V:xi+∑j∈N(i)xj≥1Where N(i) represents the neighbors of node i.
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 matplotlib import pyplot as plt
In order to solve the Pyomo model defined above, we use the CombinatorialProblem quantum object.Under the hood it tranlastes the Pyomo model to a quantum model of the QAOA algorithm, with a cost function 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 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(G, [best_solution["x"][i] for i in range(len(best_solution["x"]))])
Lastly, we can compare to the classical solution of the problem:
from pyomo.opt import SolverFactorysolver = SolverFactory("couenne")solver.solve(mds_model)mds_model.display()classical_solution = [int(pyo.value(mds_model.x[i])) for i in G.nodes]