无法导入qiskit,numpy中的属性错误:“'numpy.random'没有属性'default\rng'”

2024-10-02 00:25:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Python 3,我使用的是jupyter,当我尝试导入qiskit时,显示了以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-578b7f7e9727> in <module>
----> 1 import qiskit



~\AppData\Roaming\Python\Python36\site-packages\qiskit\quantum_info\synthesis\two_qubit_decompose.py in __init__(self, unitary_matrix)
    169         # D, P = la.eig(M2)  # this can fail for certain kinds of degeneracy
    170         for i in range(100):  # FIXME: this randomized algorithm is horrendous
--> 171             state = np.random.default_rng(i)
    172             M2real = state.normal()*M2.real + state.normal()*M2.imag
    173             _, P = la.eigh(M2real)

AttributeError: module 'numpy.random' has no attribute 'default_rng'

Tags: indefaultforjupyterrandomthislaqiskit
3条回答

我得到的错误与:

AttributeError: module 'numpy.random' has no attribute 'default_rng'

使用'1.16.2'的numpy版本

numpy.__version__
'1.16.2'

作为解决方案,您需要将以下行放在文件的顶部:

import numpy
numpy.random.bit_generator = numpy.random._bit_generator

或者,您当前的numpy版本可能是<= 1.17。因此,您需要更新NumPy版本。例如,我在Anaconda environment上将其更新为:

conda update numpy

目前的版本是:

numpy.__version__
'1.19.2'

更新需要时间,因为NumPy有很多依赖项。希望这个问题能在我这边解决

如果您在anaconda中使用jupyter-卸载、重新安装并重新启动内核对我的作用与此类似:AttributeError: module 'numpy' has no attribute '__version__'

  1. !!pip卸载-y numpy
  2. !!pip安装numpy
  3. 重新启动内核

您需要NumPy 1.17或更高版本才能拥有Qiskit所需的新RNG功能

相关问题 更多 >

    热门问题