# Max Independent Set

## Introduction

In the Maximum Independent Set Problem [1], we need to find the largest subset of vertices in a given graph, such that no two vertices in the subset are adjacent. This is an NP-Hard problem in general graph structures, with applications in various fields such as network deign, bioinformatics, and scheduling.

## Mathematical Formulation

Given a graph $$G=(V,E)$$, an independent set $$I \subseteq V$$ is a set of vertices such that no two vertices in $$I$$ are adjacent. The Maximum Independent Set Problem is the problem of finding the independent set $$I$$ with maximum cardinality. In binary form, we can represent each vertex $$v$$ being in or out of the independent set $$I$$ by a binary variable $$x_v$$, with $$x_v = 1$$ if $$v \in I$$, and $$x_v = 0$$ otherwise. The problem can then be formulated as:

Maximize $$\sum_{v \in V} x_v$$

Subject to:

$$x_{u} + x_{v} \leq 1, \forall (u, v) \in E$$

where each $$x_v \in {0,1}$$.

# Solving with the Classiq platform

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.

from typing import cast

import networkx as nx
import numpy as np
import pyomo.core as pyo
from IPython.display import Markdown, display
from matplotlib import pyplot as plt


## Building the Pyomo model from a graph input

We proceed by defining the pyomo model that will be used on the Classiq platform, using the mathematical formulation defined above:

import networkx as nx
import pyomo.core as pyo

def mis(graph: nx.Graph) -> pyo.ConcreteModel:
model = pyo.ConcreteModel()
model.x = pyo.Var(graph.nodes, domain=pyo.Binary)

@model.Constraint(graph.edges)
def independent_rule(model, node1, node2):
return model.x[node1] + model.x[node2] <= 1

model.cost = pyo.Objective(expr=sum(model.x.values()), sense=pyo.maximize)

return model


The model consists of:

• Index set declarations (model.Nodes, model.Arcs).

• Binary variable declaration for each node (model.x) indicating whether that node is chosen to be included in the set.

• Constraint rule - for each edge we require at least one of the corresponding node variables to be 0.

• Objective rule – the sum of the variables equals to the set size.

import networkx as nx

num_nodes = 8
p_edge = 0.4
graph = nx.fast_gnp_random_graph(n=num_nodes, p=p_edge, seed=12345)

mis_model = mis(graph)


mis_model.pprint()

2 Set Declarations
independent_rule_index : Size=1, Index=None, Ordered=False
Key  : Dimen : Domain : Size : Members
None :     2 :    Any :   14 : {(0, 2), (0, 4), (0, 6), (0, 7), (1, 2), (1, 4), (1, 5), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (5, 6)}
x_index : Size=1, Index=None, Ordered=False
Key  : Dimen : Domain : Size : Members
None :     1 :    Any :    8 : {0, 1, 2, 3, 4, 5, 6, 7}

1 Var Declarations
x : Size=8, Index=x_index
Key : Lower : Value : Upper : Fixed : Stale : Domain
0 :     0 :  None :     1 : False :  True : Binary
1 :     0 :  None :     1 : False :  True : Binary
2 :     0 :  None :     1 : False :  True : Binary
3 :     0 :  None :     1 : False :  True : Binary
4 :     0 :  None :     1 : False :  True : Binary
5 :     0 :  None :     1 : False :  True : Binary
6 :     0 :  None :     1 : False :  True : Binary
7 :     0 :  None :     1 : False :  True : Binary

1 Objective Declarations
cost : Size=1, Index=None, Active=True
Key  : Active : Sense    : Expression
None :   True : maximize : x[0] + x[1] + x[2] + x[3] + x[4] + x[5] + x[6] + x[7]

1 Constraint Declarations
independent_rule : Size=14, Index=independent_rule_index, Active=True
Key    : Lower : Body        : Upper : Active
(0, 2) :  -Inf : x[0] + x[2] :   1.0 :   True
(0, 4) :  -Inf : x[0] + x[4] :   1.0 :   True
(0, 6) :  -Inf : x[0] + x[6] :   1.0 :   True
(0, 7) :  -Inf : x[0] + x[7] :   1.0 :   True
(1, 2) :  -Inf : x[1] + x[2] :   1.0 :   True
(1, 4) :  -Inf : x[1] + x[4] :   1.0 :   True
(1, 5) :  -Inf : x[1] + x[5] :   1.0 :   True
(2, 4) :  -Inf : x[2] + x[4] :   1.0 :   True
(2, 5) :  -Inf : x[2] + x[5] :   1.0 :   True
(2, 6) :  -Inf : x[2] + x[6] :   1.0 :   True
(3, 4) :  -Inf : x[3] + x[4] :   1.0 :   True
(3, 5) :  -Inf : x[3] + x[5] :   1.0 :   True
(3, 6) :  -Inf : x[3] + x[6] :   1.0 :   True
(5, 6) :  -Inf : x[5] + x[6] :   1.0 :   True

