Skip to content

Commit

Permalink
Switch from qiskit.algorithms to qiskit_algorithms (qiskit-commun…
Browse files Browse the repository at this point in the history
…ity#1252)

* refactor: greedily replace qiskit.algorithms with qiskit_algorithms

This will still fail some tests because of isinstance checks but I am
using the online CI to tell me where it fails.

* fix: isinstance checks against qiskit_algorithms classes

We need to look at the full hierarchy of subclasses to properly do this.

* docs: add explanatory comment for the previous commits' workarond

* Update qiskit_algorithms import locations and docs references

* Use untyped version of qiskit-algorithms

* fix: remove unused type ignore comments

* docs: update docs to use qiskit_algorithms

* test: update tests to use qiskit_algorithms

* fix: minor lint and syntax fixes

* Update the use of algorithm_globals

- removes the use of `num_processes` because nowadays,
  `qiskit.tools.parallel_map` has more means to configure the number of
  parallel processes rendering `algorithm_globals.num_processes`
  obsolete

- replaces all other usage of `qiskit.utils.algorithm_globals` with
  `qiskit_algorithms.utils.algorithm_globals`

* docs: update AdaptVQE guide introduction

* Use validate_min from qiskit-algorithms
  • Loading branch information
mrossinek authored Sep 15, 2023
1 parent 5fcc9e6 commit eb30b6b
Show file tree
Hide file tree
Showing 51 changed files with 128 additions and 136 deletions.
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
"numpy": ("https://numpy.org/doc/stable/", None),
"scipy": ("https://docs.scipy.org/doc/scipy/", None),
"qiskit": ("https://qiskit.org/documentation/", None),
"qiskit-algorithms": ("https://qiskit.org/ecosystem/algorithms/", None),
"rustworkx": ("https://qiskit.org/ecosystem/rustworkx/", None),
"sparse": ("https://sparse.pydata.org/en/stable/", None),
}
Expand Down
12 changes: 6 additions & 6 deletions docs/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Ready to get going?...
======================

Now that Qiskit Nature is installed, let's try a chemistry application experiment
using the :class:`~qiskit.algorithms.minimum_eigensolvers.VQE` (Variational
using the :class:`~qiskit_algorithms.VQE` (Variational
Quantum Eigensolver) algorithm to compute the ground-state (minimum) energy of a
molecule.

Expand All @@ -146,7 +146,7 @@ molecule.
mapper = ParityMapper(num_particles=problem.num_particles)

# setup the classical optimizer for the VQE
from qiskit.algorithms.optimizers import L_BFGS_B
from qiskit_algorithms.optimizers import L_BFGS_B

optimizer = L_BFGS_B()

Expand All @@ -170,7 +170,7 @@ molecule.
)

# set up our actual VQE instance
from qiskit.algorithms.minimum_eigensolvers import VQE
from qiskit_algorithms import VQE

vqe = VQE(estimator, ansatz, optimizer)
# ensure that the optimizer starts in the all-zero state which corresponds to
Expand Down Expand Up @@ -224,11 +224,11 @@ are provided to it; a reduction in complexity that is particularly advantageous
for NISQ computers.

For actually finding the ground state solution, the Variational Quantum
Eigensolver (:class:`~qiskit.algorithms.minimum_eigensolvers.VQE`) algorithm is
Eigensolver (:class:`~qiskit_algorithms.VQE`) algorithm is
used. Its main three components are the estimator primitive
(:class:`~qiskit.primitives.Estimator`), wavefunction ansatz
(:class:`~qiskit_nature.second_q.circuit.library.UCCSD`), and optimizer
(:class:`~qiskit.algorithms.optimizers.L_BFGS_B`).
(:class:`~qiskit_algorithms.optimizers.L_BFGS_B`).
The :class:`~qiskit_nature.second_q.circuit.library.UCCSD` component is the only
one provided directly by Qiskit Nature and it is usually paired with the
:class:`~qiskit_nature.second_q.circuit.library.HartreeFock` initial state and
Expand All @@ -237,7 +237,7 @@ an all-zero initial point for the optimizer.
The entire problem is then solved using a
:class:`~qiskit_nature.second_q.algorithms.GroundStateEigensolver` which wraps
both, the :class:`~qiskit_nature.second_q.mappers.ParityMapper` and
:class:`~qiskit.algorithms.minimum_eigensolvers.VQE`. Since an
:class:`~qiskit_algorithms.VQE`. Since an
:class:`~qiskit_nature.second_q.problems.ElectronicStructureProblem` is provided
to it (which was the output of the
:class:`~qiskit_nature.second_q.drivers.PySCFDriver`) it also returns an
Expand Down
15 changes: 7 additions & 8 deletions docs/howtos/adapt_vqe.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
Find ground state energy using AdaptVQE
=======================================

