-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmaxCut_qiskit.py
More file actions
102 lines (79 loc) · 3.14 KB
/
Copy pathmaxCut_qiskit.py
File metadata and controls
102 lines (79 loc) · 3.14 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Hippity hoppity your code is now my property!
# https://qiskit.org/documentation/optimization/tutorials/06_examples_max_cut_and_tsp.html
# https://pypi.org/project/pygmlparser/
# useful additional packages
import numpy as np
import networkx as nx
import sys
import pygmlparser as pygmlparser
# gml parsing
from pygmlparser.Parser import Parser
from pygmlparser.Graph import Graph
# Qiskit
from qiskit.circuit.library import TwoLocal
from qiskit.primitives import StatevectorSampler
from qiskit_optimization.applications import Maxcut
from qiskit_optimization.minimum_eigensolvers import SamplingVQE
from qiskit_optimization.optimizers import SPSA
#if len(sys.argv) != 3:
# raise ValueError('This script expects exactly 2 arguments. Input file (argument 1) and output file (argument 2).')
input_path = sys.argv[1]
output_path = sys.argv[2]
# To include the weight information, we'll need to map the weight to the label
# so the parser can read it. Therefore, remove all existing "label" lines and
# replace all "weight" lines with "label" lines.
with open(input_path, 'r+') as file:
# Read all lines from the file
lines = file.readlines()
# Move the file cursor to the beginning
file.seek(0)
# Iterate through the lines, removing lines with "label" and replacing "weight" with "label"
for line in lines:
if "label" not in line:
modified_line = line.replace("weight", "label")
file.write(modified_line)
# Truncate the remaining content in case the new content is shorter than the old content
file.truncate()
# Instantiate a parser, load a file, and parse it!
parser: Parser = Parser()
parser.loadGML(input_path)
parser.parse()
# Retrieve the graph nodes
nodes: Graph.Nodes = parser.graph.graphNodes # a map of id -> Node objects
# Retrieve the graph edges
edges: Graph.Edges = parser.graph.graphEdges # list of Edge objects
# Generating a graph
n = len(nodes) # Number of nodes in graph
G = nx.Graph()
G.add_nodes_from(np.arange(0, n, 1))
elist = []
for e in edges:
elist.append((e.source_node.id, e.target_node.id, e.label))
# tuple is (i,j,weight) where (i,j) is the edge
G.add_weighted_edges_from(elist)
# Computing the weight matrix from the graph
w = np.zeros([n, n])
for i in range(n):
for j in range(n):
temp = G.get_edge_data(i, j, default=0)
if temp != 0:
weight = temp["weight"]
w[i, j] = 1 if weight is None else weight
max_cut = Maxcut(w)
qp = max_cut.to_quadratic_program()
qubitOp, offset = qp.to_ising()
# construct VQE
optimizer=SPSA(maxiter=300)
ry = TwoLocal(qubitOp.num_qubits, "ry", "cz", reps=5, entanglement="linear")
vqe = SamplingVQE(sampler=StatevectorSampler(), ansatz=ry, optimizer=optimizer)
# run VQE
result = vqe.compute_minimum_eigenvalue(qubitOp)
# save results
x = max_cut.sample_most_likely(result.eigenstate)
f = open(output_path, 'w')
f.write("energy:" + str(result.eigenvalue.real) + "\n")
f.write("time:" + str(result.optimizer_time) + "\n")
f.write("max-cut objective:" + str(result.eigenvalue.real + offset) + "\n")
f.write("solution:" + str(x) + "\n")
f.write("solution objective:" + str(qp.objective.evaluate(x)) + "\n")
f.close()