Skip to content

Commit

Permalink
Merge pull request #29 from MatP1337/main
Browse files Browse the repository at this point in the history
Fixed solve_QUBO function, which now returns the optimal_solution
  • Loading branch information
positr0nium authored Feb 13, 2024
2 parents 168f2e9 + 2cc1275 commit 7e721d8
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/qrisp/qaoa/problems/QUBO.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,7 @@ def create_QUBO_cost_operator(Q):
cost_operator function.
"""
#def QUBO_cost_operator(qv, gamma):

# for i in range(len(Q)):
# rz(-0.5*2*gamma*(0.5*Q[i][i]+0.5*sum(Q[i])), qv[i])
# for j in range(i+1, len(qv)):
# if Q[i][j] !=0:
# rzz(0.25*2*gamma*Q[i][j], qv[i], qv[j])
#return QUBO_cost_operator
#new try

def QUBO_cost_operator(qv, gamma):

gphase(-gamma/4*(np.sum(Q)+np.trace(Q)),qv[0])
Expand Down Expand Up @@ -119,7 +111,7 @@ def QUBO_problem(Q):
return QAOAProblem(create_QUBO_cost_operator(Q), RX_mixer, create_QUBO_cl_cost_function(Q))


def solve_QUBO(Q, depth, backend = None, n_solutions = 1, print_res = True):
def solve_QUBO(Q, depth, max_iter = 50, backend = None, n_solutions = 1, print_res = False):
"""
Solves a Quadratic Unconstrained Binary Optimization (QUBO) problem using the Quantum Approximate Optimization Algorithm (QAOA).
The function imports the default backend from the 'qrisp.default_backend' module.
Expand All @@ -134,15 +126,17 @@ def solve_QUBO(Q, depth, backend = None, n_solutions = 1, print_res = True):
QUBO matrix to solve.
depth : int
The depth (amount of layers) of the QAOA circuit.
max_iter : int
The maximal amount of iterations of the COBYLA optimizer in the QAOA algorithm.
backend : str
The backend to be used for the quantum/annealing simulation.
n_solutions : int
The number of solutions to consider for classical post processing. The defalut is 1.
Returns
-------
None
The function prints the runtime of the QAOA algorithm and the ``n_solutions`` best solutions with their respective costs.
optimal_solution: tuple
The function returns the optimal solution as a tuple where the first element is the cost and the second element is the optimal bitstring. If print_res is set to True, the function prints the runtime of the QAOA algorithm and the ``n_solutions`` best solutions with their respective costs.
"""

Expand All @@ -159,7 +153,7 @@ def solve_QUBO(Q, depth, backend = None, n_solutions = 1, print_res = True):
backend = backend

# Run QAOA with given quantum arguments, depth, measurement keyword arguments and maximum iterations for optimization
res = QUBO_instance.run(qarg, depth, mes_kwargs={"backend" : backend}, max_iter = 50) # runs the simulation
res = QUBO_instance.run(qarg, depth, mes_kwargs={"backend" : backend}, max_iter = max_iter) # runs the simulation
res = dict(list(res.items())[:n_solutions])

# Calculate the cost for each solution
Expand All @@ -168,7 +162,12 @@ def solve_QUBO(Q, depth, backend = None, n_solutions = 1, print_res = True):
# Sort the solutions by their cost in ascending order
sorted_costs_and_solutions = sorted(costs_and_solutions, key=itemgetter(0))

optimal_solution = sorted_costs_and_solutions[0]

if print_res is True:
# Get the top solutions and print them
for i in range(n_solutions):
print(f"Solution {i+1}: {sorted_costs_and_solutions[i][1]} with cost: {sorted_costs_and_solutions[i][0]}")
print(f"Solution {i+1}: {sorted_costs_and_solutions[i][1]} with cost: {sorted_costs_and_solutions[i][0]}")

return optimal_solution

0 comments on commit 7e721d8

Please sign in to comment.