As of Qiskit Nature v0.5, the :class:`~qiskit.algorithms.minimum_eigensolvers.AdaptVQE`
algorithm has been migrated to Qiskit Terra (released in v0.22).
This guide outlines how the :class:`~qiskit_algorithms.AdaptVQE` algorithm can
be used to find the ground state solutions of natural science problems.

This tutorial outlines how the algorithm can be used.

1. We obtain an :class:`~qiskit_nature.second_q.problems.ElectronicStructureProblem`
which we want to solve:
Expand Down Expand Up @@ -38,22 +37,22 @@ This tutorial outlines how the algorithm can be used.
),
)

4. We setup a :class:`~qiskit.algorithms.minimum_eigensolvers.VQE`:
4. We setup a :class:`~qiskit_algorithms.VQE`:

.. testcode::

import numpy as np
from qiskit.algorithms.optimizers import SLSQP
from qiskit.algorithms.minimum_eigensolvers import VQE
from qiskit_algorithms import VQE
from qiskit_algorithms.optimizers import SLSQP
from qiskit.primitives import Estimator
vqe = VQE(Estimator(), ansatz, SLSQP())
vqe.initial_point = np.zeros(ansatz.num_parameters)

5. We setup the :class:`~qiskit.algorithms.minimum_eigensolvers.AdaptVQE`:
5. We setup the :class:`~qiskit_algorithms.AdaptVQE`:

.. testcode::

from qiskit.algorithms.minimum_eigensolvers import AdaptVQE
from qiskit_algorithms import AdaptVQE
adapt_vqe = AdaptVQE(vqe)
adapt_vqe.supports_aux_operators = lambda: True # temporary fix

Expand Down
8 changes: 4 additions & 4 deletions docs/howtos/numpy_eigensolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Find excited state energies using the NumPyEigensolver
======================================================

In order to ensure a physically meaningful excited states of a hamiltonian are found when using the
:class:`~qiskit.algorithms.eigensolvers.NumPyEigensolver` one needs to set the
:attr:`~qiskit.algorithms.eigensolvers.NumPyEigensolver.filter_criterion` attribute
:class:`~qiskit_algorithms.NumPyEigensolver` one needs to set the
:attr:`~qiskit_algorithms.NumPyEigensolver.filter_criterion` attribute
of the solver.

Subclasses of :class:`~qiskit_nature.second_q.problems.BaseProblem` in Qiskit Nature provide the
Expand All @@ -30,11 +30,11 @@ Below we show how you can use this setting.
from qiskit_nature.second_q.mappers import JordanWignerMapper
mapper = JordanWignerMapper()

3. We setup our :class:`~qiskit.algorithms.eigensolvers.NumPyEigensolver`:
3. We setup our :class:`~qiskit_algorithms.NumPyEigensolver`:

.. testcode::

from qiskit.algorithms.eigensolvers import NumPyEigensolver
from qiskit_algorithms import NumPyEigensolver
algo = NumPyEigensolver(k=100)
algo.filter_criterion = problem.get_default_filter_criterion()

Expand Down
8 changes: 4 additions & 4 deletions docs/howtos/numpy_minimum_eigensolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Find ground state energy using the NumPyMinimumEigensolver
==========================================================

In order to ensure a physically meaningful ground state of a hamiltonian is found when using the
:class:`~qiskit.algorithms.minimum_eigensolvers.NumPyMinimumEigensolver` one needs to set the
:attr:`~qiskit.algorithms.minimum_eigensolvers.NumPyMinimumEigensolver.filter_criterion` attribute
:class:`~qiskit_algorithms.NumPyMinimumEigensolver` one needs to set the
:attr:`~qiskit_algorithms.NumPyMinimumEigensolver.filter_criterion` attribute
of the solver.

Subclasses of :class:`~qiskit_nature.second_q.problems.BaseProblem` in Qiskit Nature provide the
Expand All @@ -30,11 +30,11 @@ Below we show how you can use this setting.
from qiskit_nature.second_q.mappers import JordanWignerMapper
mapper = JordanWignerMapper()

