The set cover problem [1] represents a well-known problem in the fields of combinatorics, computer science, and complexity theory. It is an NP-complete problems.The problem presents us with a universal set, U, and a collection S of subsets of U.The goal is to find the smallest possible subfamily, C⊆S, whose union equals the universal set.Formally, let’s consider a universal set U=1,2,...,n and a collection S containing m subsets of U, S=S1,...,Sm with Si⊆U.The challenge of the set cover problem is to find a subset C of S of minimal size such that Si∈C⋃Si=U.
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.
We proceed by defining the pyomo model that will be used on the Classiq platform, using the mathematical formulation defined above:
import itertoolsfrom typing import Listimport pyomo.core as pyodef set_cover(sub_sets: List[List[int]]) -> pyo.ConcreteModel: entire_set = set(itertools.chain(*sub_sets)) n = max(entire_set) num_sets = len(sub_sets) assert entire_set == set( range(1, n + 1) ), f"the union of the subsets is {entire_set} not equal to range(1, {n + 1})" model = pyo.ConcreteModel() model.x = pyo.Var(range(num_sets), domain=pyo.Binary) @model.Constraint(entire_set) def independent_rule(model, num): return sum(model.x[idx] for idx in range(num_sets) if num in sub_sets[idx]) >= 1 model.cost = pyo.Objective(expr=sum(model.x.values()), sense=pyo.minimize) return model
The model contains:
Binary variable for each subset (model.x) indicating if it is included in the sub-collection.
Objective rule – the size of the sub-collection.
Constraint – the sub-collection covers the original set.
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]:
best_solution = optimization_result.solution[optimization_result.cost.idxmin()]best_solution = [best_solution["x"][i] for i in range(len(best_solution["x"]))]best_solution
Output:
[0, 1, 0, 1, 1, 0, 0, 0]
print( f"Quantum Solution: num_sets={int(sum(best_solution))}, sets={[sub_sets[i] for i in range(len(best_solution)) if best_solution[i]]}")
Lastly, we can compare to the classical solution of the problem:
from pyomo.opt import SolverFactorysolver = SolverFactory("couenne")solver.solve(set_cover_model)classical_solution = [ int(pyo.value(set_cover_model.x[i])) for i in range(len(set_cover_model.x))]print("Classical solution:", classical_solution)
Output:
Classical solution: [1, 1, 1, 1, 0, 0, 0, 0]
print( f"Classical Solution: num_sets={int(sum(classical_solution))}, sets={[sub_sets[i] for i in range(len(classical_solution)) if classical_solution[i]]}")