-
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 from samples #719
Comments
I believe with the current implementation it does not support. @AlejandroSopena could you please confirm? |
In other words, how can I compute the expectation values of a given Pauli string? Keeping in mind that I want to use the same code on a real device eventually. |
It was only implemented on the Z basis, as it only required the measurement gates we already had access to. When your other issue (#720) gets implemented, we can probably directly extend that into multi-basis Hamiltonians by computing the expectation values from samples from the individual terms of the Hamiltonian (better if we consider all commuting terms at once) and reconstructing it after the fact. It will have a performance overhead, but it should be 1-to-1 with the experimental implementation. Therefore we should focus on implementing #720 asap. A quick fix in the meantime would be to add the change of basis in the circuit with the |
But then, how is the expectation value of the default Hamiltonians computed? e.g. for the XXZ in-build Hamiltonian it contains |
I believe the expectation value in this and other examples is calculated using the state vector or density matrix. This is possible for any Hamiltonian using The |
@scarrazza I believe this is not resolved. We implemented the methods required to change the basis of the initial and final states of circuits, however we did not update the |
Good point, let's reopen it. |
I'm trying to compute the expectation value of some (non-diagonal) observable using
expectation_from_samples
but the following error occurs:Am I doing something wrong or this function does not support other observables besides Z?
The pseudocode I'm using is the following:
qc = Circuit(1)
qc.add(gates.H(0))
qc.add(gates.M(0))
result = qc(nshots=100)
obs_symb = X(0)
obs = hamiltonians.SymbolicHamiltonian(obs_symb)
result.expectation_from_samples(obs)
And the log:
`---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
in
7 obs_symb = X(0)
8 obs = hamiltonians.SymbolicHamiltonian(obs_symb)
----> 9 result.expectation_from_samples(obs)
~\AppData\Local\Continuum\anaconda3\envs\tqenv2\lib\site-packages\qibo\states.py in expectation_from_samples(self, observable)
244 freq = self.frequencies(binary=True)
245 qubit_map = self.circuit.measurement_gate.qubits
--> 246 return observable.expectation_from_samples(freq, qubit_map)
~\AppData\Local\Continuum\anaconda3\envs\tqenv2\lib\site-packages\qibo\hamiltonians\hamiltonians.py in expectation_from_samples(self, freq, qubit_map)
562 for factor in term.factors:
563 if isinstance(factor, Z) == False:
--> 564 raise_error(
565 NotImplementedError, "Observable is not a Z Pauli string."
566 )
~\AppData\Local\Continuum\anaconda3\envs\tqenv2\lib\site-packages\qibo\config.py in raise_error(exception, message, args)
43 raise exception(message, args)
44 else:
---> 45 raise exception(message)
46
47
NotImplementedError: Observable is not a Z Pauli string.`
The text was updated successfully, but these errors were encountered: