Editorial
Quantum Fourier Transform () is defined as follows:
An -qubit quantum oracle that satisfies the following transition for any integer with .
Therefore, you can solve this problem by using , the operation in Problem B2 (controlled phase shift) and inversed QFT ().
You don't have to make and as controlled gates. The circuit depth is .
Sample Code
Below is a sample program:
import math
from qiskit import QuantumCircuit
def qft(n: int) -> QuantumCircuit:
qc = QuantumCircuit(n)
for i in reversed(range(n)):
qc.h(i)
for j in reversed(range(i)):
qc.cp(math.pi / 2 ** (i - j), j, i)
for i in range(n // 2):
qc.swap(i, n - i - 1)
return qc
# B2
def crot(n: int, a: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
for i in range(n):
theta = 2 * math.pi * a * 2**i / 2**n
qc.cp(theta, n, i)
return qc
def solve(n: int, a: int) -> QuantumCircuit:
k, c = QuantumRegister(n), QuantumRegister(1)
qc = QuantumCircuit(k, c)
qc.compose(qft(n), qubits=k, inplace=True)
qc.compose(crot(n, a), qubits=[*k, *c], inplace=True)
qc.compose(qft(n).inverse(), qubits=k, inplace=True)
return qc