3. We setup our :class:`~qiskit.algorithms.minimum_eigensolvers.NumPyMinimumEigensolver`:
3. We setup our :class:`~qiskit_algorithms.minimum_eigensolvers.NumPyMinimumEigensolver`:

.. testcode::

from qiskit.algorithms.minimum_eigensolvers import NumPyMinimumEigensolver
from qiskit_algorithms import NumPyMinimumEigensolver
algo = NumPyMinimumEigensolver()
algo.filter_criterion = problem.get_default_filter_criterion()

Expand Down
10 changes: 5 additions & 5 deletions docs/howtos/vqe_ucc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Use a UCC-like ansatz with a VQE
================================

When using a :class:`~qiskit_nature.second_q.circuit.library.UCC`-style ansatz with a
:class:`~qiskit.algorithms.minimum_eigensolvers.VQE` one needs to pay particular attention to the
:attr:`~qiskit.algorithms.minimum_eigensolvers.VQE.initial_point` attribute which indicates from
:class:`~qiskit_algorithms.VQE` one needs to pay particular attention to the
:attr:`~qiskit_algorithms.VQE.initial_point` attribute which indicates from
which set of initial parameters the optimization routine should start.
By default, VQE will start from a *random* initial point. In this how to we show how one
can set a custom initial point instead (for example to guarantee that one starts from the
Expand Down Expand Up @@ -43,13 +43,13 @@ Hartree-Fock state).
),
)

4. We setup a :class:`~qiskit.algorithms.minimum_eigensolvers.VQE`:
4. We setup a :class:`~qiskit_algorithms.VQE`:

.. testcode::

import numpy as np
from qiskit.algorithms.optimizers import SLSQP
from qiskit.algorithms.minimum_eigensolvers import VQE
from qiskit_algorithms import VQE
from qiskit_algorithms.optimizers import SLSQP
from qiskit.primitives import Estimator
vqe = VQE(Estimator(), ansatz, SLSQP())

Expand Down
10 changes: 5 additions & 5 deletions docs/howtos/vqe_uvcc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Use a UVCC-like ansatz with a VQE
=================================

When using a :class:`~qiskit_nature.second_q.circuit.library.UVCC`-style ansatz with a
:class:`~qiskit.algorithms.minimum_eigensolvers.VQE` one needs to pay particular attention to the
:attr:`~qiskit.algorithms.minimum_eigensolvers.VQE.initial_point` attribute which indicates from
:class:`~qiskit_algorithms.VQE` one needs to pay particular attention to the
:attr:`~qiskit_algorithms.VQE.initial_point` attribute which indicates from
which set of initial parameters the optimization routine should start.
By default, VQE will start from a *random* initial point. In this how to we show how one
can set a custom initial point instead (for example to guarantee that one starts from the
Expand Down Expand Up @@ -40,13 +40,13 @@ we will simply show how to use the
),
)

3. We setup a :class:`~qiskit.algorithms.minimum_eigensolvers.VQE`:
3. We setup a :class:`~qiskit_algorithms.VQE`:

.. testcode::

