Skip to content

Commit

Permalink
Rename BasicAer to BasicProvider and refactor interface to follow…
Browse files Browse the repository at this point in the history
… `BackendV2` (#11422)

* Rename `BasicAer` to `BasicProvider`, rename `QasmSimulatorPy` to `BasicSimulator`, remove statevector and unitary simulators.
Replace uses of `BasicAer` with `BasicProvider`, `QasmSimulatorPy` with `BasicSimulator`, `UnitarySimulatorPy` with `quantum_info.Operator` and `StatevectorSimulatorPy` with `quantum_info.Statevector`.

* Migrate `BasicSimulator` interface from `BackendV1` to `BackendV2` (with some extra methods for backward compat.).
Refactor class, rename some internal methods to avoid non-qasm related  use of "qasm", add typehints to module to
improve clarity, update existing typehints. Update tests to expect `BackendV2`.

* Update missing references in qiskit/examples and tests

* Update missing docstrings

* Add future import

* Add type

* Fix lint

* Avoid rebuilding target/configuration when calling property

* Update configuration

* Return methods to original order

* Restore template matching

* Add support for target.num_qubits=None in transpiler pipeline. Set num_qubits in BasicSimulator to None to avoid circuits being transpiled to the simulator's max number of qubits, thus blowing up the size of the computed statevector.

Apply suggestions from Julien's code review

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Small refactorings: apply Julien's suggestions, single out unitary gate basis gate, remove properties and status (but keep configuration, as it is required by assemble), change test case (it was based on backendV1).

* Fix style

* Add release note

* Apply inline suggestions from Julien's code review

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Add suggestion from Julien's code review

* Fix lint

* Fix docs, test

* (Desperate) attempt to fix docs

* Third attempt to fix docs

* Remove execute from basic simulator tests

* Fix conflicts

* Merge remove-execute, fix conflicts

* Update example docstring

* Apply suggestions from Matt's code review

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Remove BasicProvider global instance

* Apply remaining review comments

* Fix tests

Fix forgotten examples

* Update reno

* Fix examples

* Update np random generator

* Use basic simulator instead of basicaer in GenericBackendV2

* Apply code review suggestions

* Remove unused import

* Update releasenotes/notes/refactor-basicaer-to-basicprovider-e27aff9c8e81d26e.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Update refactor-basicaer-to-basicprovider-e27aff9c8e81d26e.yaml

* Update refactor-basicaer-to-basicprovider-e27aff9c8e81d26e.yaml

---------

Co-authored-by: Julien Gacon <gaconju@gmail.com>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
  • Loading branch information
3 people authored Jan 30, 2024
1 parent 738d3e4 commit 3773444
Show file tree
Hide file tree
Showing 73 changed files with 829 additions and 1,627 deletions.
2 changes: 1 addition & 1 deletion docs/apidoc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ API Reference
assembler
dagcircuit
passmanager
providers_basicaer
providers
providers_basic_provider
providers_fake_provider
providers_models
pulse
Expand Down
6 changes: 6 additions & 0 deletions docs/apidoc/providers_basic_provider.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _qiskit-providers-basicprovider:

.. automodule:: qiskit.providers.basic_provider
:no-members:
:no-inherited-members:
:no-special-members:
6 changes: 0 additions & 6 deletions docs/apidoc/providers_basicaer.rst

This file was deleted.

10 changes: 5 additions & 5 deletions examples/python/ghz.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017.
# (C) 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
Expand All @@ -14,8 +14,8 @@
GHZ state example. It also compares running on experiment and simulator.
"""

from qiskit import QuantumCircuit
from qiskit import BasicAer, transpile
from qiskit import QuantumCircuit, transpile
from qiskit.providers.basic_provider import BasicSimulator


###############################################################
Expand All @@ -34,8 +34,8 @@
for i in range(num_qubits):
qc.measure(i, i)

sim_backend = BasicAer.get_backend("qasm_simulator")
sim_backend = BasicSimulator()
job = sim_backend.run(transpile(qc, sim_backend), shots=1024)
result = job.result()
print("Qasm simulator : ")
print("Basic simulator : ")
print(result.get_counts(qc))
7 changes: 4 additions & 3 deletions examples/python/initialize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017.
# (C) 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
Expand All @@ -15,7 +15,8 @@
"""

import math
from qiskit import QuantumCircuit, transpile, BasicAer
from qiskit import QuantumCircuit, transpile
from qiskit.providers.basic_provider import BasicSimulator


###############################################################
Expand Down Expand Up @@ -58,7 +59,7 @@
print([format(abs(x * x), ".3f") for x in desired_vector])

# Initialize on local simulator
sim_backend = BasicAer.get_backend("qasm_simulator")
sim_backend = BasicSimulator()
job = sim_backend.run(transpile(circuit, sim_backend), shots=shots)
result = job.result()

Expand Down
7 changes: 3 additions & 4 deletions examples/python/load_qasm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2018.
# (C) 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
Expand All @@ -13,14 +13,13 @@
"""Example on how to load a file into a QuantumCircuit."""

from qiskit import QuantumCircuit
from qiskit import BasicAer
from qiskit.providers.basic_provider import BasicSimulator

circ = QuantumCircuit.from_qasm_file("examples/qasm/entangled_registers.qasm")
print(circ)

# See the backend
sim_backend = BasicAer.get_backend("qasm_simulator")

sim_backend = BasicSimulator()

# Compile and run the Quantum circuit on a local simulator backend
job_sim = sim_backend.run(circ)
Expand Down
7 changes: 4 additions & 3 deletions examples/python/qft.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

import math
from qiskit import QuantumCircuit
from qiskit import transpile, BasicAer
from qiskit import transpile
from qiskit.providers.basic_provider import BasicSimulator


###############################################################
Expand Down Expand Up @@ -66,8 +67,8 @@ def qft(circ, n):
print(qft4)
print(qft5)

print("Qasm simulator")
sim_backend = BasicAer.get_backend("qasm_simulator")
print("Basic simulator")
sim_backend = BasicSimulator()
job = sim_backend.run(transpile([qft3, qft4, qft5], sim_backend), shots=1024)
result = job.result()
print(result.get_counts(qft3))
Expand Down
6 changes: 3 additions & 3 deletions examples/python/rippleadd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017.
# (C) 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
Expand All @@ -16,13 +16,13 @@
"""

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import BasicAer
from qiskit import transpile
from qiskit.providers.basic_provider import BasicSimulator

###############################################################
# Set the backend name and coupling map.
###############################################################
backend = BasicAer.get_backend("qasm_simulator")
backend = BasicSimulator()
coupling_map = [
[0, 1],
[0, 8],
Expand Down
6 changes: 3 additions & 3 deletions examples/python/teleport.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017.
# (C) 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
Expand All @@ -18,14 +18,14 @@
"""

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import BasicAer
from qiskit import transpile
from qiskit.providers.basic_provider import BasicSimulator

###############################################################
# Set the backend name and coupling map.
###############################################################
coupling_map = [[0, 1], [0, 2], [1, 2], [3, 2], [3, 4], [4, 2]]
backend = BasicAer.get_backend("qasm_simulator")
backend = BasicSimulator()

###############################################################
# Make a quantum program for quantum teleportation.
Expand Down
17 changes: 5 additions & 12 deletions examples/python/using_qiskit_terra_level_0.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2018.
# (C) 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
Expand All @@ -14,16 +14,13 @@
Example showing how to use Qiskit at introduction level.
This example shows the most basic way to use Qiskit. It builds some circuits
and runs them on both the BasicAer (local Qiskit provider) or IBM Quantum (remote IBM Quantum provider).
To control the compile parameters we have provided a transpile function which can be used
as a level 1 user.
and runs them on the BasicProvider (local Qiskit provider).
"""

# Import the Qiskit modules
from qiskit import QuantumCircuit
from qiskit import transpile, BasicAer
from qiskit import transpile
from qiskit.providers.basic_provider import BasicSimulator

# making first circuit: bell state
qc1 = QuantumCircuit(2, 2)
Expand All @@ -36,12 +33,8 @@
qc2.h([0, 1])
qc2.measure([0, 1], [0, 1])

# setting up the backend
print("(BasicAER Backends)")
print(BasicAer.backends())

# running the job
sim_backend = BasicAer.get_backend("qasm_simulator")
sim_backend = BasicSimulator()
job_sim = sim_backend.run(transpile([qc1, qc2], sim_backend))
sim_result = job_sim.result()

Expand Down
4 changes: 0 additions & 4 deletions qiskit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@
import qiskit.circuit.measure
import qiskit.circuit.reset

# Please note these are global instances, not modules.
from qiskit.providers.basicaer import BasicAer

_config = _user_config.get_config()

from qiskit.compiler import transpile, assemble, schedule, sequence
Expand All @@ -70,7 +67,6 @@

__all__ = [
"AncillaRegister",
"BasicAer",
"ClassicalRegister",
"MissingOptionalLibraryError",
"QiskitError",
Expand Down
11 changes: 6 additions & 5 deletions qiskit/circuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@
.. plot::
:include-source:
from qiskit import BasicAer, transpile, QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import transpile, QuantumRegister, ClassicalRegister, QuantumCircuit
qr = QuantumRegister(1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(qr, cr)
Expand All @@ -82,7 +81,8 @@
.. code-block::
backend = BasicAer.get_backend('qasm_simulator')
from qiskit.providers.basic_provider import BasicSimulator
backend = BasicSimulator()
tqc = transpile(qc, backend)
counts = backend.run(tqc).result().get_counts()
Expand All @@ -100,7 +100,7 @@
.. plot::
:include-source:
from qiskit import BasicAer, transpile, QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import transpile, QuantumRegister, ClassicalRegister, QuantumCircuit
qr = QuantumRegister(1)
cr = ClassicalRegister(1)
Expand All @@ -115,7 +115,8 @@
.. code-block::
backend = BasicAer.get_backend('qasm_simulator')
from qiskit.providers.basic_provider import BasicSimulator
backend = BasicSimulator()
tqc = transpile(qc, backend)
counts = backend.run(tqc).result().get_counts()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2018.
# (C) 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
Expand All @@ -11,20 +11,20 @@
# that they have been altered from the originals.

"""
====================================================================
BasicAer: Python-based Simulators (:mod:`qiskit.providers.basicaer`)
====================================================================
================================================================================
BasicProvider: Python-based Simulators (:mod:`qiskit.providers.basic_provider`)
================================================================================
.. currentmodule:: qiskit.providers.basicaer
.. currentmodule:: qiskit.providers.basic_provider
A module of Python-based quantum simulators. Simulators are accessed
via the `BasicAer` provider, e.g.:
A module of Python-based quantum simulators. Simulators can be accessed
via the `BasicProvider` provider, e.g.:
.. code-block::
from qiskit import BasicAer
from qiskit.providers.basic_provider import BasicProvider
backend = BasicAer.get_backend('qasm_simulator')
backend = BasicProvider().get_backend('basic_simulator')
Simulators
Expand All @@ -33,41 +33,34 @@
.. autosummary::
:toctree: ../stubs/
QasmSimulatorPy
StatevectorSimulatorPy
UnitarySimulatorPy
BasicSimulator
Provider
========
.. autosummary::
:toctree: ../stubs/
BasicAerProvider
BasicProvider
Job Class
=========
.. autosummary::
:toctree: ../stubs/
BasicAerJob
BasicProviderJob
Exceptions
==========
.. autosummary::
:toctree: ../stubs/
BasicAerError
BasicProviderError
"""

from .basicaerprovider import BasicAerProvider
from .basicaerjob import BasicAerJob
from .qasm_simulator import QasmSimulatorPy
from .statevector_simulator import StatevectorSimulatorPy
from .unitary_simulator import UnitarySimulatorPy
from .exceptions import BasicAerError

# Global instance to be used as the entry point for convenience.
BasicAer = BasicAerProvider()
from .basic_provider import BasicProvider
from .basic_provider_job import BasicProviderJob
from .basic_simulator import BasicSimulator
from .exceptions import BasicProviderError
Loading

0 comments on commit 3773444

Please sign in to comment.