-
Notifications
You must be signed in to change notification settings - Fork 575
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
Add __add__
to Wires class.
#812
Conversation
@@ -161,7 +192,7 @@ def indices(self, wires): | |||
""" | |||
Return the indices of the wires in this Wires object. | |||
|
|||
For example, | |||
**Example:** | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Snuck those in to match the correct version of the new functions.
Codecov Report
@@ Coverage Diff @@
## master #812 +/- ##
=======================================
Coverage 90.56% 90.57%
=======================================
Files 126 126
Lines 8213 8219 +6
=======================================
+ Hits 7438 7444 +6
Misses 775 775
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mariaschuld! This is great. Don't forget to update the changelog as well.
By the way, I'm somewhat glad that sum([wires1, wires2, wires3])
isn't natively supported. To me, this isn't very intuitive --- on first read, I'm not sure what it would do, sum the wire labels?
I guess this is because:
-
I'm so used to NumPy, where
sum
andconcatenate
are two separate operations -
sum
doesn't concatenate Python lists, either (it only acts on a single list).
pennylane/wires.py
Outdated
def __add__(self, other): | ||
"""Defines the addition to return a Wires object containing all wires of the two terms. | ||
|
||
**Example:** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**Example:** | |
**Example** |
😆
With Args:
and Kwargs:
, Sphinx now automatically removes the colon in version 2.0+ (in version 1.X, it didn't).
Also, it might be good to move it below the Args:
and Returns:
, to match other docstrings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes!
@cgogolin had the great request to add
_add__
and__radd__
to the wires class as a shortcut toall_wires()
:This PR adds the two functions and two tests.
Drawbacks:
Unfortunately, this does not allow native use of
sum([wires1, wires2, wires3])
, which would be quite elegant in our code base.sum
requires a start object which is0
by default, so the0
wire would always be added.One could pass the start object explicitly and use
sum([wires1 + wires2 + wires3], Wires([]))
which produces the same result, but is arguably more confusing thanall_wires([wires1 + wires2 + wires3])
.