Skip to content

Commit

Permalink
Merge pull request #93 from ProvideQ/fix/qiskit-knapsack-indexerror
Browse files Browse the repository at this point in the history
fixed misinterpreted result and adjusted solution readout
  • Loading branch information
koalamitice authored Oct 9, 2024
2 parents 92501d5 + 45b71a1 commit ba10274
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions solvers/qiskit/knapsack/knapsack_qiskit.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@
z = knapsack.interpret(qaoa_result)

# format results for output
achieved_sum : int = 0
included_indexes : list[int] = []

for i in z:
achieved_sum += values[i]
included_indexes.append(indexes[i])
# prevent misinterpreted indices from appearing in result
if i < number_items and indexes[i] not in included_indexes:
included_indexes.append(indexes[i])

# grab optimal result from calculated result, negate because it got turned negative for the minimizing algorithm
achieved_sum : int = -int(qaoa_result.fval)

with open(output_path, 'w') as f:
f.write(str(achieved_sum) + "\n")
Expand Down

0 comments on commit ba10274

Please sign in to comment.