Skip to content

Commit

Permalink
Remove Aer and imports from qiskit.providers.aer (#11442)
Browse files Browse the repository at this point in the history
* Remove `Aer` and imports from `qiskit.providers.aer`

This removes the `Aer` object and the metapath shenanigans that enabled
importing from `qiskit.providers.aer`.  All references to
`qiskit.providers.aer` in the Terra repository are updated to use
`qiskit_aer` instead, which is a drop-in replacement following Aer 0.11.

This commit leaves the IBMQ object in place for now, since its removal
will affect many other places in the repository and should be handled
separately.

* Fix randomised-test Aer usage

* Remove claim of pulse simulation

Co-authored-by: Elena Peña Tapia <[email protected]>

---------

Co-authored-by: Elena Peña Tapia <[email protected]>
Co-authored-by: Matthew Treinish <[email protected]>
  • Loading branch information
3 people authored Jan 17, 2024
1 parent ed79d42 commit bfc6b0d
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 171 deletions.
57 changes: 0 additions & 57 deletions qiskit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@
"qiskit._accelerate.convert_2q_block_matrix"
] = qiskit._accelerate.convert_2q_block_matrix


# Extend namespace for backwards compat
from qiskit import namespace

# Add hook to redirect imports from qiskit.providers.aer* to qiskit_aer*
# this is necessary for backwards compatibility for users when qiskit-aer
# and qiskit-terra shared the qiskit namespace
new_meta_path_finder = namespace.QiskitElementImport("qiskit.providers.aer", "qiskit_aer")
sys.meta_path = [new_meta_path_finder] + sys.meta_path

# qiskit errors operator
from qiskit.exceptions import QiskitError, MissingOptionalLibraryError

Expand All @@ -79,54 +69,7 @@

from .version import __version__


class AerWrapper:
"""Lazy loading wrapper for Aer provider."""

def __init__(self):
self.aer = None

def __bool__(self):
if self.aer is None:
try:
from qiskit.providers import aer

self.aer = aer.Aer
warnings.warn(
"The qiskit.Aer entry point will be deprecated in a future release and "
"subsequently removed. Instead you should use this "
"directly from the root of the qiskit-aer package.",
PendingDeprecationWarning,
stacklevel=2,
)
except ImportError:
return False
return True

def __getattr__(self, attr):
if not self.aer:
try:
from qiskit.providers import aer

self.aer = aer.Aer
warnings.warn(
"The qiskit.Aer entry point will be deprecated in a future release and "
"subsequently removed. Instead you should use this "
"directly from the root of the qiskit-aer package.",
PendingDeprecationWarning,
stacklevel=2,
)
except ImportError as ex:
raise MissingOptionalLibraryError(
"qiskit-aer", "Aer provider", "pip install qiskit-aer"
) from ex
return getattr(self.aer, attr)


Aer = AerWrapper()

