Skip to content
This repository was archived by the owner on Aug 21, 2023. It is now read-only.

Commit 2b61e99

Browse files
authored
updated qp tutorial (#1046)
1 parent eff7f84 commit 2b61e99

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

tutorials/optimization/1_quadratic_program.ipynb

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@
208208
"You can add a constant term as well as linear and quadratic objective function by specifying linear and quadratic terms with either list, matrix or dictionary.\n",
209209
"\n",
210210
"Note that in the LP format the quadratic part has to be scaled by a factor $1/2$.\n",
211-
"Thus, when printing as LP format, the quadratic part is first multiplied by 2 and then divided by 2 again."
211+
"Thus, when printing as LP format, the quadratic part is first multiplied by 2 and then divided by 2 again.\n",
212+
"\n",
213+
"For quadratic programs, there are 3 pieces that have to be specified: a constant (offset), a linear term ($c^{T}x$), and a quadratic term ($x^{T}Qx$).\n",
214+
"\n",
215+
"The cell below shows how to declare an objective function using a dictionary. For the linear term, keys in the dictionary correspond to variable names, and the corresponding values are the coefficients. For the quadratic term, keys in the dictionary correspond to the two variables being multiplied, and the values are again the coefficients.\n"
212216
]
213217
},
214218
{
@@ -230,6 +234,13 @@
230234
"print(mod.export_as_lp_string())"
231235
]
232236
},
237+
{
238+
"source": [
239+
"Another way to specify the quadratic program is using arrays. For the linear term, the array corresponds to the vector $c$ in the mathematical formulation. For the quadratic term, the array corresponds to the matrix $Q$. Note that the ordering of the variables ($x$ in the mathematical formulation) is the order in which the variables were originally declared in the QuadraticProgram object."
240+
],
241+
"cell_type": "markdown",
242+
"metadata": {}
243+
},
233244
{
234245
"cell_type": "code",
235246
"execution_count": 7,
@@ -240,12 +251,12 @@
240251
{
241252
"output_type": "stream",
242253
"name": "stdout",
243-
"text": "\\ This file has been generated by DOcplex\n\\ ENCODING=ISO-8859-1\n\\Problem name: my problem\n\nMinimize\n obj: x + [ 4 x*y - 2 y^2 ]/2 + 3\nSubject To\n\nBounds\n 0 <= x <= 1\n -1 <= y <= 5\n -1 <= z <= 5\n\nBinaries\n x\n\nGenerals\n y\nEnd\n\n"
254+
"text": "\\ This file has been generated by DOcplex\n\\ ENCODING=ISO-8859-1\n\\Problem name: my problem\n\nMinimize\n obj: x + [ 4 x*y - 2 z^2 ]/2 + 3\nSubject To\n\nBounds\n 0 <= x <= 1\n -1 <= y <= 5\n -1 <= z <= 5\n\nBinaries\n x\n\nGenerals\n y\nEnd\n\n"
244255
}
245256
],
246257
"source": [
247258
"# Add objective function using lists/arrays\n",
248-
"mod.minimize(constant=3, linear=[1,0], quadratic=[[0,1],[1,-1]])\n",
259+
"mod.minimize(constant=3, linear=[1,0,0], quadratic=[[0,1,0],[1,0,0],[0,0,-1]])\n",
249260
"print(mod.export_as_lp_string())"
250261
]
251262
},
@@ -271,7 +282,7 @@
271282
{
272283
"output_type": "stream",
273284
"name": "stdout",
274-
"text": "constant:\t\t\t 3\nlinear dict:\t\t\t {0: 1}\nlinear array:\t\t\t [1 0]\nlinear array as sparse matrix:\n (0, 0)\t1 \n\nquadratic dict w/ index:\t {(0, 1): 2, (1, 1): -1}\nquadratic dict w/ name:\t\t {('x', 'y'): 2, ('y', 'y'): -1}\nsymmetric quadratic dict w/ name:\t {('y', 'x'): 1, ('x', 'y'): 1, ('y', 'y'): -1}\nquadratic matrix:\n [[ 0 2]\n [ 0 -1]] \n\nsymmetric quadratic matrix:\n [[ 0 1]\n [ 1 -1]] \n\nquadratic matrix as sparse matrix:\n (0, 1)\t2\n (1, 1)\t-1\n"
285+
"text": "constant:\t\t\t 3\nlinear dict:\t\t\t {0: 1}\nlinear array:\t\t\t [1 0 0]\nlinear array as sparse matrix:\n (0, 0)\t1 \n\nquadratic dict w/ index:\t {(0, 1): 2, (2, 2): -1}\nquadratic dict w/ name:\t\t {('x', 'y'): 2, ('z', 'z'): -1}\nsymmetric quadratic dict w/ name:\t {('y', 'x'): 1, ('x', 'y'): 1, ('z', 'z'): -1}\nquadratic matrix:\n [[ 0 2 0]\n [ 0 0 0]\n [ 0 0 -1]] \n\nsymmetric quadratic matrix:\n [[ 0 1 0]\n [ 1 0 0]\n [ 0 0 -1]] \n\nquadratic matrix as sparse matrix:\n (0, 1)\t2\n (2, 2)\t-1\n"
275286
}
276287
],
277288
"source": [
@@ -312,7 +323,7 @@
312323
{
313324
"output_type": "stream",
314325
"name": "stdout",
315-
"text": "\\ This file has been generated by DOcplex\n\\ ENCODING=ISO-8859-1\n\\Problem name: my problem\n\nMinimize\n obj: x + [ 4 x*y - 2 y^2 ]/2 + 3\nSubject To\n lin_eq: x + 2 y = 3\n lin_leq: x + 2 y <= 3\n lin_geq: x + 2 y >= 3\n\nBounds\n 0 <= x <= 1\n -1 <= y <= 5\n -1 <= z <= 5\n\nBinaries\n x\n\nGenerals\n y\nEnd\n\n"
326+
"text": "\\ This file has been generated by DOcplex\n\\ ENCODING=ISO-8859-1\n\\Problem name: my problem\n\nMinimize\n obj: x + [ 4 x*y - 2 z^2 ]/2 + 3\nSubject To\n lin_eq: x + 2 y = 3\n lin_leq: x + 2 y <= 3\n lin_geq: x + 2 y >= 3\n\nBounds\n 0 <= x <= 1\n -1 <= y <= 5\n -1 <= z <= 5\n\nBinaries\n x\n\nGenerals\n y\nEnd\n\n"
316327
}
317328
],
318329
"source": [
@@ -340,7 +351,7 @@
340351
{
341352
"output_type": "stream",
342353
"name": "stdout",
343-
"text": "\\ This file has been generated by DOcplex\n\\ ENCODING=ISO-8859-1\n\\Problem name: my problem\n\nMinimize\n obj: x + [ 4 x*y - 2 y^2 ]/2 + 3\nSubject To\n lin_eq: x + 2 y = 3\n lin_leq: x + 2 y <= 3\n lin_geq: x + 2 y >= 3\n quad_eq: [ x^2 - y*z ] + x + y = 1\n quad_leq: [ x^2 - y*z ] + x + y <= 1\n quad_geq: [ x^2 - y*z ] + x + y >= 1\n\nBounds\n 0 <= x <= 1\n -1 <= y <= 5\n -1 <= z <= 5\n\nBinaries\n x\n\nGenerals\n y\nEnd\n\n"
354+
"text": "\\ This file has been generated by DOcplex\n\\ ENCODING=ISO-8859-1\n\\Problem name: my problem\n\nMinimize\n obj: x + [ 4 x*y - 2 z^2 ]/2 + 3\nSubject To\n lin_eq: x + 2 y = 3\n lin_leq: x + 2 y <= 3\n lin_geq: x + 2 y >= 3\n quad_eq: [ x^2 - y*z ] + x + y = 1\n quad_leq: [ x^2 - y*z ] + x + y <= 1\n quad_geq: [ x^2 - y*z ] + x + y >= 1\n\nBounds\n 0 <= x <= 1\n -1 <= y <= 5\n -1 <= z <= 5\n\nBinaries\n x\n\nGenerals\n y\nEnd\n\n"
344355
}
345356
],
346357
"source": [
@@ -395,7 +406,7 @@
395406
{
396407
"output_type": "stream",
397408
"name": "stdout",
398-
"text": "\\ This file has been generated by DOcplex\n\\ ENCODING=ISO-8859-1\n\\Problem name: my problem\n\nMinimize\n obj: x + [ 4 x*y - 2 y^2 ]/2 + 3\nSubject To\n lin_leq: x + 2 y <= 3\n lin_geq: x + 2 y >= 3\n quad_eq: [ x^2 - y*z ] + x + y = 1\n quad_geq: [ x^2 - y*z ] + x + y >= 1\n\nBounds\n 0 <= x <= 1\n -1 <= y <= 5\n -1 <= z <= 5\n\nBinaries\n x\n\nGenerals\n y\nEnd\n\n"
409+
"text": "\\ This file has been generated by DOcplex\n\\ ENCODING=ISO-8859-1\n\\Problem name: my problem\n\nMinimize\n obj: x + [ 4 x*y - 2 z^2 ]/2 + 3\nSubject To\n lin_leq: x + 2 y <= 3\n lin_geq: x + 2 y >= 3\n quad_eq: [ x^2 - y*z ] + x + y = 1\n quad_geq: [ x^2 - y*z ] + x + y >= 1\n\nBounds\n 0 <= x <= 1\n -1 <= y <= 5\n -1 <= z <= 5\n\nBinaries\n x\n\nGenerals\n y\nEnd\n\n"
399410
}
400411
],
401412
"source": [
@@ -504,7 +515,7 @@
504515
"output_type": "display_data",
505516
"data": {
506517
"text/plain": "<IPython.core.display.HTML object>",
507-
"text/html": "<h3>Version Information</h3><table><tr><th>Qiskit Software</th><th>Version</th></tr><tr><td>Qiskit</td><td>0.20.0</td></tr><tr><td>Terra</td><td>0.15.1</td></tr><tr><td>Aer</td><td>0.6.1</td></tr><tr><td>Ignis</td><td>0.4.0</td></tr><tr><td>Aqua</td><td>0.7.5</td></tr><tr><td>IBM Q Provider</td><td>0.8.0</td></tr><tr><th>System information</th></tr><tr><td>Python</td><td>3.8.5 (default, Jul 21 2020, 10:48:26) \n[Clang 11.0.3 (clang-1103.0.32.62)]</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 Aug 12 19:28:23 2020 JST</td></tr></table>"
518+
"text/html": "<h3>Version Information</h3><table><tr><th>Qiskit Software</th><th>Version</th></tr><tr><td>Qiskit</td><td>0.21.0</td></tr><tr><td>Terra</td><td>0.15.2</td></tr><tr><td>Aer</td><td>0.6.1</td></tr><tr><td>Ignis</td><td>0.4.0</td></tr><tr><td>Aqua</td><td>0.7.5</td></tr><tr><td>IBM Q Provider</td><td>0.9.0</td></tr><tr><th>System information</th></tr><tr><td>Python</td><td>3.8.5 (default, Jul 21 2020, 10:48:26) \n[Clang 11.0.3 (clang-1103.0.32.62)]</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'>Sat Sep 26 02:21:47 2020 JST</td></tr></table>"
508519
},
509520
"metadata": {}
510521
},
@@ -532,11 +543,11 @@
532543
}
533544
],
534545
"metadata": {
535-
"kernelspec": {
536-
"display_name": "Python 3",
537-
"language": "python",
538-
"name": "python3"
539-
},
546+
"kernelspec": {
547+
"display_name": "Python 3",
548+
"language": "python",
549+
"name": "python3"
550+
},
540551
"language_info": {
541552
"codemirror_mode": {
542553
"name": "ipython",

0 commit comments

Comments
 (0)