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

turn QuantumScript.hash into a cached property #5919

Merged
merged 6 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<h3>Improvements 🛠</h3>

* `QuantumScript.hash` is now cached, leading to performance improvements.
albi3ro marked this conversation as resolved.
Show resolved Hide resolved
[(#5919)](https://github.com/PennyLaneAI/pennylane/pull/5919)

<h3>Breaking changes 💔</h3>

<h3>Deprecations 👋</h3>
Expand All @@ -20,4 +23,5 @@

This release contains contributions from (in alphabetical order):

Yushao Chen.
Yushao Chen,
Christina Lee,
4 changes: 3 additions & 1 deletion pennylane/tape/qscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def __init__(
def __repr__(self):
return f"<{self.__class__.__name__}: wires={self.wires.tolist()}, params={self.num_params}>"

@property
@cached_property
albi3ro marked this conversation as resolved.
Show resolved Hide resolved
def hash(self):
"""int: returns an integer hash uniquely representing the quantum script"""
fingerprint = []
Expand Down Expand Up @@ -378,11 +378,13 @@ def _update(self):
"""Update all internal metadata regarding processed operations and observables"""
self._graph = None
self._specs = None
self._trainable_params = None

try:
# Invalidate cached properties so they get recalculated
del self.wires
del self.par_info
del self.hash
albi3ro marked this conversation as resolved.
Show resolved Hide resolved
except AttributeError:
pass

Expand Down
5 changes: 5 additions & 0 deletions tests/tape/test_qscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,16 @@ def test_cached_properties(self):
]
m = [qml.expval(qml.Hermitian(2 * np.eye(2), wires=0))]
qs = QuantumScript(ops, m)
assert qs.wires == qml.wires.Wires((0,))
assert isinstance(qs.par_info, list) and len(qs.par_info) > 0
old_hash = qs.hash
assert qs.trainable_params == [0, 1, 2, 3, 4, 5, 6, 7]
qs._ops = []
qs._measurements = []
qs._update()
assert qs.wires == qml.wires.Wires([])
assert isinstance(qs.par_info, list) and len(qs.par_info) == 0
albi3ro marked this conversation as resolved.
Show resolved Hide resolved
assert QuantumScript([], []).hash == qs.hash and qs.hash != old_hash

# pylint: disable=unbalanced-tuple-unpacking
def test_get_operation(self):
Expand Down
Loading