Error while running circuit.draw(output='mpl')

Viewed 34

I have just started learning Quantum Computing using Qiskit and was trying to draw my quantum circuit using the command circuit.draw(output='mpl'). However, it throws an exception:

---------------------------------------------------------------------------
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.
---------------------------------------------------------------------------

Then I tried installing the pylatexenc library, as mentioned in the exception using the command !pip install pylatexenc, imported it using from pylatexenc import * and then tried to use the command circuit.draw(output='mpl') again, but it still throws the same exception.

To make sure that the library was installed properly, I re-ran the command !pip install pylatexenc, but then it shows:

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)

Here is my complete code, I am running it in Google Collab's Jupyter Notebook:

from qiskit import *
from pylatexenc import *

qr = QuantumRegister(2)
cr = ClassicalRegister(2)

circuit = QuantumCircuit(qr, cr)

%matplotlib inline

circuit.draw(output='mpl')

One more point to add: circuit.draw() and circuit.draw(initial_state = True) seem to work properly. Thanks for your help.

1 Answers

I think understood the mistake that I was making. So, first I ran the code on the Jupyter Notebook that I had set up on my local computer and it worked perfectly fine. However, when I tried to run the same code on the online Google Collab's Jupyter Notebook, it started throwing the exception. Now I am not sure if my solution is correct, but this is the difference and by running this command in the online environment, it works:

in addition to the existing process of installing the pylatexenc library, I also had to run this command:

IBMQ.save_account('your IBMQ API key')

Related