我刚刚开始使用Qiskit学习量子计算,并试图用命令circuit.draw(output='mpl')绘制我的量子电路。然而,它引发了一个异常:
---------------------------------------------------------------------------
MissingOptionalLibraryError Traceback (most recent call last)
<ipython-input-43-bd220039ee1c> in <module>
----> 1 circuit.draw(output='mpl')
6 frames
/usr/local/lib/python3.7/dist-packages/qiskit/utils/lazy_tester.py in require_now(self, feature)
222 return
223 raise MissingOptionalLibraryError(
--> 224 libname=self._name, name=feature, pip_install=self._install, msg=self._msg
225 )
226
MissingOptionalLibraryError: "The 'pylatexenc' library is required to use 'MatplotlibDrawer'. You can install it with 'pip install pylatexenc'."
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------然后,我尝试安装pylatexenc库,正如使用命令!pip install pylatexenc在异常中提到的那样,使用from pylatexenc import *导入它,然后再次尝试使用命令circuit.draw(output='mpl'),但是它仍然抛出相同的异常。
为了确保正确安装了库,我重新运行命令!pip install pylatexenc,但随后它显示:
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: pylatexenc in /usr/local/lib/python3.7/dist-packages (2.10)这是我的完整代码,我在Google的朱庇特笔记本中运行它:
from qiskit import *
from pylatexenc import *
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
circuit = QuantumCircuit(qr, cr)
%matplotlib inline
circuit.draw(output='mpl')还有一点要补充:circuit.draw()和circuit.draw(initial_state = True)似乎工作正常。谢谢你的帮助。
发布于 2022-10-19 12:40:40
如果您使用,那么:
这会有帮助的。
发布于 2022-09-08 12:16:19
我想我明白我所犯的错误。所以,首先,我在木星笔记本上运行了代码,这是我在本地计算机上安装的,它运行得非常好。然而,当我试图在Google的朱庇特笔记本上运行相同的代码时,它开始抛出异常。现在我不确定我的解决方案是否正确,但这就是区别所在,通过在在线环境中运行这个命令,它可以工作:
除了安装pylatexenc库的现有过程之外,我还必须运行以下命令:
IBMQ.save_account('your IBMQ API key')
https://stackoverflow.com/questions/73646675
复制相似问题