|
| 1 | +Certainly! Below is a full Python implementation that: |
| 2 | + |
| 3 | +Implements the Quantum Fourier Transform (QFT) for an arbitrary number of qubits using only numpy. |
| 4 | +Implements the classical Fast Fourier Transform (FFT) using numpy.fft.fft. |
| 5 | +Compares the result of the QFT (on computational basis states) with the FFT to demonstrate their correspondence. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +🧠 |
| 12 | +Key Insight |
| 13 | + |
| 14 | + |
| 15 | +The QFT maps quantum basis states |x\rangle to quantum Fourier states using: |
| 16 | +|x\rangle \mapsto \frac{1}{\sqrt{2^n}} \sum_{k=0}^{2^n - 1} e^{2\pi i xk / 2^n} |k\rangle |
| 17 | +This is essentially the Discrete Fourier Transform (DFT) up to global phases and bit-reversals. |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +✅ Python Code |
| 23 | + |
| 24 | +import numpy as np |
| 25 | + |
| 26 | +def qft_matrix(n): |
| 27 | + """Construct the Quantum Fourier Transform matrix for n qubits.""" |
| 28 | + N = 2 ** n |
| 29 | + omega = np.exp(2j * np.pi / N) |
| 30 | + qft = np.zeros((N, N), dtype=complex) |
| 31 | + for i in range(N): |
| 32 | + for j in range(N): |
| 33 | + qft[i, j] = omega ** (i * j) |
| 34 | + return qft / np.sqrt(N) |
| 35 | + |
| 36 | +def apply_qft(state_vector): |
| 37 | + """Apply QFT to a given state vector using matrix multiplication.""" |
| 38 | + N = len(state_vector) |
| 39 | + n = int(np.log2(N)) |
| 40 | + if 2**n != N: |
| 41 | + raise ValueError("Length of state vector must be a power of 2.") |
| 42 | + QFT = qft_matrix(n) |
| 43 | + return QFT @ state_vector |
| 44 | + |
| 45 | +def compare_qft_fft(n, basis_index=0): |
| 46 | + """Compare QFT and FFT on a computational basis state |basis_index>.""" |
| 47 | + N = 2 ** n |
| 48 | + # Initialize the computational basis state |basis_index> |
| 49 | + state = np.zeros(N, dtype=complex) |
| 50 | + state[basis_index] = 1.0 |
| 51 | + |
| 52 | + # Apply QFT |
| 53 | + qft_result = apply_qft(state) |
| 54 | + |
| 55 | + # Classical FFT (with normalization) |
| 56 | + fft_result = np.fft.fft(state) / np.sqrt(N) |
| 57 | + |
| 58 | + # Compare magnitudes and phases |
| 59 | + print(f"\nComparing QFT and FFT results for |{basis_index}> with {n} qubits:") |
| 60 | + for i in range(N): |
| 61 | + print(f"Index {i:2d}: QFT = {qft_result[i]:.4f}, FFT = {fft_result[i]:.4f}, " |
| 62 | + f"Difference = {abs(qft_result[i] - fft_result[i]):.2e}") |
| 63 | + |
| 64 | + return qft_result, fft_result |
| 65 | + |
| 66 | +# Example usage |
| 67 | +if __name__ == "__main__": |
| 68 | + n_qubits = 3 # try with 2, 3, 4, etc. |
| 69 | + for basis_idx in range(2 ** n_qubits): |
| 70 | + compare_qft_fft(n_qubits, basis_idx) |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +📊 What This Code Does |
| 76 | + |
| 77 | + |
| 78 | +Builds the QFT matrix explicitly for n qubits. |
| 79 | +Applies it to a computational basis state |x\rangle. |
| 80 | +Computes the FFT of the same state using NumPy. |
| 81 | +Compares QFT and FFT outputs element-wise for magnitude and phase. |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +📝 Notes |
| 88 | + |
| 89 | + |
| 90 | +The QFT and FFT results match up to bit reversal and global phases, which do not affect measurement outcomes in quantum circuits. |
| 91 | +You can experiment by changing n_qubits and basis_idx in the if __name__ == "__main__" block. |
| 92 | + |
| 93 | + |
| 94 | +Would you also like a recursive gate-based implementation of the QFT (as in a real quantum circuit)? |
0 commit comments