1
$\begingroup$

Coding for Qiskit

I watched this video: https://www.youtube.com/watch?v=RrUTwq5jKM4

Using Mac OS Big Sur Using Jupyter 2.0.8 Using Python 3.8

The below code did not work and therefore installed pylatexenc ('pip install pylatexenc') It installed successfully. I then restarted jupyter and It still prints the same error. When I try to reinstall pylatexenc - it says it already there. I tried running while Jupyter is on and in a new terminal (Mac) window I tried to reinstall it- get the same message. Tried 'import pylatexenc *' at beginning of code - does not work - any advice

Code:

from qiskit import *
from pylatexenc import *

qr = QuantumRegister(2)

cr = ClassicalRegister(2)

circuit = QuantumCircuit(qr, cr)

%matplotlib inline

circuit.draw()

circuit.h(qr[0])

circuit.draw(output='mpl')

Here is the error message:

ImportError                               Traceback (most recent call last)
<ipython-input-8-bd220039ee1c> in <module>
----> 1 circuit.draw(output='mpl')

~/opt/anaconda3/lib/python3.8/site-packages/qiskit/circuit/quantumcircuit.py in draw(self, output, scale, filename, style, interactive, plot_barriers, reverse_bits, justify, vertical_compression, idle_wires, with_layout, fold, ax, initial_state, cregbundle)
   1496         from qiskit.visualization import circuit_drawer
   1497 
-> 1498         return circuit_drawer(self, scale=scale,
   1499                               filename=filename, style=style,
   1500                               output=output,

~/opt/anaconda3/lib/python3.8/site-packages/qiskit/visualization/circuit_visualization.py in circuit_drawer(circuit, scale, filename, style, output, interactive, plot_barriers, reverse_bits, justify, vertical_compression, idle_wires, with_layout, fold, ax, initial_state, cregbundle)
    221                                       cregbundle=cregbundle)
    222     elif output == 'mpl':
--> 223         image = _matplotlib_circuit_drawer(circuit, scale=scale,
    224                                            filename=filename, style=style,
    225                                            plot_barriers=plot_barriers,

~/opt/anaconda3/lib/python3.8/site-packages/qiskit/visualization/circuit_visualization.py in _matplotlib_circuit_drawer(circuit, scale, filename, style, plot_barriers, reverse_bits, justify, idle_wires, with_layout, fold, ax, initial_state, cregbundle)
    515 
    516     global_phase = circuit.global_phase if hasattr(circuit, 'global_phase') else None
--> 517     qcd = _matplotlib.MatplotlibDrawer(qubits, clbits, ops, scale=scale, style=style,
    518                                        plot_barriers=plot_barriers, layout=layout,
    519                                        fold=fold, ax=ax, initial_state=initial_state,

~/opt/anaconda3/lib/python3.8/site-packages/qiskit/visualization/matplotlib.py in __init__(self, qubits, clbits, ops, scale, style, plot_barriers, layout, fold, ax, initial_state, cregbundle, global_phase, qregs, cregs)
    126         self.plt_mod = plt
    127         if not HAS_PYLATEX:
--> 128             raise ImportError('The class MatplotlibDrawer needs pylatexenc. '
    129                               'to install, run "pip install pylatexenc".')
    130         self._clbit = []
$\endgroup$
2
  • $\begingroup$ Hi, just a few questions : what is the qiskit version you use? You can check via import qiskit.tools.jupyter and then run %qiskit_version_table. Also, does the drawing work when you just do this circuit.draw() ? Could you restart your notebook and run it without this line from pylatexenc import * and tell me what it did? And last, do you have matplotlib installed? You can check if it is by running this line on your notebook !pip list. $\endgroup$ – Lena Jun 9 at 8:10
  • $\begingroup$ {'qiskit-terra': '0.17.4', 'qiskit-aer': '0.8.2', 'qiskit-ignis': '0.6.0', 'qiskit-ibmq-provider': '0.13.1', 'qiskit-aqua': '0.9.1', 'qiskit': '0.26.2', 'qiskit-nature': None, 'qiskit-finance': None, 'qiskit-optimization': None, 'qiskit-machine-learning': None} $\endgroup$ – Lance Jun 9 at 10:25
1
$\begingroup$

A possible way to make sure you are installing your packages in the environment where you are running is installing them from the Jupyter notebook. You can do that with !pip command in one of the cells:

!pip install pylatexenc
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.