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

Lazy loading of Quantumscript properties #5696

Merged
merged 14 commits into from
May 24, 2024

Conversation

EmilianoG-byte
Copy link
Collaborator

@EmilianoG-byte EmilianoG-byte commented May 16, 2024

Context:
Eager initialization of parameters inside the constructor of QuantumScript induces a high classical overhead.

Description of the Change:
The following attributes of QuantumScript are no longer initialized in the constructor, but only when needed:

  • par_info
  • wires, num_wires
  • obs_sharing_wires, obs_sharing_wires_id

par_info, obs_sharing_wires, and obs_sharing_wires_id are now public attributes as they were used in several files outside the QuantumScript class. Examples of this behaviour are:

  • pennylane/_qubit_device.py
  • pennylane/pennylane/circuit_graph.py
  • pennylane/pennylane/fourier/qnode_spectrum.py
  • pennylane/pennylane/gradients/adjoint_metric_tensor.py
  • pennylane/pennylane/gradients/gradient_transform.py
  • pennylane/pennylane/transforms/tape_expand.py

The most expensive attributes: par_info and wires are now a @cached_property to avoid calculating them every time they are needed.

When used inside the QuantumTape context, the _update() method is called to invalidate the cached value and allow for recalculation.

Benefits:
Reduce in the classical overhead of creating a QuantumScript. For instance, for the following code:

n_wires = 5
n_layers = 50

dev = qml.device('null.qubit')

@qml.qnode(dev, diff_method="parameter-shift")
def circuit(params):
    qml.StronglyEntanglingLayers(params, wires=range(n_wires))
    return qml.expval(qml.PauliZ(n_wires-1))

shape = qml.StronglyEntanglingLayers.shape(n_wires=n_wires, n_layers=n_layers)
rng = np.random.default_rng(seed=42)
params = np.array(rng.random(shape))
qml.grad(circuit)(params)

The QuantumScript creation time decreases from 62.51% of the whole time, to 41.92%.

Related Shortcut Stories:
[sc-45973]

Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@EmilianoG-byte EmilianoG-byte marked this pull request as ready for review May 16, 2024 19:00
Copy link

codecov bot commented May 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.67%. Comparing base (58668d7) to head (563c81c).
Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5696      +/-   ##
==========================================
- Coverage   99.68%   99.67%   -0.01%     
==========================================
  Files         416      416              
  Lines       39049    38808     -241     
==========================================
- Hits        38925    38683     -242     
- Misses        124      125       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@EmilianoG-byte EmilianoG-byte changed the title Quantumscript initialization [WIP] Quantumscript initialization May 16, 2024
pennylane/tape/qscript.py Outdated Show resolved Hide resolved
Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

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

Looks great! Just one comment, otherwise ready to approve.

pennylane/tape/qscript.py Show resolved Hide resolved
pennylane/tape/qscript.py Outdated Show resolved Hide resolved
pennylane/tape/qscript.py Outdated Show resolved Hide resolved
pennylane/tape/tape.py Outdated Show resolved Hide resolved
@EmilianoG-byte EmilianoG-byte changed the title Quantumscript initialization Lazy loading of Quantumscript properties May 17, 2024
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

👍 Looks good to me 🎉

Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

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

Looks good, one very tiny suggestion and then I'll approve.

pennylane/tape/qscript.py Outdated Show resolved Hide resolved
@EmilianoG-byte EmilianoG-byte merged commit 9c9f650 into master May 24, 2024
40 checks passed
@EmilianoG-byte EmilianoG-byte deleted the quantumscript_initialization branch May 24, 2024 16:30
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

3 participants