Finally, by applying the inverse quantum Fourier transform (which was implemented in Problem B4) to the register ∣y⟩m, we can transition the register to the desired state.
2m1k=0∑2m−1e2m2πif(x)k∣k⟩mIQFT∣f(x)⟩m
The transition in Equation (5) is clear from the definition of the quantum Fourier transform given by
∣j⟩mQFT2m1k=0∑2m−1exp(2m2πijk)∣k⟩m
With these steps, we can implement a quantum circuit that realizes the following state transition.
∣x⟩n∣0⟩mqc∣x⟩n∣f(x)mod2m⟩m
The circuit depth is 1+max(n,m)+2m.
Sample Code
Below is a sample program:
import mathfrom qiskit import QuantumCircuit, QuantumRegisterdef 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 qcdef solve(n: int, m: int, S: list[int]) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) qc.h(y) for j in range(m): for i in range(n): theta = (2 * math.pi * S[i] / 2**m) * 2**j qc.cp(theta, x[i], y[j]) qc.compose(qft(m).inverse(), y, inplace=True) return qc