From 9c6a69aba43dd82d82602ae9ce8d7f6d07977833 Mon Sep 17 00:00:00 2001 From: Takashi Imamichi Date: Thu, 31 Aug 2023 12:05:25 +0900 Subject: [PATCH] convert migration guide for 0.5 to rst --- .../01_migration_guide_to_v0.5.ipynb | 523 ------------------ docs/migration/01_migration_guide_to_v0.5.rst | 422 ++++++++++++++ 2 files changed, 422 insertions(+), 523 deletions(-) delete mode 100644 docs/migration/01_migration_guide_to_v0.5.ipynb create mode 100644 docs/migration/01_migration_guide_to_v0.5.rst diff --git a/docs/migration/01_migration_guide_to_v0.5.ipynb b/docs/migration/01_migration_guide_to_v0.5.ipynb deleted file mode 100644 index 5c3b13c49..000000000 --- a/docs/migration/01_migration_guide_to_v0.5.ipynb +++ /dev/null @@ -1,523 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Qiskit Optimization v0.5 Migration Guide\n", - "\n", - "This tutorial will guide you through the process of migrating your code from Qiskit Optimization v0.4 to v0.5." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Overview\n", - "\n", - "Qiskit Terra v0.22 introduces new algorithm implementations that leverage [Qiskit Primitives](https://qiskit.org/documentation/apidoc/primitives.html) (Estimator and Sampler). The former algorithm implementations that leverage opflow will be deprecated in the future release.\n", - "\n", - "Qiskit Optimization v0.5 supports both the new and the former algorithms of Qiskit Terra v0.22 until the former algorithms are deprecated.\n", - "\n", - "It is not the intention to provide detailed explanations of the primitives in this migration guide. We suggest that you read the [corresponding resources](https://qiskit.org/documentation/apidoc/primitives.html) of the Qiskit Terra documentation instead.\n", - "\n", - "We use `qiskit.primitives.Sampler` in this guide as an example of Sampler implementation, which follows `qiskit.primitives.BaseSampler` interface. Users can also use other Sampler implementations such as `BackendSampler` (qiskit-terra), `AerSampler` (qiskit-aer), and Qiskit Runtime Sampler (qiskit-ibm-runtime)." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## `MinimumEigenOptimizer`\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The former algorithms exist in `qiskit.algorithms.minimum_eigen_solvers` and we can access them by `qiskit.algorithms.*`. On the other hand, the new algorithms exist in `qiskit.algorithms.minimum_eigensolvers` and we can access them by `qiskit.algorithms.minimum_eigensolvers.*`. Note that the difference is `minimum_eigen_solvers` (former) and `minimum_eigensolvers` (new).\n", - "\n", - "`MinimumEigenOptimizer` of Qiskit Optimization can use `qiskit.algorithms.MinimumEigenSolver` interface of the former algorithms and \n", - "`qiskit.algorithms.minimum_eigensolvers.SamplingMinimumEigensolver` interface of the new algorithms.\n", - "Note that `MinimumEigenOptimizer` cannot basically handle `qiskit.algorithms.minimum_eigensolvers.MinimumEigensolver` of the new algorithms.\n", - "But there is an exception. `MinimumEigenOptimizer` can handle `algorithms.minimum_eigensolver.NumPyMinimumEigensolver` though it inherits `qiskit.algorithms.minimum_eigensolvers.MinimumEigensolver`. It is because `algorithms.minimum_eigensolver.NumPyMinimumEigensolver` has an extension that allows users to access the eigen states.\n", - "\n", - "The following is the corresponding table.\n", - "\n", - "|Former algorithm | New algorithm | \n", - "|-----------------|:--------------|\n", - "|`qiskit.algorithms.MinimumEigenSolver`|`qiskit.algorithms.minimum_eigensolvers.SamplingMinimumEigensolver`|\n", - "|`qiskit.algorithms.NumPyMinimumEigensolver`|`qiskit.algorithms.minimum_eigensolver.NumPyMinimumEigensolver`|\n", - "|`qiskit.algorithms.QAOA`|`qiskit.algorithms.minimum_eigensolvers.QAOA`|\n", - "|`qiskit.algorithms.VQE`|`qiskit.algorithms.minimum_eigensolvers.SamplingVQE`|" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Setup of a problem" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Problem name: sample\n", - "\n", - "Maximize\n", - " x - 2*y\n", - "\n", - "Subject to\n", - " No constraints\n", - "\n", - " Binary variables (2)\n", - " x y\n", - "\n" - ] - } - ], - "source": [ - "from qiskit_optimization import QuadraticProgram\n", - "\n", - "problem = QuadraticProgram(\"sample\")\n", - "problem.binary_var(\"x\")\n", - "problem.binary_var(\"y\")\n", - "problem.maximize(linear={\"x\": 1, \"y\": -2})\n", - "print(problem.prettyprint())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### NumPyMinimumEigensolver\n", - "\n", - "Previously" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "```python\n", - "from qiskit.algorithms import NumPyMinimumEigensolver\n", - "\n", - "from qiskit_optimization.algorithms import MinimumEigenOptimizer\n", - "\n", - "mes = NumPyMinimumEigensolver()\n", - "meo = MinimumEigenOptimizer(min_eigen_solver=mes)\n", - "result = meo.solve(problem)\n", - "print(result)\n", - "```" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "New" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "fval=1.0, x=1.0, y=0.0, status=SUCCESS\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/var/folders/lp/wqsk49dx0y595_730yr48twh0000gn/T/ipykernel_41632/3429859093.py:1: DeprecationWarning: ``qiskit.algorithms`` has been migrated to an independent package: https://github.com/qiskit-community/qiskit-algorithms. The ``qiskit.algorithms`` import path is deprecated as of qiskit-terra 0.25.0 and will be removed no earlier than 3 months after the release date. Please run ``pip install qiskit_algorithms`` and use ``import qiskit_algorithms`` instead.\n", - " from qiskit.algorithms.minimum_eigensolvers import NumPyMinimumEigensolver\n" - ] - } - ], - "source": [ - "from qiskit.algorithms.minimum_eigensolvers import NumPyMinimumEigensolver\n", - "\n", - "from qiskit_optimization.algorithms import MinimumEigenOptimizer\n", - "\n", - "mes = NumPyMinimumEigensolver()\n", - "meo = MinimumEigenOptimizer(min_eigen_solver=mes)\n", - "result = meo.solve(problem)\n", - "print(result)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### QAOA\n", - "\n", - "Previously" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "```python\n", - "from qiskit import BasicAer\n", - "from qiskit.algorithms import QAOA\n", - "from qiskit.algorithms.optimizers import COBYLA\n", - "from qiskit.utils import QuantumInstance\n", - "\n", - "from qiskit_optimization.algorithms import MinimumEigenOptimizer\n", - "\n", - "backend = BasicAer.get_backend(\"qasm_simulator\")\n", - "shots = 1000\n", - "qins = QuantumInstance(backend=backend, shots=shots)\n", - "mes = QAOA(optimizer=COBYLA(), quantum_instance=qins)\n", - "meo = MinimumEigenOptimizer(min_eigen_solver=mes)\n", - "result = meo.solve(problem)\n", - "print(result)\n", - "```" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "New" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "fval=1.0, x=1.0, y=0.0, status=SUCCESS\n" - ] - } - ], - "source": [ - "from qiskit.algorithms.minimum_eigensolvers import QAOA\n", - "from qiskit.algorithms.optimizers import COBYLA\n", - "from qiskit.primitives import Sampler\n", - "\n", - "from qiskit_optimization.algorithms import MinimumEigenOptimizer\n", - "\n", - "shots = 1000\n", - "mes = QAOA(sampler=Sampler(), optimizer=COBYLA())\n", - "meo = MinimumEigenOptimizer(min_eigen_solver=mes)\n", - "result = meo.solve(problem)\n", - "print(result)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### VQE (former) → SamplingVQE (new)\n", - "\n", - "Previously" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "```python\n", - "from qiskit import BasicAer\n", - "from qiskit.algorithms import VQE\n", - "from qiskit.algorithms.optimizers import COBYLA\n", - "from qiskit.circuit.library import RealAmplitudes\n", - "from qiskit.utils import QuantumInstance\n", - "\n", - "from qiskit_optimization.algorithms import MinimumEigenOptimizer\n", - "\n", - "backend = BasicAer.get_backend(\"qasm_simulator\")\n", - "shots = 1000\n", - "qins = QuantumInstance(backend=backend, shots=shots)\n", - "mes = VQE(ansatz=RealAmplitudes(), optimizer=COBYLA(), quantum_instance=qins)\n", - "meo = MinimumEigenOptimizer(min_eigen_solver=mes)\n", - "result = meo.solve(problem)\n", - "print(result)\n", - "```" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "New" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "fval=1.0, x=1.0, y=0.0, status=SUCCESS\n" - ] - } - ], - "source": [ - "from qiskit.algorithms.minimum_eigensolvers import SamplingVQE\n", - "from qiskit.algorithms.optimizers import COBYLA\n", - "from qiskit.circuit.library import RealAmplitudes\n", - "from qiskit.primitives import Sampler\n", - "\n", - "from qiskit_optimization.algorithms import MinimumEigenOptimizer\n", - "\n", - "mes = SamplingVQE(sampler=Sampler(), ansatz=RealAmplitudes(), optimizer=COBYLA())\n", - "meo = MinimumEigenOptimizer(min_eigen_solver=mes)\n", - "result = meo.solve(problem)\n", - "print(result)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "An error occurs due to `VQE` with `Estimator`. You can use `SamplingVQE` with `Sampler` instead (see the previous cell)." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "MinimumEigenOptimizer does not support this VQE. You can use qiskit.algorithms.minimum_eigensolvers.SamplingVQE instead.\n" - ] - } - ], - "source": [ - "from qiskit.algorithms.minimum_eigensolvers import VQE\n", - "from qiskit.algorithms.optimizers import COBYLA\n", - "from qiskit.circuit.library import RealAmplitudes\n", - "from qiskit.primitives import Estimator\n", - "\n", - "from qiskit_optimization.algorithms import MinimumEigenOptimizer\n", - "\n", - "mes = VQE(estimator=Estimator(), ansatz=RealAmplitudes(), optimizer=COBYLA())\n", - "try:\n", - " meo = MinimumEigenOptimizer(min_eigen_solver=mes)\n", - "except TypeError as ex:\n", - " print(ex)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## `WarmStartQAOAOptimizer`" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`WarmStartQAOAOptimizer` can use both the former `qiskit.algorithms.QAOA` and the new `qiskit.algorithms.minimum_eigensolvers.QAOA` as follows." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Previously" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "```python\n", - "from qiskit import BasicAer\n", - "from qiskit.algorithms import QAOA\n", - "from qiskit.algorithms.optimizers import COBYLA\n", - "from qiskit.utils import QuantumInstance\n", - "\n", - "from qiskit_optimization.algorithms import WarmStartQAOAOptimizer, SlsqpOptimizer\n", - "\n", - "backend = BasicAer.get_backend(\"qasm_simulator\")\n", - "shots = 1000\n", - "qins = QuantumInstance(backend=backend, shots=shots)\n", - "qaoa = QAOA(optimizer=COBYLA(), quantum_instance=qins)\n", - "optimizer = WarmStartQAOAOptimizer(\n", - " pre_solver=SlsqpOptimizer(), relax_for_pre_solver=True, qaoa=qaoa, epsilon=0.25\n", - ")\n", - "result = optimizer.solve(problem)\n", - "print(result)\n", - "```" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "New" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "fval=1.0, x=1.0, y=0.0, status=SUCCESS\n" - ] - } - ], - "source": [ - "from qiskit.algorithms.minimum_eigensolvers import QAOA\n", - "from qiskit.algorithms.optimizers import COBYLA\n", - "from qiskit.primitives import Sampler\n", - "\n", - "from qiskit_optimization.algorithms import WarmStartQAOAOptimizer, SlsqpOptimizer\n", - "\n", - "qaoa = QAOA(sampler=Sampler(), optimizer=COBYLA())\n", - "optimizer = WarmStartQAOAOptimizer(\n", - " pre_solver=SlsqpOptimizer(), relax_for_pre_solver=True, qaoa=qaoa, epsilon=0.25\n", - ")\n", - "result = optimizer.solve(problem)\n", - "print(result)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## `GroverOptimizer`" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`GroverOptimizer` supports `BaseSampler` instead of `QuantumInstance`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Previously" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "```python\n", - "from qiskit import BasicAer\n", - "from qiskit.algorithms.optimizers import COBYLA\n", - "from qiskit.utils import QuantumInstance\n", - "\n", - "from qiskit_optimization.algorithms import GroverOptimizer\n", - "\n", - "backend = BasicAer.get_backend(\"qasm_simulator\")\n", - "shots = 1000\n", - "qins = QuantumInstance(backend=backend, shots=shots)\n", - "optimizer = GroverOptimizer(num_value_qubits=3, num_iterations=3, quantum_instance=qins)\n", - "result = optimizer.solve(problem)\n", - "print(result)\n", - "```" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "New" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "fval=1.0, x=1.0, y=0.0, status=SUCCESS\n" - ] - } - ], - "source": [ - "from qiskit.algorithms.optimizers import COBYLA\n", - "from qiskit.primitives import Sampler\n", - "\n", - "from qiskit_optimization.algorithms import GroverOptimizer\n", - "\n", - "optimizer = GroverOptimizer(num_value_qubits=3, num_iterations=3, sampler=Sampler())\n", - "result = optimizer.solve(problem)\n", - "print(result)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "

