Skip to content

Commit

Permalink
Always hook omp functions in Mac (#2128)
Browse files Browse the repository at this point in the history
* always hook omp functions in Mac

* fix recent test failures with the latest qiskit

---------

Co-authored-by: Jun Doi <doichan@jp.ibm.com>
  • Loading branch information
hhorii and doichanj authored May 21, 2024
1 parent 4d330e4 commit b706ee1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
4 changes: 4 additions & 0 deletions qiskit_aer/backends/backend_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

from .compatibility import Statevector, DensityMatrix, StabilizerState, Operator, SuperOp

# pylint: disable=import-error, no-name-in-module, abstract-method
from .controller_wrappers import aer_initialize_libraries

# Available system memory
SYSTEM_MEMORY_GB = psutil.virtual_memory().total / (1024**3)

Expand All @@ -35,6 +38,7 @@
# Location where we put external libraries that will be
# loaded at runtime by the simulator extension
LIBRARY_DIR = os.path.dirname(__file__)
aer_initialize_libraries(LIBRARY_DIR)

LEGACY_METHOD_MAP = {
"statevector_cpu": ("statevector", "CPU"),
Expand Down
2 changes: 2 additions & 0 deletions qiskit_aer/backends/wrappers/aer_controller_binding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ void read_value(const py::tuple &t, size_t index, T &v) {

template <typename MODULE>
void bind_aer_controller(MODULE m) {
m.def("aer_initialize_libraries", &initialize_libraries);

py::class_<ControllerExecutor<Controller>> aer_ctrl(m,
"aer_controller_execute");
aer_ctrl.def(py::init<>());
Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/init_omp_first-c9d19dbfa1a0fc2b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
In Mac, to use OpenMP, setup of hooks to omp functions is necessary. However, this setup
works only for backend services and not for quantum_info classes of ``AerStatevector`` and
``AerDensityMatrix``. This fix calls the setup in quantum_info also.
18 changes: 6 additions & 12 deletions src/controllers/controller_execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,15 @@

namespace AER {

void initialize_libraries(const std::string &lib_dir) {
// Fix for MacOS and OpenMP library double initialization crash.
// Issue: https://github.com/Qiskit/qiskit-aer/issues/1
Hacks::maybe_load_openmp(lib_dir);
}

template <class controller_t, typename inputdata_t>
Result controller_execute(const inputdata_t &qobj) {
controller_t controller;

// Fix for MacOS and OpenMP library double initialization crash.
// Issue: https://github.com/Qiskit/qiskit-aer/issues/1
if (Parser<inputdata_t>::check_key("config", qobj)) {
std::string path;
const auto &config = Parser<inputdata_t>::get_value("config", qobj);
Parser<inputdata_t>::get_value(path, "library_dir", config);
Hacks::maybe_load_openmp(path);
}
return controller.execute(qobj);
}

Expand Down Expand Up @@ -229,9 +226,6 @@ Result controller_execute(std::vector<std::shared_ptr<Circuit>> &input_circs,
auto time_taken =
std::chrono::duration<double>(myclock_t::now() - timer_start).count();

// Fix for MacOS and OpenMP library double initialization crash.
// Issue: https://github.com/Qiskit/qiskit-aer/issues/1
Hacks::maybe_load_openmp(config.library_dir);
controller.set_config(config);
auto ret = controller.execute(circs, noise_model, config);

Expand Down

0 comments on commit b706ee1

Please sign in to comment.