1
$\begingroup$

I have coded this from a tutorial:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, Aer, execute
from qiskit.visualization import plot_histogram
import cirq

m_simulator = Aer.backends(name="Quantum_Simulation")
qreg = QuantumRegister(2)
creg = ClassicalRegister(2)
entangler = QuantumCircuit(qreg, creg)
entangler.h(0)
entangler.cx(0, 1)
entangler.measure(0, 0)
entangler.measure(1, 1)
entangler.draw(output="mpl")

For some reason, entangler.draw(output="mpl") is not working. If someone can let me know why, that would be greatly appreciated. The link I am using is: https://towardsdatascience.com/what-is-quantum-entanglement-anyway-4ea97df4bb0e

$\endgroup$

3 Answers 3

2
$\begingroup$

The return type of QuantumCircuit.draw is matplotlib.figure.Figure and, therefore, needs a figure manager. Without entering in a lot of details, here are your options:

  • Run your code in a Jupyter notebook: Copy paste your code in a Jupyter notebook, other locally, in IBM Quantum Lab (it has Qiskit preinstalled), or in Google Colab, for example.
  • Use a different drawer: Change your last line to print(entangler), this will print your circuit in text mode.
  • Save the output into a file: Change your last line to entangler.draw(output="mpl", filename='entangler.png'). This will save the image as a PNG file.
$\endgroup$
0
$\begingroup$

If you are running in a python script, then you need to either save the figure, or you need to display the generated figure. To display it, you can import matplotlib and display using this code snippet:

import matplotlib.pyplot as plt
plt.show()
$\endgroup$
2
  • $\begingroup$ I have just added that: from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, Aer, execute from qiskit.visualization import plot_histogram import cirq import matplotlib.pyplot as plt m_simulator = Aer.backends(name="Quantum_Simulation") qreg = QuantumRegister(2) creg = ClassicalRegister(2) entangler = QuantumCircuit(qreg, creg) entangler.h(0) entangler.cx(0, 1) entangler.measure(0, 0) entangler.measure(1, 1) entangler.draw(output="mpl") plt.show() And for some reason, that is not working. $\endgroup$
    – Sid Meka
    Jul 28, 2023 at 12:51
  • $\begingroup$ I should also mention that by default, library pylatexenc is not installed with Qiskit -- I think that this library may be required for this fig.show() command to work. Was the error you were getting a pylatexenc installation error? $\endgroup$ Aug 3, 2023 at 20:44
0
$\begingroup$

I got it to temporarily work with the following code:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, Aer, execute
from qiskit.visualization import plot_histogram
import cirq
import matplotlib.pyplot as plt

m_simulator = Aer.backends(name="Quantum_Simulation")
qreg = QuantumRegister(2)
creg = ClassicalRegister(2)
entangler = QuantumCircuit(qreg, creg)
entangler.h(0)
entangler.cx(0, 1)
entangler.measure(0, 0)
entangler.measure(1, 1)
print(entangler.draw(output="text"))

I got entangler.draw to work. Now, I will try to see if other code will work in modifying this.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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