Stuck with H.5.2

Hi,
I am having a hard time to solve the codercise H.5.2. This is what I have so far:

Could someone give me a hint? :slight_smile:

Hi @Qimi,

it looks like you have the right idea but have mixed up the order of operations. If you translate Equation (6) in H.5 to the Hamiltonian of the problem, you can see that the cross-term (the one with X_{0} X_{1}) should act on the state first.

In other words, if you have a sequence of operations U_{3} U_{2} U_{1}, the operation on the right (U_{1}) will act on the state first, followed by U_{2} and then U_{3}. Remember that in PennyLane, the first gate in the circuit is the first one that acts on the state.

Your solution has the cross-term of the Hamiltonian acting on the state last so you are implicitly assuming that U_{3} U_{2} U_{1} = U_{1} U_{2} U_{3} which is not generally true.

Let me know if that helps!

@DanielNino27 Thank you! I could solve the codercise with your hint.

1 Like

My pleasure @Qimi ! Glad to see you are continuing to make progress!

Why can’t I get the same result with following Hamiltonian:

coeffs = [alpha*time/n, beta*time/n] 
obs = [qml.PauliZ(0) + qml.PauliZ(1), 
       qml.PauliX(0) @ qml.PauliX(1)] 

H = qml.dot(coeffs, obs)

for _ in range(n):
    qml.evolve(H)

Hi @ccanberry,

Welcome to the Forum!

There are several things happening here.

On one hand notice that the order of the terms in the Hamiltonian matters. Look at the picture just above Exercise H.5.3 (on the right hand side of the Codebook). What do you notice about the order in which U1 and U2 are added to the circuit? Daniel’s comment above can also help you figure out the issue here.

On the other hand, qml.evolve isn’t really the best tool for the task. If you wanted to use a predefined PennyLane function, you might want to use qml.ApproxTimeEvolution instead. Note that the coefficients for the Hamiltonian don’t depend on the time or the number of steps, so you’ll need to adapt them accordingly. Also, note that you won’t need a for loop when using ApproxTimeEvolution, it’s a smart function that already does this for you. Make sure to check the docs for more details.

Finally, it looks like you helped us uncover a mistake in the sign for both coefficients! We will be fixing this in the Codebook shortly (by the end of the week) but for now you can just add a “minus” sign to your coefficients while we get the fix up.

Final note: ApproxTimeEvolution is not required to solve the codercise, there is another way to solve it too.

Thank you for helping us improve the Codebook!