Given an -bit combo, we would like to know the optimal number of Grover steps that allow us to maximize the probability of observing the solution. Too many steps will rotate us past the solution and away from it! We are searching through a space of possibilities. Let us assume that t required is proportional to a power of :

To make a guess at the easiest thing to do is take the binary logarithm of the power law relationship:

Thus, if is indeed proportional to a power of , should be a straight line of slope and intercept as a function of So let's generate data for and . We will loop over different system sizes and build a circuit for each.

Let's start by completing the function below, which implements Grover search for a given secret combination and number of Grover steps. The oracle, Hadamard transform and diffusion operator are provided as oracle(combo), hadamard_transform(my_wires), and diffusion(n_bits).

Note. The inner circuit is required since we are going to be changing the number of qubits, which is implicitly specified by the length of the secret combination.

def grover_iter(combo, num_steps):
"""Run Grover search for a given secret combination and a number of iterations.

Args:
combo (list[int]): The secret combination, represented as a list of bits.
num_steps (int): The number of Grover iterations to perform.

Returns:
array[float]: Probability for observing different outcomes.
"""
n_bits = len(combo)
query_register = list(range(n_bits))
aux = [n_bits]
all_wires = query_register + aux
dev = qml.device("default.qubit", wires=all_wires)

@qml.qnode(dev)
def inner_circuit():
##################
# YOUR CODE HERE #
##################
# IMPLEMENT THE GROVER CIRCUIT
return qml.probs(wires=query_register)

return inner_circuit()

or to submit your code

To interact with codercises, please switch to a larger screen size.

Learning Objectives:

  • Understand the exact geometry of a Grover step.
  • Compute the scaling of the optimal number of Grover steps for a single solution.