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
Changes from 1 commit
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
Prev Previous commit
Moved comments in code to markdown. Added comments to figures.
olaiBM committed Sep 11, 2023
commit 1b0d2f33a3caa623ed52387f39f6729c63d11017
8 changes: 4 additions & 4 deletions ExactCover/CVaR_ExactCover.ipynb
Original file line number Diff line number Diff line change
@@ -75,7 +75,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sampling cost landscape for all methods"
"# Sampling cost landscape for all methods\n",
"Below the cost landscape of both the standard QAOA method and the QAOA CVaR method is shown. They are seen to be different as is expected since the cost is calculated differently for the two methods."
]
},
{
@@ -109,8 +110,6 @@
}
],
"source": [
"# Evaluating the QAOA circuit with uniformly sampled angles for gamma and beta, just to see how sampling measurements from \n",
"# the circuits roughly looks like.\n",
"qaoaec.sample_cost_landscape(angles={\"gamma\": [0,np.pi/2,10], \"beta\": [0,np.pi/2,10]})\n",
"qaoaec_CVaR.sample_cost_landscape(angles={\"gamma\": [0,np.pi/2,10], \"beta\": [0,np.pi/2,10]})"
]
@@ -238,7 +237,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Compute and plot approximation ratio"
"# Compute and plot approximation ratio\n",
"Below the approximation ratio is plotted for the two methods. The QAOA method with CVaR is seen to perform better."
]
},
{
21 changes: 11 additions & 10 deletions ExactCover/ComparisonOptimizer_ExactCover.ipynb
Original file line number Diff line number Diff line change
@@ -177,7 +177,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plot cost landscape for one optimizer method"
"# Plotting the cost landscape\n",
"The cost landscape is of course the same for all the optimizer methods and is plotted below."
]
},
{
@@ -311,7 +312,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Compute and plot approximation ratio"
"# Plotting the approximation ratio\n",
"Below the approximation ratios are plotted for the different optimizers."
]
},
{
@@ -339,8 +341,7 @@
"pl.xlim(1-0.05,maxdepth+0.05)\n",
"_=pl.ylabel('appr. ratio')\n",
"_=pl.xlabel('depth')\n",
"_=pl.legend(loc=\"lower right\", framealpha=1)\n",
"#ax.xaxis.set_major_locator(MaxNLocator(integer=True))"
"_=pl.legend(loc=\"lower right\", framealpha=1)"
]
},
{
@@ -396,7 +397,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotting the values for the parameters found by the different optimizers"
"# Plotting the values for the parameters found by the different optimizers\n",
"Below we plot the values for $\\gamma$ and $\\beta$ parameters found by the different optimizers."
]
},
{
@@ -423,15 +425,15 @@
"pl.xlim(1-.25,maxdepth+.25)\n",
"_=pl.ylabel('parameter')\n",
"_=pl.xlabel('depth')\n",
"_=pl.legend()\n",
"#ax.xaxis.set_major_loc"
"_=pl.legend()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotting the number of function evaluations used by each optimizer "
"# Plotting the number of function evaluations used by each optimizer \n",
"Below the number of function evaluations used by the different optimizers are plotted for increasing depths. "
]
},
{
@@ -457,8 +459,7 @@
"pl.xlim(1-.05,maxdepth+.05)\n",
"_=pl.ylabel('number of function evaluations')\n",
"_=pl.xlabel('depth')\n",
"_=pl.legend()\n",
"#ax.xaxis.set_major_locator(MaxNLocator(integer=True))"
"_=pl.legend()"
]
},
{
51 changes: 29 additions & 22 deletions ExactCover/ToyExample_ExactCover.ipynb

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions MaxCut/CVaR.ipynb
Original file line number Diff line number Diff line change
@@ -33,7 +33,8 @@
"id": "3d17ca29",
"metadata": {},
"source": [
"# Creating problem instance"
"# Creating problem instance\n",
"Data for graph generation is found at https://github.com/OpenQuantumComputing/data"
]
},
{
@@ -56,8 +57,6 @@
"source": [
"numV=21 #Number of vertices\n",
"G = nx.read_gml(\"/Users/olaib/QuantumComputing/Data/data/sample_graphs/w_ba_n\"+str(numV)+\"_k4_0.gml\")\n",
"#Data for graph generation is found at https://github.com/OpenQuantumComputing/data\n",
"\n",
"pos = nx.spring_layout(G)\n",
"nx.draw_networkx(G,pos=pos)"
]
@@ -108,7 +107,8 @@
"id": "9cf2d87b",
"metadata": {},
"source": [
"# Sampling cost landscape for all methods"
"# Sampling cost landscape for all methods\n",
"Evaluating the QAOA circuit with uniformly sampled angles for gamma and beta, just to see how sampling measurements from the circuits roughly looks like."
]
},
{
@@ -137,18 +137,18 @@
}
],
"source": [
"# Evaluating the QAOA circuit with uniformly sampled angles for gamma and beta, just to see how sampling measurements from \n",
"# the circuits roughly looks like.\n",
"qaoamc.sample_cost_landscape(angles={\"gamma\": [0,np.pi/2,10], \"beta\": [0,np.pi/2,10]})\n",
"qaoamc_CVaR.sample_cost_landscape(angles={\"gamma\": [0,np.pi/2,10], \"beta\": [0,np.pi/2,10]})\n"
"qaoamc_CVaR.sample_cost_landscape(angles={\"gamma\": [0,np.pi/2,10], \"beta\": [0,np.pi/2,10]})"
]
},
{
"cell_type": "markdown",
"id": "4d07da3c",
"metadata": {},
"source": [
"# Plotting cost landscape, for both QAOA instances"
"# Plotting cost landscape, for both QAOA instances\n",
"\n",
"Below the cost landscape of both the standard QAOA method and the CVaR method is shown. They are seen to be relatively similar, but with some differences as is expected since the cost value is calculated differently for the two methods."
]
},
{
@@ -193,7 +193,7 @@
"_=pl.xlabel(r'$\\gamma$')\n",
"_=pl.ylabel(r'$\\beta$')\n",
"ax = pl.gca()\n",
"_=pl.title('Expectation value Cvar')\n",
"_=pl.title('Expectation value CVaR')\n",
"im = ax.imshow(qaoamc_CVaR.E,interpolation='bicubic',origin='lower',extent=[0,np.pi/2,0,np.pi/2])\n",
"divider = make_axes_locatable(ax)\n",
"cax = divider.append_axes(\"right\", size=\"5%\", pad=0.05)\n",
@@ -243,7 +243,9 @@
"id": "1bcbed36",
"metadata": {},
"source": [
"# Plotting the approximation ratio"
"# Plotting the approximation ratio\n",
"\n",
"Below the approximation ratio is plotted for both methods. The CVaR method clearly outperforms the standard QAOA method."
]
},
{
18 changes: 12 additions & 6 deletions MaxCut/ComparisonOptimizers.ipynb
Original file line number Diff line number Diff line change
@@ -36,7 +36,8 @@
"id": "0e9efef7",
"metadata": {},
"source": [
"# Creating problem instance"
"# Creating problem instance\n",
"Data for graph generation is found at https://github.com/OpenQuantumComputing/data"
]
},
{
@@ -59,7 +60,6 @@
"source": [
"numV=10 #Number of vertices\n",
"G = nx.read_gml(\"/Users/olaib/QuantumComputing/Data//data/sample_graphs/w_ba_n\"+str(numV)+\"_k4_0.gml\")\n",
"#Data for graph generation is found at https://github.com/OpenQuantumComputing/data\n",
"\n",
"pos = nx.spring_layout(G)\n",
"nx.draw_networkx(G,pos=pos)\n",
@@ -196,7 +196,8 @@
"id": "1d22743b",
"metadata": {},
"source": [
"# Plot cost landscape for one optimizer method"
"# Plotting the cost landscape\n",
"The cost landscape is of course the same for all the optimizer methods and is plotted below."
]
},
{
@@ -303,7 +304,8 @@
"id": "31d6f857",
"metadata": {},
"source": [
"# Plotting the approximation ratios"
"# Plotting the approximation ratios\n",
"Below the approximation ratios are plotted for the different optimizers. From the figure we see that the different optimizers give roughly the same approximation ratio."
]
},
{
@@ -341,7 +343,10 @@
"id": "c68bf8dc",
"metadata": {},
"source": [
"# Plotting the values for the parameters"
"# Plotting the values for the parameters\n",
"Below we plot the values for $\\gamma$ and $\\beta$ parameters found by the different optimizers. The values found are seen to be relatively similar for all the optimizers (except for an outlier at depth = 10 for the cobyla method). Importantly, the value for $\\gamma$ is seen to be increasing with depth and the value of $\\beta$ is seen to be decreasing with depth, in accordance with the heuristic strategy used to find the values for the parameters (see Quantum Approximate Optimization Algorithm: Performance, Mechanism, and Implementation on Near-Term Devices by Zhou et al.)\n",
"\n",
"(MISLEADING?? really: new parameters)"
]
},
{
@@ -378,7 +383,8 @@
"id": "881d8034",
"metadata": {},
"source": [
"# Plotting the number of function evaluations used by each optimizer"
"# Plotting the number of function evaluations used by each optimizer\n",
"Below the number of function evaluations used by the different optimizers are plotted for increasing depths. "
]
},
{
45 changes: 26 additions & 19 deletions MaxCut/ToyExampleQOA.ipynb

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions PortfolioOptimization/CVaR_PortOpt.ipynb
Original file line number Diff line number Diff line change
@@ -74,7 +74,13 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Creating QAOA instances for all methods"
"# Creating QAOA instances for all methods\n",
"Below we create a standard QAOA instance and a QAOA instance using CVaR for both the penalty method and for the XY-mixer method.\n",
"The gamma_scale parameter is used to adjust the search range for the gamma parameter. \n",
"\n",
"For information about the scale parameter see Sebastian Brandhofer et al. “Benchmarking the performance of portfolio optimization with QAOA”. In: Quantum Information Processing 22.1 (2023), page 25-26)\n",
"or Zichang He et al. Alignment between Initial State and Mixer Improves QAOA Performance for \n",
"Constrained Portfolio Optimization page 7 (2023)"
]
},
{
@@ -85,11 +91,6 @@
"source": [
"gamma_scale = 5000 #Adjusts the search range for the gamma parameter \n",
"\n",
"#See Sebastian Brandhofer et al. “Benchmarking the performance of portfolio optimization with QAOA”. In:\n",
"#Quantum Information Processing 22.1 (2023), page 25-26)\n",
"#or Zichang He et al. #Alignment between Initial State and Mixer Improves QAOA Performance for \n",
"#Constrained Portfolio Optimization page 7 (2023)\n",
"\n",
"alpha = 0.1 # value for CVaR (Conditional Value at Risk)\n",
"params_penalty = {\n",
" \"risk\" : 0.5*gamma_scale,\n",
@@ -121,7 +122,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sampling cost landscape for all methods"
"# Sampling cost landscape for all methods\n",
"Below the cost landscape is samples for all the methods. Note that the beta range is different for the penalty method and the XY-mixer method, due to different periods.\n"
]
},
{
@@ -167,10 +169,7 @@
"qaoaport_opt_penalty_CVaR.sample_cost_landscape(angles={\"gamma\": [0,np.pi/2,10], \"beta\": [0,np.pi/2,10]})\n",
"\n",
"qaoaport_opt_XYmixer.sample_cost_landscape(angles={\"gamma\": [0,np.pi/2,10], \"beta\": [0,2*np.pi,10]})\n",
"qaoaport_opt_XYmixer_CVaR.sample_cost_landscape(angles={\"gamma\": [0,np.pi/2,10], \"beta\": [0,2*np.pi,10]})\n",
"\n",
"#Note that the beta range is different for for the penalty method and the XY-mixer method, due to \n",
"#different periods in the mixers."
"qaoaport_opt_XYmixer_CVaR.sample_cost_landscape(angles={\"gamma\": [0,np.pi/2,10], \"beta\": [0,2*np.pi,10]})"
]
},
{
@@ -299,7 +298,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plot approximation ratio"
"# Plot approximation ratio\n",
"Below the approximation ratio is plotted for all the methods."
]
},
{
Loading