Illustrating Shor’s algorithm with Qiskit code
In this section, we present two Qiskit programs. One program illustrates, in a straightforward manner, the application of Qiskit’s QFT function. The other uses special tricks to construct a circuit that scales for large numbers. Neither program implements Shor’s algorithm in complete detail.
Testing the QFT
We begin this example with the required import
statements:
from qiskit import QuantumCircuit, Aer, executefrom qiskit.circuit.library import QFT from qiskit.tools.visualization import plot_histogram import numpy as np
With public key 15
and coprime 7
, we create a vector
containing eight values of 7n % 15:
public_key = 15coprime = 7 #coprime = 11 vector = [] for i in range(8): vector.append(coprime**i % public_key) norm = np.linalg.norm(vector) statevector = vector / norm print('vector:') print(vector) print() print('statevector:') print(statevector)
The...