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?


1 Answer 1


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.

  • $\begingroup$ Thank you! That's what I have been doing so far, but I'd like to access the wavefunction by cirq.simulator.simulate because the other option takes very long to compute and I'd like to use a gpu simulator or similar to speed things up $\endgroup$ Commented Nov 25, 2020 at 10:13
  • $\begingroup$ 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! $\endgroup$ Commented Nov 25, 2020 at 13:39
  • $\begingroup$ 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)) $\endgroup$ Commented Nov 25, 2020 at 19:33
  • $\begingroup$ 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 $\endgroup$ Commented Nov 25, 2020 at 19:36
  • $\begingroup$ 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. $\endgroup$ Commented Dec 1, 2020 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.