Search code examples
quantum-computingqiskit

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


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.


Solution

  • If you use google collab, then:

    1. Use "pip install pylatexenc",
    2. Restart the runtime,
    3. Execute your code again.

    This should help.