-
Notifications
You must be signed in to change notification settings - Fork 615
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lazy loading of Quantumscript properties (#5696)
**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: ```python 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]
- Loading branch information
1 parent
e2141d1
commit 9c9f650
Showing
14 changed files
with
179 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.