[英]Getting 'Figure(..x..)' when drawing quantum circuit with qiskit “mpl” output mode
[英]Error while running circuit.draw(output='mpl')
我刚刚开始使用 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 Collab 的 Jupyter Notebook 中运行它:
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)
似乎工作正常。 谢谢你的帮助。
我认为理解我所犯的错误。 因此,首先我在本地计算机上设置的 Jupyter Notebook 上运行代码,它运行得非常好。 但是,当我尝试在在线 Google Collab 的 Jupyter Notebook 上运行相同的代码时,它开始抛出异常。 现在我不确定我的解决方案是否正确,但这是不同之处,通过在在线环境中运行此命令,它可以工作:
除了安装pylatexenc
库的现有过程之外,我还必须运行以下命令:
IBMQ.save_account('your IBMQ API key')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.