Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a PennyLane style for circuits and matplotlib plots #3950

Merged
merged 33 commits into from Aug 18, 2023

Conversation

ikurecic
Copy link
Contributor

@ikurecic ikurecic commented Mar 28, 2023

Enables use of draw_mpl with style 'pennylane' and matplotlib.pyplot with style 'pennylane.drawer.plot' to create circuit diagrams and plots in the PennyLane style.

Note: The relevant Matplotlib style sheet installs together with PennyLane and can be used independently of PennyLane, but it will not show up in the list of available styles (matplotlib.pyplot.style.available)
Note2: This style looks best with the Quicksand Bold font (if font not seen in plots after installation, delete Matplotlib font cache file), but if the user doesn't have it installed, it will fall back on the Matplotlib default — no errors are expected to spawn
Note3 (for testing): Matplotlib doesn't like pip install -e . and will resist you. Stick to pip install .

  • Circuit example:
import pennylane as qml

dev = qml.device('lightning.qubit', wires=(0,1,2,3))

@qml.qnode(dev)
def circuit(x, z):
    qml.QFT(wires=(0,1,2,3))
    qml.IsingXX(1.234, wires=(0,2))
    qml.Toffoli(wires=(0,1,2))
    qml.CSWAP(wires=(0,2,3))
    qml.RX(x, wires=0)
    qml.CRZ(z, wires=(3,0))
    return qml.expval(qml.PauliZ(0))

fig, ax = qml.draw_mpl(circuit, style=’pennylane’)(1,1)
fig.show()

image

  • Plot example:
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('pennylane.drawer.plot')
for i in range(3):
    plt.plot(np.random.rand(10))

image

@codecov
Copy link

codecov bot commented Mar 28, 2023

Codecov Report

Merging #3950 (bcae2f6) into master (edefd31) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #3950   +/-   ##
=======================================
  Coverage   99.71%   99.71%           
=======================================
  Files         378      378           
  Lines       34231    34245   +14     
=======================================
+ Hits        34134    34148   +14     
  Misses         97       97           
Files Changed Coverage Δ
pennylane/drawer/draw.py 100.00% <ø> (ø)
pennylane/drawer/style.py 99.09% <100.00%> (+0.13%) ⬆️

@ikurecic
Copy link
Contributor Author

ikurecic commented Mar 31, 2023

EOW status:

  • Plots: PennyLane palette, uses Quicksand Bold font if installed (if not, falls back on sans-serif).
    image
  • Circuits: Same as plots.
    image

@josh146 , do you want me to un-draft this and see about including it? I know a circuit plot redesign is in sights, but Tarik is fine with using this as a starting point. ☺

@josh146
Copy link
Member

josh146 commented Mar 31, 2023

@ikurecic I think this is a really nice iterative improvement! I'll also tag @trbromley here in terms of, where this could fit into the PL Q2 roadmap

@ikurecic
Copy link
Contributor Author

ikurecic commented Aug 8, 2023

Current status:

  • Plots: PennyLane palette (main three colors are the brightest blue, mid-level pink, and brightest yellow), uses Quicksand Bold font if present (if not, falls back on sans-serif and shouldn't spawn warnings).
    image
  • Circuits: Same color scheme as plots, but skips the yellow. (I'm not fully vibing with the color scheme, but I'm not sure we have a great option to incorporate the yellow here…)
    image

@ikurecic ikurecic marked this pull request as ready for review August 9, 2023 08:18
@ikurecic ikurecic marked this pull request as draft August 9, 2023 12:33
@ikurecic ikurecic marked this pull request as ready for review August 11, 2023 09:06
Copy link
Contributor

@timmysilv timmysilv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks great! I'd like some product people to weigh in on how we announce/inform users about the plot style in general because it's not something we've really done before, but I think it'll be awesome to have. the MPL circuits look great, I'm wondering about how to factor in a third colour as well but we don't really need it. maybe measurements? idk. I do think we shouldn't put the config at the top-level because people probably see it all the time. also do you know where that files appear when someone pip installs PL?

pennylane/plot.mplstyle Outdated Show resolved Hide resolved
tests/drawer/test_style.py Outdated Show resolved Hide resolved
@ikurecic ikurecic requested a review from josh146 August 11, 2023 21:01
Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ikurecic! Only minor changes on my end.

Circuits: Same color scheme as plots, but skips the yellow. (I'm not fully vibing with the color scheme, but I'm not sure we have a great option to incorporate the yellow here…)

We could have blue for single wire, yellow for multi wire, or vice versa? I also like the idea of using yellow for measurements.

Note: don't forget to also update the docstring of qml.draw_mpl to list pennylane as an available style.

doc/code/qml_drawer.rst Outdated Show resolved Hide resolved
doc/code/qml_drawer.rst Outdated Show resolved Hide resolved
@ikurecic ikurecic changed the title Adding a PennyLane plot style to matplotlib Adding a PennyLane style for circuits and matplotlib plots Aug 14, 2023
@ikurecic
Copy link
Contributor Author

Hey @timmysilv and @josh146 , if you're happy with the Matplotlib file path, I would suggest merging this as is now, because adding a PennyLane yellow for measurements in the circuit diagram would take some extra structures (not adjusting the style, but adjusting how styles work) and I don't think I have the time for that right now, unfortunately.

I'd update it the next time there's an empty block in my calendar (i.e., before the next release). :)
Sounds good?

Copy link
Contributor

@timmysilv timmysilv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super excited to have this in!

pennylane/drawer/draw.py Outdated Show resolved Hide resolved
pennylane/drawer/style.py Show resolved Hide resolved
Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to go from my end, once the docstring https://xanaduai-pennylane--3950.com.readthedocs.build/en/3950/code/qml_drawer.html is correctly showing the new style :)

doc/code/qml_drawer.rst Show resolved Hide resolved
doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
@mudit2812 mudit2812 merged commit 0a8cc84 into master Aug 18, 2023
40 checks passed
@mudit2812 mudit2812 deleted the ivana-matplotlib branch August 18, 2023 13:10
mlxd pushed a commit that referenced this pull request Aug 23, 2023
* Initial look

* Adding style for mpl circuit plots

* Added Quicksand to mpl style

* reformatting

* changed test_style.py

* facecolor = white; added test

* fixing idiotic mistake ♥

* def almost black

* setup file add

* setup fixes + changelog

* adjusted wording

* forgot to format 🤦‍♂️

* trying to circumvent black

* black fail lied

* added pennylane to use_style

* Style sheet location + setup

* adjusted changelog to new style folder

* Apply suggestions from code review

Co-authored-by: Matthew Silverman <matthews@xanadu.ai>

* add circuit font logic

* added to qml_drawer.rst list

* added style example

* pls

* pennylane in docs

* pragma for if in style

* format

* Apply suggestions from code review

Co-authored-by: Matthew Silverman <matthews@xanadu.ai>

* final clarifications

* re quicksand bold

---------

Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants