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

Make tape.trainable_params a list #1904

Merged
merged 4 commits into from
Nov 17, 2021
Merged

Conversation

dwierichs
Copy link
Contributor

Context:
Recently, the trainable_params property of QuantumTapes, which is a set, 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 that

>>> {3, 1}
{1, 3}

This makes constructing small tests that fail based on trainable_params being a set quite hard.

Description of the Change:
QuantumTape.trainable_params is made a list instead, with an intermediate conversion to set 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:

@dwierichs dwierichs changed the title main commit Make tape.trainable_params a list Nov 16, 2021
@codecov
Copy link

codecov bot commented Nov 16, 2021

Codecov Report

Merging #1904 (d5b99d5) into master (e465f6f) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1904   +/-   ##
=======================================
  Coverage   98.90%   98.90%           
=======================================
  Files         219      219           
  Lines       16790    16791    +1     
=======================================
+ Hits        16606    16607    +1     
  Misses        184      184           
Impacted Files Coverage Δ
pennylane/tape/tape.py 98.92% <100.00%> (+<0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e465f6f...d5b99d5. Read the comment docs.

Copy link
Member

@josh146 josh146 left a 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)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
[(#1xxx)](https://github.com/PennyLaneAI/pennylane/pull/1xxx)
[(#1904)](https://github.com/PennyLaneAI/pennylane/pull/1904)

we're almost at the #2000 milestone 🤯

Copy link
Member

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

Copy link
Contributor Author

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! :)

Comment on lines +499 to +501
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
Copy link
Member

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"]
Copy link
Member

Choose a reason for hiding this comment

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

a nice micro-optimization :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

:)

@dwierichs dwierichs merged commit f9146e1 into master Nov 17, 2021
@dwierichs dwierichs deleted the trainable_params-as-list branch November 17, 2021 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants