pythonquantum-computingqiskit

How can I save quantum gates as a graphic in png/svg format using Qiskit?


I am working with Qiskit for programming quantum circuits. Everything works fine but there is one thing which I didn't find out.

Here is my Python code:

from qiskit import QuantumCircuit

qc = QuantumCircuit(2)

qc.h(0)
qc.cx(0, 1)

qc.draw(output = "mpl")

This is my output:

Quantum Circuit

I am only interested into the graphical representation of the Hadamard gate (red one). I need it for a description. I don't need the wires and the qubits. How can I save the Hadamard gate as png/svg file using Qiskit?


Solution

  • This worked with me:

    from qiskit import QuantumCircuit
    from qiskit.visualization import circuit_drawer
    
    qc = QuantumCircuit(1)
    qc.h(0)
    circuit_img = circuit_drawer(qc, output='mpl', scale=2)
    
    circuit_img.savefig('hadamard_gate.png') # save figure as PNG
    circuit_img.savefig('hadamard_gate.svg') # save figure as SVG
    

    cf. https://docs.quantum.ibm.com/api/qiskit/qiskit.visualization.circuit_drawer for additional info