How to draw the given qnode with the detailed quantum gates using pennylane templates

Hi Pennylane community,

As a beginner using this amazing tool, I would like to know how I can draw my quantum circuit with detailed quantum gates using Pennylane templates such as AngleEmbedding, BasicEntangledLayers, QFT, etc.

def circuit(inputs, weights):
            qml.templates.AngleEmbedding(inputs, wires=range(3))
            qml.templates.BasicEntanglerLayers(weights, wires=range(3))
            return [qml.expval(qml.PauliZ(wires=w)) for w in range(3)]
X=[1,2,3]
weights=[pi, pi, pi]
weights = np.reshape(weights, (1, -1))
qml.drawer.use_style("pennylane")
fig, ax = qml.draw_mpl(circuit)(X,weights)

download

Hi @yousrabou ,

Welcome to the Forum, and great question!

The first thing you would need to do is turn your circuit into a QNode. Directly above the definition of your circuit you can add @qml.qnode(dev). This way you will attach the circuit to the dev device.

dev = qml.device("default.qubit")
@qml.qnode(dev)
def circuit(inputs, weights):
...

Now you can add a level keyword argument when you’re making the drawing:

fig, ax = qml.draw_mpl(circuit,level=None,decimals=1)(X,weights);

In this case I also added a decimals keyword argument to get the value of the angles applied in each gate.

You can check out the Usage details section of the qml.drawer.draw_mpl() documentation to learn more about the levels keyword argument.

:bulb: Pro tip
You don’t need to write qml.templates.AngleEmbedding. You can simply write qml.AngleEmbedding (slightly shorter) and it will work in the same way!

Feel free to post any other questions you have! This was a great question.

1 Like

Hello, madame

I appreciate your detailed reply and tips; they have been really helpful.

1 Like