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

[BUG] Zero-valued JVPs of gradient-free tapes with shot vectors have the wrong shape #6220

Closed
1 task done
dwierichs opened this issue Sep 5, 2024 · 0 comments · Fixed by #6219
Closed
1 task done
Labels
bug 🐛 Something isn't working

Comments

@dwierichs
Copy link
Contributor

Expected behavior

Found by @astralcai

Gradient-free tapes with shot vectors can be differentiated, giving a zero with the right output shape.

Actual behavior

The output shape is off.

Additional information

No response

Source code

import jax
import jax.numpy as jnp
import pennylane as qml

dev = qml.device("default.qubit", wires=1)

def cost(theta):
    tape1 = qml.tape.QuantumScript([qml.RX(0.5, wires=0)], [qml.expval(qml.Z(0))], shots=[20, 20])
    tape2 = qml.tape.QuantumScript([qml.RY(theta, wires=0)], [qml.expval(qml.Z(0))], shots=[20, 20])
    res1, res2 = qml.execute([tape1, tape2], dev, gradient_fn=qml.gradients.param_shift)
    return res1, res2

params = jnp.array(0.5)
jax.jacobian(cost)(params)

Tracebacks

TypeError: Custom JVP rule _execute_and_compute_jvp for function _execute_wrapper must produce primal and tangent outputs with equal container (pytree) structures, but got PyTreeDef(((*, *), (*, *))) and PyTreeDef((*, (*, *))) respectively.

System information

pl dev

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@dwierichs dwierichs added the bug 🐛 Something isn't working label Sep 5, 2024
mudit2812 pushed a commit that referenced this issue Sep 10, 2024
**Context:**
If a tape has no trainable parameters, generic zero-valued JVPs are
created for it in JVP calculations.
However, these generic calculations are not taking shot vectors
correctly into account, also because they do not use
`MeasurementProcess.shape` correctly.

**Description of the Change:**
Change the generic zero-valued JVPs so they are compatible with shot
vectors.

**Benefits:**
One bug less.

**Possible Drawbacks:**
N/A

**Related GitHub Issues:**
Fixes #6220 

[sc-73033]

---------

Co-authored-by: Christina Lee <[email protected]>
mudit2812 pushed a commit that referenced this issue Sep 12, 2024
**Context:**
If a tape has no trainable parameters, generic zero-valued JVPs are
created for it in JVP calculations.
However, these generic calculations are not taking shot vectors
correctly into account, also because they do not use
`MeasurementProcess.shape` correctly.

**Description of the Change:**
Change the generic zero-valued JVPs so they are compatible with shot
vectors.

**Benefits:**
One bug less.

**Possible Drawbacks:**
N/A

**Related GitHub Issues:**
Fixes #6220 

[sc-73033]

---------

Co-authored-by: Christina Lee <[email protected]>
mudit2812 pushed a commit that referenced this issue Sep 16, 2024
**Context:**
If a tape has no trainable parameters, generic zero-valued JVPs are
created for it in JVP calculations.
However, these generic calculations are not taking shot vectors
correctly into account, also because they do not use
`MeasurementProcess.shape` correctly.

**Description of the Change:**
Change the generic zero-valued JVPs so they are compatible with shot
vectors.

**Benefits:**
One bug less.

**Possible Drawbacks:**
N/A

**Related GitHub Issues:**
Fixes #6220 

[sc-73033]

---------

Co-authored-by: Christina Lee <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant