-
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
Make tape.trainable_params
a list
#1904
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1904 +/- ##
=======================================
Coverage 98.90% 98.90%
=======================================
Files 219 219
Lines 16790 16791 +1
=======================================
+ Hits 16606 16607 +1
Misses 184 184
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 for fixing this @dwierichs!
* `QuantumTape.trainable_params` now is a list instead of a set. This | ||
means that `tape.trainable_params` will return a list unlike before, | ||
but setting the `trainable_params` with a set works exactly as before. | ||
[(#1xxx)](https://github.com/PennyLaneAI/pennylane/pull/1xxx) |
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.
[(#1xxx)](https://github.com/PennyLaneAI/pennylane/pull/1xxx) | |
[(#1904)](https://github.com/PennyLaneAI/pennylane/pull/1904) |
we're almost at the #2000 milestone 🤯
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.
@dwierichs I think you forgot this one 😆 I'll incorporate it into #1807, I'm currently resolving changelog conflicts over there
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.
Yeah, sorry, I forgot the second occurrence of the PR number in there 😅 Maria already pointed this out to me as well :D
Thanks for taking care of it! :)
self._par_info.keys() is assumed to be sorted | ||
As its order is maintained, this assumes that self._par_info | ||
is created in a sorted manner, as in _update_par_info |
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.
lucky 😬
|
||
# get the corresponding operation parameter index | ||
# (that is, index of the parameter within the operation) | ||
p_idx = self._par_info[t_idx]["p_idx"] | ||
p_idx = info["p_idx"] |
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.
a nice micro-optimization :)
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.
:)
Context:
Recently, the
trainable_params
property ofQuantumTape
s, which is aset
, caused bugs based on the fact that it is not guaranteed to maintain its ordering.In particular for larger tapes these rare instances seem to occur, which likely is the reason why it was not caught before.
As an example, consider the "unordered" set
{3, 1}
. As integer hashes are the integers themselves, a "canonical" ordering for python is according to the hash table, so thatThis makes constructing small tests that fail based on
trainable_params
being a set quite hard.Description of the Change:
QuantumTape.trainable_params
is made alist
instead, with an intermediate conversion toset
and subsequent sorting within the attribute setter function to exclude duplicates and assure the entries are sorted. Whenever the uniqueness and ordering of the entries is guaranteed, the private attribute_trainable_params
may be set directly, as is done in_update_trainable_params
.Benefits:
Fixes rare but apparently increasingly important edge case bugs where the operation/parameter ordering was disturbed by the loss of ordering within
trainable_params
.Possible Drawbacks:
For very large tapes with many trainable parameters, a lookup will be slower.
Related GitHub Issues: