|
4 | 4 | "cell_type": "markdown", |
5 | 5 | "metadata": {}, |
6 | 6 | "source": [ |
7 | | - "# Quadratically Constrained Quadratic Programs" |
| 7 | + "# Quadratically Constrained Quadratic Programs\n", |
| 8 | + "The latest version of this notebook is available on https://github.com/Qiskit/qiskit/tree/master/docs/tutorials." |
8 | 9 | ] |
9 | 10 | }, |
10 | 11 | { |
|
18 | 19 | "cell_type": "markdown", |
19 | 20 | "metadata": {}, |
20 | 21 | "source": [ |
21 | | - "In this tutorial, we briefly introduce how to construct an optimization model with Qiskit Optimization.\n", |
22 | | - "You can find detailed information at [Qiskit Aqua API references](https://qiskit.org/documentation/apidoc/optimization/optimization.html).\n", |
23 | | - "\n", |
24 | | - "Qiskit Optimization introduces `QuadraticProgram` class to make a model of an optimization problem.\n", |
| 22 | + "In this tutorial, we briefly introduce how to build optimization problems using Qiskit's optimization module.\n", |
| 23 | + "Qiskit introduces the `QuadraticProgram` class to make a model of an optimization problem.\n", |
25 | 24 | "More precicely, it deals with quadratically constrained quadratic programs given as follows:\n", |
26 | | - "\n", |
27 | 25 | "$$\n", |
28 | 26 | "\\begin{align}\n", |
29 | | - "\\text{minimize}\\quad& x^\\top Q_0 x + c^\\top x \\\\\n", |
30 | | - "\\text{subject to}\\quad& A x \\leq b \\\\\n", |
31 | | - "& x^\\top Q_i x + a_i^\\top x \\leq r_i, \\quad 1,\\dots,i,\\dots,q \\\\\n", |
| 27 | + "\\text{minimize}\\quad& x^\\top Q_0 x + c^\\top x\\\\\n", |
| 28 | + "\\text{subject to}\\quad& A x \\leq b\\\\\n", |
| 29 | + "& x^\\top Q_i x + a_i^\\top x \\leq r_i, \\quad 1,\\dots,i,\\dots,q\\\\\n", |
32 | 30 | "& l_i \\leq x_i \\leq u_i, \\quad 1,\\dots,i,\\dots,n,\n", |
33 | 31 | "\\end{align}\n", |
34 | 32 | "$$\n", |
35 | | - "\n", |
36 | 33 | "where the $Q_i$ are $n \\times n$ matrices, $A$ is a $m \\times n$ matrix , $x$, and $c$ are $n$-dimensional vectors, $b$ is an $m$-dimensional vector, and where $x$ can defined as binary, integer, or continuous variables.\n", |
37 | 34 | "In addition to \"$\\leq$\" constraints 'QuadraticProgram' also supports \"$\\geq$\" and \"$=$\"." |
38 | 35 | ] |
|
41 | 38 | "cell_type": "markdown", |
42 | 39 | "metadata": {}, |
43 | 40 | "source": [ |
44 | | - "## Loading a Quadratic Program from an LP file" |
| 41 | + "## Loading a `Quadratic Program` from an LP file" |
45 | 42 | ] |
46 | 43 | }, |
47 | 44 | { |
|
53 | 50 | }, |
54 | 51 | { |
55 | 52 | "cell_type": "code", |
56 | | - "execution_count": 2, |
| 53 | + "execution_count": 1, |
57 | 54 | "metadata": {}, |
58 | | - "outputs": [ |
59 | | - { |
60 | | - "name": "stderr", |
61 | | - "output_type": "stream", |
62 | | - "text": [ |
63 | | - "/opt/miniconda3/lib/python3.7/site-packages/qiskit_aqua-0.7.0-py3.7.egg/qiskit/aqua/operators/primitive_ops/pauli_op.py:25: DeprecationWarning: The module qiskit.extensions.standard is deprecated as of 0.14.0 and will be removed no earlier than 3 months after the release. You should import the standard gates from qiskit.circuit.library.standard_gates instead.\n", |
64 | | - " from qiskit.extensions.standard import RZGate, RYGate, RXGate, XGate, YGate, ZGate, IGate\n" |
65 | | - ] |
66 | | - } |
67 | | - ], |
| 55 | + "outputs": [], |
68 | 56 | "source": [ |
69 | 57 | "from qiskit.optimization import QuadraticProgram" |
70 | 58 | ] |
|
73 | 61 | "cell_type": "markdown", |
74 | 62 | "metadata": {}, |
75 | 63 | "source": [ |
76 | | - "You start with an empty model or load a problem from LP files.\n", |
| 64 | + "You start with an empty model.\n", |
77 | 65 | "You can display your optimization problem with a method `pprint_as_string` and `print_as_lp_string`." |
78 | 66 | ] |
79 | 67 | }, |
80 | | - { |
81 | | - "cell_type": "code", |
82 | | - "execution_count": 3, |
83 | | - "metadata": {}, |
84 | | - "outputs": [ |
85 | | - { |
86 | | - "name": "stdout", |
87 | | - "output_type": "stream", |
88 | | - "text": [ |
89 | | - "\\ This file has been generated by DOcplex\n", |
90 | | - "\\ ENCODING=ISO-8859-1\n", |
91 | | - "\\Problem name: my problem\n", |
92 | | - "\n", |
93 | | - "Minimize\n", |
94 | | - " obj: x - y + 10 z + [ x^2 - 2 y*z ]/2\n", |
95 | | - "Subject To\n", |
96 | | - " lin_eq: x + 2 y = 1\n", |
97 | | - " lin_leq: x + 2 y <= 1\n", |
98 | | - " lin_geq: x + 2 y >= 1\n", |
99 | | - " quad_eq: [ x^2 - y*z + 2 z^2 ] + x + y = 1\n", |
100 | | - " quad_leq: [ x^2 - y*z + 2 z^2 ] + x + y <= 1\n", |
101 | | - " quad_geq: [ x^2 - y*z + 2 z^2 ] + x + y >= 1\n", |
102 | | - "\n", |
103 | | - "Bounds\n", |
104 | | - " 0 <= x <= 1\n", |
105 | | - " -1 <= y <= 5\n", |
106 | | - " -1 <= z <= 5\n", |
107 | | - "\n", |
108 | | - "Binaries\n", |
109 | | - " x\n", |
110 | | - "\n", |
111 | | - "Generals\n", |
112 | | - " y\n", |
113 | | - "End\n", |
114 | | - "\n" |
115 | | - ] |
116 | | - } |
117 | | - ], |
118 | | - "source": [ |
119 | | - "# load an LP file\n", |
120 | | - "mod = QuadraticProgram()\n", |
121 | | - "mod.read_from_lp_file('aux_files/sample.lp')\n", |
122 | | - "print(mod.export_as_lp_string())" |
123 | | - ] |
124 | | - }, |
125 | 68 | { |
126 | 69 | "cell_type": "markdown", |
127 | 70 | "metadata": {}, |
128 | 71 | "source": [ |
129 | | - "Qiskit Optimization supports the conversion from Docplex model. You can easily make a model of an optimization problem with Docplex.\n", |
| 72 | + "Qiskit's optimization module supports the conversion from Docplex model. You can easily make a model of an optimization problem with Docplex.\n", |
130 | 73 | "You can find the documentation of Docplex at https://ibmdecisionoptimization.github.io/docplex-doc/mp/index.html\n", |
131 | 74 | "\n", |
132 | 75 | "You can load a Docplex model to `QuadraticProgram` by invoking `from_docplex`." |
|
136 | 79 | "cell_type": "markdown", |
137 | 80 | "metadata": {}, |
138 | 81 | "source": [ |
139 | | - "## Loading a QuadraticProgram from a docplex model" |
| 82 | + "## Loading a `QuadraticProgram` from a docplex model" |
140 | 83 | ] |
141 | 84 | }, |
142 | 85 | { |
143 | 86 | "cell_type": "code", |
144 | | - "execution_count": 4, |
| 87 | + "execution_count": 2, |
145 | 88 | "metadata": {}, |
146 | 89 | "outputs": [ |
147 | 90 | { |
|
187 | 130 | }, |
188 | 131 | { |
189 | 132 | "cell_type": "code", |
190 | | - "execution_count": 5, |
| 133 | + "execution_count": 3, |
191 | 134 | "metadata": {}, |
192 | 135 | "outputs": [ |
193 | 136 | { |
|
229 | 172 | "cell_type": "markdown", |
230 | 173 | "metadata": {}, |
231 | 174 | "source": [ |
232 | | - "## Directly constructing a QuadraticProgram" |
| 175 | + "## Directly constructing a `QuadraticProgram`" |
233 | 176 | ] |
234 | 177 | }, |
235 | 178 | { |
|
242 | 185 | }, |
243 | 186 | { |
244 | 187 | "cell_type": "code", |
245 | | - "execution_count": 6, |
| 188 | + "execution_count": 4, |
246 | 189 | "metadata": {}, |
247 | 190 | "outputs": [ |
248 | 191 | { |
|
273 | 216 | "cell_type": "markdown", |
274 | 217 | "metadata": {}, |
275 | 218 | "source": [ |
276 | | - "Qiskit Optimization stack supports three types of variables:\n", |
| 219 | + "The `QuadraticProgram` supports three types of variables:\n", |
277 | 220 | "- Binary varible\n", |
278 | 221 | "- Integer variable\n", |
279 | 222 | "- Continuous variable\n", |
|
288 | 231 | }, |
289 | 232 | { |
290 | 233 | "cell_type": "code", |
291 | | - "execution_count": 7, |
| 234 | + "execution_count": 5, |
292 | 235 | "metadata": {}, |
293 | 236 | "outputs": [ |
294 | 237 | { |
|
339 | 282 | }, |
340 | 283 | { |
341 | 284 | "cell_type": "code", |
342 | | - "execution_count": 8, |
| 285 | + "execution_count": 6, |
343 | 286 | "metadata": {}, |
344 | 287 | "outputs": [ |
345 | 288 | { |
|
377 | 320 | }, |
378 | 321 | { |
379 | 322 | "cell_type": "code", |
380 | | - "execution_count": 9, |
| 323 | + "execution_count": 7, |
381 | 324 | "metadata": {}, |
382 | 325 | "outputs": [ |
383 | 326 | { |
|
427 | 370 | }, |
428 | 371 | { |
429 | 372 | "cell_type": "code", |
430 | | - "execution_count": 10, |
| 373 | + "execution_count": 8, |
431 | 374 | "metadata": {}, |
432 | 375 | "outputs": [ |
433 | 376 | { |
|
487 | 430 | }, |
488 | 431 | { |
489 | 432 | "cell_type": "code", |
490 | | - "execution_count": 11, |
| 433 | + "execution_count": 9, |
491 | 434 | "metadata": {}, |
492 | 435 | "outputs": [ |
493 | 436 | { |
|
537 | 480 | }, |
538 | 481 | { |
539 | 482 | "cell_type": "code", |
540 | | - "execution_count": 12, |
| 483 | + "execution_count": 10, |
541 | 484 | "metadata": {}, |
542 | 485 | "outputs": [ |
543 | 486 | { |
|
590 | 533 | }, |
591 | 534 | { |
592 | 535 | "cell_type": "code", |
593 | | - "execution_count": 13, |
| 536 | + "execution_count": 11, |
594 | 537 | "metadata": {}, |
595 | 538 | "outputs": [ |
596 | 539 | { |
|
618 | 561 | }, |
619 | 562 | { |
620 | 563 | "cell_type": "code", |
621 | | - "execution_count": 14, |
| 564 | + "execution_count": 12, |
622 | 565 | "metadata": {}, |
623 | 566 | "outputs": [ |
624 | 567 | { |
|
678 | 621 | }, |
679 | 622 | { |
680 | 623 | "cell_type": "code", |
681 | | - "execution_count": 15, |
| 624 | + "execution_count": 13, |
682 | 625 | "metadata": {}, |
683 | 626 | "outputs": [ |
684 | 627 | { |
|
719 | 662 | }, |
720 | 663 | { |
721 | 664 | "cell_type": "code", |
722 | | - "execution_count": 16, |
| 665 | + "execution_count": 14, |
723 | 666 | "metadata": {}, |
724 | 667 | "outputs": [ |
725 | 668 | { |
|
752 | 695 | }, |
753 | 696 | { |
754 | 697 | "cell_type": "code", |
755 | | - "execution_count": 17, |
| 698 | + "execution_count": 15, |
756 | 699 | "metadata": {}, |
757 | 700 | "outputs": [ |
758 | 701 | { |
|
773 | 716 | }, |
774 | 717 | { |
775 | 718 | "cell_type": "code", |
776 | | - "execution_count": 18, |
| 719 | + "execution_count": 16, |
777 | 720 | "metadata": {}, |
778 | 721 | "outputs": [ |
779 | 722 | { |
780 | 723 | "data": { |
781 | 724 | "text/html": [ |
782 | | - "<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.0</td></tr><tr><td>Aer</td><td>0.6.0</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.6.1</td></tr><tr><th>System information</th></tr><tr><td>Python</td><td>3.7.7 (default, Mar 26 2020, 10:32:53) \n", |
783 | | - "[Clang 4.0.1 (tags/RELEASE_401/final)]</td></tr><tr><td>OS</td><td>Darwin</td></tr><tr><td>CPUs</td><td>4</td></tr><tr><td>Memory (Gb)</td><td>16.0</td></tr><tr><td colspan='2'>Wed Apr 29 12:59:03 2020 EDT</td></tr></table>" |
| 725 | + "<h3>Version Information</h3><table><tr><th>Qiskit Software</th><th>Version</th></tr><tr><td>Qiskit</td><td>0.18.3</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.8.0.dev0+7056f9b</td></tr><tr><td>IBM Q Provider</td><td>0.6.1</td></tr><tr><th>System information</th></tr><tr><td>Python</td><td>3.7.7 (default, Apr 18 2020, 02:59:53) \n", |
| 726 | + "[GCC 9.3.0]</td></tr><tr><td>OS</td><td>Linux</td></tr><tr><td>CPUs</td><td>4</td></tr><tr><td>Memory (Gb)</td><td>15.143501281738281</td></tr><tr><td colspan='2'>Wed May 06 22:35:12 2020 JST</td></tr></table>" |
784 | 727 | ], |
785 | 728 | "text/plain": [ |
786 | 729 | "<IPython.core.display.HTML object>" |
|
0 commit comments