Skip to content

Commit

Permalink
Hadamard gradient transform (#3625)
Browse files Browse the repository at this point in the history
* Draft

* Trainable params

* Working prototype for RX RY RZ

* Add op_id

* Change tape p idx

* Multiple measurements support

* Probs

* Rot added

* Rename

* Indep params

* Add tests

* probs working

* Test expval and probs

* CR Ising and black

* Typos

* Tests aux wires

* Decomposition

* Small changes

* Small changes

* More black

* Pylint tests

* Pylint tests

* Fix test

* Typo

* Update

* Apply suggestions from code review

Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: Matthew Silverman <matthews@xanadu.ai>

* Apply black

* From review

* From review

* Test PauliRot

* pylint tests

* Fix multi measurement and multi tape

* Doc

* Changelog

* Change test name

* Fix doc

* Apply suggestions from code review

Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>

* Review

* Update doc/releases/changelog-dev.md

Co-authored-by: Isaac De Vlugt <34751083+isaacdevlugt@users.noreply.github.com>

* Pylint

* Add to doc

* Apply suggestions from code review

Co-authored-by: Matthew Silverman <matthews@xanadu.ai>

* Note

* Add init expand

* Update pennylane/gradients/hadamard_gradient.py

Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>

* Black

---------

Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
Co-authored-by: Isaac De Vlugt <34751083+isaacdevlugt@users.noreply.github.com>
  • Loading branch information
4 people authored Feb 3, 2023
1 parent 30b58bf commit e412162
Show file tree
Hide file tree
Showing 9 changed files with 1,569 additions and 2 deletions.
22 changes: 22 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@

<h4>Always differentiable 📈</h4>

* The Hadamard test gradient tranform is now available via `qml.gradients.hadamard_grad`.
[#3625](https://github.com/PennyLaneAI/pennylane/pull/3625)

`qml.gradients.hadamard_grad` is a hardware-compatible transform that calculates the
gradient of a quantum circuit using the Hadamard test. Note that the device requires an
auxiliary wire to calculate the gradient.

```pycon
>>> dev = qml.device("default.qubit", wires=2)
>>> @qml.qnode(dev)
... def circuit(params):
... qml.RX(params[0], wires=0)
... qml.RY(params[1], wires=0)
... qml.RX(params[2], wires=0)
... return qml.expval(qml.PauliZ(0))
>>> params = np.array([0.1, 0.2, 0.3], requires_grad=True)
>>> qml.gradients.hadamard_grad(circuit)(params)
(tensor([-0.3875172], requires_grad=True),
tensor([-0.18884787], requires_grad=True),
tensor([-0.38355704], requires_grad=True))
```

* The gradient transform `qml.gradients.spsa_grad` is now registered as a
differentiation method for `QNode`s.
[#3440](https://github.com/PennyLaneAI/pennylane/pull/3440)
Expand Down
3 changes: 3 additions & 0 deletions pennylane/gradients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
param_shift_cv
param_shift_hessian
spsa_grad
hadamard_grad
Custom gradients
^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -317,6 +318,7 @@ def my_custom_gradient(tape, **kwargs):
from . import parameter_shift_hessian
from . import finite_difference
from . import spsa_gradient
from . import hadamard_gradient

from .gradient_transform import gradient_transform, SUPPORTED_GRADIENT_KWARGS
from .hessian_transform import hessian_transform
Expand All @@ -327,6 +329,7 @@ def my_custom_gradient(tape, **kwargs):
from .vjp import compute_vjp, batch_vjp, vjp, compute_vjp_multi_new, compute_vjp_single_new
from .jvp import batch_jvp, jvp, compute_jvp_multi, compute_jvp_single
from .spsa_gradient import spsa_grad
from .hadamard_gradient import hadamard_grad

from .hamiltonian_grad import hamiltonian_grad
from .general_shift_rules import (
Expand Down
1 change: 1 addition & 0 deletions pennylane/gradients/gradient_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"shots",
"strategy",
"validate_params",
"device_wires",
]


Expand Down
Loading

0 comments on commit e412162

Please sign in to comment.