|
1 | 1 | { |
2 | 2 | "cells": [ |
3 | 3 | { |
4 | | - "attachments": {}, |
5 | 4 | "cell_type": "markdown", |
6 | 5 | "metadata": {}, |
7 | 6 | "source": [ |
|
16 | 15 | "source": [ |
17 | 16 | "# _*Qiskit Finance: Portfolio Optimization*_ \n", |
18 | 17 | "\n", |
19 | | - "The latest version of this notebook is available on https://github.com/Qiskit/qiskit-iqx-tutorials.\n", |
| 18 | + "The latest version of this notebook is available on https://github.com/Qiskit/qiskit-tutorials.\n", |
20 | 19 | "\n", |
21 | 20 | "***\n", |
22 | 21 | "### Contributors\n", |
|
66 | 65 | "outputs": [], |
67 | 66 | "source": [ |
68 | 67 | "from qiskit import BasicAer\n", |
| 68 | + "from qiskit.circuit.library import TwoLocal\n", |
69 | 69 | "from qiskit.aqua import QuantumInstance\n", |
70 | | - "from qiskit.finance.ising import portfolio\n", |
71 | | - "from qiskit.optimization.ising.common import sample_most_likely\n", |
| 70 | + "from qiskit.finance.applications.ising import portfolio\n", |
| 71 | + "from qiskit.optimization.applications.ising.common import sample_most_likely\n", |
72 | 72 | "from qiskit.finance.data_providers import RandomDataProvider\n", |
73 | | - "from qiskit.aqua.algorithms import VQE, QAOA, ExactEigensolver\n", |
| 73 | + "from qiskit.aqua.algorithms import VQE, QAOA, NumPyMinimumEigensolver\n", |
74 | 74 | "from qiskit.aqua.components.optimizers import COBYLA\n", |
75 | | - "from qiskit.aqua.components.variational_forms import RY\n", |
76 | 75 | "import numpy as np\n", |
77 | 76 | "import datetime" |
78 | 77 | ] |
|
158 | 157 | " return x\n", |
159 | 158 | "\n", |
160 | 159 | "def print_result(result):\n", |
161 | | - " selection = sample_most_likely(result['eigvecs'][0])\n", |
| 160 | + " selection = sample_most_likely(result.eigenstate)\n", |
162 | 161 | " value = portfolio.portfolio_value(selection, mu, sigma, q, budget, penalty)\n", |
163 | 162 | " print('Optimal: selection {}, value {:.4f}'.format(selection, value))\n", |
164 | | - "\n", |
165 | | - " probabilities = np.abs(result['eigvecs'][0])**2\n", |
| 163 | + " \n", |
| 164 | + " eigenvector = result.eigenstate if isinstance(result.eigenstate, np.ndarray) else result.eigenstate.to_matrix()\n", |
| 165 | + " probabilities = np.abs(eigenvector)**2\n", |
166 | 166 | " i_sorted = reversed(np.argsort(probabilities))\n", |
167 | 167 | " print('\\n----------------- Full result ---------------------')\n", |
168 | 168 | " print('selection\\tvalue\\t\\tprobability')\n", |
|
178 | 178 | "cell_type": "markdown", |
179 | 179 | "metadata": {}, |
180 | 180 | "source": [ |
181 | | - "### ExactEigensolver (as a classical reference)\n", |
| 181 | + "### NumPyMinimumEigensolver (as a classical reference)\n", |
182 | 182 | "Lets solve the problem. First classically...\n", |
183 | 183 | "\n", |
184 | | - "We can now use the Operator we built above without regard to the specifics of how it was created. We set the algorithm for the ExactEigensolver so we can have a classical reference. The problem is set for 'ising'. Backend is not required since this is computed classically not using quantum computation. The result is returned as a dictionary." |
| 184 | + "We can now use the Operator we built above without regard to the specifics of how it was created. We set the algorithm for the NumPyMinimumEigensolver so we can have a classical reference. The problem is set for 'ising'. Backend is not required since this is computed classically not using quantum computation. The result is returned as a dictionary." |
185 | 185 | ] |
186 | 186 | }, |
187 | 187 | { |
|
193 | 193 | "name": "stdout", |
194 | 194 | "output_type": "stream", |
195 | 195 | "text": [ |
196 | | - "Optimal: selection [0. 0. 1. 1.], value -0.0196\n", |
| 196 | + "Optimal: selection [1 0 0 1], value 0.0041\n", |
197 | 197 | "\n", |
198 | 198 | "----------------- Full result ---------------------\n", |
199 | 199 | "selection\tvalue\t\tprobability\n", |
200 | 200 | "---------------------------------------------------\n", |
201 | | - " [0 0 1 1]\t-0.0196\t\t1.0000\n", |
202 | | - " [1 1 1 1]\t15.9850\t\t0.0000\n", |
203 | | - " [0 1 1 1]\t3.9823\t\t0.0000\n", |
204 | | - " [1 0 1 1]\t3.9831\t\t0.0000\n", |
205 | | - " [1 1 0 1]\t4.0007\t\t0.0000\n", |
206 | | - " [0 1 0 1]\t-0.0018\t\t0.0000\n", |
207 | | - " [1 0 0 1]\t-0.0014\t\t0.0000\n", |
208 | | - " [0 0 0 1]\t3.9961\t\t0.0000\n", |
209 | | - " [1 1 1 0]\t3.9887\t\t0.0000\n", |
210 | | - " [0 1 1 0]\t-0.0139\t\t0.0000\n", |
211 | | - " [1 0 1 0]\t-0.0131\t\t0.0000\n", |
212 | | - " [0 0 1 0]\t3.9843\t\t0.0000\n", |
213 | | - " [1 1 0 0]\t0.0044\t\t0.0000\n", |
214 | | - " [0 1 0 0]\t4.0021\t\t0.0000\n", |
215 | | - " [1 0 0 0]\t4.0023\t\t0.0000\n", |
| 201 | + " [1 0 0 1]\t0.0041\t\t1.0000\n", |
| 202 | + " [1 1 1 1]\t16.0109\t\t0.0000\n", |
| 203 | + " [0 1 1 1]\t4.0083\t\t0.0000\n", |
| 204 | + " [1 0 1 1]\t4.0079\t\t0.0000\n", |
| 205 | + " [0 0 1 1]\t0.0050\t\t0.0000\n", |
| 206 | + " [1 1 0 1]\t4.0071\t\t0.0000\n", |
| 207 | + " [0 1 0 1]\t0.0044\t\t0.0000\n", |
| 208 | + " [0 0 0 1]\t4.0012\t\t0.0000\n", |
| 209 | + " [1 1 1 0]\t4.0095\t\t0.0000\n", |
| 210 | + " [0 1 1 0]\t0.0069\t\t0.0000\n", |
| 211 | + " [1 0 1 0]\t0.0066\t\t0.0000\n", |
| 212 | + " [0 0 1 0]\t4.0038\t\t0.0000\n", |
| 213 | + " [1 1 0 0]\t0.0057\t\t0.0000\n", |
| 214 | + " [0 1 0 0]\t4.0031\t\t0.0000\n", |
| 215 | + " [1 0 0 0]\t4.0029\t\t0.0000\n", |
216 | 216 | " [0 0 0 0]\t16.0000\t\t0.0000\n" |
217 | 217 | ] |
218 | 218 | } |
219 | 219 | ], |
220 | 220 | "source": [ |
221 | | - "exact_eigensolver = ExactEigensolver(qubitOp, k=1)\n", |
| 221 | + "exact_eigensolver = NumPyMinimumEigensolver(qubitOp)\n", |
222 | 222 | "result = exact_eigensolver.run()\n", |
223 | 223 | "\n", |
224 | 224 | "print_result(result)" |
|
243 | 243 | "name": "stdout", |
244 | 244 | "output_type": "stream", |
245 | 245 | "text": [ |
246 | | - "Optimal: selection [1. 0. 1. 0.], value -0.0131\n", |
| 246 | + "Optimal: selection [0. 0. 1. 1.], value 0.0050\n", |
247 | 247 | "\n", |
248 | 248 | "----------------- Full result ---------------------\n", |
249 | 249 | "selection\tvalue\t\tprobability\n", |
250 | 250 | "---------------------------------------------------\n", |
251 | | - " [1 0 1 0]\t-0.0131\t\t0.6997\n", |
252 | | - " [0 1 0 1]\t-0.0018\t\t0.1837\n", |
253 | | - " [0 1 1 0]\t-0.0139\t\t0.0634\n", |
254 | | - " [1 1 0 0]\t0.0044\t\t0.0296\n", |
255 | | - " [1 0 0 1]\t-0.0014\t\t0.0218\n", |
256 | | - " [0 0 1 1]\t-0.0196\t\t0.0018\n", |
257 | | - " [1 1 0 1]\t4.0007\t\t0.0000\n", |
258 | | - " [0 1 1 1]\t3.9823\t\t0.0000\n", |
259 | | - " [0 0 1 0]\t3.9843\t\t0.0000\n", |
260 | | - " [0 1 0 0]\t4.0021\t\t0.0000\n", |
261 | | - " [0 0 0 0]\t16.0000\t\t0.0000\n", |
262 | | - " [1 0 0 0]\t4.0023\t\t0.0000\n", |
263 | | - " [0 0 0 1]\t3.9961\t\t0.0000\n", |
264 | | - " [1 1 1 0]\t3.9887\t\t0.0000\n", |
265 | | - " [1 1 1 1]\t15.9850\t\t0.0000\n", |
266 | | - " [1 0 1 1]\t3.9831\t\t0.0000\n" |
| 251 | + " [0 0 1 1]\t0.0050\t\t0.6656\n", |
| 252 | + " [0 1 0 1]\t0.0044\t\t0.3151\n", |
| 253 | + " [0 1 1 0]\t0.0069\t\t0.0153\n", |
| 254 | + " [1 0 0 1]\t0.0041\t\t0.0029\n", |
| 255 | + " [1 1 0 0]\t0.0057\t\t0.0005\n", |
| 256 | + " [1 0 1 0]\t0.0066\t\t0.0003\n", |
| 257 | + " [1 0 0 0]\t4.0029\t\t0.0001\n", |
| 258 | + " [1 1 1 0]\t4.0095\t\t0.0001\n", |
| 259 | + " [1 1 0 1]\t4.0071\t\t0.0000\n", |
| 260 | + " [1 0 1 1]\t4.0079\t\t0.0000\n", |
| 261 | + " [0 1 0 0]\t4.0031\t\t0.0000\n", |
| 262 | + " [0 1 1 1]\t4.0083\t\t0.0000\n", |
| 263 | + " [0 0 0 1]\t4.0012\t\t0.0000\n", |
| 264 | + " [0 0 1 0]\t4.0038\t\t0.0000\n", |
| 265 | + " [1 1 1 1]\t16.0109\t\t0.0000\n", |
| 266 | + " [0 0 0 0]\t16.0000\t\t0.0000\n" |
267 | 267 | ] |
268 | 268 | } |
269 | 269 | ], |
|
273 | 273 | "\n", |
274 | 274 | "cobyla = COBYLA()\n", |
275 | 275 | "cobyla.set_options(maxiter=500)\n", |
276 | | - "ry = RY(qubitOp.num_qubits, depth=3, entanglement='full')\n", |
| 276 | + "ry = TwoLocal(qubitOp.num_qubits, 'ry', 'cz', reps=3, entanglement='full')\n", |
277 | 277 | "vqe = VQE(qubitOp, ry, cobyla)\n", |
278 | 278 | "vqe.random_seed = seed\n", |
279 | 279 | "\n", |
|
302 | 302 | "name": "stdout", |
303 | 303 | "output_type": "stream", |
304 | 304 | "text": [ |
305 | | - "Optimal: selection [0. 0. 1. 1.], value -0.0196\n", |
| 305 | + "Optimal: selection [0. 1. 1. 0.], value 0.0069\n", |
306 | 306 | "\n", |
307 | 307 | "----------------- Full result ---------------------\n", |
308 | 308 | "selection\tvalue\t\tprobability\n", |
309 | 309 | "---------------------------------------------------\n", |
310 | | - " [0 0 1 1]\t-0.0196\t\t0.1668\n", |
311 | | - " [0 1 1 0]\t-0.0139\t\t0.1667\n", |
312 | | - " [1 0 1 0]\t-0.0131\t\t0.1667\n", |
313 | | - " [0 1 0 1]\t-0.0018\t\t0.1666\n", |
314 | | - " [1 0 0 1]\t-0.0014\t\t0.1666\n", |
315 | | - " [1 1 0 0]\t0.0044\t\t0.1665\n", |
316 | | - " [0 1 1 1]\t3.9823\t\t0.0000\n", |
317 | | - " [1 0 1 1]\t3.9831\t\t0.0000\n", |
318 | | - " [1 1 1 0]\t3.9887\t\t0.0000\n", |
319 | | - " [0 1 0 0]\t4.0021\t\t0.0000\n", |
320 | | - " [1 0 0 0]\t4.0023\t\t0.0000\n", |
321 | | - " [0 0 1 0]\t3.9843\t\t0.0000\n", |
322 | | - " [1 1 1 1]\t15.9850\t\t0.0000\n", |
323 | | - " [0 0 0 1]\t3.9961\t\t0.0000\n", |
324 | | - " [1 1 0 1]\t4.0007\t\t0.0000\n", |
325 | | - " [0 0 0 0]\t16.0000\t\t0.0000\n" |
| 310 | + " [0 1 1 0]\t0.0069\t\t0.1670\n", |
| 311 | + " [1 0 1 0]\t0.0066\t\t0.1669\n", |
| 312 | + " [1 1 0 0]\t0.0057\t\t0.1667\n", |
| 313 | + " [0 0 1 1]\t0.0050\t\t0.1664\n", |
| 314 | + " [0 1 0 1]\t0.0044\t\t0.1662\n", |
| 315 | + " [1 0 0 1]\t0.0041\t\t0.1661\n", |
| 316 | + " [0 0 0 1]\t4.0012\t\t0.0001\n", |
| 317 | + " [1 0 0 0]\t4.0029\t\t0.0001\n", |
| 318 | + " [0 1 0 0]\t4.0031\t\t0.0001\n", |
| 319 | + " [0 0 1 0]\t4.0038\t\t0.0001\n", |
| 320 | + " [1 1 0 1]\t4.0071\t\t0.0001\n", |
| 321 | + " [1 0 1 1]\t4.0079\t\t0.0001\n", |
| 322 | + " [0 1 1 1]\t4.0083\t\t0.0001\n", |
| 323 | + " [1 1 1 0]\t4.0095\t\t0.0001\n", |
| 324 | + " [0 0 0 0]\t16.0000\t\t0.0000\n", |
| 325 | + " [1 1 1 1]\t16.0109\t\t0.0000\n" |
326 | 326 | ] |
327 | 327 | } |
328 | 328 | ], |
|
356 | 356 | { |
357 | 357 | "data": { |
358 | 358 | "text/html": [ |
359 | | - "<h3>Version Information</h3><table><tr><th>Qiskit Software</th><th>Version</th></tr><tr><td>Qiskit</td><td>None</td></tr><tr><td>Terra</td><td>0.11.0.dev0+4164568</td></tr><tr><td>Aer</td><td>0.4.0</td></tr><tr><td>Ignis</td><td>0.3.0.dev0+6145a4e</td></tr><tr><td>Aqua</td><td>0.7.0.dev0+2d7a26f</td></tr><tr><td>IBM Q Provider</td><td>0.4.0rc1</td></tr><tr><th>System information</th></tr><tr><td>Python</td><td>3.7.4 (default, Aug 13 2019, 15:17:50) \n", |
360 | | - "[Clang 4.0.1 (tags/RELEASE_401/final)]</td></tr><tr><td>OS</td><td>Darwin</td></tr><tr><td>CPUs</td><td>2</td></tr><tr><td>Memory (Gb)</td><td>8.0</td></tr><tr><td colspan='2'>Mon Nov 11 14:14:33 2019 EST</td></tr></table>" |
| 359 | + "<h3>Version Information</h3><table><tr><th>Qiskit Software</th><th>Version</th></tr><tr><td>Qiskit</td><td>None</td></tr><tr><td>Terra</td><td>0.14.1</td></tr><tr><td>Aer</td><td>0.5.1</td></tr><tr><td>Ignis</td><td>0.3.0</td></tr><tr><td>Aqua</td><td>0.7.0</td></tr><tr><td>IBM Q Provider</td><td>0.7.0</td></tr><tr><th>System information</th></tr><tr><td>Python</td><td>3.7.4 (default, Aug 13 2019, 15:17:50) \n", |
| 360 | + "[Clang 4.0.1 (tags/RELEASE_401/final)]</td></tr><tr><td>OS</td><td>Darwin</td></tr><tr><td>CPUs</td><td>2</td></tr><tr><td>Memory (Gb)</td><td>8.0</td></tr><tr><td colspan='2'>Wed May 13 21:06:53 2020 EDT</td></tr></table>" |
361 | 361 | ], |
362 | 362 | "text/plain": [ |
363 | 363 | "<IPython.core.display.HTML object>" |
|
369 | 369 | { |
370 | 370 | "data": { |
371 | 371 | "text/html": [ |
372 | | - "<div style='width: 100%; background-color:#d5d9e0;padding-left: 10px; padding-bottom: 10px; padding-right: 10px; padding-top: 5px'><h3>This code is a part of Qiskit</h3><p>© Copyright IBM 2017, 2019.</p><p>This code is licensed under the Apache License, Version 2.0. You may<br>obtain a copy of this license in the LICENSE.txt file in the root directory<br> of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.<p>Any modifications or derivative works of this code must retain this<br>copyright notice, and modified files need to carry a notice indicating<br>that they have been altered from the originals.</p></div>" |
| 372 | + "<div style='width: 100%; background-color:#d5d9e0;padding-left: 10px; padding-bottom: 10px; padding-right: 10px; padding-top: 5px'><h3>This code is a part of Qiskit</h3><p>© Copyright IBM 2017, 2020.</p><p>This code is licensed under the Apache License, Version 2.0. You may<br>obtain a copy of this license in the LICENSE.txt file in the root directory<br> of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.<p>Any modifications or derivative works of this code must retain this<br>copyright notice, and modified files need to carry a notice indicating<br>that they have been altered from the originals.</p></div>" |
373 | 373 | ], |
374 | 374 | "text/plain": [ |
375 | 375 | "<IPython.core.display.HTML object>" |
|
409 | 409 | "name": "python", |
410 | 410 | "nbconvert_exporter": "python", |
411 | 411 | "pygments_lexer": "ipython3", |
412 | | - "version": "3.7.3" |
| 412 | + "version": "3.7.4" |
413 | 413 | }, |
414 | 414 | "varInspector": { |
415 | 415 | "cols": { |
|
0 commit comments