【问题标题】:Error while running circuit.draw(output='mpl')运行电路时出错。draw(output=\'mpl\')
【发布时间】:2022-10-19 20:40:40
【问题描述】:

我刚刚开始使用 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) 似乎工作正常。谢谢你的帮助。

【问题讨论】:

    标签: quantum-computing qiskit


    【解决方案1】:

    我认为理解我所犯的错误。因此,首先我在本地计算机上设置的 Jupyter Notebook 上运行代码,它运行得非常好。但是,当我尝试在在线 Google Collab 的 Jupyter Notebook 上运行相同的代码时,它开始抛出异常。现在我不确定我的解决方案是否正确,但这是不同之处,通过在在线环境中运行此命令,它可以工作:

    除了安装pylatexenc 库的现有过程之外,我还必须运行以下命令:

    IBMQ.save_account('your IBMQ API key')

    【讨论】:

      【解决方案2】:

      如果您使用 google colab,则:

      1. 使用“pip install pylatexenc”,
      2. 重新启动运行时,
      3. 再次执行您的代码。

        这应该会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-05
        • 1970-01-01
        • 2021-11-13
        • 2017-09-01
        • 2016-02-11
        • 2022-12-27
        • 2018-07-23
        • 2016-05-22
        相关资源
        最近更新 更多