-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproblems.py
More file actions
58 lines (43 loc) · 1.79 KB
/
Copy pathproblems.py
File metadata and controls
58 lines (43 loc) · 1.79 KB
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
import numpy as np
import networkx as nx
import collections
from qiskit_optimization.applications import Maxcut
class MaxCut():
def __init__(self, size, weighted=False, regularity=None, seed=0, edges=None):
if regularity:
self.graph = nx.random_regular_graph(regularity, size, seed)
self.edges = len(self.graph.edges)
else:
self.edges = edges
self.graph = nx.gnm_random_graph(size, edges, seed=seed)
self.w = nx.to_numpy_matrix(self.graph, nodelist=sorted(self.graph.nodes()))
if weighted:
for i in range(size):
for j in range(size):
if i<j and self.w[i,j] != 0:
self.w[i,j] = round(np.random.uniform(0,1), 2)
self.w[j,i] = self.w[i,j]
def get_qubit_operator(self):
maxcut = Maxcut(self.w)
qp = maxcut.to_quadratic_program()
qubitOp, offset = qp.to_ising()
return qubitOp, offset
def optimal_cost_brute_force(self):
optimal_cost = 0
number_of_qubits = len(self.w)
best_string = 0
costs = collections.defaultdict(list)
for b in range(2**number_of_qubits):
x = [int(t) for t in reversed(list(bin(b)[2:].zfill(number_of_qubits)))]
cost = 0
for i in range(number_of_qubits):
for j in range(number_of_qubits):
cost += self.w[i,j] * x[i] * (1-x[j])
cost = np.round(cost,5)
x.reverse()
costs[cost].append(x)
if optimal_cost < cost:
optimal_cost = cost
costs = sorted(costs.items())
optimal_strings = costs[-1][1]
return optimal_cost, optimal_strings