Skip to content

Commit

Permalink
Adding a PennyLane style for circuits and matplotlib plots (#3950)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
ikurecic and timmysilv committed Aug 18, 2023
1 parent edefd31 commit 0a8cc84
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 6 deletions.
Binary file modified doc/_static/style/black_white_dark_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/style/black_white_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/style/default_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/_static/style/pennylane_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/style/sketch_dark_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/style/sketch_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/style/solarized_dark_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/style/solarized_light_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions doc/code/qml_drawer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ Currently Available Styles
.. |sks| image:: ../_static/style/sketch_style.png
:width: 100%

.. |pls| image:: ../_static/style/pennylane_style.png
:width: 100%

.. |skd| image:: ../_static/style/sketch_dark_style.png
:width: 100%

Expand All @@ -68,7 +71,7 @@ Currently Available Styles
+-----+-----+-----+
+|bws|+|bwd|+|sks|+
+-----+-----+-----+
+|skd|+|sol|+|sod|+
+|pls|+|skd|+|sol|+
+-----+-----+-----+
+|def|+ + +
+|sod|+|def|+ +
+-----+-----+-----+
5 changes: 4 additions & 1 deletion doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ array([False, False])
* CI now runs tests with Tensorflow 2.13.0
[(#4472)](https://github.com/PennyLaneAI/pennylane/pull/4472)

* `draw_mpl` accepts `style='pennylane'` to draw PennyLane-style circuit diagrams, and `style.use` in `matplotlib.pyplot` accepts `pennylane.drawer.plot` to create PennyLane-style plots. If the font Quicksand Bold isn't available, an available default font is used instead. [(#3950)](https://github.com/PennyLaneAI/pennylane/pull/3950)

<h3>Breaking changes 💔</h3>

* Gradient transforms no longer implicitly cast `float32` parameters to `float64`. Finite diff
Expand Down Expand Up @@ -438,7 +440,8 @@ Stepan Fomichev,
Lillian M. A. Frederiksen,
Soran Jahangiri,
Edward Jiang,
Korbinian Kottmann
Korbinian Kottmann,
Ivana Kurečić,
Christina Lee,
Vincent Michaud-Rioux,
Romain Moyard,
Expand Down
2 changes: 1 addition & 1 deletion pennylane/drawer/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def draw_mpl(
show_all_wires (bool): If True, all wires, including empty wires, are printed.
decimals (int): How many decimal points to include when formatting operation parameters.
Default ``None`` will omit parameters from operation labels.
style (str): visual style of plot. Valid strings are ``{'black_white', 'black_white_dark', 'sketch',
style (str): visual style of plot. Valid strings are ``{'black_white', 'black_white_dark', 'sketch', 'pennylane',
'sketch_dark', 'solarized_light', 'solarized_dark', 'default'}``. If no style is specified, the
global style set with :func:`~.use_style` will be used, and the initial default is 'black_white'.
If you would like to use your environment's current rcParams, set `style` to "rcParams".
Expand Down
25 changes: 25 additions & 0 deletions pennylane/drawer/plot.mplstyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# PennyLane plot palette
axes.prop_cycle: cycler('color', ['70CEFF', 'FF87EB', 'FFE096', 'D7A2F6', 'FDC9DE', 'E1EBA8', 'C6FDD9', 'B5F2ED'])

patch.linewidth: 0.5
patch.facecolor: white
patch.edgecolor: BCBABA
patch.antialiased: True

axes.facecolor: white
axes.edgecolor: BCBABA
axes.linewidth: 1
axes.grid: True
axes.titlesize: x-large
axes.labelcolor: BCBABA

xtick.color: black
xtick.direction: out
ytick.color: black
ytick.direction: out

grid.color: F0EEEF
grid.linestyle: :

font.sans-serif: Quicksand, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
font.weight: bold
27 changes: 26 additions & 1 deletion pennylane/drawer/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
To add a new style:
* create a private function that modifies ``plt.rcParams``.
* add an entry to the private dictionary ``_style_map``.
* Add an entry to ``doc/code/qml.drawer.rst``
* update the docstrings for ``use_style`` and ``draw_mpl``.
* Add an entry to ``doc/code/qml_drawer.rst``
* Add a test in ``tests/drawer/test_style.py``
Use the decorator ``_needs_mpl`` on style functions to raise appropriate
Expand All @@ -28,6 +29,7 @@
_has_mpl = True # pragma: no cover
try: # pragma: no cover
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
except (ModuleNotFoundError, ImportError) as e: # pragma: no cover
_has_mpl = False

Expand Down Expand Up @@ -131,6 +133,27 @@ def _sketch():
plt.rcParams["path.sketch"] = (1, 100, 2)


@_needs_mpl
def _pennylane():
"""Apply the PennyLane style to matplotlib's configuration. This function
modifies ``plt.rcParams``.
"""
almost_black = "#151515"
plt.rcParams["figure.facecolor"] = "white"
plt.rcParams["savefig.facecolor"] = "white"
plt.rcParams["axes.facecolor"] = "#FFB5F1"
plt.rcParams["patch.facecolor"] = "#D5F0FD"
plt.rcParams["patch.edgecolor"] = almost_black
plt.rcParams["patch.linewidth"] = 2.0
plt.rcParams["patch.force_edgecolor"] = True
plt.rcParams["lines.color"] = "black"
plt.rcParams["text.color"] = "black"
if "Quicksand" in fm.get_font_names(): # pragma: no cover
plt.rcParams["font.family"] = "Quicksand"
plt.rcParams["font.weight"] = "bold"
plt.rcParams["path.sketch"] = (1, 250, 1)


@_needs_mpl
def _sketch_dark():
"""Apply the sketch dark style to matplotlib's configuration. This function
Expand All @@ -154,6 +177,7 @@ def _sketch_dark():
"black_white": _black_white,
"black_white_dark": _black_white_dark,
"sketch": _sketch,
"pennylane": _pennylane,
"sketch_dark": _sketch_dark,
"solarized_light": _solarized_light,
"solarized_dark": _solarized_dark,
Expand Down Expand Up @@ -184,6 +208,7 @@ def use_style(style: str):
* ``'black_white'``
* ``'black_white_dark'``
* ``'sketch'``
* ``'pennylane'``
* ``'sketch_dark'``
* ``'solarized_light'``
* ``'solarized_dark'``
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"provides": ["pennylane"],
"install_requires": requirements,
"extras_require": {"kernels": ["cvxpy", "cvxopt"]},
"package_data": {"pennylane": ["devices/tests/pytest.ini"]},
"package_data": {"pennylane": ["devices/tests/pytest.ini", "drawer/plot.mplstyle"]},
"include_package_data": True,
}

Expand Down
20 changes: 20 additions & 0 deletions tests/drawer/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def test_available_styles():
"black_white",
"black_white_dark",
"sketch",
"pennylane",
"sketch_dark",
"solarized_light",
"solarized_dark",
Expand Down Expand Up @@ -94,6 +95,25 @@ def test_sketch_style():
assert plt.rcParams["path.sketch"] == (1, 100, 2)


def test_pennylane_style():
"""Tests the pennylane style sets ``plt.rcParams`` with correct values"""

_set_style("pennylane")

almost_black = "#151515" # less harsh than full black
assert plt.rcParams["figure.facecolor"] == "white"
assert plt.rcParams["savefig.facecolor"] == "white"
assert plt.rcParams["axes.facecolor"] == "#FFB5F1"
assert plt.rcParams["patch.facecolor"] == "#D5F0FD"
assert plt.rcParams["patch.edgecolor"] == almost_black
assert plt.rcParams["patch.linewidth"] == 2.0
assert plt.rcParams["patch.force_edgecolor"] # = True
assert plt.rcParams["lines.color"] == "black"
assert plt.rcParams["text.color"] == "black"
assert plt.rcParams["font.weight"] == "bold"
assert plt.rcParams["path.sketch"] == (1, 250, 1)


def test_sketch_style_dark():
"""Tests the sketch style dark sets ``plt.rcParams`` with correct values"""

Expand Down

0 comments on commit 0a8cc84

Please sign in to comment.