[英]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.