From 87b1a929b53f3cb97478cbb0b29d54d4f52821fa Mon Sep 17 00:00:00 2001 From: Hiroshi Horii Date: Thu, 9 May 2024 01:18:47 +0900 Subject: [PATCH 1/2] always hook omp functions in Mac --- qiskit_aer/backends/backend_utils.py | 4 ++++ .../wrappers/aer_controller_binding.hpp | 2 ++ .../notes/init_omp_first-c9d19dbfa1a0fc2b.yaml | 6 ++++++ src/controllers/controller_execute.hpp | 18 ++++++------------ 4 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 releasenotes/notes/init_omp_first-c9d19dbfa1a0fc2b.yaml diff --git a/qiskit_aer/backends/backend_utils.py b/qiskit_aer/backends/backend_utils.py index 09a199380b..214080645d 100644 --- a/qiskit_aer/backends/backend_utils.py +++ b/qiskit_aer/backends/backend_utils.py @@ -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) @@ -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"), diff --git a/qiskit_aer/backends/wrappers/aer_controller_binding.hpp b/qiskit_aer/backends/wrappers/aer_controller_binding.hpp index c530cfc8f0..997d38adf7 100644 --- a/qiskit_aer/backends/wrappers/aer_controller_binding.hpp +++ b/qiskit_aer/backends/wrappers/aer_controller_binding.hpp @@ -86,6 +86,8 @@ void read_value(const py::tuple &t, size_t index, T &v) { template void bind_aer_controller(MODULE m) { + m.def("aer_initialize_libraries", &initialize_libraries); + py::class_> aer_ctrl(m, "aer_controller_execute"); aer_ctrl.def(py::init<>()); diff --git a/releasenotes/notes/init_omp_first-c9d19dbfa1a0fc2b.yaml b/releasenotes/notes/init_omp_first-c9d19dbfa1a0fc2b.yaml new file mode 100644 index 0000000000..ecf2a49cd4 --- /dev/null +++ b/releasenotes/notes/init_omp_first-c9d19dbfa1a0fc2b.yaml @@ -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. diff --git a/src/controllers/controller_execute.hpp b/src/controllers/controller_execute.hpp index 88d9c460f1..14f05d67ba 100644 --- a/src/controllers/controller_execute.hpp +++ b/src/controllers/controller_execute.hpp @@ -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 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::check_key("config", qobj)) { - std::string path; - const auto &config = Parser::get_value("config", qobj); - Parser::get_value(path, "library_dir", config); - Hacks::maybe_load_openmp(path); - } return controller.execute(qobj); } @@ -229,9 +226,6 @@ Result controller_execute(std::vector> &input_circs, auto time_taken = std::chrono::duration(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); From db3b83b5150ee0559766779532a38a4b427f6039 Mon Sep 17 00:00:00 2001 From: Hiroshi Horii Date: Mon, 13 May 2024 12:02:25 +0900 Subject: [PATCH 2/2] fix recent test failures with the latest qiskit --- test/terra/primitives/test_sampler_v2.py | 10 ++++++++-- test/terra/states/test_aer_densitymatrix.py | 8 ++++---- test/terra/states/test_aer_statevector.py | 10 +++++----- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/test/terra/primitives/test_sampler_v2.py b/test/terra/primitives/test_sampler_v2.py index 0fe645e263..ecf9cc6048 100644 --- a/test/terra/primitives/test_sampler_v2.py +++ b/test/terra/primitives/test_sampler_v2.py @@ -513,6 +513,12 @@ def test_circuit_with_unitary(self): self.assertEqual(len(result), 1) self._assert_allclose(result[0].data.meas, np.array({1: self._shots})) + def get_data_bin_len(self, data): + if "keys" in dir(data): # qiskit 1.1 or later + return len(data.keys()) + else: + return len(astuple(data)) + def test_circuit_with_multiple_cregs(self): """Test for circuit with multiple classical registers.""" cases = [] @@ -581,7 +587,7 @@ def test_circuit_with_multiple_cregs(self): result = sampler.run([qc], shots=self._shots).result() self.assertEqual(len(result), 1) data = result[0].data - self.assertEqual(len(astuple(data)), 3) + self.assertEqual(self.get_data_bin_len(data), 3) for creg in qc.cregs: self.assertTrue(hasattr(data, creg.name)) self._assert_allclose(getattr(data, creg.name), np.array(target[creg.name])) @@ -614,7 +620,7 @@ def test_circuit_with_aliased_cregs(self): result = sampler.run([qc2], shots=self._shots).result() self.assertEqual(len(result), 1) data = result[0].data - self.assertEqual(len(astuple(data)), 3) + self.assertEqual(self.get_data_bin_len(data), 3) for creg_name in target: self.assertTrue(hasattr(data, creg_name)) self._assert_allclose(getattr(data, creg_name), np.array(target[creg_name])) diff --git a/test/terra/states/test_aer_densitymatrix.py b/test/terra/states/test_aer_densitymatrix.py index 61aacb20cc..55a1a26a86 100644 --- a/test/terra/states/test_aer_densitymatrix.py +++ b/test/terra/states/test_aer_densitymatrix.py @@ -145,12 +145,12 @@ def test_QFT(self): self.assertEqual(1, len(counts)) self.assertTrue("0000" in counts) - def test_single_qubit_QV(self): + def test_two_qubit_QV(self): """Test single qubit QuantumVolume""" - state = AerDensityMatrix(QuantumVolume(1)) + state = AerDensityMatrix(QuantumVolume(2)) counts = state.sample_counts(shots=1024) - self.assertEqual(1, len(counts)) - self.assertTrue("0" in counts) + self.assertEqual(4, len(counts)) + self.assertTrue("00" in counts) def test_evolve(self): """Test evolve method for circuits""" diff --git a/test/terra/states/test_aer_statevector.py b/test/terra/states/test_aer_statevector.py index 0a78d681d5..0b11b1681d 100644 --- a/test/terra/states/test_aer_statevector.py +++ b/test/terra/states/test_aer_statevector.py @@ -153,12 +153,12 @@ def test_QFT(self): self.assertEqual(1, len(counts)) self.assertTrue("0000" in counts) - def test_single_qubit_QV(self): - """Test single qubit QuantumVolume""" - state = AerStatevector(QuantumVolume(1)) + def test_two_qubit_QV(self): + """Test two qubit QuantumVolume""" + state = AerStatevector(QuantumVolume(2)) counts = state.sample_counts(shots=1024) - self.assertEqual(1, len(counts)) - self.assertTrue("0" in counts) + self.assertEqual(4, len(counts)) + self.assertTrue("00" in counts) def test_evolve(self): """Test method and device properties"""