In a combination lock, we can represent the oracle as a unitary operator:

where is the right -bit combination for the lock as a basis vector. Applying the oracle is called a query. It tells us when the combination is correct by flipping the sign, so we can try to incorporate it into our lock-breaking circuit. Let's start by creating the oracle operation as a matrix.

The simplest way to do this is to create the identity matrix

and then simply change the entry corresponding to the solution from to .

For this codercise, write a function that returns the oracle in matrix form for a given secret combination.

def oracle_matrix(combo):
"""Return the oracle matrix for a secret combination.
Args:
combo (list[int]): A list of bits representing a secret combination.
Returns:
array[float]: The matrix representation of the oracle.
"""
index = np.ravel_multi_index(combo, [2]*len(combo)) # Index of solution
my_array = np.identity(2**len(combo)) # Create the identity matrix

##################
# YOUR CODE HERE #
##################

# MODIFY DIAGONAL ENTRY CORRESPONDING TO SOLUTION INDEX

return my_array


or to submit your code

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

Learning Objectives:

  • Explain the role of oracles in quantum algorithms.
  • Express the action of an oracle as a unitary matrix applied to computational basis states.