Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Toy example, CVaR comparison and Optimizer comparison files to #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
342 changes: 342 additions & 0 deletions ExactCover/CVaR_ExactCover.ipynb

Large diffs are not rendered by default.

495 changes: 495 additions & 0 deletions ExactCover/ComparisonOptimizer_ExactCover.ipynb

Large diffs are not rendered by default.

523 changes: 0 additions & 523 deletions ExactCover/ExactCover 14 5.ipynb

This file was deleted.

735 changes: 0 additions & 735 deletions ExactCover/ExactCover 4 2.ipynb

This file was deleted.

736 changes: 0 additions & 736 deletions ExactCover/ExactCover 6 3.ipynb

This file was deleted.

278 changes: 278 additions & 0 deletions ExactCover/ToyExample_ExactCover.ipynb

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions ExactCover/utilities_exactCover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import math
import itertools

def computeOptimalSolution(minus_cost_func, isFeasible_func, FR):
cost = math.inf
sol = ""
nL = FR.shape[1]
for s in [''.join(i) for i in itertools.product('01', repeat =nL)]:
if isFeasible_func(s):
if -minus_cost_func(s)<cost:
cost = -minus_cost_func(s)
sol = s

return cost, sol


def computeAverageApproxRatio(hist, mincost, minus_cost_func):
#Include the cost of infeasible solutions in average approx ratio or give them approx ratio = 0?
tot_shots = 0
avg_approx_ratio = 0
for key in hist:
shots = hist[key]
tot_shots += shots
cost = -minus_cost_func(key[::-1]) #Qiskit uses big endian encoding, cost function uses litle endian encoding.
#Therefore the string is reversed before passing it to the cost function.
approx_ratio = mincost/cost #Only do this to feasible solutions?? And give them 0
avg_approx_ratio += approx_ratio*shots
avg_approx_ratio = avg_approx_ratio/tot_shots
return avg_approx_ratio






139 changes: 95 additions & 44 deletions MaxCut/CVaR.ipynb

Large diffs are not rendered by default.

235 changes: 143 additions & 92 deletions MaxCut/ComparisonOptimizers.ipynb

Large diffs are not rendered by default.

207 changes: 75 additions & 132 deletions MaxCut/ToyExampleQOA.ipynb

Large diffs are not rendered by default.

506 changes: 0 additions & 506 deletions PortfolioOptimization/4_asset_optimize.ipynb

This file was deleted.

580 changes: 0 additions & 580 deletions PortfolioOptimization/6_asset_optimize.ipynb

This file was deleted.

813 changes: 0 additions & 813 deletions PortfolioOptimization/8_asset_optimize.ipynb

This file was deleted.

391 changes: 391 additions & 0 deletions PortfolioOptimization/CVaR_PortOpt.ipynb

Large diffs are not rendered by default.

816 changes: 816 additions & 0 deletions PortfolioOptimization/ComparisonOptimizers_PortOpt.ipynb

Large diffs are not rendered by default.

445 changes: 445 additions & 0 deletions PortfolioOptimization/ToyExample_PortOpt.ipynb

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions PortfolioOptimization/utilities_portOpt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import math
import itertools


def approxRatio(cost, max_feasible, min_feasible):
#Approximation ratio for feasible solutions of the portfolio optimization problem. Unfeasible solutions
#have approximation ratio zero.
#https://link.springer.com/article/10.1007/s11128-022-03766-5
approx_ratio = (cost-max_feasible)/(min_feasible-max_feasible)
return approx_ratio




def computeMinMaxCosts(N_assets, minusCostFunction, isFeasible):
best_sol= None
min_cost = math.inf
costs_feasible = []
costs = []
for s in [''.join(i) for i in itertools.product('01', repeat = N_assets)]:

c_penalty = -minusCostFunction(s) #function returns -cost
costs.append(c_penalty)
if isFeasible(s):
costs_feasible.append(c_penalty)
if c_penalty < min_cost and isFeasible(s):

best_sol = s[::-1] #Qiskit uses big endian encoding, cost function uses litle endian encoding.
#Therefore the string is reversed before passing it to the cost function.
min_cost= c_penalty
else:
pass
return min_cost, max(costs_feasible), best_sol




def computeAverageApproxRatio(hist, max_feasible, min_feasible, minusCostFunction, isFeasible):
#Takes in histogram and computes the average approximation ratio
tot_shots = 0
avg_approx_ratio = 0

for key in hist:

shots = hist[key]
tot_shots = tot_shots + shots

if isFeasible(key):
cost = -minusCostFunction(key[::-1]) #Qiskit uses big endian encoding, cost function uses litle endian encoding.
#Therefore the string is reversed before passing it to the cost function.
approx_for_key = approxRatio(cost, max_feasible, min_feasible)*shots
avg_approx_ratio += approx_for_key

avg_approx_ratio = avg_approx_ratio/tot_shots
return avg_approx_ratio
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
# optimization
Algorithms for optimization tasks (operations research)
# Openquantumcomputing QAOA
A python library for for quantum optimization using QAOA on quantum computer simulators.

# Installation instructions
The latest version of openquantumcomputing is installed directly from PyPI by the following command:

'''
pip install openquantumcomputing
'''
...
# Example notebooks
Under the optimization folder a number of examples of use of the library can be found in different notebooks, for different optimization problems. All of the notebooks are roughly structured in the following way:
1. Importing necassary modules
2. Creating problem instance
3. Creating QAOA instance
4. Increasing depth for the QAOA instances
5. Computing and plotting the approximation ratio for the given problem