Comments (9)
Update:
After some investigation with the help of @soranjh, it appears that the error is raised if the given Hamiltonian has an identity term with a non-zero coefficient.
E.g.
import pennylane as qml
from pennylane.optimize.adaptive import append_gate
from pennylane import numpy as pnp
coeffs = pnp.array([0.2, -0.543, 1], requires_grad=False)
obs = [qml.X(0) @ qml.Z(1), qml.Z(0) @ qml.X(1), qml.I(0)]
H = qml.ops.LinearCombination(coeffs, obs)
operator_pool = [qml.CRX(0.0, [0, 1]), qml.CRY(0.0, [1, 2])]
dev = qml.device("default.qubit", wires=10)
@qml.transforms.split_non_commuting
@qml.qnode(dev, diff_method="adjoint")
def circuit():
for i in range(3):
qml.RX(0.5, i)
return qml.expval(H)
circuit()
params = pnp.array([0.1, 0.2], requires_grad=True)
qml.grad(lambda p: append_gate(circuit, p, operator_pool)())(params)
A proposed solution is in #6022
from pennylane.
Note that the issue is not particularily with the qulacs device, but with split_non_commuting
.
Sticking qml.transforms.split_non_commuting
onto the qnode causes the same error.
from pennylane.optimize.adaptive import append_gate
from pennylane import numpy as np
H, qubits = qml.qchem.molecular_hamiltonian(
["Li", "H"],
np.array([0.0, 0.0, 0.0, 0.0, 0.0, 2.969280527]),
active_electrons=2,
active_orbitals=5
)
operator_pool = [qml.CRX(0.0, [0, 1]), qml.CRY(0.0, [1, 2])]
dev = qml.device("default.qubit", wires=10)
@qml.transforms.split_non_commuting
@qml.qnode(dev, diff_method="parameter-shift")
def circuit():
for i in range(3):
qml.RX(0.5, i)
return qml.expval(H)
params = np.array([0.1, 0.2], requires_grad=True)
qml.grad(lambda p: append_gate(circuit, p, operator_pool)())(params)
from pennylane.
This may also be related to #5824
from pennylane.
Thanks @albi3ro! I suspected it was something to do with the Hamiltonian, but didn't realize it was because H
had non-commuting terms.
Is the bug present with just split_non_commuting
and non-H-commuting-supporting-devices + grad
? In which case, we could remove the append_gate
transform from the minimal error example
from pennylane.
It has less to do with the qulacs simulator, and more about a device that doesn't natively support hamiltonians.
Why append_gate
causes this, i have no idea.
from pennylane.
oops, typo in my message, have updated it!
from pennylane.
Never mind. This has nothing to do with append_gate
.
The issue can also be replicated with:
H = pnp.array(0.5, requires_grad=False) * qml.Y(0)
dev = qml.device("default.qubit", wires=10)
@qml.transforms.split_non_commuting
@qml.qnode(dev, diff_method="adjoint")
def circuit(x):
qml.RX(x, 0)
return qml.expval(H)
params = pnp.array(0.1, requires_grad=True)
qml.grad(circuit)(params)
from pennylane.
Thanks, that's great! Much more minimal now
from pennylane.
Sorry for re-opening this issue. The original code block (w/ minor changes) still raises a NonDifferentiableError
:
import pennylane as qml
from pennylane.optimize.adaptive import append_gate
from pennylane import numpy as np
H, qubits = qml.qchem.molecular_hamiltonian(
["Li", "H"],
np.array([0.0, 0.0, 0.0, 0.0, 0.0, 2.969280527]),
active_electrons=2,
active_orbitals=5
)
operator_pool = [qml.CRX(0.0, [0, 1]), qml.CRY(0.0, [1, 2])]
dev = qml.device("default.qubit", wires=10)
@qml.transforms.split_non_commuting
@qml.qnode(dev, diff_method="adjoint")
def circuit():
for i in range(3):
qml.RX(0.5, i)
return qml.expval(H)
params = np.array([0.1, 0.2], requires_grad=True)
qml.grad(lambda p: append_gate(circuit, p, operator_pool)())(params)
Error:
>>> qml.grad(lambda p: append_gate(circuit, p, operator_pool)())(params)
NonDifferentiableError: -6.917117171256823 is non-differentiable. Set the requires_grad attribute to True.
from pennylane.
Related Issues (20)
- [BUG] Shape mismatch occurs in gradient calculation when using an indexing operation with batched inputs and autograd HOT 1
- [BUG] qml.ThermalRelaxationError argument typo HOT 1
- [BUG] `qml.exp` does not work with `jax.jit` and workflows that require decomposition, since `Exp.decomposition` is not traceable HOT 5
- [BUG] `qml.equal` doesn't work with `PauliError` instances
- [BUG] `single_qubit_fusion` gets rid of the `GlobalPhase`
- [BUG] `BasisRotation` is not JAX compatible HOT 2
- [BUG] `BasisState` does not work with `jax.jit`
- [BUG] `BasisEmbedding` gives the wrong decomposition
- [BUG] `BasisEmbedding` does not work with `lightning.qubit` and `jax.jit` HOT 2
- [BUG] Mid-circuit measurements cannot be added to tapes in transforms HOT 9
- [BUG] jax interface does not support multicontrol gates HOT 1
- [BUG] `qml.qsvt` breaks with some angles
- [BUG] qml.expval returns unexpected values HOT 2
- LinAlgError: Singular matrix when trying to do qml.lie_closure on Heisenberg model
- [BUG] Discrepancy in output values of the example of `qml.pulse.drive` HOT 1
- [BUG] `SPSAOptimizer.step_and_cost` ignores keyword arguments HOT 1
- [BUG] Loading the MQT Bench dataset does not work as expected. HOT 1
- [BUG] Array indexing error when sampling after mid-circuit measurements are performed on other wires HOT 3
- NotImplementedError: VJP of eigvals wrt argnums (0,) not defined[BUG] HOT 2
- Implement `Hermitian.decomposition`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pennylane.