import numpy as np
from qiskit.algorithms.optimizers import SLSQP
from qiskit.algorithms.minimum_eigensolvers import VQE
from qiskit_algorithms import VQE
from qiskit_algorithms.optimizers import SLSQP
from qiskit.primitives import Estimator
vqe = VQE(Estimator(), ansatz, SLSQP())

Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/01_electronic_structure.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@
"metadata": {},
"outputs": [],
"source": [
"from qiskit.algorithms.minimum_eigensolvers import NumPyMinimumEigensolver\n",
"from qiskit_algorithms import NumPyMinimumEigensolver\n",
"from qiskit_nature.second_q.algorithms import GroundStateEigensolver\n",
"from qiskit_nature.second_q.mappers import JordanWignerMapper\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/02_vibrational_structure.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@
"metadata": {},
"outputs": [],
"source": [
"from qiskit.algorithms.minimum_eigensolvers import NumPyMinimumEigensolver\n",
"from qiskit_algorithms import NumPyMinimumEigensolver\n",
"from qiskit_nature.second_q.algorithms import GroundStateEigensolver\n",
"\n",
"solver = GroundStateEigensolver(\n",
Expand Down
12 changes: 6 additions & 6 deletions docs/tutorials/03_ground_state_solvers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"metadata": {},
"outputs": [],
"source": [
"from qiskit.algorithms.minimum_eigensolvers import NumPyMinimumEigensolver\n",
"from qiskit_algorithms import NumPyMinimumEigensolver\n",
"\n",
"numpy_solver = NumPyMinimumEigensolver()"
]
Expand All @@ -99,8 +99,8 @@
"metadata": {},
"outputs": [],
"source": [
"from qiskit.algorithms.minimum_eigensolvers import VQE\n",
"from qiskit.algorithms.optimizers import SLSQP\n",
"from qiskit_algorithms import VQE\n",
"from qiskit_algorithms.optimizers import SLSQP\n",
"from qiskit.primitives import Estimator\n",
"from qiskit_nature.second_q.circuit.library import HartreeFock, UCCSD\n",
"\n",
Expand All @@ -127,7 +127,7 @@
"\n",
"1. An Estimator primitive: these were released as part of Qiskit Terra 0.22. To learn more about primitives, check out [this resource](https://qiskit.org/documentation/apidoc/primitives.html).\n",
"2. A variational form: here we use the Unitary Coupled Cluster (UCC) ansatz (see for instance [Physical Review A 98.2 (2018): 022322]). Since it is a chemistry standard, a factory is already available allowing a fast initialization of a VQE with UCC. The default is to use all single and double excitations. However, the excitation type (S, D, SD) as well as other parameters can be selected. We also prepend the `UCCSD` variational form with a `HartreeFock` initial state, which initializes the occupation of our qubits according to the problem which we are trying solve.\n",
"3. An optimizer: this is the classical piece of code in charge of optimizing the parameters in our variational form. See [the corresponding documentation of Qiskit Terra](https://qiskit.org/documentation/stubs/qiskit.algorithms.optimizers.html) for more information.\n",
"3. An optimizer: this is the classical piece of code in charge of optimizing the parameters in our variational form. See [the corresponding documentation](https://qiskit.org/ecosystem/algorithms/apidocs/qiskit_algorithms.optimizers.html) for more information.\n",
"\n",
"One could also use any available ansatz / initial state or even define one's own. For instance,"
]
Expand All @@ -138,7 +138,7 @@
"metadata": {},
"outputs": [],
"source": [
"from qiskit.algorithms.minimum_eigensolvers import VQE\n",
"from qiskit_algorithms import VQE\n",
"from qiskit.circuit.library import TwoLocal\n",
"\n",
"tl_circuit = TwoLocal(\n",
Expand Down Expand Up @@ -314,7 +314,7 @@
}
],
"source": [
"from qiskit.algorithms.minimum_eigensolvers import NumPyMinimumEigensolver\n",
"from qiskit_algorithms import NumPyMinimumEigensolver\n",
"from qiskit_nature.second_q.drivers import GaussianForcesDriver\n",
"from qiskit_nature.second_q.mappers import DirectMapper\n",
"from qiskit_nature.second_q.problems import HarmonicBasis\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/04_excited_states_solvers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"metadata": {},
"outputs": [],
"source": [
"from qiskit.algorithms.eigensolvers import NumPyEigensolver\n",
"from qiskit_algorithms import NumPyEigensolver\n",
"\n",
"numpy_solver = NumPyEigensolver(k=4, filter_criterion=es_problem.get_default_filter_criterion())"
]
Expand Down Expand Up @@ -134,8 +134,8 @@
"metadata": {},
"outputs": [],
"source": [
"from qiskit.algorithms.minimum_eigensolvers import VQE\n",
"from qiskit.algorithms.optimizers import SLSQP\n",
"from qiskit_algorithms import VQE\n",
"from qiskit_algorithms.optimizers import SLSQP\n",
"from qiskit.primitives import Estimator\n",
"from qiskit_nature.second_q.algorithms import GroundStateEigensolver, QEOM, EvaluationRule\n",
"from qiskit_nature.second_q.circuit.library import HartreeFock, UCCSD\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/07_leveraging_qiskit_runtime.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"metadata": {},
"outputs": [],
"source": [
"from qiskit.algorithms.minimum_eigensolvers import NumPyMinimumEigensolver\n",
"from qiskit_algorithms import NumPyMinimumEigensolver\n",
"from qiskit_nature.second_q.algorithms.ground_state_solvers import GroundStateEigensolver\n",
"\n",
"np_solver = NumPyMinimumEigensolver()\n",
Expand Down Expand Up @@ -207,7 +207,7 @@
"metadata": {},
"outputs": [],
"source": [
"from qiskit.algorithms.optimizers import SPSA\n",
"from qiskit_algorithms.optimizers import SPSA\n",
"\n",
"optimizer = SPSA(maxiter=100)\n",
"\n",
Expand All @@ -227,7 +227,7 @@
"metadata": {},
"outputs": [],
"source": [
"from qiskit.algorithms.minimum_eigensolvers import VQE\n",
"from qiskit_algorithms import VQE\n",
"from qiskit.primitives import Estimator\n",
"\n",
"estimator = Estimator()\n",
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/10_lattice_models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@
}
],
"source": [
"from qiskit.algorithms.minimum_eigensolvers import NumPyMinimumEigensolver\n",
"from qiskit_algorithms import NumPyMinimumEigensolver\n",
"from qiskit_nature.second_q.algorithms import GroundStateEigensolver\n",
"from qiskit_nature.second_q.mappers import JordanWignerMapper\n",
"\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/12_deuteron_binding_energy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"import matplotlib.pyplot as plt\n",
"from IPython.display import display, clear_output\n",
"from qiskit.primitives import Estimator\n",
"from qiskit.algorithms.minimum_eigensolvers import VQE\n",
"from qiskit.algorithms.observables_evaluator import estimate_observables\n",
"from qiskit.algorithms.optimizers import COBYLA, SLSQP\n",
"from qiskit_algorithms import VQE\n",
"from qiskit_algorithms.observables_evaluator import estimate_observables\n",
"from qiskit_algorithms.optimizers import COBYLA, SLSQP\n",
"from qiskit.circuit import QuantumCircuit, Parameter\n",
"from qiskit.circuit.library import TwoLocal\n",
"from qiskit.quantum_info import Pauli, SparsePauliOp\n",
Expand Down
4 changes: 1 addition & 3 deletions qiskit_nature/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@
"""

from qiskit.algorithms.minimum_eigen_solvers.minimum_eigen_solver import (
ListOrDict as ListOrDictType,
)
from qiskit_algorithms.list_or_dict import ListOrDict as ListOrDictType

from .exceptions import QiskitNatureError, UnsupportMethodError
from .logging import logging
Expand Down
10 changes: 5 additions & 5 deletions qiskit_nature/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def get_levels_for_names(self, names: List[str]) -> Dict[str, int]:
"""Return logging levels for module names.
Args:
names: list of module names (qiskit_nature.second_q.drivers, qiskit.algorithms etc)
names: list of module names (qiskit_nature.second_q.drivers, qiskit_algorithms etc)
Returns:
Dictionary mapping names to effective level
"""
Expand All @@ -89,7 +89,7 @@ def set_levels_for_names(
"""Set logging levels for module names.
Args:
name_levels: Dictionary of module names (qiskit_nature.second_q, qiskit.algorithms etc)
name_levels: Dictionary of module names (qiskit_nature.second_q, qiskit_algorithms etc)
to desired level
add_default_handler: add or not the default stream handler
"""
Expand Down Expand Up @@ -138,7 +138,7 @@ def remove_all_handlers(self, names: List[str]) -> None:
"""Remove all handlers from modules.
Args:
names: list of module names (qiskit_nature.second_q.drivers, qiskit.algorithms etc)
names: list of module names (qiskit_nature.second_q.drivers, qiskit_algorithms etc)
"""
for name in names:
logger = python_logging.getLogger(name)
Expand All @@ -150,7 +150,7 @@ def remove_default_handler(self, names: List[str]) -> None:
"""Remove default handler from modules.
Args:
names: list of module names (qiskit_nature.second_q.drivers, qiskit.algorithms etc)
names: list of module names (qiskit_nature.second_q.drivers, qiskit_algorithms etc)
"""
for name in names:
logger = python_logging.getLogger(name)
Expand All @@ -165,7 +165,7 @@ def log_to_file(
"""Logs modules to file.
Args:
names: list of module names (qiskit_nature.second_q.drivers, qiskit.algorithms etc)
names: list of module names (qiskit_nature.second_q.drivers, qiskit_algorithms etc)
path: file path
mode: file open mode. If it is not specified, 'a' is used to append
Returns:
Expand Down
Loading

0 comments on commit eb30b6b

Please sign in to comment.