[HS.4.2a]Incorrect: Something is off with your Hamiltonian!

Hi,

When i coding HS.4.2a PrepSelPrep on the Walk operator, it show incorrect.

error log>>>

Incorrect: Something is off with your Hamiltonian!

my code>>>

offes = [1/4,1/4,1/2]
objs = [qml.Z(5),qml.Z(6),qml.Z(5)@qml.Z(6)]
H = qml.Hamiltonian(offes,objs)

dev = qml.device(“default.qubit”, wires = range(7))

walk_powers_1 = [qml.pow(qml.Qubitization(H, control = [3,4]), i) for i in range(0,3)]
walk_powers_2 = [qml.pow(qml.adjoint(qml.Qubitization(H, control = [3,4])), i) for i in reversed(range(1,3))]

bessel_1 = [0.765197686557943666, 0.44005058574493355, 0.11490348493190051]
J0, J1, J2 = bessel_1

norm_factor = J0 + 2J1 + 2J2

lcu_coeffs = [
J2 / norm_factor,
J1 / norm_factor,
J0 / norm_factor,
J1 / norm_factor,
J2 / norm_factor
]

lcu_ops = [
qml.s_prod(-1, walk_powers_2[0]), # - W^{-2}
qml.s_prod(-1j, walk_powers_2[1]), # -i W^{-1}
walk_powers_1[0], # I
qml.s_prod(1j, walk_powers_1[1]), # +i W^{1}
qml.s_prod(-1, walk_powers_1[2]) # - W^{2}
]

lcu = qml.dot(lcu_coeffs, lcu_ops)

@qml.qnode(dev)
def prep_sel_prep_walk():
qml.PrepSelPrep(lcu, control=[0,1,2])
return qml.state()

encoded_matrix = qml.matrix(prep_sel_prep_walk, wire_order=range(7))()[:4, :4]
print("The encoded matrix is: ", encoded_matrix)

output>>>

The encoded matrix is:  [[0.286+1.930e-16j 0.   +0.000e+00j 0.   +0.000e+00j 0.   +0.000e+00j]
 [0.   +0.000e+00j 0.469-1.039e-16j 0.   +0.000e+00j 0.   +0.000e+00j]
 [0.   +0.000e+00j 0.   +0.000e+00j 0.469-1.047e-16j 0.   +0.000e+00j]
 [0.   +0.000e+00j 0.   +0.000e+00j 0.   +0.000e+00j 0.531-1.501e-17j]]

Hi @Ring, welcome to the Forum!

Your solution is almost there :raising_hands: Notice however that the last term of the Hamiltonian in the description of the Codercise is not a product of Pauli Z gates as you have in the objs list. That is what’s causing the error you are seeing.

There is one additional problem that you’ll run into after correcting the Hamiltonian, please doublecheck your lcu_ops list, one of the terms has a sign error.

I hope that helps, and good luck!