From c53501f4b18d15514f5ebb66e3f73ab68961df4a Mon Sep 17 00:00:00 2001 From: woodsp-ibm Date: Fri, 16 Feb 2024 11:14:46 -0500 Subject: [PATCH] Fix tutorials to work with Qiskit 1.0 --- docs/tutorials/00_amplitude_estimation.ipynb | 10 +- .../tutorials/01_portfolio_optimization.ipynb | 2 +- .../02_portfolio_diversification.ipynb | 2 +- .../03_european_call_option_pricing.ipynb | 2 +- .../04_european_put_option_pricing.ipynb | 2 +- docs/tutorials/05_bull_spread_pricing.ipynb | 2 +- docs/tutorials/06_basket_option_pricing.ipynb | 2 +- .../07_asian_barrier_spread_pricing.ipynb | 2 +- docs/tutorials/08_fixed_income_pricing.ipynb | 2 +- docs/tutorials/09_credit_risk_analysis.ipynb | 2 +- docs/tutorials/10_qgan_option_pricing.ipynb | 2 +- docs/tutorials/11_time_series.ipynb | 2 +- docs/tutorials/tutorial_magics.py | 96 +++++++++++++++++++ 13 files changed, 112 insertions(+), 16 deletions(-) create mode 100644 docs/tutorials/tutorial_magics.py diff --git a/docs/tutorials/00_amplitude_estimation.ipynb b/docs/tutorials/00_amplitude_estimation.ipynb index 5f1cd4f8..c1124ae7 100644 --- a/docs/tutorials/00_amplitude_estimation.ipynb +++ b/docs/tutorials/00_amplitude_estimation.ipynb @@ -323,7 +323,7 @@ "source": [ "ae_circuit = ae.construct_circuit(problem)\n", "ae_circuit.decompose().draw(\n", - " \"mpl\", style=\"iqx\"\n", + " \"mpl\", style=\"clifford\"\n", ") # decompose 1 level: exposes the Phase estimation circuit!" ] }, @@ -349,7 +349,7 @@ "\n", "\n", "basis_gates = [\"h\", \"ry\", \"cry\", \"cx\", \"ccx\", \"p\", \"cp\", \"x\", \"s\", \"sdg\", \"y\", \"t\", \"cz\"]\n", - "transpile(ae_circuit, basis_gates=basis_gates, optimization_level=2).draw(\"mpl\", style=\"iqx\")" + "transpile(ae_circuit, basis_gates=basis_gates, optimization_level=2).draw(\"mpl\", style=\"clifford\")" ] }, { @@ -413,7 +413,7 @@ ], "source": [ "iae_circuit = iae.construct_circuit(problem, k=3)\n", - "iae_circuit.draw(\"mpl\", style=\"iqx\")" + "iae_circuit.draw(\"mpl\", style=\"clifford\")" ] }, { @@ -531,7 +531,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" @@ -547,7 +547,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, diff --git a/docs/tutorials/01_portfolio_optimization.ipynb b/docs/tutorials/01_portfolio_optimization.ipynb index c0384123..facac02d 100644 --- a/docs/tutorials/01_portfolio_optimization.ipynb +++ b/docs/tutorials/01_portfolio_optimization.ipynb @@ -400,7 +400,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/02_portfolio_diversification.ipynb b/docs/tutorials/02_portfolio_diversification.ipynb index 04a590d2..0fa929d5 100644 --- a/docs/tutorials/02_portfolio_diversification.ipynb +++ b/docs/tutorials/02_portfolio_diversification.ipynb @@ -787,7 +787,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/03_european_call_option_pricing.ipynb b/docs/tutorials/03_european_call_option_pricing.ipynb index 8b371422..aeb46c4d 100644 --- a/docs/tutorials/03_european_call_option_pricing.ipynb +++ b/docs/tutorials/03_european_call_option_pricing.ipynb @@ -739,7 +739,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/04_european_put_option_pricing.ipynb b/docs/tutorials/04_european_put_option_pricing.ipynb index 462e6a2b..26a33a52 100644 --- a/docs/tutorials/04_european_put_option_pricing.ipynb +++ b/docs/tutorials/04_european_put_option_pricing.ipynb @@ -497,7 +497,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/05_bull_spread_pricing.ipynb b/docs/tutorials/05_bull_spread_pricing.ipynb index 195bc683..98b6d394 100644 --- a/docs/tutorials/05_bull_spread_pricing.ipynb +++ b/docs/tutorials/05_bull_spread_pricing.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_basket_option_pricing.ipynb b/docs/tutorials/06_basket_option_pricing.ipynb index 82677513..2e71391a 100644 --- a/docs/tutorials/06_basket_option_pricing.ipynb +++ b/docs/tutorials/06_basket_option_pricing.ipynb @@ -583,7 +583,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/07_asian_barrier_spread_pricing.ipynb b/docs/tutorials/07_asian_barrier_spread_pricing.ipynb index a25cc670..a0b38f29 100644 --- a/docs/tutorials/07_asian_barrier_spread_pricing.ipynb +++ b/docs/tutorials/07_asian_barrier_spread_pricing.ipynb @@ -688,7 +688,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/08_fixed_income_pricing.ipynb b/docs/tutorials/08_fixed_income_pricing.ipynb index f05b4abf..6cd051d6 100644 --- a/docs/tutorials/08_fixed_income_pricing.ipynb +++ b/docs/tutorials/08_fixed_income_pricing.ipynb @@ -439,7 +439,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/09_credit_risk_analysis.ipynb b/docs/tutorials/09_credit_risk_analysis.ipynb index 9d71f1c0..f917f761 100644 --- a/docs/tutorials/09_credit_risk_analysis.ipynb +++ b/docs/tutorials/09_credit_risk_analysis.ipynb @@ -1272,7 +1272,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/10_qgan_option_pricing.ipynb b/docs/tutorials/10_qgan_option_pricing.ipynb index e2b36afc..4f6ff533 100644 --- a/docs/tutorials/10_qgan_option_pricing.ipynb +++ b/docs/tutorials/10_qgan_option_pricing.ipynb @@ -344,7 +344,7 @@ } ], "source": [ - "import qiskit.tools.jupyter\n", + "import tutorial_magics\n", "\n", "%qiskit_version_table\n", "%qiskit_copyright" diff --git a/docs/tutorials/11_time_series.ipynb b/docs/tutorials/11_time_series.ipynb index 62431a53..95d2d5ac 100644 --- a/docs/tutorials/11_time_series.ipynb +++ b/docs/tutorials/11_time_series.ipynb @@ -536,7 +536,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 00000000..22d88bdd --- /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 = "
" + html += "

This code is a part of a Qiskit project

" + 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.

" + html += "
" + 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 = "

Version Information

" + html += "" + html += "" + + 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"" + + html += "" + + sys_info = [ + ("Python version", platform.python_version()), + ("OS", "%s" % platform.system()), + ] + + for name, version in sys_info: + html += f"" + + html += "" % time.strftime("%a %b %d %H:%M:%S %Y %Z") + html += "
SoftwareVersion
{name}{version}
System information
{name}{version}
%s
" + + return display(HTML(html)) + + +_IP = get_ipython() +if _IP is not None: + _IP.register_magics(VersionTable) + _IP.register_magics(Copyright)