-
Notifications
You must be signed in to change notification settings - Fork 61
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
Expectation value of a Hamiltonian #283
Comments
Do you have references about this feature? |
I do not have any, but I don't think it is necessary. Since we measure in the Z basis, it would be immediate in this case. For the X and Y basis, I would apply a Hadamard gate or the equivalent in Y to measure in another basis |
Thanks for opening this, it is certainly not possible to do directly with Qibo. For now, I will try to find a temporary solution using custom code and will write here if I come up with something. It would be great to add it as a feature. I agree that a reference is not needed, I am just not sure how easy it would be to implement this with the current representation we have for Hamiltonians, which is mainly based on numpy arrays. We may need redesign this a bit. |
I found a relatively simple way to do this using our existing # construct the circuit that produces the measurements in the Z basis
c = models.Circuit(nqubits)
c.add(...) # all your gates
# measure all qubits in the Z basis
c.add(gates.M(*range(nqubits)))
# execute
result = c(nshots=nshots)
# get the decimal samples as np.array
measurements = result.samples(binary=False).numpy()
# get the final state vector as np.array
# note this is the state before the measurement (not collapsed)
final_state = c.final_state.numpy()
# construct your Hamiltonian as `TrotterHamiltonian`, eg.
ham = hamiltonians.TFIM(nqubits, trotter=True)
# calculate the EV using measurements
ev = 0
psiloc0 = final_state[measurements]
# add Hamiltonian terms one-by-one
for term in ham.terms():
psiloc = term(np.copy(final_state)).numpy()[measurements]
# np.copy is important here, otherwise the custom operator will alter the state
# and the remaining terms will be wrong!
ev += (psiloc / psiloc0).mean()
# (optional) calculate exact EV to compare
exact_ev = ham.expectation(final_state) If you try this you will see that Perhaps it is not exactly what we should implement in Qibo but let me know if it helps. |
In order to make clearer what the code in the previous post does, the idea is based in the following equation for the expectation value of an arbitrary operator A: Note that while As I said, this approach still uses the full state vector so it is not the one you would use in the quantum computer, however I think it can give an estimate of the statistical noise in the EV, since it is not an exact matrix multiplication calculation. It is also slightly easier to implement because it does not require rotating and re-measuring for every term like the pure quantum approach would require. |
Thanks for your notes and your code Stavros, it is now clear to me. The fact that we need the state to compute the hamiltonian is not a problem for me, since we are simulating in the most efficient manner we are able to do. I just have a question: you are using a hamiltonian TFIM which can be trotterized. What if we want to compute the expected value of a Hamiltonian that has not been defined in the same way? Can I just trotterize any hamiltonian (from a qibo perspective) |
Thanks for checking this.
Yes, you can trotterize (from the qibo perspective) any Hamiltonian. The I think the easiest way to define your custom Hamiltonian as |
Hi all,
I am working on a new algorithm that is to be implementable in an actual experiment. In order to get the feeling about what is going on, I would need to compute the expectation value for a given Hamiltonian with shots and not with the exact wavefunction of the state. In other words, it would be like measuring in the real physical world. Is it possible?
Thank you all
The text was updated successfully, but these errors were encountered: