Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save mpl image to file #108

Closed
camillerievan opened this issue Mar 13, 2023 · 3 comments
Closed

How to save mpl image to file #108

camillerievan opened this issue Mar 13, 2023 · 3 comments

Comments

@camillerievan
Copy link

Hi,

Nice library. Is there a way to save the mpl image to a file, rather than display it on screen?

Thanks

Evan

@Hiroya-W
Copy link
Contributor

Hi @camillerievan

If you are running from the command line (not on Jupyter Notebook), you can use the following program.

from qulacs import QuantumCircuit
import matplotlib.pyplot as plt
from qulacsvis.visualization import MPLCircuitlDrawer # Use instead of circuit_drawer

# Build a your quantum circuit
circuit = QuantumCircuit(3)
circuit.add_X_gate(0)
circuit.add_Y_gate(1)
circuit.add_Z_gate(2)
circuit.add_dense_matrix_gate(
    [0, 1], [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]
)
circuit.add_CNOT_gate(2, 0)
circuit.add_X_gate(2)

drawer = MPLCircuitlDrawer(circuit)
fig = drawer.draw()
plt.savefig("output.png") # save to file

However, in many cases you will want to run it on Jupyter Notebook. This is apparently broken. Sorry. I am working on fixing this.

@camillerievan
Copy link
Author

Thanks - works great!

@Hiroya-W
Copy link
Contributor

Fixed. Upgrade qulacsvis to v0.7.0 and use it this way.

circuit_drawer(circuit, "mpl", filename="output.png")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants