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

Fix jacobian shape in VJP for measurements with shape and batched inputs #5986

Merged
Merged
3 changes: 3 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
* `qml.AmplitudeEmbedding` has better support for features using low precision integer data types.
[(#5969)](https://github.com/PennyLaneAI/pennylane/pull/5969)

* Jacobian shape is fixed for measurements with dimension in `qml.gradients.vjp.compute_vjp_single`.
[(5986)](https://github.com/PennyLaneAI/pennylane/pull/5986)

<h3>Contributors ✍️</h3>

This release contains contributions from (in alphabetical order):
majafranz marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion pennylane/gradients/vjp.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def compute_vjp_single(dy, jac, num=None):

# Single measurement with dimension e.g. probs
else:
jac = qml.math.stack(jac)
jac = qml.math.reshape(qml.math.stack(jac), (-1, num))
mudit2812 marked this conversation as resolved.
Show resolved Hide resolved
try:
res = jac @ dy_row
except Exception: # pylint: disable=broad-except
Expand Down
16 changes: 16 additions & 0 deletions tests/gradients/core/test_vjp.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,19 @@ def test_reduction_extend(self):
# list will correspond to a single input parameter of the combined
# tapes.
assert len(res) == sum(len(t.trainable_params) for t in tapes)

def test_batched_params_probs_jacobian(self):
"""Test if jacobian gets calculated when inputs are batched, multiple
trainable parameters are used and the measurement has a shape (probs)"""

@qml.qnode(qml.device("default.qubit"), diff_method="parameter-shift")
def circuit(x, data):
qml.RX(x[0], 0)
qml.RX(x[1], 0)
qml.RY(data, 0)
return qml.probs(wires=0)

x = qml.numpy.array([0.5, 0.8], requires_grad=True)
data = qml.numpy.array([1.2, 2.3, 3.4], requires_grad=False)
circuit(x, data)
qml.jacobian(circuit)(x, data)
mudit2812 marked this conversation as resolved.
Show resolved Hide resolved
Loading