-
Notifications
You must be signed in to change notification settings - Fork 22
/
graph_partitioning.py
71 lines (57 loc) · 2.28 KB
/
graph_partitioning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright 2019 D-Wave Systems, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------ Import necessary packages ----
import networkx as nx
from collections import defaultdict
from itertools import combinations
from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite
import math
# ------- Set tunable parameters -------
num_reads = 1000
gamma = 80
# ------- Set up our graph -------
G = nx.gnp_random_graph(40, 0.2)
print("Graph on {} nodes created with {} out of {} possible edges.".format(len(G.nodes), len(G.edges), len(G.nodes) * (len(G.nodes)-1) / 2))
# ------- Set up our QUBO dictionary -------
# Initialize our Q matrix
Q = defaultdict(int)
# Fill in Q matrix
for u, v in G.edges:
Q[(u,u)] += 1
Q[(v,v)] += 1
Q[(u,v)] += -2
for i in G.nodes:
Q[(i,i)] += gamma*(1-len(G.nodes))
for i, j in combinations(G.nodes, 2):
Q[(i,j)] += 2*gamma
# ------- Run our QUBO on the QPU -------
# Set chain strength
chain_strength = gamma*len(G.nodes)
# Run the QUBO on the solver from your config file
sampler = EmbeddingComposite(DWaveSampler())
response = sampler.sample_qubo(Q,
chain_strength=chain_strength,
num_reads=num_reads,
label='Example - Graph Partitioning')
# See if the best solution found is feasible, and if so print the number of cut edges.
sample = response.record.sample[0]
# In the case when n is odd, the set may have one more or one fewer nodes
if sum(sample) in [math.floor(len(G.nodes)/2), math.ceil(len(G.nodes)/2)]:
num_cut_edges = 0
for u, v in G.edges:
num_cut_edges += sample[u] + sample[v] - 2*sample[u]*sample[v]
print("Valid partition found with", num_cut_edges, "cut edges.")
else:
print("Invalid partition.")