Obtaining expectation value of Hamiltonian for a basis state

Hi!

I am experimenting with QAOA and PennyLane following the tutorial. Everything works as expected. However, once that I have obtained the optimal parameters for the circuit, I would like to actually obtain solutions to the problem represented by the cost Hamiltonian and to compute the cost of those solutions.

What I am doing it to use qml.sample to obtain samples from the QAOA circuit with the optimal parameters and thus I obtain some arrays like [0 1 0] or [1 1 0]. I also have a Hamiltonian defined by

H1 = 2 * qml.PauliZ(0) @ qml.PauliZ(1) + qml.PauliZ(2)

My question is: what is the easiest way to obtain the expectation value of H1 with respect to, say, [0 1 0]? That is, I want to compute <010|H1|010> from my definition of H1 as above and from [0 1 0]. Is there a function to do that?

Thanks in advance!

Hi @combarro, welcome to the forum!

I’m not sure that I understand your question but I will try to give you some guidance.

If I understand correctly, you want to calculate the expectation value of H1 for the state |010>.

One option would be to prepare state |010> in your circuit and then perform a measurement qml.expval(H1)

You can find a similar example in our VQE tutorial. Here you will notice that we build the electronic hamiltonian H, then we build a circuit (in your case the circuit would look different), and then within the cost function we calculate the expectation value of H for that particular circuit.

Node I.10 of the Xanadu Quantum Codebook can also be helpful in understanding how to compute the expectation values of an observable.

Please let me know if this answers your question!

Thanks! Yes, I had thought about creating a circuit to prepare the state and then using qml.expval.

However, I was looking for a more direct way of doing the computation, like a statevector matrix multiplication or something. Thanks anyway!

Hi @combarro, you could use numpy to do a vector matrix multiplication. You can use np.dot or “@” for instance. I would recommend that you check that you’re getting the right answer though.

Yes, I thought of that. But then I need to create the statevector of [0 1 0] anyway. I am not sure if there is a function in PennyLane to do it.

Hi @combarro, we do have a function: qml.QubitStateVector. In the docs of the function you can find an example of how to use it. Basically you pass a numpy array with the ket vector and you also pass the wires where you want to prepare that state.

Please let me know if this is what you were looking for!

Thanks. It is not exactly what I was looking for, because I would still need to pass the amplitudes vector to the method. I was thinking of some function that would prepare it from the ket expression |010> or from the integer 2. Thanks anyway.

@combarro how about qml.BasisState :slight_smile:

That is closer to what I was looking for, but not exactly there yet. I still need to use that inside of a circuit. I wanted something that could compute <010|H1|010> with no reference to circuits at all. This is something that can be done efficiently (on the size of H1 and of the basis ket) with a classical algorithm, so no need to invoke circuits. Anyway, thank you very much.

Hi @combarro, if you need to do something outside of a circuit then it is effectively all classical. I would recommend going with the options offered by numpy (np.dot or “@”) as mentioned before. Your Bra and Ket would just be numpy arrays.

OK, thanks so much for the explanations!

No problem @combarro! Let us know if you have any further questions.