Shor's algorithm
Ang pahinang ito ay hindi pa naisalin. Nakikita mo ang orihinal na bersyon sa Ingles.
Usage estimate: Three seconds on an Eagle r3 processor (NOTE: This is an estimate only. Your runtime might vary.)
Shor's algorithm, developed by Peter Shor in 1994, is a groundbreaking quantum algorithm for factoring integers in polynomial time. Its significance lies in its ability to factor large integers exponentially faster than any known classical algorithm, threatening the security of widely used cryptographic systems like RSA, which rely on the difficulty of factoring large numbers. By efficiently solving this problem on a sufficiently powerful quantum computer, Shor's algorithm could revolutionize fields such as cryptography, cybersecurity, and computational mathematics, underscoring the transformative power of quantum computation.
This tutorial focuses on demonstrating Shor's algorithm by factoring 15 on a quantum computer.
First, we define the order finding problem and construct corresponding circuits from the quantum phase estimation protocol. Next, we run the order finding circuits on real hardware using shortest-depth circuits we can transpile. The last section completes Shor's algorithm by connecting the order finding problem to integer factorization.
We end the tutorial with a discussion on other demonstrations of Shor's algorithm on real hardware, focusing on both generic implementations and those tailored to factoring specific integers such as 15 and 21. Note: This tutorial focuses more on the implementation and demonstration of the circuits concerning Shor's algorithm. For an in-depth educational resource on the material, please refer to the Fundamentals of quantum algorithms course by Dr. John Watrous, and papers in the References section.
Requirements
Before starting this tutorial, ensure that you have the following installed:
- Qiskit SDK v2.0 or later, with visualization support
- Qiskit Runtime v0.40 or later (
pip install qiskit-ibm-runtime)
Setup
import numpy as np
import pandas as pd
from fractions import Fraction
from math import floor, gcd, log
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.circuit.library import QFT, UnitaryGate
from qiskit.transpiler import CouplingMap, generate_preset_pass_manager
from qiskit.visualization import plot_histogram
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit_ibm_runtime import SamplerV2 as Sampler
Step 1: Map classical inputs to a quantum problem
Background
Shor's algorithm for integer factorization utilizes an intermediary problem known as the order finding problem. In this section, we demonstrate how to solve the order finding problem using quantum phase estimation.
Phase estimation problem
In the phase estimation problem, we're given a quantum state of qubits, along with a unitary quantum circuit that acts on qubits. We're promised that is an eigenvector of the unitary matrix that describes the action of the circuit, and our goal is to compute or approximate the eigenvalue to which corresponds. In other words, the circuit should output an approximation to the number satisfying
The goal of the phase estimation circuit is to approximate in bits. Mathematically speaking, we would like to find such that , where . The following image shows the quantum circuit that estimates in bits by making a measurement on qubits.
In the above circuit, top qubits are initiated in the state, and the bottom qubits are initiated in , which is promised to be an eigenvector of . The first ingredient in the phase estimation circuit are the controlled-unitary operations that are responsible for performing a phase kickback to their corresponding control qubit. These controlled unitaries are exponentiated in accordance to the position of the control qubit, ranging from the least significant bit to the most significant bit. Since is an eigenvector of , the state of the bottom qubits is not affected by this operation, but the phase information of the eigenvalue propagates to the top qubits.
It turns out that after the phase kickback operation via controlled-unitaries, all possible states of the top qubits are orthonormal to each other for each eigenvector of the unitary . Therefore, these states are perfectly distinguishable, and we can rotate the basis they form back to the computational basis to make a measurement. A mathematical analysis shows that this rotation matrix corresponds to the inverse quantum Fourier transform (QFT) in -dimensional Hilbert space. The intuition behind this is that the periodic structure of the modular exponentiation operators is encoded in the quantum state, and the QFT converts this periodicity into measurable peaks in the frequency domain.
For a more in-depth understanding of why the QFT circuit is employed in Shor's algorithm, we refer the reader to the Fundamentals of quantum algorithms course. We are now ready to use the phase estimation circuit for order finding.
Order finding problem
To define the order finding problem, we begin with some number theory concepts. First, for any given positive integer , define the set as All arithmetic operations in are performed modulo . In particular, all elements that are coprime with are special and constitute as For an element , the smallest positive integer such that is defined as the order of modulo . As we will see later, finding the order of an will allow us factor . To construct the order finding circuit from the phase estimation circuit, we need two considerations. First, we need to define the unitary that will allow us to find the order , and second, we need to define an eigenvector of to prepare the initial state of the phase estimation circuit.
To connect the order finding problem to phase estimation, we consider the operation defined on a system whose classical states correspond to , where we multiply by a fixed element . In particular, we define this multiplication operator such that for each . Note that it's implicit that we're taking the product modulo inside of the ket on the right-hand side of the equation. A mathematical analysis shows that is an unitary operator. Furthermore, it turns out that has eigenvector and eigenvalue pairs that allow us to connect the order of to the phase estimation problem. Specifically, for any choice of , we have that is an eigenvector of whose corresponding eigenvalue is , where