Skip to content
Closed
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
1 change: 1 addition & 0 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
.. note::
This page was generated from `{{ docname }}`__.

Run interactively in the `IBM Quantum lab <https://quantum-computing.ibm.com/jupyter/tutorial/{{ env.doc2path(env.docname, base=None)|replace("tutorials/", "") }}>`_.

__ https://github.com/Qiskit/qiskit-tutorials/blob/master/{{ docname }}

Expand Down
37 changes: 24 additions & 13 deletions tutorials/optimization/1_quadratic_program.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@
"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",
"\n",
"Note that in the LP format the quadratic part has to be scaled by a factor $1/2$.\n",
"Thus, when printing as LP format, the quadratic part is first multiplied by 2 and then divided by 2 again."
"Thus, when printing as LP format, the quadratic part is first multiplied by 2 and then divided by 2 again.\n",
"\n",
"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",
"\n",
"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"
]
},
{
Expand All @@ -230,6 +234,13 @@
"print(mod.export_as_lp_string())"
]
},
{
"source": [
"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."
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 7,
Expand All @@ -240,12 +251,12 @@
{
"output_type": "stream",
"name": "stdout",
"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"
"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"
}
],
"source": [
"# Add objective function using lists/arrays\n",
"mod.minimize(constant=3, linear=[1,0], quadratic=[[0,1],[1,-1]])\n",
"mod.minimize(constant=3, linear=[1,0,0], quadratic=[[0,1,0],[1,0,0],[0,0,-1]])\n",
"print(mod.export_as_lp_string())"
]
},
Expand All @@ -271,7 +282,7 @@
{
"output_type": "stream",
"name": "stdout",
"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"
"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"
}
],
"source": [
Expand Down Expand Up @@ -312,7 +323,7 @@
{
"output_type": "stream",
"name": "stdout",
"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"
"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"
}
],
"source": [
Expand Down Expand Up @@ -340,7 +351,7 @@
{
"output_type": "stream",
"name": "stdout",
"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"
"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"
}
],
"source": [
Expand Down Expand Up @@ -395,7 +406,7 @@
{
"output_type": "stream",
"name": "stdout",
"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"
"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"
}
],
"source": [
Expand Down Expand Up @@ -504,7 +515,7 @@
"output_type": "display_data",
"data": {
"text/plain": "<IPython.core.display.HTML object>",
"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>"
"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>"
},
"metadata": {}
},
Expand Down Expand Up @@ -532,11 +543,11 @@
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
Expand Down