Skip to content

Commit

Permalink
Add AerProvider function for optimizing backend options
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseclectic committed Sep 26, 2020
1 parent 54ba369 commit bbe7a82
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
52 changes: 52 additions & 0 deletions qiskit/providers/aer/aerprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from qiskit.providers import BaseProvider
from qiskit.providers.providerutils import filter_backends

from .aererror import AerError
from .backends.aerbackend import AerBackend
from .backends.qasm_simulator import QasmSimulator
from .backends.statevector_simulator import StatevectorSimulator
from .backends.unitary_simulator import UnitarySimulator
Expand Down Expand Up @@ -48,3 +50,53 @@ def backends(self, name=None, filters=None, **kwargs):

def __str__(self):
return 'AerProvider'

@staticmethod
def optimize_backend_options(min_qubits=10, max_qubits=20, ntrials=10):
"""Set optimal OpenMP and fusion options for backend."""
from .profile import profile_parallel_threshold
from .profile import profile_fusion_threshold

# Profile
profile = {}

# Profile OpenMP threshold
try:
parallel_threshold = profile_parallel_threshold(
QasmSimulator(),
min_qubits=min_qubits, max_qubits=max_qubits, ntrials=ntrials)

profile['statevector_parallel_threshold'] = parallel_threshold
except AerError:
pass

# Profile CPU fusion threshold
try:
fusion_threshold = profile_fusion_threshold(
QasmSimulator(), gpu=False,
min_qubits=min_qubits, max_qubits=max_qubits, ntrials=ntrials)
profile['fusion_threshold'] = fusion_threshold
except AerError:
pass

# Profile GPU fusion threshold
try:
fusion_threshold_gpu = profile_fusion_threshold(
QasmSimulator(), gpu=True,
min_qubits=min_qubits, max_qubits=max_qubits, ntrials=ntrials)
profile['fusion_threshold_gpu'] = fusion_threshold_gpu
except AerError:
pass

# TODO: Write profile to a local qiskitaerrc file so this doesn't
# need to be re-run on a system and the following can be loaded
# in the AerBackend class from the rc file if it is found
if 'statevector_parallel_threshold' in profile:
AerBackend._statevector_parallel_threshold = profile[
'statevector_parallel_threshold']
if 'fusion_threshold' in profile:
AerBackend._fusion_threshold = profile['fusion_threshold']
if 'fusion_threshold_gpu' in profile:
AerBackend._fusion_threshold_gpu = profile['fusion_threshold_gpu']

return profile
16 changes: 16 additions & 0 deletions qiskit/providers/aer/backends/aerbackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,25 @@ def _format_qobj(self, qobj, backend_options, noise_model):
output = qobj.to_dict()
# Add new parameters to config from backend options
config = output["config"]

if backend_options is not None:
for key, val in backend_options.items():
config[key] = val if not hasattr(val, 'to_dict') else val.to_dict()

# Add default OpenMP options
if 'statevector_parallel_threshold' not in config and hasattr(
self, '_statevector_parallel_threshold'):
config['statevector_parallel_threshold'] = self._statevector_parallel_threshold

# Add default fusion options
if 'fusion_threshold' not in config:
if 'gpu' in config.get('method', '') and hasattr(self, '_fusion_threshold_gpu'):
# Set GPU fusion threshold
config['fusion_threshold'] = self._fusion_threshold_gpu
elif hasattr(self, '_fusion_threshold'):
# Set CPU fusion threshold
config['fusion_threshold'] = self._fusion_threshold

# Add noise model to config
if noise_model is not None:
config["noise_model"] = noise_model
Expand Down

0 comments on commit bbe7a82

Please sign in to comment.