Skip to content

Commit

Permalink
remove configuration and properties from AerBackend
Browse files Browse the repository at this point in the history
  • Loading branch information
doichanj committed Aug 8, 2024
1 parent 5efea57 commit 7211d09
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 402 deletions.
82 changes: 38 additions & 44 deletions qiskit_aer/backends/aer_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import logging
from qiskit.providers import convert_to_target
from qiskit.providers.options import Options
from qiskit.providers.models import QasmBackendConfiguration
from qiskit.providers.backend import BackendV2, BackendV1
from qiskit.transpiler.target import target_to_backend_properties

Expand Down Expand Up @@ -682,11 +681,6 @@ class AerSimulator(AerBackend):
"backend_version": __version__,
"n_qubits": MAX_QUBITS_STATEVECTOR,
"url": "https://github.com/Qiskit/qiskit-aer",
"simulator": True,
"local": True,
"conditional": True,
"open_pulse": False,
"memory": True,
"max_shots": int(1e6),
"description": "A C++ QasmQobj simulator with noise",
"coupling_map": None,
Expand All @@ -713,9 +707,7 @@ class AerSimulator(AerBackend):

_AVAILABLE_DEVICES = None

def __init__(
self, configuration=None, properties=None, provider=None, target=None, **backend_options
):
def __init__(self, configuration=None, provider=None, target=None, **backend_options):
self._controller = aer_controller_execute()

# Update available methods and devices for class
Expand All @@ -728,10 +720,11 @@ def __init__(

# Default configuration
if configuration is None:
configuration = QasmBackendConfiguration.from_dict(AerSimulator._DEFAULT_CONFIGURATION)
configuration = AerSimulator._DEFAULT_CONFIGURATION

# set backend name from method and device in option
if "from" not in configuration.backend_name:
backend_name = configuration["backend_name"]
if "from" not in backend_name:
method = "automatic"
device = "CPU"
for key, value in backend_options.items():
Expand All @@ -740,17 +733,17 @@ def __init__(
if key == "device":
device = value
if method not in [None, "automatic"]:
configuration.backend_name += f"_{method}"
backend_name += f"_{method}"
if device not in [None, "CPU"]:
configuration.backend_name += f"_{device}".lower()
backend_name += f"_{device}".lower()
configuration["backend_name"] = backend_name

# Cache basis gates since computing the intersection
# of noise model, method, and config gates is expensive.
self._cached_basis_gates = self._BASIS_GATES["automatic"]

super().__init__(
configuration,
properties=properties,
provider=provider,
target=target,
backend_options=backend_options,
Expand Down Expand Up @@ -846,36 +839,38 @@ def from_backend(cls, backend, **options):
else:
description = backend.description

configuration = QasmBackendConfiguration(
backend_name=f"aer_simulator_from({backend.name})",
backend_version=backend.backend_version,
n_qubits=backend.num_qubits,
basis_gates=backend.operation_names,
gates=[],
local=True,
simulator=True,
conditional=True,
open_pulse=False,
memory=False,
max_shots=int(1e6),
coupling_map=(
configuration = {
"backend_name": f"aer_simulator_from({backend.name})",
"backend_version": backend.backend_version,
"n_qubits": backend.num_qubits,
"basis_gates": backend.operation_names,
"max_shots": int(1e6),
"coupling_map": (
None if backend.coupling_map is None else list(backend.coupling_map.get_edges())
),
max_experiments=backend.max_circuits,
description=description,
)
properties = target_to_backend_properties(backend.target)
"max_experiments": backend.max_circuits,
"description": description,
}
target = backend.target
elif isinstance(backend, BackendV1):
# BackendV1 will be removed in Qiskit 2.0, so we will remove this soon
warnings.warn(
" from_backend using V1 based backend is deprecated as of Aer 0.15"
" and will be removed no sooner than 3 months from that release"
" date. Please use backends based on V2.",
DeprecationWarning,
stacklevel=2,
)
# Get configuration and properties from backend
configuration = copy.copy(backend.configuration())
config = backend.configuration()
properties = copy.copy(backend.properties())

# Customize configuration name
name = configuration.backend_name
configuration.backend_name = f"aer_simulator_from({name})"
name = config.backend_name
config.backend_name = f"aer_simulator_from({name})"

target = convert_to_target(configuration, properties, None, NAME_MAPPING)
target = convert_to_target(config, properties, None, NAME_MAPPING)
configuration = config.to_dict()
else:
raise TypeError(
"The backend argument requires a BackendV2 or BackendV1 object, "
Expand All @@ -892,7 +887,7 @@ def from_backend(cls, backend, **options):
options["noise_model"] = noise_model

# Initialize simulator
sim = cls(configuration=configuration, properties=properties, target=target, **options)
sim = cls(configuration=configuration, target=target, **options)
return sim

def available_methods(self):
Expand All @@ -911,16 +906,15 @@ def configuration(self):
Returns:
BackendConfiguration: the configuration for the backend.
"""
config = copy.copy(self._configuration)
for key, val in self._options_configuration.items():
setattr(config, key, val)
config = self._configuration.copy()
config.update(self._options_configuration)

method = getattr(self.options, "method", "automatic")

# Update basis gates based on custom options, config, method,
# and noise model
config.custom_instructions = self._CUSTOM_INSTR[method]
config.basis_gates = self._cached_basis_gates + config.custom_instructions
config["custom_instructions"] = self._CUSTOM_INSTR[method]
config["basis_gates"] = self._cached_basis_gates + config["custom_instructions"]
return config

def _execute_circuits(self, aer_circuits, noise_model, config):
Expand Down Expand Up @@ -1008,7 +1002,7 @@ def _basis_gates(self):
# Compute intersection with method basis gates
method = getattr(self._options, "method", "automatic")
method_gates = self._BASIS_GATES[method]
config_gates = self._configuration.basis_gates
config_gates = self._configuration.get("basis_gates", [])
if config_gates:
basis_gates = set(config_gates).intersection(method_gates)
else:
Expand Down Expand Up @@ -1062,8 +1056,8 @@ def _set_method_config(self, method=None):
description = None
n_qubits = None

if self._configuration.coupling_map:
n_qubits = max(list(map(max, self._configuration.coupling_map))) + 1
if self._configuration.get("coupling_map", None) is not None:
n_qubits = max(list(map(max, self._configuration["coupling_map"]))) + 1

self._set_configuration_option("description", description)
self._set_configuration_option("n_qubits", n_qubits)
Expand Down
Loading

0 comments on commit 7211d09

Please sign in to comment.