Open KetpuntoG opened 2 months ago
Would have to do an appropriate re-packaging in https://github.com/PennyLaneAI/pennylane/blob/master/pennylane/ops/op_math/sum.py#L445 and https://github.com/PennyLaneAI/pennylane/blob/master/pennylane/ops/op_math/sum.py#L459 (same for Prod
).
I actually dont understand how to do that with qml.math
, as I keep "losing" the requires_grad
attribute 🤔
>>> coeffs = [pnp.array(0.5, requires_grad=True)]
>>> coeffs_new = qml.math.array(coeffs)
>>> coeffs, coeffs_new
([tensor(0.5, requires_grad=True)], array([0.5]))
(same for asarray
Also, what happens if one coefficient requires grad and the other doesn't?
Also, what happens if one coefficient requires grad and the other doesn't?
hmm I am not sure which one should be the correct one but the important thing is that it is common in both cases I would say
Actually this produces an SProd
, not a Sum
coeffs = pnp.array([0.5], requires_grad=True) op = qml.dot(coeffs, [X(0)])
@KetpuntoG Would you be able to provide more context regarding when this causes an issue?
I'm creating the template qml.Qubitization that takes as input a Hamiltonian. In this line I wrote:
coeffs, ops = hamiltonian.terms()
If the Hamiltonian is a LinearCombination
, this will be differentiable because coeffs = np.array([......], requires_grad = True)
. However, if I use qml.dot
to define the Hamiltonian, this will not be differentiable because coeffs = [np.array(, True), np.array(, True),....]
that I cannot put together again to make it differentiable. (We tried hstack
but it doesn't work). Maybe is related with this?
I'm creating the template qml.Qubitization that takes as input a Hamiltonian. In this line I wrote:
coeffs, ops = hamiltonian.terms()
If the Hamiltonian is a
LinearCombination
, this will be differentiable becausecoeffs = np.array([......], requires_grad = True)
. However, if I useqml.dot
to define the Hamiltonian, this will not be differentiable becausecoeffs = [np.array(, True), np.array(, True),....]
that I cannot put together again to make it differentiable. (We triedhstack
but it doesn't work). Maybe is related with this?
Thank you for the context! I'll look into this.
I'm creating the template qml.Qubitization that takes as input a Hamiltonian. In this line I wrote:
coeffs, ops = hamiltonian.terms()
If the Hamiltonian is a
LinearCombination
, this will be differentiable becausecoeffs = np.array([......], requires_grad = True)
. However, if I useqml.dot
to define the Hamiltonian, this will not be differentiable becausecoeffs = [np.array(, True), np.array(, True),....]
that I cannot put together again to make it differentiable. (We triedhstack
but it doesn't work). Maybe is related with this?
If the problem with hstack
is that it loses requires_grad
, would something like
qml.math.convert_like(qml.math.hstack(coeffs), coeffs[0])
fix the issue?
qml.math.convert_like(qml.math.hstack(coeffs), coeffs[0])
Nice! This is what I was looking for! Thanks!
@KetpuntoG you can use that in your code when you call terms
to re-package the coeffs
, but we should add that as well to Prod.terms()
, Sum.terms()
as well as SProd.terms()
.
Nice! This is what I was looking for! Thanks! @KetpuntoG you can use that in your code when you call
terms
to re-package thecoeffs
, but we should add that as well toProd.terms()
,Sum.terms()
as well asSProd.terms()
.
In this case terms()
will now always return an array instead of a list for the coefficients. Is that expected?
I could not get it to work. We can try to fix it once the Sum has better grad support :)
Expected behavior
(after discussion with @Qottmann )
The problem is that
Sum.terms()
doesn't neatly re-package the coefficients but just puts them in a list. This is a problem sinceSum
andLinearCombination
have different behaviors (causing me problems with differentiability). The expected behavior would be:Actual behavior
Additional information
No response
Source code
No response
Tracebacks
No response
System information
Existing GitHub issues