Skip to content

Commit

Permalink
Merge pull request #56 from ProvideQ/feature/max-cut-add-weight
Browse files Browse the repository at this point in the history
Add weight into qiskit max cut calculation
  • Loading branch information
schweikart authored Jan 9, 2024
2 parents f8782be + eca0882 commit 4ec0ab1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
21 changes: 20 additions & 1 deletion qiskit/max-cut/maxCut_qiskit.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@
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)
Expand All @@ -49,7 +68,7 @@
elist = []

for e in edges:
elist.append((e.source_node.id, e.target_node.id, 1)) # TODO: extract edge weight
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ graph [
edge [
source 1
target 2
weight 5
]
edge [
source 2
target 3
weight 1
]
edge [
source 3
target 1
weight 2
]
]

0 comments on commit 4ec0ab1

Please sign in to comment.