Exercice in the cookook Codercise G.2.1

I stuck in this code. The amplitude display is not correct after n=5 steps. Please check the image in the encloser.

Here the code:
qml.broadcast(qml.Hadamard, pattern=“single”, wires=range(n_bits))
grover_op_matrix = np.dot(diffusion_matrix(), oracle_matrix(combo))

# Apply Grover operator n times
for _ in range(num_steps-1):
    qml.QubitUnitary(grover_op_matrix, wires=range(n_bits))

I really don’t find the difference. Do I need to round something?
Please give me some clue.


Hi @quantumel!

It’s true that the graph is right and the error you get isn’t very indicative of the issue.
The issue here is more conceptual than functional. Notice that you’re not actually applying the Grover operator n times. You’re applying it “num_steps-1” times. My guess is that you’re increasing “num_steps” by one but then subtracting it in the for loop. Why don’t you modify the for loop to actually apply the operator “num_steps” times, and update the value you have for num_steps too?

I hope this helps!

1 Like