Can draw_mpl() show observable?

Hello,

I often need to change the observable (i.e. the basis for measurement) and investigate the effects of that.
I use draw_mpl() for debugging because it is easy to use, but it does not seem to display the observable.
Is it possible to obtain information about what observable is being measured as an image?

code:

import pennylane as qml
import matplotlib.pyplot as plt

observables = [qml.X(0)@qml.X(1), qml.Y(0)@qml.Y(1), qml.Z(0)@qml.Z(1)]

dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev)
def circuit(x):
    qml.RX(x, wires=0)
    qml.Hadamard(wires=1)
    qml.CNOT(wires=[0, 1])
    
    # Even if these are set to observables[1] or [2], the output image will not change.
    return qml.expval(observables[0])

fig, ax = qml.draw_mpl(circuit, level='device')(0.5)
plt.show()

output:
output

output of qml.about():

Name: PennyLane
Version: 0.38.0
...
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info:           macOS-15.0-arm64-arm-64bit
Python version:          3.12.4
Numpy version:           1.26.4
Scipy version:           1.14.1
Installed devices:
- lightning.qubit (PennyLane_Lightning-0.38.0)
- default.clifford (PennyLane-0.38.0)
- default.gaussian (PennyLane-0.38.0)
- default.mixed (PennyLane-0.38.0)
- default.qubit (PennyLane-0.38.0)
- default.qubit.autograd (PennyLane-0.38.0)
- default.qubit.jax (PennyLane-0.38.0)
- default.qubit.legacy (PennyLane-0.38.0)
- default.qubit.tf (PennyLane-0.38.0)
- default.qubit.torch (PennyLane-0.38.0)
- default.qutrit (PennyLane-0.38.0)
- default.qutrit.mixed (PennyLane-0.38.0)
- default.tensor (PennyLane-0.38.0)
- null.qubit (PennyLane-0.38.0)

Hi @yytt_star, welcome to the Forum!

Unfortunately I don’t think qml.draw_mpl() allows you to easily do this. If it’s just for debugging I would recommend using the text-based qml.draw() instead. When using this drawer you should print the output of your circuit as shown below:

print(qml.draw(circuit)(0.5))

This will show you the output you expect:

0: ──RX(0.50)─╭●─┤ ╭<X@X>
1: ──H────────╰X─┤ ╰<X@X>

Let me know if this helps!

Thank you! @CatalinaAlbornoz

I see, qml.draw() seems like a promising option. I’d like to try using it.

Also, I would like to contribute to the PennyLane community in some way. So, would it be okay if I made this feature request about qml.draw_mpl() into an enhancement issue on GitHub?
(At first glance, it seems that there is no similar issue on GitHub yet)

Yes absolutely! I think that could be a cool feature. Thanks for the suggestion!

Another very good way of contributing to the community is by going around the discussion Forum and answering someone’s question or contributing to the conversation on a topic that you like.

1 Like

Thanks! I have created the issue:

Yeah, I would also like to contribute this forum from now on.

1 Like

Thanks @yytt_star, I look forward to seeing you around!

Ans thanks for making the issue :ok_hand: