In the quantum pair-testing scheme, for each pair labelled by we implement the following computation

where the top line stands for qubits and is the oracle that adds a phase to the solution : The last qubit will be in state if the solution is present, and if it is not.

Implement this circuit and return the probabilities on the last qubit. The function oracle_matrix is defined for you. You can expand the box below to see the docstring and implementation.

Details.
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 my_array[index, index] = -1 return my_array
n_bits = 4
dev = qml.device("default.qubit", wires=n_bits)

@qml.qnode(dev)
def pair_circuit(x_tilde, combo):
"""Test a pair labelled by x_tilde for the presence of a solution.
Args:
x_tilde (list[int]): An (n_bits - 1)-string labelling the pair to test.
combo (list[int]): A secret combination of n_bits 0s and 1s.
Returns:
array[float]: Probabilities on the last qubit.
"""
for i in range(n_bits-1): # Initialize x_tilde part of state
if x_tilde[i] == 1:
qml.PauliX(wires=i)

##################
# YOUR CODE HERE #
##################
return qml.probs(wires=n_bits-1)

or to submit your code

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

Learning Objectives:

  • Describe how the oracle can be applied to a pair of candidate solutions to determine if the secret combination is present.
  • Determine the average number of queries required to find a solution when testing in pairs.