Skip to content

Commit

Permalink
Directly use psutil to get total system memory (#2002)
Browse files Browse the repository at this point in the history
Currently Aer is using Qiskit's local_hardware_info() function which to
determine the total amount of system memory which is used to compute the
largest statevector the system can build. However, this function wasn't
really intended to be used outside of Qiskit and also Qiskit is looking
to remove the memory reporting (see: Qiskit/qiskit#11254). This commit
just pivots to using psutil directly which is what qiskit is doing
internally.
  • Loading branch information
mtreinish authored Nov 21, 2023
1 parent c806180 commit 6f0b94f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
6 changes: 4 additions & 2 deletions qiskit_aer/backends/backend_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
"""
import os
from math import log2
from qiskit.utils import local_hardware_info

import psutil
from qiskit.circuit import QuantumCircuit
from qiskit.compiler import assemble
from qiskit.qobj import QasmQobjInstruction
from qiskit.result import ProbDistribution
from qiskit.quantum_info import Clifford

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

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

# Max number of qubits for complex double statevector
# given available system memory
Expand Down
5 changes: 3 additions & 2 deletions qiskit_aer/backends/statevector_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import copy
import logging
from warnings import warn
from qiskit.utils import local_hardware_info

import psutil
from qiskit.providers.options import Options
from qiskit.providers.models import QasmBackendConfiguration

Expand Down Expand Up @@ -364,7 +365,7 @@ def _validate(self, qobj):
if n_qubits > max_qubits:
raise AerError(
f"Number of qubits ({n_qubits}) is greater than max ({max_qubits}) "
f'for "{name}" with {int(local_hardware_info()["memory"])} GB system memory.'
f'for "{name}" with {int(psutil.virtual_memory().total / (1024**3))} GB system memory.'
)

if qobj.config.shots != 1:
Expand Down
5 changes: 3 additions & 2 deletions qiskit_aer/backends/unitary_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import copy
import logging
from warnings import warn
from qiskit.utils import local_hardware_info

import psutil
from qiskit.providers.options import Options
from qiskit.providers.models import QasmBackendConfiguration

Expand Down Expand Up @@ -351,7 +352,7 @@ def _validate(self, qobj):
raise AerError(
f"Number of qubits ({n_qubits}) is greater than "
f'max ({max_qubits}) for "{name}" with '
f"{int(local_hardware_info()['memory'])} GB system memory."
f"{int(psutil.virtual_memory().total / (1024**3))} GB system memory."
)
if qobj.config.shots != 1:
logger.info('"%s" only supports 1 shot. Setting shots=1.', name)
Expand Down
9 changes: 9 additions & 0 deletions releasenotes/notes/psutil-added-ffb2a4b5956fa03d.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
upgrade:
- |
Added `psutil <https://pypi.org/project/psutil/>`__ as a dependency for
Qiskit Aer. This is used to determine the amount of physical resources
available. ``psutil`` is currently a dependency of Qiskit, which is a
requirement for Qiskit Aer, so ``psutil`` was effectively already required
for any ``qiskit-aer`` installation. But, as qiskit-aer is now using it
directly is now a direct dependency for ``qiskit-aer``.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"qiskit>=0.44.0",
"numpy>=1.16.3",
"scipy>=1.0",
"psutil>=5",
]

classifiers = [
Expand Down

0 comments on commit 6f0b94f

Please sign in to comment.