5 Declarations: x_index x independent_rule_index independent_rule cost


## Setting Up the Classiq Problem Instance

In order to solve the Pyomo model defined above, we use the Classiq combinatorial optimization engine. For the quantum part of the QAOA algorithm (QAOAConfig) - define the number of repetitions (num_layers):

from classiq import construct_combinatorial_optimization_model
from classiq.applications.combinatorial_optimization import OptimizerConfig, QAOAConfig

qaoa_config = QAOAConfig(num_layers=3)


For the classical optimization part of the QAOA algorithm we define the maximum number of classical iterations (max_iteration) and the $$\alpha$$-parameter (alpha_cvar) for running CVaR-QAOA, an improved variation of the QAOA algorithm [3]:

optimizer_config = OptimizerConfig(max_iteration=60, alpha_cvar=0.7)


Lastly, we load the model, based on the problem and algorithm parameters, which we can use to solve the problem:

qmod = construct_combinatorial_optimization_model(
pyo_model=mis_model,
qaoa_config=qaoa_config,
optimizer_config=optimizer_config,
)


We also set the quantum backend we want to execute on:

from classiq import set_execution_preferences
from classiq.execution import ClassiqBackendPreferences, ExecutionPreferences

backend_preferences = ExecutionPreferences(
backend_preferences=ClassiqBackendPreferences(backend_name="simulator")
)

qmod = set_execution_preferences(qmod, backend_preferences)

from classiq import write_qmod

write_qmod(qmod, "max_independent_set")


## Synthesizing the QAOA Circuit and Solving the Problem

We can now synthesize and view the QAOA circuit (ansatz) used to solve the optimization problem:

from classiq import show, synthesize

qprog = synthesize(qmod)
show(qprog)

Opening: https://platform.classiq.io/circuit/dc38b391-6ea3-46bf-9d12-15a1f70c0579?version=0.45.0.dev0%2Bcf1b9b7ccc


We now solve the problem by calling the execute function on the quantum program we have generated:

from classiq import execute

res = execute(qprog).result()


We can check the convergence of the run:

from classiq.execution import VQESolverResult

vqe_result = res[0].value
vqe_result.convergence_graph


# Optimization Results

We can also examine the statistics of the algorithm:

import pandas as pd

from classiq.applications.combinatorial_optimization import (
get_optimization_solution_from_pyo,
)

solution = get_optimization_solution_from_pyo(
mis_model, vqe_result=vqe_result, penalty_energy=qaoa_config.penalty_energy
)
optimization_result = pd.DataFrame.from_records(solution)

probability cost solution count
3 0.032 3.0 [0, 0, 1, 1, 0, 0, 0, 1] 32
57 0.005 3.0 [1, 1, 0, 1, 0, 0, 0, 0] 5
30 0.010 3.0 [0, 0, 0, 0, 1, 0, 1, 1] 10
20 0.011 3.0 [0, 1, 0, 0, 0, 0, 1, 1] 11
18 0.012 3.0 [0, 0, 0, 0, 1, 1, 0, 1] 12

And the histogram:

optimization_result.hist("cost", weights=optimization_result["probability"])

array([[<Axes: title={'center': 'cost'}>]], dtype=object)


Let us plot the solution:

best_solution = optimization_result.solution[optimization_result.cost.idxmax()]

independent_set = [node for node in graph.nodes if best_solution[node] == 1]
print("Independent Set: ", independent_set)
print("Size of Independent Set: ", len(independent_set))

Independent Set:  [2, 3, 7]
Size of Independent Set:  3

nx.draw_kamada_kawai(graph, with_labels=True)
graph,
with_labels=True,
nodelist=independent_set,
node_color="r",
)


## Comparison to a classical solver

Lastly, we can compare to the classical solution of the problem:

from pyomo.opt import SolverFactory

solver = SolverFactory("couenne")
solver.solve(mis_model)
classical_solution = [pyo.value(mis_model.x[i]) for i in graph.nodes]

independent_set_classical = [
node for node in graph.nodes if np.allclose(classical_solution[node], 1)
]
print("Classical Independent Set: ", independent_set_classical)
print("Size of Classical Independent Set: ", len(independent_set_classical))

Classical Independent Set:  [0, 1, 3]
Size of Classical Independent Set:  3

nx.draw_kamada_kawai(graph, with_labels=True)