Skip to content

Commit

Permalink
improve flexibility and performance of AER::Config
Browse files Browse the repository at this point in the history
  • Loading branch information
hhorii committed Mar 14, 2023
1 parent 0366c51 commit 41b015f
Show file tree
Hide file tree
Showing 16 changed files with 464 additions and 1,322 deletions.
22 changes: 17 additions & 5 deletions qiskit_aer/backends/aer_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,10 @@ def compile_circuit(circuits, basis_gates=None, optypes=None):


def generate_aer_config(
circuits: List[QuantumCircuit], backend_options: Options, **run_options
circuits: List[QuantumCircuit],
default_options: Options,
backend_options: Options,
**run_options,
) -> AerConfig:
"""generates a configuration to run simulation.
Expand All @@ -355,12 +358,21 @@ def generate_aer_config(
config = AerConfig()
config.memory_slots = memory_slots
config.n_qubits = num_qubits
options = {}
for key, value in backend_options.__dict__.items():
if hasattr(config, key) and value is not None:
setattr(config, key, value)
if value is not None:
if hasattr(config, key):
setattr(config, key, value)
else:
options[key] = value
for key, value in run_options.items():
if hasattr(config, key) and value is not None:
setattr(config, key, value)
if value is not None:
if hasattr(config, key):
setattr(config, key, value)
else:
options[key] = value
if len(options) > 0:
config.set_options(options)
return config


Expand Down
7 changes: 6 additions & 1 deletion qiskit_aer/backends/aerbackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def __init__(
if backend_options is not None:
self.set_options(**backend_options)

# Set default option to identify diffs
self._system_default_options = self._default_options()

def _convert_circuit_binds(self, circuit, binds):
parameterizations = []
for index, instruction in enumerate(circuit.data):
Expand Down Expand Up @@ -230,7 +233,9 @@ def _run_circuits(self, circuits, parameter_binds, **run_options):
circuits, noise_model = self._compile(circuits, **run_options)
if parameter_binds:
run_options["parameterizations"] = self._convert_binds(circuits, parameter_binds)
config = generate_aer_config(circuits, self.options, **run_options)
config = generate_aer_config(
circuits, self._system_default_options, self.options, **run_options
)

# Submit job
job_id = str(uuid.uuid4())
Expand Down
484 changes: 41 additions & 443 deletions qiskit_aer/backends/wrappers/aer_controller_binding.hpp

Large diffs are not rendered by default.

39 changes: 13 additions & 26 deletions src/controllers/aer_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,9 @@ void Controller::set_config(const Config &config) {

#ifdef _OPENMP
// Load OpenMP maximum thread settings
if (config.max_parallel_threads.has_value())
max_parallel_threads_ = config.max_parallel_threads.value();
if (config.max_parallel_experiments.has_value())
max_parallel_experiments_ = config.max_parallel_experiments.value();
if (config.max_parallel_shots.has_value())
max_parallel_shots_ = config.max_parallel_shots.value();
config.get_option(max_parallel_threads_, "max_parallel_threads");
config.get_option(max_parallel_experiments_, "max_parallel_experiments");
config.get_option(max_parallel_shots_, "max_parallel_shots");
// Limit max threads based on number of available OpenMP threads
auto omp_threads = omp_get_max_threads();
max_parallel_threads_ = (max_parallel_threads_ > 0)
Expand All @@ -418,49 +415,39 @@ void Controller::set_config(const Config &config) {
#endif

// Load configurations for parallelization

if (config.max_memory_mb.has_value())
max_memory_mb_ = config.max_memory_mb.value();
config.get_option(max_memory_mb_, "max_memory_mb");

// for debugging
if (config._parallel_experiments.has_value()) {
parallel_experiments_ = config._parallel_experiments.value();
if (config.get_option(parallel_experiments_, "_parallel_experiments"))
explicit_parallelization_ = true;
}

// for debugging
if (config._parallel_shots.has_value()) {
parallel_shots_ = config._parallel_shots.value();
if (config.get_option(parallel_shots_, "_parallel_shots"))
explicit_parallelization_ = true;
}

// for debugging
if (config._parallel_state_update.has_value()) {
parallel_state_update_ = config._parallel_state_update.value();
if (config.get_option(parallel_state_update_, "_parallel_state_update"))
explicit_parallelization_ = true;
}

if (explicit_parallelization_) {
parallel_experiments_ = std::max<int>({parallel_experiments_, 1});
parallel_shots_ = std::max<int>({parallel_shots_, 1});
parallel_state_update_ = std::max<int>({parallel_state_update_, 1});
}

if (config.accept_distributed_results.has_value())
accept_distributed_results_ = config.accept_distributed_results.value();
config.get_option(accept_distributed_results_, "accept_distributed_results");

// enable multiple qregs if cache blocking is enabled
if (config.blocking_qubits.has_value())
cache_block_qubit_ = config.blocking_qubits.value();
config.get_option(cache_block_qubit_, "blocking_qubits");

// enable batched multi-shots/experiments optimization
batched_shots_gpu_ = config.batched_shots_gpu;
batched_shots_gpu_max_qubits_ = config.batched_shots_gpu_max_qubits;
config.get_option(batched_shots_gpu_, "batched_shots_gpu");
config.get_option(batched_shots_gpu_max_qubits_,
"batched_shots_gpu_max_qubits");

// cuStateVec configs
cuStateVec_enable_ = false;
if (config.cuStateVec_enable.has_value())
cuStateVec_enable_ = config.cuStateVec_enable.value();
config.get_option(cuStateVec_enable_, "cuStateVec_enable");

// Override automatic simulation method with a fixed method
std::string method = config.method;
Expand Down
Loading

0 comments on commit 41b015f

Please sign in to comment.