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

Minor improvement to CircuitDrawer class interface #1640

Merged
merged 105 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
80d4159
initial commit
albi3ro Jul 29, 2021
54a2fe5
fix mistake
albi3ro Jul 29, 2021
e62a42b
add examples
albi3ro Jul 29, 2021
b1637d4
Merge branch 'master' into circuitdrawer
albi3ro Jul 29, 2021
6f81cf0
add to docs, change folder name
albi3ro Jul 29, 2021
860f959
Merge branch 'circuitdrawer' of https://github.com/PennyLaneAI/pennyl…
albi3ro Jul 29, 2021
d9e7343
trying to get images in documentation
albi3ro Jul 29, 2021
2180823
Merge branch 'master' into circuitdrawer
albi3ro Jul 29, 2021
7137c96
Merge branch 'master' into circuitdrawer
albi3ro Aug 9, 2021
e7428f0
remove configuration dictionaries, wires to init
albi3ro Aug 11, 2021
191ab09
add color to all methods
albi3ro Aug 13, 2021
57876da
Add usage details
albi3ro Aug 16, 2021
f44a84f
Merge branch 'master' into circuitdrawer
albi3ro Aug 16, 2021
f247897
Add tests
albi3ro Aug 17, 2021
aaa23e8
Merge branch 'master' into circuitdrawer
albi3ro Aug 17, 2021
3f53fa5
try to fix docstring
albi3ro Aug 17, 2021
e90556a
Merge branch 'circuitdrawer' of https://github.com/PennyLaneAI/pennyl…
albi3ro Aug 17, 2021
309b7ed
move to circuit_drawer module, add some tests
albi3ro Aug 17, 2021
dbc2435
trying to fix docs
albi3ro Aug 17, 2021
d38291d
always generate figures in correct folder
albi3ro Aug 17, 2021
2264df5
more tests
albi3ro Aug 17, 2021
46d19c8
Merge branch 'master' into circuitdrawer
albi3ro Aug 17, 2021
9ff05af
Merge branch 'circuitdrawer' of https://github.com/PennyLaneAI/pennyl…
albi3ro Aug 17, 2021
f643bf2
Merge branch 'master' into circuitdrawer
albi3ro Aug 17, 2021
4dda03d
documentation experiment
albi3ro Aug 17, 2021
0b890ec
Merge branch 'circuitdrawer' of https://github.com/PennyLaneAI/pennyl…
albi3ro Aug 17, 2021
596cb51
changelog, black, trying to fix docs
albi3ro Aug 18, 2021
1163497
Merge branch 'master' into circuitdrawer
albi3ro Aug 19, 2021
6d358aa
Update doc/code/qml_circuit_drawer.rst
albi3ro Aug 19, 2021
8eeef97
fix docs hopefully, more tests
albi3ro Aug 20, 2021
e263ae7
Merge branch 'master' into circuitdrawer
albi3ro Aug 20, 2021
0f2e15c
oops
albi3ro Aug 20, 2021
51f8842
Apply suggestions from code review
albi3ro Aug 20, 2021
80e1a08
testing some ideas
albi3ro Aug 20, 2021
4febca4
Merge branch 'circuitdrawer' of https://github.com/PennyLaneAI/pennyl…
albi3ro Aug 20, 2021
092afac
fix conflict
albi3ro Aug 20, 2021
a38722b
oops
albi3ro Aug 20, 2021
8463cb1
Merge branch 'master' into circuitdrawer
antalszava Aug 22, 2021
e85a909
add controlo, some coloring updates
albi3ro Aug 24, 2021
22eaeaa
more color fixing
albi3ro Aug 24, 2021
5b302b7
change formatting syntax
albi3ro Aug 25, 2021
bcd60f9
update changelog
albi3ro Aug 25, 2021
7b21fa4
Merge branch 'master' into circuitdrawer
albi3ro Aug 25, 2021
7b9d6f2
Merge branch 'master' into circuitdrawer
albi3ro Aug 26, 2021
1abc8be
Merge branch 'master' into circuitdrawer
albi3ro Aug 30, 2021
7edfa10
Merge branch 'master' into circuitdrawer
albi3ro Aug 30, 2021
497c228
fix images, rcparams example
albi3ro Aug 31, 2021
6196707
Merge branch 'master' into circuitdrawer
albi3ro Aug 31, 2021
059898a
Apply suggestions from code review
albi3ro Sep 1, 2021
0f924e4
documentation updates
albi3ro Sep 1, 2021
5068a33
tests passing with updated version
albi3ro Sep 1, 2021
684ab4b
Merge branch 'master' into circuitdrawer
albi3ro Sep 1, 2021
8edebab
fix tests
albi3ro Sep 1, 2021
3bdfddc
get tests passing
albi3ro Sep 1, 2021
e376f75
black
albi3ro Sep 1, 2021
959c092
dict to {}
albi3ro Sep 1, 2021
e2cebcc
Merge branch 'master' into circuitdrawer
albi3ro Sep 1, 2021
b893243
Merge branch 'master' into circuitdrawer
albi3ro Sep 3, 2021
86a7e0e
docstring change, updated images
albi3ro Sep 3, 2021
12d4c32
images
albi3ro Sep 3, 2021
b2a6ffc
getting documentation to build
albi3ro Sep 3, 2021
b8869fb
minor doc update
albi3ro Sep 3, 2021
9d48bfe
fix link
albi3ro Sep 3, 2021
5764a92
black
albi3ro Sep 3, 2021
653c384
Apply suggestions from code review
albi3ro Sep 7, 2021
44cdfb0
Merge branch 'master' into circuitdrawer
albi3ro Sep 7, 2021
b87b4be
remove partial black frame
albi3ro Sep 7, 2021
2589dbd
Merge branch 'master' into circuitdrawer
albi3ro Sep 7, 2021
fc293f2
black
albi3ro Sep 7, 2021
98f349b
Merge branch 'circuitdrawer' of https://github.com/PennyLaneAI/pennyl…
albi3ro Sep 7, 2021
226412d
update tests
albi3ro Sep 7, 2021
c4d001e
Merge branch 'master' into circuitdrawer
albi3ro Sep 7, 2021
011e57b
Apply suggestions from code review
albi3ro Sep 9, 2021
62ad323
Merge branch 'master' into circuitdrawer
albi3ro Sep 9, 2021
673a824
change kwargs to options
albi3ro Sep 9, 2021
8490d26
add some spaces
albi3ro Sep 9, 2021
947d58e
move fig, ax, and shape config vars
albi3ro Sep 9, 2021
367177c
Merge branch 'master' into circuitdrawer
albi3ro Sep 9, 2021
1abede9
attributes in the docstring
albi3ro Sep 9, 2021
f8fe518
make fig and ax properties
albi3ro Sep 9, 2021
6bd6c6e
minor wording change
albi3ro Sep 9, 2021
b153186
minor wording change
albi3ro Sep 9, 2021
bea73ba
initial commit
albi3ro Sep 10, 2021
9a231f2
changelog, tests
albi3ro Sep 10, 2021
5413124
black
albi3ro Sep 10, 2021
0ba11a6
Merge branch 'master' into circuit-drawer-charsets
albi3ro Sep 10, 2021
df05eff
Merge branch 'master' into circuitdrawer
albi3ro Sep 10, 2021
208b27a
Merge branch 'master' into circuit-drawer-charsets
albi3ro Sep 13, 2021
ff6ef31
Apply suggestions from code review
albi3ro Sep 13, 2021
cf38411
Merge branch 'master' into circuitdrawer
albi3ro Sep 13, 2021
a3fadda
add options for control type gates
albi3ro Sep 13, 2021
e51afe5
Merge branch 'master' into circuit-drawer-charsets
albi3ro Sep 13, 2021
472a663
remove zorder from keywords
albi3ro Sep 13, 2021
48a9954
changelog
albi3ro Sep 13, 2021
527d81d
Merge branch 'master' into circuitdrawer
albi3ro Sep 13, 2021
68301a7
code factor
albi3ro Sep 13, 2021
7f905b1
Merge branch 'circuitdrawer' of https://github.com/PennyLaneAI/pennyl…
albi3ro Sep 13, 2021
d57abeb
black
albi3ro Sep 13, 2021
2c72c6c
Apply suggestions from code review
albi3ro Sep 13, 2021
77693d5
Merge branch 'circuitdrawer' of https://github.com/PennyLaneAI/pennyl…
albi3ro Sep 13, 2021
60169c5
Merge branch 'master' into circuit-drawer-charsets
albi3ro Sep 13, 2021
ab5641e
Merge branch 'circuit-drawer-charsets' of https://github.com/PennyLan…
albi3ro Sep 13, 2021
ee87c92
trying to fix some git problem
albi3ro Sep 13, 2021
b278baa
fixing git catastrophe
albi3ro Sep 13, 2021
930ef33
git fixing
albi3ro Sep 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@

