Can I use barrier gates in pennylane-qiskit plugin?

I want to visualize a pennylane circuit in latex rather than ascii.
Altough `penny-qiskit" plugin can do it, measurement operations were mixed with gate operations.

n_qubits = 6
dev_qiskit = qml.device("qiskit.aer", wires=n_qubits)

@qml.qnode(dev_qiskit)
def f(x):
    qml.RX(x, wires=0)
    qml.RZ(x, wires=0)
    qml.RZ(x, wires=2)
    qml.RZ(x, wires=4)    
    qml.CNOT(wires=[0,1])
    return qml.expval(qml.PauliZ(0))

f(0.3)

dev_qiskit._circuit.draw('mpl')

image

This image is very complicated!

I expect the followring result.

image

This image was generated by qiskit and its barrier function.

from qiskit import QuantumCircuit
n_qubits = 6
circ = QuantumCircuit(n_qubits)
circ.rx(0.3,0)
circ.barrier()
circ.rz(0.3,0)
circ.rz(0.3,2)
circ.rz(0.3,4)
circ.barrier()
circ.cnot(0,1)
circ.measure_all()
circ.draw('mpl')

I want to generate same image with using pennylane-qiskit.
Because, I’m a pennylane-lover.
Is it possible?

Hi @Kuma-quant, I agree that it would be ideal to have a barrier gate but there isn’t one yet. There’s actually an open issue on this subject so if you’re interested you could work on this.

One workaround that you could do is use our new circuit_drawer, which will actually be default from the next PennyLane release. This drawer allows you to customize the look of your circuit and add things such as lines (like a barrier).

Here’s an example of how you can use this drawer.

from pennylane.circuit_drawer import MPLDrawer
import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrow

drawer = MPLDrawer(n_wires=2, n_layers=5)
drawer.label([r"$|0\rangle$", r"$|\theta\rangle$"])
drawer.box_gate(layer=0, wires=(0,1), text="CRX")
drawer.CNOT(layer=2, wires=(0,1))
drawer.ctrl(layer=3, wires=0, wires_target=1)
drawer.box_gate(layer=3, wires=1, text="Y")
drawer.SWAP(layer=4, wires=(0,1))

drawer.measure(layer=5, wires=0)
drawer.measure(layer=5, wires=1)

barrier = FancyArrow(1,1.5,0,-2, ls='--')
drawer.ax.add_patch(barrier)

Please let me know if this helps and if you have any further questions!

Thanks! I’m looking forward to the next PennyLane release.

@Kuma-quant The next release is coming tomorrow!

The drawer however will not be default in v0.19 but in the next one. You can still use the drawer since v0.18 in the way I showed before. I know it requires some coding but it’s useful that you can make your circuit look exactly the way you want.

Please let me know if you need any help in using the drawer or if you have any additional question!

I also expect the circuit drawer will be integrated with qnode.

qml.circuit_drawer(my_qnode, output='mpl')

This is my dream!

Yes, as far as I know the goal is for it to be integrated from v0.20.0 so we’ll just have to hang on a little more!