diff --git a/conf.py b/conf.py index 65134651a..b135ed7db 100644 --- a/conf.py +++ b/conf.py @@ -105,6 +105,7 @@ .. note:: This page was generated from `{{ docname }}`__. + Run interactively in the `IBM Quantum lab `_. __ https://github.com/Qiskit/qiskit-tutorials/blob/master/{{ docname }} diff --git a/tutorials/optimization/1_quadratic_program.ipynb b/tutorials/optimization/1_quadratic_program.ipynb index 4b6d6938a..98590dece 100644 --- a/tutorials/optimization/1_quadratic_program.ipynb +++ b/tutorials/optimization/1_quadratic_program.ipynb @@ -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" ] }, { @@ -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, @@ -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())" ] }, @@ -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": [ @@ -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": [ @@ -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": [ @@ -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": [ @@ -504,7 +515,7 @@ "output_type": "display_data", "data": { "text/plain": "", - "text/html": "

Version Information

Qiskit SoftwareVersion
Qiskit0.20.0
Terra0.15.1
Aer0.6.1
Ignis0.4.0
Aqua0.7.5
IBM Q Provider0.8.0
System information
Python3.8.5 (default, Jul 21 2020, 10:48:26) \n[Clang 11.0.3 (clang-1103.0.32.62)]
OSDarwin
CPUs2
Memory (Gb)8.0
Wed Aug 12 19:28:23 2020 JST
" + "text/html": "

Version Information

Qiskit SoftwareVersion
Qiskit0.21.0
Terra0.15.2
Aer0.6.1
Ignis0.4.0
Aqua0.7.5
IBM Q Provider0.9.0
System information
Python3.8.5 (default, Jul 21 2020, 10:48:26) \n[Clang 11.0.3 (clang-1103.0.32.62)]
OSDarwin
CPUs2
Memory (Gb)8.0
Sat Sep 26 02:21:47 2020 JST
" }, "metadata": {} }, @@ -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",