PennyLaneAI / pennylane

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
https://pennylane.ai
Apache License 2.0
2.17k stars 568 forks source link

[BUG] `split_non_commuting` is not differentiable with trainable observables #5837

Closed astralcai closed 2 weeks ago

astralcai commented 2 weeks ago

Expected behavior

import pennylane as qml
import numpy as np
import pennylane.numpy as pnp

dev = qml.device("default.qubit", wires=2, shots=50000)

@qml.qnode(dev)
def circuit(coeff1, coeff2):
    qml.RX(np.pi / 4, wires=0)
    qml.RY(np.pi / 4, wires=1)
    return qml.expval(
        qml.Hamiltonian([coeff1, coeff2], [qml.Y(0) @ qml.Z(1), qml.X(1)])
    )

def cost(theta, phi):
    return circuit(theta, phi)

params = pnp.array(pnp.pi / 4), pnp.array(3 * pnp.pi / 4)
>>> qml.jacobian(cost)(*params)
(array(-0.49796), array(0.7084))

Actual behavior

If we apply split_non_commuting:

>>> circuit = qml.transforms.split_non_commuting(circuit)
>>> qml.jacobian(cost)(*params)
(array(0.), array(0.))

Additional information

This is caused by this line: https://github.com/PennyLaneAI/pennylane/blob/d70f61aed57497712f3c5c86033aa9747c7fb029/pennylane/transforms/split_non_commuting.py#L696

Source code

No response

Tracebacks

No response

System information

Name: PennyLane
Version: 0.37.0.dev0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /Users/astral.cai/Workspace/pennylane
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing_extensions
Required-by: PennyLane_Lightning

Platform info:           macOS-14.5-arm64-arm-64bit
Python version:          3.9.19
Numpy version:           1.26.4
Scipy version:           1.12.0
Installed devices:
- default.clifford (PennyLane-0.37.0.dev0)
- default.gaussian (PennyLane-0.37.0.dev0)
- default.mixed (PennyLane-0.37.0.dev0)
- default.qubit (PennyLane-0.37.0.dev0)
- default.qubit.autograd (PennyLane-0.37.0.dev0)
- default.qubit.jax (PennyLane-0.37.0.dev0)
- default.qubit.legacy (PennyLane-0.37.0.dev0)
- default.qubit.tf (PennyLane-0.37.0.dev0)
- default.qubit.torch (PennyLane-0.37.0.dev0)
- default.qutrit (PennyLane-0.37.0.dev0)
- default.qutrit.mixed (PennyLane-0.37.0.dev0)
- null.qubit (PennyLane-0.37.0.dev0)
- lightning.qubit (PennyLane-Lightning-0.36.0)

Existing GitHub issues