You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

mpirun无法导入已安装Qiskit包的问题求助

Qiskit MPI运行问题排查与解决

问题描述

  • 用python3 test-mpi.py可正常执行Qiskit代码,但直接用mpirun调用时出现SegmentationFault错误及权限提示
  • 用sudo mpirun --allow-run-as-root python3 test-mpi.py执行时,出现ModuleNotFoundError: No module named 'qiskit.providers.aer',该模块已安装且普通执行可用,此错误在系统重启后出现

sudo mpirun报错信息

sudo mpirun --allow-run-as-root python3 test-mpi.py

Traceback (most recent call last):
  File "/home/dell/qiskit-aer-main/demo-files/test-mpi.py", line 3, in <module>
from qiskit.providers.aer import *
ModuleNotFoundError: No module named 'qiskit.providers.aer'
Traceback (most recent call last):
  File "/home/dell/qiskit-aer-main/demo-files/test-mpi.py", line 3, in <module>
from qiskit.providers.aer import *
ModuleNotFoundError: No module named 'qiskit.providers.aer'
Traceback (most recent call last):
  File "/home/dell/qiskit-aer-main/demo-files/test-mpi.py", line 3, in <module>
from qiskit.providers.aer import *
ModuleNotFoundError: No module named 'qiskit.providers.aer'
Traceback (most recent call last):
  File "/home/dell/qiskit-aer-main/demo-files/test-mpi.py", line 3, in <module>
from qiskit.providers.aer import *
ModuleNotFoundError: No module named 'qiskit.providers.aer'
--------------------------------------------------------------------------
Primary job  terminated normally, but 1 process returned
a non-zero exit code. Per user-direction, the job has been aborted.
--------------------------------------------------------------------------
--------------------------------------------------------------------------
mpirun detected that one or more processes exited with non-zero status, thus causing
the job to be terminated. The first process to do so was:

Process name: [[51849,1],0]
Exit code:    1

测试代码

from qiskit import *
from qiskit.circuit.library import *
from qiskit.providers.aer import *
sim = AerSimulator(method='statevector')
shots = 100
depth=10
qubits = 35
circuit = transpile(QuantumVolume(qubits, depth, seed=0),
backend=sim,
optimization_level=0)
circuit.measure_all()
result = execute(circuit,sim,shots=shots,seed_simulator=12345).result()
if result.to_dict()['metadata']['mpi_rank'] == 0:
    print (result.to_dict()['metadata'])

普通执行正常输出

python3 test-mpi.py

{'time_taken_execute': 0.222742047, 'mpi_rank': 0, 'time_taken_parameter_binding': 8.8427e-05, 'num_mpi_processes': 1, 'num_processes_per_experiments': 1, 'omp_enabled': True, 'max_gpu_memory_mb': 0, 'max_memory_mb': 15903, 'parallel_experiments': 1}

解决建议

1. 解决sudo mpirun的模块找不到问题

  • 保留用户环境变量执行:用sudo -E mpirun --allow-run-as-root python3 test-mpi.py,-E参数会让root继承当前普通用户的环境变量(包括PYTHONPATH),从而找到已安装的Qiskit模块
  • 指定普通用户的Python路径:先执行which python3获取普通用户的Python绝对路径,比如得到/home/dell/.local/bin/python3,然后用sudo mpirun --allow-run-as-root /home/dell/.local/bin/python3 test-mpi.py
  • 不推荐给root单独安装Qiskit,避免环境混乱

2. 解决mpirun直接执行的SegmentationFault与权限问题

  • 检查MPI版本兼容性:执行mpirun --version查看系统MPI版本,若Qiskit Aer是源码编译的,需确认编译时使用的MPI版本与系统版本一致,不一致则重新编译Aer并指定系统MPI路径
  • 修复权限问题:确保普通用户对当前工作目录、临时文件目录(如/tmp)有读写权限,可执行chmod -R 755 /home/dell/qiskit-aer-main/demo-files调整目录权限
  • 减少MPI进程数测试:尝试用mpirun -n 2 python3 test-mpi.py指定少量进程运行,排查是否是多进程资源冲突导致的段错误

内容的提问来源于stack exchange,提问作者aneela

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 09:13:16