__all__ = [
"Aer",
"AncillaRegister",
"BasicAer",
"ClassicalRegister",
Expand Down
76 changes: 0 additions & 76 deletions qiskit/namespace.py

This file was deleted.

52 changes: 22 additions & 30 deletions qiskit/providers/fake_provider/fake_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ def _parse_channels(self, channels):

def _setup_sim(self):
if _optionals.HAS_AER:
from qiskit.providers import aer
from qiskit_aer import AerSimulator

self.sim = aer.AerSimulator()
self.sim = AerSimulator()
if self.target and self._props_dict:
noise_model = self._get_noise_model_from_backend_v2()
self.sim.set_options(noise_model=noise_model)
Expand Down Expand Up @@ -199,9 +199,9 @@ def _default_options(cls):
default values set
"""
if _optionals.HAS_AER:
from qiskit.providers import aer
from qiskit_aer import AerSimulator

return aer.AerSimulator._default_options()
return AerSimulator._default_options()
else:
return basicaer.QasmSimulatorPy._default_options()

Expand Down Expand Up @@ -348,7 +348,7 @@ def run(self, run_input, **options):
"QuantumCircuit, Schedule, or a list of either" % circuits
)
if pulse_job: # pulse job
raise QiskitError("Pulse simulation is currently not supported for V2 fake backends.")
raise QiskitError("Pulse simulation is currently not supported for fake backends.")
# circuit job
if not _optionals.HAS_AER:
warnings.warn("Aer not found using BasicAer and no noise", RuntimeWarning)
Expand All @@ -375,13 +375,13 @@ def _get_noise_model_from_backend_v2(

from qiskit.circuit import Delay
from qiskit.providers.exceptions import BackendPropertyError
from qiskit.providers.aer.noise import NoiseModel
from qiskit.providers.aer.noise.device.models import (
from qiskit_aer.noise import NoiseModel
from qiskit_aer.noise.device.models import (
_excited_population,
basic_device_gate_errors,
basic_device_readout_errors,
)
from qiskit.providers.aer.noise.passes import RelaxationNoisePass
from qiskit_aer.noise.passes import RelaxationNoisePass

if self._props_dict is None:
self._set_props_dict_from_json()
Expand All @@ -402,7 +402,7 @@ def _get_noise_model_from_backend_v2(
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
module="qiskit.providers.aer.noise.device.models",
module="qiskit_aer.noise.device.models",
)
gate_errors = basic_device_gate_errors(
properties,
Expand Down Expand Up @@ -458,10 +458,10 @@ def __init__(self, configuration, time_alive=10):

def _setup_sim(self):
if _optionals.HAS_AER:
from qiskit.providers import aer
from qiskit.providers.aer.noise import NoiseModel
from qiskit_aer import AerSimulator
from qiskit_aer.noise import NoiseModel

self.sim = aer.AerSimulator()
self.sim = AerSimulator()
if self.properties():
noise_model = NoiseModel.from_backend(self)
self.sim.set_options(noise_model=noise_model)
Expand Down Expand Up @@ -526,9 +526,9 @@ def properties(self):
@classmethod
def _default_options(cls):
if _optionals.HAS_AER:
from qiskit.providers import aer
from qiskit_aer import QasmSimulator

return aer.QasmSimulator._default_options()
return QasmSimulator._default_options()
else:
return basicaer.QasmSimulatorPy._default_options()

Expand All @@ -552,20 +552,12 @@ def run(self, run_input, **kwargs):
"QuantumCircuit, Schedule, or a list of either" % circuits
)
if pulse_job:
if _optionals.HAS_AER:
from qiskit.providers import aer
from qiskit.providers.aer.pulse import PulseSystemModel

system_model = PulseSystemModel.from_backend(self)
sim = aer.Aer.get_backend("pulse_simulator")
job = sim.run(circuits, system_model=system_model, **kwargs)
else:
raise QiskitError("Unable to run pulse schedules without qiskit-aer installed")
else:
if self.sim is None:
self._setup_sim()
if not _optionals.HAS_AER:
warnings.warn("Aer not found using BasicAer and no noise", RuntimeWarning)
self.sim._options = self._options
job = self.sim.run(circuits, **kwargs)
raise QiskitError("Pulse simulation is currently not supported for fake backends.")
# circuit job
if not _optionals.HAS_AER:
warnings.warn("Aer not found using BasicAer and no noise", RuntimeWarning)
if self.sim is None:
self._setup_sim()
self.sim._options = self._options
job = self.sim.run(circuits, **kwargs)
return job
2 changes: 1 addition & 1 deletion qiskit/test/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

import functools
import os
import unittest
from typing import Union, Callable, Type, Iterable
import unittest

from qiskit.utils import wrap_method

Expand Down
6 changes: 3 additions & 3 deletions qiskit/utils/optionals.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
:widths: 25 75
* - .. py:data:: HAS_AER
- :mod:`Qiskit Aer <qiskit.providers.aer>` provides high-performance simulators for the
quantum circuits constructed within Qiskit.
- `Qiskit Aer <https://qiskit.org/ecosystem/aer/>` provides high-performance simulators for
the quantum circuits constructed within Qiskit.
* - .. py:data:: HAS_IBMQ
- The :mod:`Qiskit IBMQ Provider <qiskit.providers.ibmq>` is used for accessing IBM Quantum
Expand Down Expand Up @@ -224,7 +224,7 @@
_logger = _logging.getLogger(__name__)

HAS_AER = _LazyImportTester(
"qiskit.providers.aer",
"qiskit_aer",
name="Qiskit Aer",
install="pip install qiskit-aer",
)
Expand Down
13 changes: 13 additions & 0 deletions releasenotes/notes/remove-aer-hooks-1144714bbbdd0fe8.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
upgrade:
- |
The object ``qiskit.Aer`` has been removed following its deprecation in Qiskit 0.46. You can
instead use ``qiskit_aer.Aer``, which is a drop-in replacement.
- |
Importing from ``qiskit.providers.aer`` will no longer work, following its deprecation in Qiskit
0.46. You should instead import from ``qiskit_aer``, which is a drop-in replacement.
- |
Pulse jobs are no longer supported in fake backends, following the deprecation and removal
of the underlying simulation functionality in Qiskit Aer. For pulse-level simulation, outside
the context of circuit objects, consider using a special-purpose library such as
`Qiskit Dynamics <https://qiskit.org/ecosystem/dynamics/>`__.
2 changes: 1 addition & 1 deletion test/python/transpiler/test_sabre_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def test_no_infinite_loop(self, method):
if not optionals.HAS_AER:
return

from qiskit import Aer
from qiskit_aer import Aer

sim = Aer.get_backend("aer_simulator")
in_results = sim.run(qc, shots=4096).result().get_counts()
Expand Down
5 changes: 3 additions & 2 deletions test/randomized/test_transpiler_equivalence.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

import hypothesis.strategies as st

from qiskit import transpile, Aer, qasm2
from qiskit import transpile, qasm2
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.circuit import Measure, Reset, Gate, Barrier
from qiskit.providers.fake_provider import (
Expand Down Expand Up @@ -91,10 +91,11 @@
)
from qiskit.test.base import dicts_almost_equal


# pylint: disable=wildcard-import,unused-wildcard-import
from qiskit.circuit.library.standard_gates import *

from qiskit_aer import Aer # pylint: disable=wrong-import-order

default_profile = "transpiler_equivalence"
settings.register_profile(
default_profile,
Expand Down
2 changes: 1 addition & 1 deletion tools/find_optional_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _main():
"ipywidgets",
"scipy.stats",
"matplotlib",
"qiskit.providers.aer",
"qiskit_aer",
"qiskit.providers.ibmq",
"qiskit.ignis",
"qiskit.aqua",
Expand Down

0 comments on commit bfc6b0d

Please sign in to comment.