pennylane icon indicating copy to clipboard operation
pennylane copied to clipboard

Equivalent 'plot distribution' functionality similar to qiskit implemented for PennyLane for quick circuit probability outputs.

Open JustinWoodring opened this issue 1 year ago • 8 comments

Feature details

Users of qiskit will recognize that the 'plot_distribution' functionality represents a convenient way to visualize circuit output probabilities with minimal configuration.

Similar to how draw_mpl automatically outputs a circuit diagram in PennyLane, equivalent functionality for plotting probability distributions based on the qml probs output for a circuit would be very convenient.

I would like to implement this feature myself but I wanted to get a consensus on whether this would be a wanted or accepted feature before I begin the work.

Implementation

I believe that creating a function for qml.plot_probabilities(circuit) or something similar which takes the output of a circuit return qml.probs() would be a good way to implement this but there are perhaps better ways.

How important would you say this feature is?

1: Not important. Would be nice to have.

Additional information

https://docs.quantum.ibm.com/api/qiskit/qiskit.visualization.plot_distribution

JustinWoodring avatar Feb 10 '24 20:02 JustinWoodring

As I stated in the thread I would like to work on this if consensus is established that this would be a desirable feature if implemented

JustinWoodring avatar Feb 10 '24 20:02 JustinWoodring

Thanks @JustinWoodring for posting, we definitely appreciate feature requests and subsequent code contributions.

In this case, I'd first like to understand a little more about the motivations for this feature. One option is to use qml.probs() in a QNode and then to manually create a Matplotlib bar chart. What are the main challenges with this approach?

We've generally tried to steer away from providing convenience wrappers, especially around Matplotlib, because it can limit the ability for users to customize their plot and it can also generate more work in testing.

One thing we could do is have a how-to guide (e.g., here) focused around all of the things you can do with visualization in PennyLane.

trbromley avatar Feb 12 '24 19:02 trbromley

The main motivation is certainly convenience. In my opinion this provides a simple way to way rapidly visualize the results. Although there are slower more manual means of creating these charts, a fast visual is very useful to reduce the work especially while prototyping an algorithm, where work is focused more on the general outcome and less on the exact visual presentation of the outcome.

To clarify this isn't something to reduce users' ability to plot but rather a simple, quick way to get a probability distribution that is properly labeled and sized without the need for manual engineering of a plot.

A how-to guide could potentially be adequate. Obviously I'll leave that decision to your discretion.

JustinWoodring avatar Feb 12 '24 21:02 JustinWoodring

Thanks for the extra context @JustinWoodring. If you have a QNode that already returns a probability distribution with qml.probs(), I'm not sure that having a qml.plot_probabilities(circuit) will add much more than just manually calling Matplotlib's bar-chart drawer.

However, if you have a QNode that returns something else (e.g., an expectation value), but you quickly want to look at the probabilities, then perhaps a qml.probabilities(circuit) could work, similar to qml.matrix(circuit). Though this starts to encroach on some existing open questions about how to easily swap between different measurement processes in a QNode.

For now, a strong how-to guide that covers visualization functionality might actually be quite a value-add for users. If you're interested in contributing that then we can definitely talk more, but no worries if not!

trbromley avatar Feb 14 '24 18:02 trbromley

I second Tom's idea that a how-to guide around visualizations would be great.

CatalinaAlbornoz avatar Feb 14 '24 19:02 CatalinaAlbornoz

I'd be happy to contribute a how-to guide! I think even something as simple as that could very helpful for pennylane novices looking to interpret results efficiently. :)

JustinWoodring avatar Feb 15 '24 00:02 JustinWoodring

Awesome @JustinWoodring! We look forward to seeing your guide.

CatalinaAlbornoz avatar Feb 22 '24 18:02 CatalinaAlbornoz

@JustinWoodring as a guide for writing this how-to, make sure to read this PR description. It will give you a lot of insights on what we would expect from a how-to.

You can take this demo as an example!

I hope these resources help you.

CatalinaAlbornoz avatar Feb 22 '24 19:02 CatalinaAlbornoz