Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove psutil as a dependency #11336

Merged
merged 16 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions qiskit/providers/basicaer/qasm_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@
import logging
import warnings

from math import log2
from collections import Counter
import numpy as np

from qiskit.utils.multiprocessing import local_hardware_info
from qiskit.providers.models import QasmBackendConfiguration
from qiskit.result import Result
from qiskit.providers.backend import BackendV1
Expand All @@ -53,12 +51,10 @@
class QasmSimulatorPy(BackendV1):
"""Python implementation of an OpenQASM 2 simulator."""

MAX_QUBITS_MEMORY = int(log2(local_hardware_info()["memory"] * (1024**3) / 16))

DEFAULT_CONFIGURATION = {
"backend_name": "qasm_simulator",
"backend_version": "2.1.0",
"n_qubits": min(24, MAX_QUBITS_MEMORY),
"n_qubits": 24,
"url": "https://github.com/Qiskit/qiskit-terra",
"simulator": True,
"local": True,
Expand Down
6 changes: 1 addition & 5 deletions qiskit/providers/basicaer/statevector_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
"""

import logging
from math import log2
from qiskit.utils.multiprocessing import local_hardware_info
from qiskit.providers.basicaer.exceptions import BasicAerError
from qiskit.providers.models import QasmBackendConfiguration
from .qasm_simulator import QasmSimulatorPy
Expand All @@ -36,12 +34,10 @@
class StatevectorSimulatorPy(QasmSimulatorPy):
"""Python statevector simulator."""

MAX_QUBITS_MEMORY = int(log2(local_hardware_info()["memory"] * (1024**3) / 16))

DEFAULT_CONFIGURATION = {
"backend_name": "statevector_simulator",
"backend_version": "1.1.0",
"n_qubits": min(24, MAX_QUBITS_MEMORY),
"n_qubits": 24,
"url": "https://github.com/Qiskit/qiskit-terra",
"simulator": True,
"local": True,
Expand Down
6 changes: 1 addition & 5 deletions qiskit/providers/basicaer/unitary_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@
import logging
import uuid
import time
from math import log2, sqrt
import warnings

import numpy as np

from qiskit.circuit.quantumcircuit import QuantumCircuit
from qiskit.utils.multiprocessing import local_hardware_info
from qiskit.providers.models import QasmBackendConfiguration
from qiskit.providers.backend import BackendV1
from qiskit.providers.options import Options
Expand All @@ -55,12 +53,10 @@
class UnitarySimulatorPy(BackendV1):
"""Python implementation of a unitary simulator."""

MAX_QUBITS_MEMORY = int(log2(sqrt(local_hardware_info()["memory"] * (1024**3) / 16)))

DEFAULT_CONFIGURATION = {
"backend_name": "unitary_simulator",
"backend_version": "1.1.0",
"n_qubits": min(24, MAX_QUBITS_MEMORY),
"n_qubits": 24,
"url": "https://github.com/Qiskit/qiskit-terra",
"simulator": True,
"local": True,
Expand Down
12 changes: 8 additions & 4 deletions qiskit/utils/multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

import multiprocessing as mp
import platform

import psutil
import os


def local_hardware_info():
Expand All @@ -27,13 +26,18 @@ def local_hardware_info():
Returns:
dict: The hardware information.
"""

if hasattr(os, "sched_getaffinity"):
num_cpus = int(len(os.sched_getaffinity(0)) / 2) or 1
else:
num_cpus = 1
mtreinish marked this conversation as resolved.
Show resolved Hide resolved

results = {
"python_compiler": platform.python_compiler(),
"python_build": ", ".join(platform.python_build()),
"python_version": platform.python_version(),
"os": platform.system(),
"memory": psutil.virtual_memory().total / (1024**3),
"cpus": psutil.cpu_count(logical=False) or 1,
"cpus": num_cpus,
}
return results

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
upgrade:
- |
Removed psutil for detecting number of cpus and memory. the information provided does not add sufficient value to justify the additional dependencies and overhead.
mtreinish marked this conversation as resolved.
Show resolved Hide resolved
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
rustworkx>=0.13.0
numpy>=1.17,<2
psutil>=5
scipy>=1.5
sympy>=1.3
dill>=0.3
Expand Down
11 changes: 6 additions & 5 deletions test/python/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

"""Tests for qiskit/utils"""

from unittest import mock
import os
from unittest import mock, skipIf

from qiskit.utils.multiprocessing import local_hardware_info
from qiskit.test import QiskitTestCase
Expand All @@ -21,11 +22,11 @@
class TestUtil(QiskitTestCase):
"""Tests for qiskit/_util.py"""

@skipIf(not hasattr(os, "sched_getaffinity"), "ched_getaffinity is only available on linux")
mtreinish marked this conversation as resolved.
Show resolved Hide resolved
@mock.patch("platform.system", return_value="Linux")
@mock.patch("psutil.virtual_memory")
@mock.patch("psutil.cpu_count", return_value=None)
def test_local_hardware_none_cpu_count(self, cpu_count_mock, vmem_mock, platform_mock):
@mock.patch("os.sched_getaffinity", return_value={})
def test_local_hardware_none_cpu_count(self, cpu_count_mock, platform_mock):
"""Test cpu count fallback to 1 when true value can't be determined"""
del cpu_count_mock, vmem_mock, platform_mock # unused
del cpu_count_mock, platform_mock # unused
result = local_hardware_info()
self.assertEqual(1, result["cpus"])
2 changes: 1 addition & 1 deletion test/python/transpiler/test_mappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class TestsSabreSwap(SwapperCommonTestCases, QiskitTestCase):
"""Test SwapperCommonTestCases using SabreSwap."""

pass_class = SabreSwap
additional_args = {"seed": 1242}
additional_args = {"seed": 1242, "trials": 2}


if __name__ == "__main__":
Expand Down