<h3>Improvements</h3>

* ``qml.circuit_drawer.CircuitDrawer`` can accept a string for the ``charset`` keyword, instead of a ``CharSet`` object.
[(#1640)](https://github.com/PennyLaneAI/pennylane/pull/1640)

* The slowest tests, more than 1.5 seconds, now have the pytest mark `slow`, and can be
selected or deselected during local execution of tests.
[(#1633)](https://github.com/PennyLaneAI/pennylane/pull/1633)
Expand Down
32 changes: 22 additions & 10 deletions pennylane/circuit_drawer/circuit_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import pennylane as qml
from pennylane.wires import Wires
from .charsets import UnicodeCharSet
from .charsets import CHARSETS, CharSet
from .representation_resolver import RepresentationResolver
from .grid import Grid

Expand Down Expand Up @@ -46,7 +46,7 @@ class CircuitDrawer:
raw_operation_grid (list[list[~.Operation]]): The CircuitGraph's operations
raw_observable_grid (list[list[qml.operation.Observable]]): The CircuitGraph's observables
wires (Wires): all wires on the device for which the circuit is drawn
charset (pennylane.circuit_drawer.CharSet, optional): The CharSet that shall be used for drawing.
charset (str, pennylane.circuit_drawer.CharSet, optional): The CharSet that shall be used for drawing.
show_all_wires (bool): If True, all wires, including empty wires, are printed.
"""

Expand All @@ -55,21 +55,33 @@ def __init__(
raw_operation_grid,
raw_observable_grid,
wires,
charset=UnicodeCharSet,
charset=None,
show_all_wires=False,
):
self.operation_grid = Grid(raw_operation_grid)
self.observable_grid = Grid(raw_observable_grid)
self.wires = wires
self.active_wires = self.extract_active_wires(raw_operation_grid, raw_observable_grid)
self.charset = charset

if charset is None:
self.charset = CHARSETS["unicode"]()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is CHARSETS["unicode"]() the same as charsets.UnicodeCharSet?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

An instance of that, yes. Another change in this PR is that we use instances of that class instead of the class itself.

Copy link
Contributor

Choose a reason for hiding this comment

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

Another change in this PR is that we use instances of that class instead of the class itself.

Could this change have any downsides or raise potential unexpected issues, e.g. when passing it along to the representation resolver?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nope. everything is static methods.

elif isinstance(charset, type) and issubclass(charset, CharSet):
self.charset = charset()
else:
if charset not in CHARSETS:
raise ValueError(
"Charset {} is not supported. Supported charsets: {}.".format(
albi3ro marked this conversation as resolved.
Show resolved Hide resolved
charset, ", ".join(CHARSETS.keys())
)
)
self.charset = CHARSETS[charset]()

if show_all_wires:
# if the provided wires include empty wires, make sure they are included
# as active wires
self.active_wires = wires.all_wires([wires, self.active_wires])

self.representation_resolver = RepresentationResolver(charset)
self.representation_resolver = RepresentationResolver(self.charset)
self.operation_representation_grid = Grid()
self.observable_representation_grid = Grid()
self.operation_decoration_indices = []
Expand All @@ -91,15 +103,15 @@ def __init__(

CircuitDrawer.pad_representation(
self.operation_representation_grid,
charset.WIRE,
self.charset.WIRE,
"",
2 * charset.WIRE,
2 * self.charset.WIRE,
self.operation_decoration_indices,
)

CircuitDrawer.pad_representation(
self.operation_representation_grid,
charset.WIRE,
self.charset.WIRE,
"",
"",
set(range(self.operation_grid.num_layers)) - set(self.operation_decoration_indices),
Expand All @@ -108,14 +120,14 @@ def __init__(
CircuitDrawer.pad_representation(
self.observable_representation_grid,
" ",
charset.MEASUREMENT + " ",
self.charset.MEASUREMENT + " ",
" ",
self.observable_decoration_indices,
)

CircuitDrawer.pad_representation(
self.observable_representation_grid,
charset.WIRE,
self.charset.WIRE,
"",
"",
set(range(self.observable_grid.num_layers)) - set(self.observable_decoration_indices),
Expand Down
11 changes: 2 additions & 9 deletions pennylane/circuit_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import numpy as np

from pennylane.wires import Wires
from .circuit_drawer import CHARSETS, CircuitDrawer
from .circuit_drawer import CircuitDrawer


def _by_idx(x):
Expand Down Expand Up @@ -604,18 +604,11 @@ def draw(self, charset="unicode", wire_order=None, show_all_wires=False):

grid, obs = self.greedy_layers(wire_order=wire_order, show_all_wires=show_all_wires)

if charset not in CHARSETS:
raise ValueError(
"Charset {} is not supported. Supported charsets: {}.".format(
charset, ", ".join(CHARSETS.keys())
)
)

drawer = CircuitDrawer(
grid,
obs,
wires=wire_order or self.wires,
charset=CHARSETS[charset],
charset=charset,
show_all_wires=show_all_wires,
)

Expand Down
36 changes: 36 additions & 0 deletions tests/circuit_drawer/test_circuit_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pennylane.circuit_drawer import CircuitDrawer
from pennylane.circuit_drawer.circuit_drawer import _remove_duplicates
from pennylane.circuit_drawer.grid import Grid, _transpose
from pennylane.circuit_drawer.charsets import CHARSETS, UnicodeCharSet, AsciiCharSet
from pennylane.wires import Wires

from pennylane.measure import state
Expand Down Expand Up @@ -65,6 +66,41 @@ def test_remove_duplicates(self, input, expected_output):
]


class TestInitialization:
def test_charset_default(self):

drawer_None = CircuitDrawer(
dummy_raw_operation_grid, dummy_raw_observable_grid, Wires(range(6)), charset=None
)

assert isinstance(drawer_None.charset, UnicodeCharSet)

@pytest.mark.parametrize("charset", ("unicode", "ascii"))
def test_charset_string(self, charset):

drawer_str = CircuitDrawer(
dummy_raw_operation_grid, dummy_raw_observable_grid, Wires(range(6)), charset=charset
)

assert isinstance(drawer_str.charset, CHARSETS[charset])

@pytest.mark.parametrize("charset", (UnicodeCharSet, AsciiCharSet))
def test_charset_class(self, charset):

drawer_class = CircuitDrawer(
dummy_raw_operation_grid, dummy_raw_observable_grid, Wires(range(6)), charset=charset
)

assert isinstance(drawer_class.charset, charset)

def test_charset_error(self):

with pytest.raises(ValueError, match=r"Charset nope is not supported."):
albi3ro marked this conversation as resolved.
Show resolved Hide resolved
CircuitDrawer(
dummy_raw_operation_grid, dummy_raw_observable_grid, Wires(range(6)), charset="nope"
)


@pytest.fixture
def dummy_circuit_drawer():
"""A dummy CircuitDrawer instance."""
Expand Down