The quantum component of Shor's algorithm searches efficiently for nontrivial square roots using an algorithm called period finding. Given an operator and a state , we want to find integer such that:

One can show that the state is the superposition of eigenvectors of with associated eigenvalues for . For this reason, if we apply Quantum Phase Estimation (QPE) on this state, we will receive a superposition of states with amplitudes containing the different values of , and by performing a series of measurements, we can recover the value of .

This is the circuit we have to build for our procedure. We can do so easily using the qml.QuantumPhaseEstimation operation, thanks to which we will only have to worry about defining the unitary matrix , and creating the state.

Implement the above circuit for . We can do so easily using the qml.QuantumPhaseEstimation template! Finally, we will use the get_phase function to translate the output to a decimal value.

def U():
qml.SWAP(wires=[2, 3])
qml.SWAP(wires=[1, 2])
qml.SWAP(wires=[0, 1])
for i in range(4):
qml.PauliX(wires=i)


matrix = qml.matrix(U, wire_order=range(4))()

n_target_wires = 4
target_wires = range(n_target_wires)
n_estimation_wires = 3
estimation_wires = range(4, 4 + n_estimation_wires)


dev = qml.device("default.qubit", shots=1, wires=n_target_wires + n_estimation_wires)


@qml.qnode(dev)
def circuit(matrix):
"""Return a sample after taking a shot at the estimation wires.

Args:
matrix (array[complex]): matrix representation of U.

Returns:
array[float]: a sample after taking a shot at the estimation wires.
"""

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

# CREATE THE INITIAL STATE |0001> ON TARGET WIRES

or to submit your code

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

Learning Objectives:

  • Define the period of an operator U.
  • List the steps involved in the period finding algorithm.