# Cirq.simulate expectation value of a Hamiltonian

I want to simulate the final state of an ansatz in cirq using simulate. Now I want to calculate the expectation value of a Hamiltonian.

How do I do this? I can only find simulator.run examples in cirq. But I want to access the wavefunction and therefore would need simulator.simulate.

Is there a function in cirq I can use or how could I do this?

You can get the wavefunction from cirq.final_state_vector(circuit). Then you can define your observable as an instance of cirq.PauliSum, on which you will be able to use the expectation_from_state_vector() method to get the expectation value.
• Can you clarify what you mean by cirq.simulator.simulate? cirq.final_state_vector uses cirq.Simulator. It does get slow after a certain number of qubits though. Alternatively you can use qsim and qsimcirq - however, the step to calculate the expectation value based on the wave function will be still on the slower side today, we have an issue open for delegating expectation value calculation to the simulator: github.com/quantumlib/Cirq/issues/3492. I'd still recommend giving qsim a go! – Balint Pato Nov 25 '20 at 13:39
• I meant somethink like here: cirq.readthedocs.io/en/stable/docs/simulation.html e.g. import numpy as np circuit = cirq.Circuit() circuit.append(basic_circuit(False)) result = simulator.simulate(circuit, qubit_order=[q0, q1]) print(np.around(result.final_state, 3))  – Schrödinger314 Nov 25 '20 at 19:33
• Or can I specify the simulator before using cirq.final_state_vector(circuit) or cirq.final_density_matrix(circuit) ? Up to now I just used it without specifying anything – Schrödinger314 Nov 25 '20 at 19:36
• You won't be able to define the simultor in cirq.final_state_vector and final_density_matrix. I'm still not sure what you are asking for. cirq.Simulator provides the final state vector as you described, by using simulator.simulate() and then the result.final_state has it. Then you can pass that state vector to a PauliSum's expectation_from_* method to get the expectation value. – Balint Pato Dec 1 '20 at 3:44