Skip to content

Commit

Permalink
enable AerConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
hhorii committed Feb 27, 2023
1 parent 1137082 commit 687f2d6
Show file tree
Hide file tree
Showing 14 changed files with 426 additions and 122 deletions.
22 changes: 12 additions & 10 deletions qiskit_aer/backends/aer_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from qiskit.qobj import QobjExperimentHeader
from qiskit_aer.aererror import AerError
# pylint: disable=import-error, no-name-in-module
from qiskit_aer.backends.controller_wrappers import AerCircuit
from qiskit_aer.backends.controller_wrappers import AerCircuit, AerConfig
from .backend_utils import circuit_optypes
from ..library.control_flow_instructions import AerMark, AerJump

Expand Down Expand Up @@ -333,12 +333,11 @@ def compile_circuit(circuits, basis_gates=None, optypes=None):
"""
return AerCompiler().compile(circuits, basis_gates, optypes)


def generate_aer_config(
circuits: List[QuantumCircuit],
backend_options: Options,
**run_options
) -> List[AerCircuit]:
) -> AerConfig:
"""generates a configuration to run simulation.
Args:
Expand All @@ -347,17 +346,20 @@ def generate_aer_config(
run_options: run options
Returns:
dict to run Aer
AerConfig to run Aer
"""
num_qubits = max(circuit.num_qubits for circuit in circuits)
memory_slots = max(circuit.num_clbits for circuit in circuits)

config = {
'memory_slots': memory_slots,
'n_qubits': num_qubits,
**backend_options.__dict__,
**run_options
}
config = AerConfig()
config.memory_slots = memory_slots
config.n_qubits = num_qubits
for key, value in backend_options.__dict__.items():
if hasattr(config, key) and value:
setattr(config, key, value)
for key, value in run_options.items():
if hasattr(config, key) and value:
setattr(config, key, value)
return config


Expand Down
6 changes: 3 additions & 3 deletions qiskit_aer/backends/backend_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def cpp_execute_circuits(controller, aer_circuits, noise_model, config):

# Location where we put external libraries that will be
# loaded at runtime by the simulator extension
config['library_dir'] = LIBRARY_DIR
config.library_dir = LIBRARY_DIR

noise_model = noise_model.to_dict(serializable=True) if noise_model else {}

Expand Down Expand Up @@ -218,9 +218,9 @@ def map_legacy_method_options(qobj):

def map_legacy_method_config(config):
"""Map legacy method names of qasm simulator to aer simulator options"""
method = config["method"] if "method" in config else None
method = config.method
if method in LEGACY_METHOD_MAP:
config["method"], config["device"] = LEGACY_METHOD_MAP[method]
config.method, config.device = LEGACY_METHOD_MAP[method]
return config


Expand Down
342 changes: 318 additions & 24 deletions qiskit_aer/backends/wrappers/aer_controller_binding.hpp

Large diffs are not rendered by default.

24 changes: 13 additions & 11 deletions src/controllers/aer_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,15 +397,16 @@ void Controller::set_config(const Config &config) {
validation_threshold_ = config.validation_threshold;

// Load config for memory (creg list data)
save_creg_memory_ = config.memory;
if (config.memory.has_value())
save_creg_memory_ = config.memory.value();

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

// Load configurations for parallelization

if (config.max_memory_mb)
if (config.max_memory_mb.has_value())
max_memory_mb_ = config.max_memory_mb.value();

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

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

// for debugging
if (config._parallel_state_update) {
if (config._parallel_state_update.has_value()) {
parallel_state_update_ = config._parallel_state_update.value();
explicit_parallelization_ = true;
}
Expand All @@ -449,19 +450,20 @@ void Controller::set_config(const Config &config) {
parallel_state_update_ = std::max<int>({parallel_state_update_, 1});
}

if (config.accept_distributed_results)
if (config.accept_distributed_results.has_value())
accept_distributed_results_ = config.accept_distributed_results.value();

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

//enable batched multi-shots/experiments optimization
batched_shots_gpu_ = config.batched_shots_gpu;
batched_shots_gpu_max_qubits_ = config.batched_shots_gpu_max_qubits;

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

// Override automatic simulation method with a fixed method
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/controller_execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ Result controller_execute(std::vector<Circuit>& input_circs, AER::Noise::NoiseMo
int_t seed = -1;
uint_t seed_shift = 0;

if (config.seed_simulator)
seed = config.seed_simulator;
if (config.seed_simulator.has_value())
seed = config.seed_simulator.value();
else
seed = circs[0].seed;

Expand Down
5 changes: 2 additions & 3 deletions src/framework/circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,8 @@ void Circuit::set_metadata(const AER::Config &config, bool truncation) {
num_memory = memory_slots;

// Check for specified n_qubits
if (config.n_qubits) {
// uint_t n_qubits = config["n_qubits"];
uint_t n_qubits = config.n_qubits;
if (config.n_qubits.has_value()) {
uint_t n_qubits = config.n_qubits.value();
if (n_qubits < num_qubits) {
throw std::invalid_argument("Invalid Qobj experiment: n_qubits < instruction qubits.");
}
Expand Down
76 changes: 40 additions & 36 deletions src/framework/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ struct optional {
exist = false;
}

operator bool() const {
// operator bool() const {
// return exist;
// }

bool has_value() const {
return exist;
}
};
Expand Down Expand Up @@ -273,28 +277,28 @@ struct Config {
enable_truncation = other.enable_truncation;
zero_threshold = other.zero_threshold;
validation_threshold = other.validation_threshold;
if (other.max_parallel_threads) max_parallel_threads.value(other.max_parallel_threads.value());
if (other.max_parallel_experiments) max_parallel_experiments.value(other.max_parallel_experiments.value());
if (other.max_parallel_shots) max_parallel_shots.value(other.max_parallel_shots.value());
if (other.max_memory_mb) max_memory_mb.value(other.max_memory_mb.value());
if (other.max_parallel_threads.has_value()) max_parallel_threads.value(other.max_parallel_threads.value());
if (other.max_parallel_experiments.has_value()) max_parallel_experiments.value(other.max_parallel_experiments.value());
if (other.max_parallel_shots.has_value()) max_parallel_shots.value(other.max_parallel_shots.value());
if (other.max_memory_mb.has_value()) max_memory_mb.value(other.max_memory_mb.value());
fusion_enable = other.fusion_enable;
fusion_verbose = other.fusion_verbose;
if (other.fusion_max_qubit) fusion_max_qubit.value(other.fusion_max_qubit.value());
if (other.fusion_threshold) fusion_threshold.value(other.fusion_threshold.value());
if (other.accept_distributed_results) accept_distributed_results.value(other.accept_distributed_results.value());
if (other.memory) memory.value(other.memory.value());
if (other.fusion_max_qubit.has_value()) fusion_max_qubit.value(other.fusion_max_qubit.value());
if (other.fusion_threshold.has_value()) fusion_threshold.value(other.fusion_threshold.value());
if (other.accept_distributed_results.has_value()) accept_distributed_results.value(other.accept_distributed_results.value());
if (other.memory.has_value()) memory.value(other.memory.value());
// noise_model=None,
if (other.seed_simulator) seed_simulator.value(other.seed_simulator.value());
if (other.seed_simulator.has_value()) seed_simulator.value(other.seed_simulator.value());
// # cuStateVec (cuQuantum) option
if (other.cuStateVec_enable) cuStateVec_enable.value(other.cuStateVec_enable.value());
if (other.cuStateVec_enable.has_value()) cuStateVec_enable.value(other.cuStateVec_enable.value());
// # cache blocking for multi-GPUs/MPI options
if (other.blocking_qubits) blocking_qubits.value(other.blocking_qubits.value());
if (other.blocking_qubits.has_value()) blocking_qubits.value(other.blocking_qubits.value());
blocking_enable = other.blocking_enable;
if (other.chunk_swap_buffer_qubits) chunk_swap_buffer_qubits.value(other.chunk_swap_buffer_qubits.value());
if (other.chunk_swap_buffer_qubits.has_value()) chunk_swap_buffer_qubits.value(other.chunk_swap_buffer_qubits.value());
// # multi-shots optimization options (GPU only)
batched_shots_gpu = other.batched_shots_gpu;
batched_shots_gpu_max_qubits = other.batched_shots_gpu_max_qubits;
if (other.num_threads_per_device) num_threads_per_device.value(other.num_threads_per_device.value());
if (other.num_threads_per_device.has_value()) num_threads_per_device.value(other.num_threads_per_device.value());
// # statevector options
statevector_parallel_threshold = other.statevector_parallel_threshold;
statevector_sample_measure_opt = other.statevector_sample_measure_opt;
Expand All @@ -310,7 +314,7 @@ struct Config {
extended_stabilizer_probabilities_snapshot_samples = other.extended_stabilizer_probabilities_snapshot_samples;
// # MPS options
matrix_product_state_truncation_threshold = other.matrix_product_state_truncation_threshold;
if (other.matrix_product_state_max_bond_dimension) matrix_product_state_max_bond_dimension.value(other.matrix_product_state_max_bond_dimension.value());
if (other.matrix_product_state_max_bond_dimension.has_value()) matrix_product_state_max_bond_dimension.value(other.matrix_product_state_max_bond_dimension.value());
mps_sample_measure_algorithm = other.mps_sample_measure_algorithm;
mps_log_data = other.mps_log_data;
mps_swap_direction = other.mps_swap_direction;
Expand All @@ -323,30 +327,30 @@ struct Config {
// system configurations
param_table = other.param_table;
library_dir = other.library_dir;
if (other.n_qubits) n_qubits.value(other.n_qubits.value());
if (other.n_qubits.has_value()) n_qubits.value(other.n_qubits.value());
global_phase = other.global_phase;
memory_slots = other.memory_slots;
if (other._parallel_experiments) _parallel_experiments.value(other._parallel_experiments.value());
if (other._parallel_shots) _parallel_shots.value(other._parallel_shots.value());
if (other._parallel_state_update) _parallel_state_update.value(other._parallel_state_update.value());
if (other._parallel_experiments) _parallel_experiments.value(other._parallel_experiments.value());
if (other.fusion_allow_kraus) fusion_allow_kraus.value(other.fusion_allow_kraus.value());
if (other.fusion_allow_superop) fusion_allow_superop.value(other.fusion_allow_superop.value());
if (other.fusion_parallelization_threshold) fusion_parallelization_threshold.value(other.fusion_parallelization_threshold.value());
if (other._fusion_enable_n_qubits) _fusion_enable_n_qubits.value(other._fusion_enable_n_qubits.value());
if (other._fusion_enable_n_qubits_1) _fusion_enable_n_qubits_1.value(other._fusion_enable_n_qubits_1.value());
if (other._fusion_enable_n_qubits_2) _fusion_enable_n_qubits_2.value(other._fusion_enable_n_qubits_2.value());
if (other._fusion_enable_n_qubits_3) _fusion_enable_n_qubits_3.value(other._fusion_enable_n_qubits_3.value());
if (other._fusion_enable_n_qubits_4) _fusion_enable_n_qubits_4.value(other._fusion_enable_n_qubits_4.value());
if (other._fusion_enable_n_qubits_5) _fusion_enable_n_qubits_5.value(other._fusion_enable_n_qubits_5.value());
if (other._fusion_enable_diagonal) _fusion_enable_diagonal.value(other._fusion_enable_diagonal.value());
if (other._fusion_min_qubit) _fusion_min_qubit.value(other._fusion_min_qubit.value());
if (other.fusion_cost_factor) fusion_cost_factor.value(other.fusion_cost_factor.value());
if (other._parallel_experiments.has_value()) _parallel_experiments.value(other._parallel_experiments.value());
if (other._parallel_shots.has_value()) _parallel_shots.value(other._parallel_shots.value());
if (other._parallel_state_update.has_value()) _parallel_state_update.value(other._parallel_state_update.value());
if (other._parallel_experiments.has_value()) _parallel_experiments.value(other._parallel_experiments.value());
if (other.fusion_allow_kraus.has_value()) fusion_allow_kraus.value(other.fusion_allow_kraus.value());
if (other.fusion_allow_superop.has_value()) fusion_allow_superop.value(other.fusion_allow_superop.value());
if (other.fusion_parallelization_threshold.has_value()) fusion_parallelization_threshold.value(other.fusion_parallelization_threshold.value());
if (other._fusion_enable_n_qubits.has_value()) _fusion_enable_n_qubits.value(other._fusion_enable_n_qubits.value());
if (other._fusion_enable_n_qubits_1.has_value()) _fusion_enable_n_qubits_1.value(other._fusion_enable_n_qubits_1.value());
if (other._fusion_enable_n_qubits_2.has_value()) _fusion_enable_n_qubits_2.value(other._fusion_enable_n_qubits_2.value());
if (other._fusion_enable_n_qubits_3.has_value()) _fusion_enable_n_qubits_3.value(other._fusion_enable_n_qubits_3.value());
if (other._fusion_enable_n_qubits_4.has_value()) _fusion_enable_n_qubits_4.value(other._fusion_enable_n_qubits_4.value());
if (other._fusion_enable_n_qubits_5.has_value()) _fusion_enable_n_qubits_5.value(other._fusion_enable_n_qubits_5.value());
if (other._fusion_enable_diagonal.has_value()) _fusion_enable_diagonal.value(other._fusion_enable_diagonal.value());
if (other._fusion_min_qubit.has_value()) _fusion_min_qubit.value(other._fusion_min_qubit.value());
if (other.fusion_cost_factor.has_value()) fusion_cost_factor.value(other.fusion_cost_factor.value());

if (other.superoperator_parallel_threshold) superoperator_parallel_threshold.value(other.superoperator_parallel_threshold.value());
if (other.unitary_parallel_threshold) unitary_parallel_threshold.value(other.unitary_parallel_threshold.value());
if (other.memory_blocking_bits) memory_blocking_bits.value(other.memory_blocking_bits.value());
if (other.extended_stabilizer_norm_estimation_default_samples) extended_stabilizer_norm_estimation_default_samples.value(other.extended_stabilizer_norm_estimation_default_samples.value());
if (other.superoperator_parallel_threshold.has_value()) superoperator_parallel_threshold.value(other.superoperator_parallel_threshold.value());
if (other.unitary_parallel_threshold.has_value()) unitary_parallel_threshold.value(other.unitary_parallel_threshold.value());
if (other.memory_blocking_bits.has_value()) memory_blocking_bits.value(other.memory_blocking_bits.value());
if (other.extended_stabilizer_norm_estimation_default_samples.has_value()) extended_stabilizer_norm_estimation_default_samples.value(other.extended_stabilizer_norm_estimation_default_samples.value());
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ void State::set_config(const Config &config)
// Set the error upper bound in the stabilizer rank approximation
approximation_error_ = config.extended_stabilizer_approximation_error;
// Set the number of samples used in the norm estimation routine
norm_estimation_samples_ = config.extended_stabilizer_norm_estimation_default_samples;
if (config.extended_stabilizer_norm_estimation_default_samples.has_value())
norm_estimation_samples_ = config.extended_stabilizer_norm_estimation_default_samples.value();
// Set the desired number of repetitions of the norm estimation step. If not explicitly set, we
// compute a default basd on the approximation error
norm_estimation_repetitions_ = std::llrint(std::log2(1. / approximation_error_));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void State::set_config(const Config &config) {
// Set threshold for truncating Schmidt coefficients
MPS_Tensor::set_truncation_threshold(config.matrix_product_state_truncation_threshold);

if (config.matrix_product_state_max_bond_dimension)
if (config.matrix_product_state_max_bond_dimension.has_value())
MPS_Tensor::set_max_bond_dimension(config.matrix_product_state_max_bond_dimension.value());
else
MPS_Tensor::set_max_bond_dimension(UINT64_MAX);
Expand Down
4 changes: 2 additions & 2 deletions src/simulators/state_chunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,10 @@ void StateChunk<state_t>::set_config(const Config &config)
BaseState::set_config(config);

num_threads_per_group_ = 1;
if (config.num_threads_per_device)
if (config.num_threads_per_device.has_value())
num_threads_per_group_ = config.num_threads_per_device.value();

if (config.chunk_swap_buffer_qubits)
if (config.chunk_swap_buffer_qubits.has_value())
chunk_swap_buffer_qubits_ = config.chunk_swap_buffer_qubits.value();

#ifdef AER_CUSTATEVEC
Expand Down
3 changes: 2 additions & 1 deletion src/simulators/superoperator/superoperator_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ size_t State<data_t>::required_memory_mb(

template <class data_t> void State<data_t>::set_config(const Config &config) {
// Set OMP threshold for state update functions
omp_qubit_threshold_ = config.superoperator_parallel_threshold;
if (config.superoperator_parallel_threshold.has_value())
omp_qubit_threshold_ = config.superoperator_parallel_threshold.value();

// Set threshold for truncating snapshots
json_chop_threshold_ = config.zero_threshold;
Expand Down
3 changes: 2 additions & 1 deletion src/simulators/unitary/unitary_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ void State<unitary_matrix_t>::set_config(const Config &config)
BaseState::set_config(config);

// Set OMP threshold for state update functions
omp_qubit_threshold_ = config.unitary_parallel_threshold;
if (config.unitary_parallel_threshold.has_value())
omp_qubit_threshold_ = config.unitary_parallel_threshold.value();

// Set threshold for truncating snapshots
json_chop_threshold_ = config.zero_threshold;
Expand Down
4 changes: 2 additions & 2 deletions src/transpile/cacheblocking.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ void CacheBlocking::set_config(const Config &config)
{
CircuitOptimization::set_config(config);

if (config.blocking_qubits)
if (config.blocking_qubits.has_value())
block_bits_ = config.blocking_qubits.value();

if(block_bits_ >= 1){
blocking_enabled_ = true;
}

if (config.memory_blocking_bits) {
if (config.memory_blocking_bits.has_value()) {
memory_blocking_bits_ = config.memory_blocking_bits.value();
if(memory_blocking_bits_ >= 10){ //blocking qubit should be <=10
memory_blocking_bits_ = 10;
Expand Down
Loading

0 comments on commit 687f2d6

Please sign in to comment.