Skip to content

Commit

Permalink
Fix tutorials to work with Qiskit 1.0 (#610)
Browse files Browse the repository at this point in the history
(cherry picked from commit dea16dd)
  • Loading branch information
woodsp-ibm authored and mergify[bot] committed Feb 20, 2024
1 parent 39672fc commit f18138b
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/tutorials/01_quadratic_program.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"import tutorial_magics\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/02_converters_for_quadratic_programs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"import tutorial_magics\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/03_minimum_eigen_optimizer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"import tutorial_magics\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/04_grover_optimizer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"import tutorial_magics\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/05_admm_optimizer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"import tutorial_magics\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
Expand Down
19 changes: 16 additions & 3 deletions docs/tutorials/06_examples_max_cut_and_tsp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -972,7 +971,7 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"import tutorial_magics\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/07_examples_vehicle_routing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"import tutorial_magics\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/08_cvar_optimization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"import tutorial_magics\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/09_application_classes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"import tutorial_magics\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
Expand Down
8 changes: 4 additions & 4 deletions docs/tutorials/10_warm_start_qaoa.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -700,7 +700,7 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"import tutorial_magics\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
Expand All @@ -717,7 +717,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -731,7 +731,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.9.7"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"import tutorial_magics\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/12_quantum_random_access_optimizer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"import tutorial_magics\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
Expand Down
96 changes: 96 additions & 0 deletions docs/tutorials/tutorial_magics.py
Original file line number Diff line number Diff line change
@@ -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 = "<div style='width: 100%; background-color:#d5d9e0;"
html += "padding-left: 10px; padding-bottom: 10px; padding-right: 10px; padding-top: 5px'>"
html += "<h3>This code is a part of a Qiskit project</h3>"
html += "<p>&copy; Copyright IBM 2017, %s.</p>" % now.year
html += "<p>This code is licensed under the Apache License, Version 2.0. You may<br>"
html += "obtain a copy of this license in the LICENSE.txt file in the root directory<br> "
html += "of this source tree or at http://www.apache.org/licenses/LICENSE-2.0."

html += "<p>Any modifications or derivative works of this code must retain this<br>"
html += "copyright notice, and modified files need to carry a notice indicating<br>"
html += "that they have been altered from the originals.</p>"
html += "</div>"
return display(HTML(html))


@magics_class
class VersionTable(Magics):
"""A class of status magic functions."""

@line_magic
def qiskit_version_table(self, line="", cell=None):
"""
Print an HTML-formatted table with version numbers for Qiskit and its
dependencies. This should make it possible to reproduce the environment
and the calculation later on.
"""
html = "<h3>Version Information</h3>"
html += "<table>"
html += "<tr><th>Software</th><th>Version</th></tr>"

packages = {"qiskit": qiskit.__version__}
qiskit_modules = {module.split(".")[0] for module in modules.keys() if "qiskit" in module}

for qiskit_module in qiskit_modules:
packages[qiskit_module] = getattr(modules[qiskit_module], "__version__", None)

for name, version in packages.items():
if version:
html += f"<tr><td><code>{name}</code></td><td>{version}</td></tr>"

html += "<tr><th colspan='2'>System information</th></tr>"

sys_info = [
("Python version", platform.python_version()),
("OS", "%s" % platform.system()),
]

for name, version in sys_info:
html += f"<tr><td>{name}</td><td>{version}</td></tr>"

html += "<tr><td colspan='2'>%s</td></tr>" % time.strftime("%a %b %d %H:%M:%S %Y %Z")
html += "</table>"

return display(HTML(html))


_IP = get_ipython()
if _IP is not None:
_IP.register_magics(VersionTable)
_IP.register_magics(Copyright)

0 comments on commit f18138b

Please sign in to comment.