-
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
Cache mapped wires in Device
#1270
Conversation
Hello. You may have forgotten to update the changelog!
|
Codecov Report
@@ Coverage Diff @@
## master #1270 +/- ##
=======================================
Coverage 98.13% 98.13%
=======================================
Files 147 147
Lines 11250 11256 +6
=======================================
+ Hits 11040 11046 +6
Misses 210 210
Continue to review full report at Codecov.
|
Some benchmarks: GradientComputation_light.time_gradient Commit 8fe31ad
Commit 051f845
ML_light Commit 8fe31ad Mem:
Time:
Commit 051f845 Mem:
Time:
|
pennylane/_device.py
Outdated
@@ -340,6 +341,9 @@ def map_wires(self, wires): | |||
Returns: | |||
Wires: wires with new labels | |||
""" | |||
if wires in self._cached_wires: | |||
return self._cached_wires[wires] | |||
|
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.
Nice solution for now. When we are refactoring the device class (if this ever happens :) I will help turning all the current patches into a runtime-conscious solution overall!
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.
Looks good from my end! Agree with @mariaschuld re: having a more runtime specific solution.
A very minor thought that crossed my head while writing this - would @functools.lru_cache
achieve the same result with less complexity?
Context:
Wires used internally in devices get mapped based on the initial map wiring defined. Although doing this wire mapping is not too computationally intensive when done a single time, when running an optimization task where it's done thousands of times, the cost for computing mapped wires is non-negligible in terms of time.
Often devices query the mapping for the same wires several times. As the wire mapping of a device is set only when the device is created, it is safe to assume that it's immutable. Therefore we can assume that the value of mapping wires will also not change over the life of the device.
Description of the Change:
map_wires
, theDevice
class now caches the key-value pair of the wires and the mapped wires in a dictionaryhash
of aWires
object is cachedBenefits:
More performant device and
Wires
class usage.Possible Drawbacks:
N/A
Related GitHub Issues:
N/A