From f18138bdb1cb6aa11d818c8e8aa89551d2d63a82 Mon Sep 17 00:00:00 2001 From: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> Date: Tue, 20 Feb 2024 01:22:15 -0500 Subject: [PATCH] Fix tutorials to work with Qiskit 1.0 (#610) (cherry picked from commit dea16dd7d71e77fbefdd44b0b62cdb7fab67f14e) --- docs/tutorials/01_quadratic_program.ipynb | 2 +- ...02_converters_for_quadratic_programs.ipynb | 2 +- .../03_minimum_eigen_optimizer.ipynb | 2 +- docs/tutorials/04_grover_optimizer.ipynb | 2 +- docs/tutorials/05_admm_optimizer.ipynb | 2 +- .../06_examples_max_cut_and_tsp.ipynb | 19 +++- .../07_examples_vehicle_routing.ipynb | 2 +- docs/tutorials/08_cvar_optimization.ipynb | 2 +- docs/tutorials/09_application_classes.ipynb | 2 +- docs/tutorials/10_warm_start_qaoa.ipynb | 8 +- ...ical_optimization_solvers_and_models.ipynb | 2 +- .../12_quantum_random_access_optimizer.ipynb | 2 +- docs/tutorials/tutorial_magics.py | 96 +++++++++++++++++++ 13 files changed, 126 insertions(+), 17 deletions(-) create mode 100644 docs/tutorials/tutorial_magics.py diff --git a/docs/tutorials/01_quadratic_program.ipynb b/docs/tutorials/01_quadratic_program.ipynb index 2a6295c17..4eaf3f628 100644 --- a/docs/tutorials/01_quadratic_program.ipynb +++ b/docs/tutorials/01_quadratic_program.ipynb @@ -825,7 +825,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/02_converters_for_quadratic_programs.ipynb b/docs/tutorials/02_converters_for_quadratic_programs.ipynb index 0c7f3e8d2..219a3394f 100644 --- a/docs/tutorials/02_converters_for_quadratic_programs.ipynb +++ b/docs/tutorials/02_converters_for_quadratic_programs.ipynb @@ -697,7 +697,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/03_minimum_eigen_optimizer.ipynb b/docs/tutorials/03_minimum_eigen_optimizer.ipynb index 3639a2c9a..f62c33ec6 100644 --- a/docs/tutorials/03_minimum_eigen_optimizer.ipynb +++ b/docs/tutorials/03_minimum_eigen_optimizer.ipynb @@ -644,7 +644,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/04_grover_optimizer.ipynb b/docs/tutorials/04_grover_optimizer.ipynb index d0d1b75f8..f9ac537e7 100644 --- a/docs/tutorials/04_grover_optimizer.ipynb +++ b/docs/tutorials/04_grover_optimizer.ipynb @@ -228,7 +228,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/05_admm_optimizer.ipynb b/docs/tutorials/05_admm_optimizer.ipynb index cf760d5d7..fd1de2f3d 100644 --- a/docs/tutorials/05_admm_optimizer.ipynb +++ b/docs/tutorials/05_admm_optimizer.ipynb @@ -447,7 +447,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/06_examples_max_cut_and_tsp.ipynb b/docs/tutorials/06_examples_max_cut_and_tsp.ipynb index 42a79bac2..6acffc117 100644 --- a/docs/tutorials/06_examples_max_cut_and_tsp.ipynb +++ b/docs/tutorials/06_examples_max_cut_and_tsp.ipynb @@ -115,7 +115,6 @@ "import numpy as np\n", "import networkx as nx\n", "\n", - "from qiskit.tools.visualization import plot_histogram\n", "from qiskit.circuit.library import TwoLocal\n", "from qiskit_optimization.applications import Maxcut, Tsp\n", "from qiskit_algorithms import SamplingVQE, NumPyMinimumEigensolver\n", @@ -972,7 +971,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" @@ -987,8 +986,22 @@ } ], "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, "language_info": { - "name": "python" + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.7" } }, "nbformat": 4, diff --git a/docs/tutorials/07_examples_vehicle_routing.ipynb b/docs/tutorials/07_examples_vehicle_routing.ipynb index e73a9acb9..2200155bc 100644 --- a/docs/tutorials/07_examples_vehicle_routing.ipynb +++ b/docs/tutorials/07_examples_vehicle_routing.ipynb @@ -796,7 +796,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/08_cvar_optimization.ipynb b/docs/tutorials/08_cvar_optimization.ipynb index 0bd59a656..c6d23396b 100644 --- a/docs/tutorials/08_cvar_optimization.ipynb +++ b/docs/tutorials/08_cvar_optimization.ipynb @@ -339,7 +339,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/09_application_classes.ipynb b/docs/tutorials/09_application_classes.ipynb index de9ff4268..1ecf1c14d 100644 --- a/docs/tutorials/09_application_classes.ipynb +++ b/docs/tutorials/09_application_classes.ipynb @@ -524,7 +524,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/10_warm_start_qaoa.ipynb b/docs/tutorials/10_warm_start_qaoa.ipynb index 87d726bf0..a781a6dcb 100644 --- a/docs/tutorials/10_warm_start_qaoa.ipynb +++ b/docs/tutorials/10_warm_start_qaoa.ipynb @@ -46,7 +46,7 @@ "# Qiskit imports\n", "from qiskit_algorithms import QAOA, NumPyMinimumEigensolver\n", "from qiskit_algorithms.optimizers import COBYLA\n", - "from qiskit_algorithms.utils.algorithm_globals import algorithm_globals\n", + "from qiskit_algorithms.utils import algorithm_globals\n", "from qiskit.primitives import Sampler\n", "from qiskit_optimization.algorithms import MinimumEigenOptimizer, CplexOptimizer\n", "from qiskit_optimization import QuadraticProgram\n", @@ -700,7 +700,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" @@ -717,7 +717,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -731,7 +731,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.13" + "version": "3.9.7" } }, "nbformat": 4, diff --git a/docs/tutorials/11_using_classical_optimization_solvers_and_models.ipynb b/docs/tutorials/11_using_classical_optimization_solvers_and_models.ipynb index 7702a8390..4c55df683 100644 --- a/docs/tutorials/11_using_classical_optimization_solvers_and_models.ipynb +++ b/docs/tutorials/11_using_classical_optimization_solvers_and_models.ipynb @@ -556,7 +556,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/12_quantum_random_access_optimizer.ipynb b/docs/tutorials/12_quantum_random_access_optimizer.ipynb index da7c3031a..e6e463b4a 100644 --- a/docs/tutorials/12_quantum_random_access_optimizer.ipynb +++ b/docs/tutorials/12_quantum_random_access_optimizer.ipynb @@ -851,7 +851,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/tutorial_magics.py b/docs/tutorials/tutorial_magics.py new file mode 100644 index 000000000..22d88bdd6 --- /dev/null +++ b/docs/tutorials/tutorial_magics.py @@ -0,0 +1,96 @@ +# This code is part of a Qiskit project +# +# (C) Copyright IBM 2017, 2024 +# +# This code is licensed under the Apache License, Version 2.0. You may +# obtain a copy of this license in the LICENSE.txt file in the root directory +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +# +# Any modifications or derivative works of this code must retain this +# copyright notice, and modified files need to carry a notice indicating +# that they have been altered from the originals. +# pylint: disable=unused-argument + +"""A module for version and copyright magics.""" + +import datetime +import platform +import time +from sys import modules + +from IPython import get_ipython +from IPython.core.magic import line_magic, Magics, magics_class +from IPython.display import HTML, display + +import qiskit + + +@magics_class +class Copyright(Magics): + """A class of status magic functions.""" + + @line_magic + def qiskit_copyright(self, line="", cell=None): + """A Jupyter magic function return qiskit copyright""" + now = datetime.datetime.now() + + html = "
© Copyright IBM 2017, %s.
" % now.year + html += "This code is licensed under the Apache License, Version 2.0. You may
"
+ html += "obtain a copy of this license in the LICENSE.txt file in the root directory
"
+ html += "of this source tree or at http://www.apache.org/licenses/LICENSE-2.0."
+
+ html += "
Any modifications or derivative works of this code must retain this
"
+ html += "copyright notice, and modified files need to carry a notice indicating
"
+ html += "that they have been altered from the originals.
Software | Version |
---|---|
{name} | {version} |
System information | |
{name} | {version} |
%s |