Skip to content

Commit

Permalink
pennylane style is non-xkcd, pennylane_sketch is squiggly (#4880)
Browse files Browse the repository at this point in the history
**Context:**
The PennyLane MPL-drawer style is great, but some users would prefer
smoother plots for more professional contexts, eg. publications. We
should continue to offer the xkcd-like style, but perhaps "pennylane"
should be the non-xkcd style. I propose we make this change, and add
"pennylane_sketch" to keep the squiggly, existing version.

**Description of the Change:**
- Rename the "pennylane" style to "pennylane_sketch"
- Introduce the "pennylane" style to be identical to the old style, but
with non-sketch lines

**Benefits:**
A clean "pennylane" style for professional-looking data plotting, plus
the sketch style is still offered.

**Possible Drawbacks:**
Would be breaking for the "pennylane" style. We can do a deprecation
cycle if people prefer that - it's easy enough to add a warning, just
lmk. I'd prefer not to add the warning, because then people will use the
"pennylane" style that we recommend and get a warning in their face!

[sc-50495]
  • Loading branch information
timmysilv authored Nov 28, 2023
1 parent b77f674 commit 0aad62b
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 8 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_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/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: 7 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@
* The `rot` decomposition now has support for returning a global phase.
[(#4869)](https://github.com/PennyLaneAI/pennylane/pull/4869)

* The `"pennylane_sketch"` MPL-drawer style has been added. This is the same as the `"pennylane"`
style, but with sketch-style lines.
[(#4880)](https://github.com/PennyLaneAI/pennylane/pull/4880)

<h3>Breaking changes 💔</h3>

* The decomposition of `GroverOperator` now has an additional global phase operation.
Expand All @@ -171,6 +175,9 @@
because it depended on the now-deprecated `Observable.return_type` property.
[(#4762)](https://github.com/PennyLaneAI/pennylane/pull/4762)

* The `"pennylane"` MPL-drawer style now draws straight lines instead of sketch-style lines.
[(#4880)](https://github.com/PennyLaneAI/pennylane/pull/4880)

<h3>Deprecations 👋</h3>

* `single_tape_transform`, `batch_transform`, `qfunc_transform`, and `op_transform` are deprecated.
Expand Down
10 changes: 5 additions & 5 deletions pennylane/drawer/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,11 @@ 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', '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".
Setting style does not modify matplotlib global plotting settings.
style (str): visual style of plot. Valid strings are ``{'black_white', 'black_white_dark', 'sketch',
'pennylane', 'pennylane_sketch', '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". Setting style does not modify matplotlib global plotting settings.
fontsize (float or str): fontsize for text. Valid strings are
``{'xx-small', 'x-small', 'small', 'medium', large', 'x-large', 'xx-large'}``.
Default is ``14``.
Expand Down
11 changes: 11 additions & 0 deletions pennylane/drawer/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ def _pennylane():
if "Quicksand" in {font.name for font in fm.FontManager().ttflist}: # pragma: no cover
plt.rcParams["font.family"] = "Quicksand"
plt.rcParams["font.weight"] = "bold"
plt.rcParams["path.sketch"] = None


@_needs_mpl
def _pennylane_sketch():
"""Apply the PennyLane-Sketch style to matplotlib's configuration. This function
modifies ``plt.rcParams``.
"""
_pennylane()
plt.rcParams["path.sketch"] = (1, 250, 1)


Expand Down Expand Up @@ -178,6 +187,7 @@ def _sketch_dark():
"black_white_dark": _black_white_dark,
"sketch": _sketch,
"pennylane": _pennylane,
"pennylane_sketch": _pennylane_sketch,
"sketch_dark": _sketch_dark,
"solarized_light": _solarized_light,
"solarized_dark": _solarized_dark,
Expand Down Expand Up @@ -209,6 +219,7 @@ def use_style(style: str):
* ``'black_white_dark'``
* ``'sketch'``
* ``'pennylane'``
* ``'pennylane_sketch'``
* ``'sketch_dark'``
* ``'solarized_light'``
* ``'solarized_dark'``
Expand Down
8 changes: 5 additions & 3 deletions tests/drawer/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def test_available_styles():
"black_white_dark",
"sketch",
"pennylane",
"pennylane_sketch",
"sketch_dark",
"solarized_light",
"solarized_dark",
Expand Down Expand Up @@ -95,10 +96,11 @@ def test_sketch_style():
assert plt.rcParams["path.sketch"] == (1, 100, 2)


def test_pennylane_style():
@pytest.mark.parametrize("style,sketch", [("pennylane", None), ("pennylane_sketch", (1, 250, 1))])
def test_pennylane_style(style, sketch):
"""Tests the pennylane style sets ``plt.rcParams`` with correct values"""

_set_style("pennylane")
_set_style(style)

almost_black = "#151515" # less harsh than full black
assert plt.rcParams["figure.facecolor"] == "white"
Expand All @@ -111,7 +113,7 @@ def test_pennylane_style():
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)
assert plt.rcParams["path.sketch"] == sketch


def test_sketch_style_dark():
Expand Down

0 comments on commit 0aad62b

Please sign in to comment.