-
Notifications
You must be signed in to change notification settings - Fork 575
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
Hamiltonians created via addition produce incorrect results with ExpvalCost(optimize=True)
#1389
Comments
Thanks for reporting this @KetpuntoG! Someone from the team will help investigate and pin down this bug shortly 🙂 |
ExpvalCost(optimize=True)
Hi @KetpuntoG, Thank you for drawing our attention to this! 🙂 Could you perhaps post a non-working example? I attempted to reproduce the error, though, with a naive example, the import pennylane as qml
import numpy as np
dev = qml.device('default.qubit', wires=4)
coeffs = [1]
obs1 = [
qml.PauliZ(0) @ qml.PauliZ(1)
]
H1 = qml.vqe.Hamiltonian(coeffs, obs1)
obs2 = [
qml.PauliZ(1) @ qml.PauliZ(0)
]
H2 = qml.vqe.Hamiltonian(coeffs, obs2)
ansatz = qml.templates.StronglyEntanglingLayers
dev = qml.device("default.qubit", wires=4)
params = np.ones((3,4,3))
cost1 = qml.ExpvalCost(ansatz, H1 + H2, dev, parallel=False)
cost2 = qml.ExpvalCost(ansatz, H1 + H2, dev, parallel=True)
print(np.allclose(cost1(params), cost2(params)))
When checking the terms of the Hamiltonian that is the sum of print((H1 + H2).terms)
Just for internally keeping track of bugs, removing the bug label for now, but I'll readd once we've tracked down the underlying specific issue. 🙂 |
Hi! Here is part of the code. In the first part I am creating a 19 qubit Hamiltonian with random coefficients:
In my case the output is: vqe |
Hi @KetpuntoG, thank you! Definitely something odd going on, we'll be investigating this. Thanks again for catching and reporting it. :) |
Hi @KetpuntoG, thanks again for catching this! We now have a PR addressing the issue. There are two relevant points that are unrelated to the bug:
Would suggest modifying the second for i in range(size):
if np.random.rand() < prob and i!=j:
obs.append(qml.PauliZ(wires = j) @ qml.PauliZ(wires = i)) |
Good afternoon,
I was having problem with vqe when I set the flag: optmize = True . I noticed that there were errors when entering Hamiltonians of the form Z1@Z2 + Z2@Z1. We know it is the same as 2.Z1.Z2 but the optimizer seems to have some kind of problem with this kind of Hamiltonians (vqe with optimize = False works correctly in this situation).
Although by preprocessing the Hamiltonian we avoid the problem, I was writing to take the problem into account.
Greetings!
The text was updated successfully, but these errors were encountered: