Skip to content

Commit

Permalink
copy AerSimulator._default_options() into AER::Config to consitently …
Browse files Browse the repository at this point in the history
…recognize default values.

Use AerSimulator._default_options() to identify values not to be transfered to C++.
Fix a bug regarding statevector_sample_measure_opt
  • Loading branch information
hhorii committed Mar 20, 2023
1 parent baa2f78 commit 11a462b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 14 deletions.
6 changes: 3 additions & 3 deletions qiskit_aer/backends/aer_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,23 +339,23 @@ def compile_circuit(circuits, basis_gates=None, optypes=None):
def generate_aer_config(
circuits: List[QuantumCircuit],
backend_options: Options,
default_options: Options = None,
**run_options,
) -> AerConfig:
"""generates a configuration to run simulation.
Args:
circuits: circuit(s) to be converted
backend_options: backend options
default_options: default options
run_options: run options
Returns:
AerConfig to run Aer
"""
num_qubits = max(circuit.num_qubits for circuit in circuits)
memory_slots = max(circuit.num_clbits for circuit in circuits)
default_options = default_options if default_options is not None else {}
from . import AerSimulator # prevent circular import

default_options = AerSimulator._default_options()

config = AerConfig()
config.memory_slots = memory_slots
Expand Down
1 change: 1 addition & 0 deletions qiskit_aer/backends/aer_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ def __init__(self, configuration=None, properties=None, provider=None, **backend

@classmethod
def _default_options(cls):
# Must be equivalent to AER::Config::__AER_SIMULATOR_DEFAULTS
return Options(
# Global options
shots=1024,
Expand Down
7 changes: 1 addition & 6 deletions qiskit_aer/backends/aerbackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ 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 @@ -233,9 +230,7 @@ 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, self._system_default_options, **run_options
)
config = generate_aer_config(circuits, self.options, **run_options)

# Submit job
job_id = str(uuid.uuid4())
Expand Down
80 changes: 76 additions & 4 deletions src/framework/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ struct Config {
using pos_t = std::pair<uint_t, uint_t>;
using exp_params_t = std::vector<std::pair<pos_t, std::vector<double>>>;

const static json_t __AER_SIMULATOR_DEFAULTS;

uint_t shots = 1024;
std::string method = "automatic";
std::string device = "CPU";
Expand All @@ -91,19 +93,24 @@ struct Config {

json_t options;

bool _check_option(const json_t &js, const std::string &key) const {
return js.find(key) != js.end() && !js[key].is_null();
}

bool check_option(const std::string &key) const {
// returns false if the value is 'null'
if (options.find(key) != options.end() && !options[key].is_null())
if (_check_option(options, key) ||
_check_option(__AER_SIMULATOR_DEFAULTS, key))
return true;
else
return false;
}

template <typename T>
bool get_option(T &var, const std::string &key) const {
if (check_option(key)) {
bool _get_option(const json_t &js, T &var, const std::string &key) const {
if (_check_option(js, key)) {
try {
var = options[key].get<T>();
var = js[key].get<T>();
return true;
} catch (std::exception &e) {
throw std::invalid_argument(
Expand All @@ -114,6 +121,12 @@ struct Config {
}
}

template <typename T>
bool get_option(T &var, const std::string &key) const {
return _get_option(options, var, key) ||
_get_option(__AER_SIMULATOR_DEFAULTS, var, key);
}

void clear() {
shots = 1024;
method = "automatic";
Expand Down Expand Up @@ -164,6 +177,65 @@ struct Config {
}
};

const json_t Config::__AER_SIMULATOR_DEFAULTS = {
{"shots", 1024},
{"method", "automatic"},
{"device", "CPU"},
{"precision", "double"},
{"executor", nullptr},
{"max_job_size", nullptr},
{"max_shot_size", nullptr},
{"enable_truncation", true},
{"zero_threshold", 1e-10},
{"validation_threshold", nullptr},
{"max_parallel_threads", nullptr},
{"max_parallel_experiments", nullptr},
{"max_parallel_shots", nullptr},
{"max_memory_mb", nullptr},
{"fusion_enable", true},
{"fusion_verbose", false},
{"fusion_max_qubit", nullptr},
{"fusion_threshold", nullptr},
{"accept_distributed_results", nullptr},
{"memory", nullptr},
{"noise_model", nullptr},
{"seed_simulator", nullptr},

{"cuStateVec_enable", false},

{"blocking_qubits", nullptr},
{"blocking_enable", false},
{"chunk_swap_buffer_qubits", nullptr},

{"batched_shots_gpu", false},
{"batched_shots_gpu_max_qubits", 16},
{"num_threads_per_device", 1},

{"statevector_parallel_threshold", 14},
{"statevector_sample_measure_opt", 10},

{"stabilizer_max_snapshot_probabilities", 32},

{"extended_stabilizer_sampling_method", "resampled_metropolis"},
{"extended_stabilizer_metropolis_mixing_time", 5000},
{"extended_stabilizer_approximation_error", 0.05},
{"extended_stabilizer_norm_estimation_samples", 100},
{"extended_stabilizer_norm_estimation_repetitions", 3},
{"extended_stabilizer_parallel_threshold", 100},
{"extended_stabilizer_probabilities_snapshot_samples", 3000},

{"matrix_product_state_truncation_threshold", 1e-16},
{"matrix_product_state_max_bond_dimension", nullptr},
{"mps_sample_measure_algorithm", "mps_heuristic"},
{"mps_log_data", false},
{"mps_swap_direction", "mps_swap_left"},
{"chop_threshold", 1e-8},
{"mps_parallel_threshold", 14},
{"mps_omp_threads", 1},

{"tensor_network_num_sampling_qubits", 10},
{"use_cuTensorNet_autotuning", false}};

// Json conversion function
inline void from_json(const json_t &js, Config &config) {
get_value(config.shots, "shots", js);
Expand Down
2 changes: 1 addition & 1 deletion src/simulators/statevector/statevector_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ void State<statevec_t>::set_config(const Config &config) {

// Set the sample measure indexing size
int index_size;
if (config.get_option(index_size, "statevector_parallel_threshold")) {
if (config.get_option(index_size, "statevector_sample_measure_opt")) {
for (int_t i = 0; i < BaseState::qregs_.size(); i++)
BaseState::qregs_[i].set_sample_measure_index_size(index_size);
}
Expand Down

0 comments on commit 11a462b

Please sign in to comment.