Version Information

SoftwareVersion
qiskitNone
qiskit-terra0.45.0.dev0+0a0e262
qiskit_optimization0.6.0
System information
Python version3.10.13
Python compilerClang 14.0.3 (clang-1403.0.22.14.1)
Python buildmain, Aug 24 2023 22:36:46
OSDarwin
CPUs10
Memory (Gb)64.0
Wed Aug 30 23:18:15 2023 JST
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "

This code is a part of Qiskit

© Copyright IBM 2017, 2023.

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.

" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import qiskit.tools.jupyter\n", - "\n", - "%qiskit_version_table\n", - "%qiskit_copyright" - ] - } - ], - "metadata": { - "language_info": { - "name": "python" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/docs/migration/01_migration_guide_to_v0.5.rst b/docs/migration/01_migration_guide_to_v0.5.rst new file mode 100644 index 000000000..97dd3caca --- /dev/null +++ b/docs/migration/01_migration_guide_to_v0.5.rst @@ -0,0 +1,422 @@ +Qiskit Optimization v0.5 Migration Guide +======================================== + +This tutorial will guide you through the process of migrating your code +from Qiskit Optimization v0.4 to v0.5. + +Overview +-------- + +Qiskit Terra v0.22 introduces new algorithm implementations that +leverage `Qiskit +Primitives `__ +(Estimator and Sampler). The former algorithm implementations that +leverage opflow will be deprecated in the future release. + +Qiskit Optimization v0.5 supports both the new and the former algorithms +of Qiskit Terra v0.22 until the former algorithms are deprecated. + +It is not the intention to provide detailed explanations of the +primitives in this migration guide. We suggest that you read the +`corresponding +resources `__ +of the Qiskit Terra documentation instead. + +We use ``qiskit.primitives.Sampler`` in this guide as an example of +Sampler implementation, which follows ``qiskit.primitives.BaseSampler`` +interface. Users can also use other Sampler implementations such as +``BackendSampler`` (qiskit-terra), ``AerSampler`` (qiskit-aer), and +Qiskit Runtime Sampler (qiskit-ibm-runtime). + +``MinimumEigenOptimizer`` +------------------------- + +The former algorithms exist in +``qiskit.algorithms.minimum_eigen_solvers`` and we can access them by +``qiskit.algorithms.*``. On the other hand, the new algorithms exist in +``qiskit.algorithms.minimum_eigensolvers`` and we can access them by +``qiskit.algorithms.minimum_eigensolvers.*``. Note that the difference +is ``minimum_eigen_solvers`` (former) and ``minimum_eigensolvers`` +(new). + +``MinimumEigenOptimizer`` of Qiskit Optimization can use +``qiskit.algorithms.MinimumEigenSolver`` interface of the former +algorithms and +``qiskit.algorithms.minimum_eigensolvers.SamplingMinimumEigensolver`` +interface of the new algorithms. Note that ``MinimumEigenOptimizer`` +cannot basically handle +``qiskit.algorithms.minimum_eigensolvers.MinimumEigensolver`` of the new +algorithms. But there is an exception. ``MinimumEigenOptimizer`` can +handle ``algorithms.minimum_eigensolver.NumPyMinimumEigensolver`` though +it inherits +``qiskit.algorithms.minimum_eigensolvers.MinimumEigensolver``. It is +because ``algorithms.minimum_eigensolver.NumPyMinimumEigensolver`` has +an extension that allows users to access the eigen states. + +The following is the corresponding table. + ++-------------------------------------+--------------------------------+ +| Former algorithm | New algorithm | ++=====================================+================================+ +| ``qis | ``qiskit. | +| kit.algorithms.MinimumEigenSolver`` | algorithms.minimum_eigensolver | +| | s.SamplingMinimumEigensolver`` | ++-------------------------------------+--------------------------------+ +| ``qiskit.a | ``qis | +| lgorithms.NumPyMinimumEigensolver`` | kit.algorithms.minimum_eigenso | +| | lver.NumPyMinimumEigensolver`` | ++-------------------------------------+--------------------------------+ +| ``qiskit.algorithms.QAOA`` | ``qiskit.algorith | +| | ms.minimum_eigensolvers.QAOA`` | ++-------------------------------------+--------------------------------+ +| ``qiskit.algorithms.VQE`` | ``qiskit.algorithms.mini | +| | mum_eigensolvers.SamplingVQE`` | ++-------------------------------------+--------------------------------+ + +Setup of a problem + +.. code:: python + + from qiskit_optimization import QuadraticProgram + + problem = QuadraticProgram("sample") + problem.binary_var("x") + problem.binary_var("y") + problem.maximize(linear={"x": 1, "y": -2}) + print(problem.prettyprint()) + + +.. parsed-literal:: + + Problem name: sample + + Maximize + x - 2*y + + Subject to + No constraints + + Binary variables (2) + x y + + + +NumPyMinimumEigensolver +~~~~~~~~~~~~~~~~~~~~~~~ + +Previously + +.. code:: python + + from qiskit.algorithms import NumPyMinimumEigensolver + + from qiskit_optimization.algorithms import MinimumEigenOptimizer + + mes = NumPyMinimumEigensolver() + meo = MinimumEigenOptimizer(min_eigen_solver=mes) + result = meo.solve(problem) + print(result) + + +.. parsed-literal:: + + fval=1.0, x=1.0, y=0.0, status=SUCCESS + + +New + +.. code:: python + + from qiskit.algorithms.minimum_eigensolvers import NumPyMinimumEigensolver + + from qiskit_optimization.algorithms import MinimumEigenOptimizer + + mes = NumPyMinimumEigensolver() + meo = MinimumEigenOptimizer(min_eigen_solver=mes) + result = meo.solve(problem) + print(result) + + +.. parsed-literal:: + + fval=1.0, x=1.0, y=0.0, status=SUCCESS + + +QAOA +~~~~ + +Previously + +.. code:: python + + from qiskit import BasicAer + from qiskit.algorithms import QAOA + from qiskit.algorithms.optimizers import COBYLA + from qiskit.utils import QuantumInstance + + from qiskit_optimization.algorithms import MinimumEigenOptimizer + + backend = BasicAer.get_backend("qasm_simulator") + shots = 1000 + qins = QuantumInstance(backend=backend, shots=shots) + mes = QAOA(optimizer=COBYLA(), quantum_instance=qins) + meo = MinimumEigenOptimizer(min_eigen_solver=mes) + result = meo.solve(problem) + print(result) + + +.. parsed-literal:: + + fval=1.0, x=1.0, y=0.0, status=SUCCESS + + +New + +.. code:: python + + from qiskit.algorithms.minimum_eigensolvers import QAOA + from qiskit.algorithms.optimizers import COBYLA + from qiskit.primitives import Sampler + + from qiskit_optimization.algorithms import MinimumEigenOptimizer + + shots = 1000 + mes = QAOA(sampler=Sampler(), optimizer=COBYLA()) + meo = MinimumEigenOptimizer(min_eigen_solver=mes) + result = meo.solve(problem) + print(result) + + +.. parsed-literal:: + + fval=1.0, x=1.0, y=0.0, status=SUCCESS + + +VQE (former) → SamplingVQE (new) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Previously + +.. code:: python + + from qiskit import BasicAer + from qiskit.algorithms import VQE + from qiskit.algorithms.optimizers import COBYLA + from qiskit.circuit.library import RealAmplitudes + from qiskit.utils import QuantumInstance + + from qiskit_optimization.algorithms import MinimumEigenOptimizer + + backend = BasicAer.get_backend("qasm_simulator") + shots = 1000 + qins = QuantumInstance(backend=backend, shots=shots) + mes = VQE(ansatz=RealAmplitudes(), optimizer=COBYLA(), quantum_instance=qins) + meo = MinimumEigenOptimizer(min_eigen_solver=mes) + result = meo.solve(problem) + print(result) + + +.. parsed-literal:: + + fval=1.0, x=1.0, y=0.0, status=SUCCESS + + +New + +.. code:: python + + from qiskit.algorithms.minimum_eigensolvers import SamplingVQE + from qiskit.algorithms.optimizers import COBYLA + from qiskit.circuit.library import RealAmplitudes + from qiskit.primitives import Sampler + + from qiskit_optimization.algorithms import MinimumEigenOptimizer + + mes = SamplingVQE(sampler=Sampler(), ansatz=RealAmplitudes(), optimizer=COBYLA()) + meo = MinimumEigenOptimizer(min_eigen_solver=mes) + result = meo.solve(problem) + print(result) + + +.. parsed-literal:: + + fval=1.0, x=1.0, y=0.0, status=SUCCESS + + +An error occurs due to ``VQE`` with ``Estimator``. You can use +``SamplingVQE`` with ``Sampler`` instead (see the previous cell). + +.. code:: python + + from qiskit.algorithms.minimum_eigensolvers import VQE + from qiskit.algorithms.optimizers import COBYLA + from qiskit.circuit.library import RealAmplitudes + from qiskit.primitives import Estimator + + from qiskit_optimization.algorithms import MinimumEigenOptimizer + + mes = VQE(estimator=Estimator(), ansatz=RealAmplitudes(), optimizer=COBYLA()) + try: + meo = MinimumEigenOptimizer(min_eigen_solver=mes) + except TypeError as ex: + print(ex) + + +.. parsed-literal:: + + MinimumEigenOptimizer does not support this VQE. You can use qiskit.algorithms.minimum_eigensolvers.SamplingVQE instead. + + +``WarmStartQAOAOptimizer`` +-------------------------- + +``WarmStartQAOAOptimizer`` can use both the former +``qiskit.algorithms.QAOA`` and the new +``qiskit.algorithms.minimum_eigensolvers.QAOA`` as follows. + +Previously + +.. code:: python + + from qiskit import BasicAer + from qiskit.algorithms import QAOA + from qiskit.algorithms.optimizers import COBYLA + from qiskit.utils import QuantumInstance + + from qiskit_optimization.algorithms import WarmStartQAOAOptimizer, SlsqpOptimizer + + backend = BasicAer.get_backend("qasm_simulator") + shots = 1000 + qins = QuantumInstance(backend=backend, shots=shots) + qaoa = QAOA(optimizer=COBYLA(), quantum_instance=qins) + optimizer = WarmStartQAOAOptimizer( + pre_solver=SlsqpOptimizer(), relax_for_pre_solver=True, qaoa=qaoa, epsilon=0.25 + ) + result = optimizer.solve(problem) + print(result) + + +.. parsed-literal:: + + fval=1.0, x=1.0, y=0.0, status=SUCCESS + + +New + +.. code:: python + + from qiskit.algorithms.minimum_eigensolvers import QAOA + from qiskit.algorithms.optimizers import COBYLA + from qiskit.primitives import Sampler + + from qiskit_optimization.algorithms import WarmStartQAOAOptimizer, SlsqpOptimizer + + qaoa = QAOA(sampler=Sampler(), optimizer=COBYLA()) + optimizer = WarmStartQAOAOptimizer( + pre_solver=SlsqpOptimizer(), relax_for_pre_solver=True, qaoa=qaoa, epsilon=0.25 + ) + result = optimizer.solve(problem) + print(result) + + +.. parsed-literal:: + + fval=1.0, x=1.0, y=0.0, status=SUCCESS + + +``GroverOptimizer`` +------------------- + +``GroverOptimizer`` supports both ``QuantumInstance`` and +``BaseSampler``. But users must specify one of them. + +Previously + +.. code:: python + + from qiskit import BasicAer + from qiskit.algorithms.optimizers import COBYLA + from qiskit.utils import QuantumInstance + + from qiskit_optimization.algorithms import GroverOptimizer + + backend = BasicAer.get_backend("qasm_simulator") + shots = 1000 + qins = QuantumInstance(backend=backend, shots=shots) + optimizer = GroverOptimizer(num_value_qubits=3, num_iterations=3, quantum_instance=qins) + result = optimizer.solve(problem) + print(result) + + +.. parsed-literal:: + + fval=1.0, x=1.0, y=0.0, status=SUCCESS + + +New + +.. code:: python + + from qiskit.algorithms.optimizers import COBYLA + from qiskit.primitives import Sampler + + from qiskit_optimization.algorithms import GroverOptimizer + + optimizer = GroverOptimizer(num_value_qubits=3, num_iterations=3, sampler=Sampler()) + result = optimizer.solve(problem) + print(result) + + +.. parsed-literal:: + + fval=0.0, x=0.0, y=0.0, status=SUCCESS + + +An error occurs because both ``quantum_instance`` and ``sampler`` are +set. You can set only one of them. + +.. code:: python + + from qiskit import BasicAer + from qiskit.algorithms.optimizers import COBYLA + from qiskit.utils import QuantumInstance + from qiskit.primitives import Sampler + + from qiskit_optimization.algorithms import GroverOptimizer + + backend = BasicAer.get_backend("qasm_simulator") + shots = 1000 + qins = QuantumInstance(backend=backend, shots=shots) + try: + optimizer = GroverOptimizer( + num_value_qubits=3, num_iterations=3, quantum_instance=qins, sampler=Sampler() + ) + # raises an error because both quantum_instance and sampler are set. + except ValueError as ex: + print(ex) + + +.. parsed-literal:: + + Only one of quantum_instance or sampler can be passed, not both! + + +.. code:: python + + import qiskit.tools.jupyter + + %qiskit_version_table + %qiskit_copyright + + + +.. raw:: html + +

Version Information

Qiskit SoftwareVersion
qiskit-terra0.23.0
qiskit-aer0.11.1
qiskit-optimization0.5.0
qiskit-machine-learning0.6.0
System information
Python version3.9.15
Python compilerClang 14.0.0 (clang-1400.0.29.102)
Python buildmain, Oct 11 2022 22:27:25
OSDarwin
CPUs4
Memory (Gb)16.0
Tue Dec 06 22:08:13 2022 JST
+ + + +.. raw:: html + +

This code is a part of Qiskit

© Copyright IBM 2017, 2022.

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.

+