Skip to content

Commit 3614b54

Browse files
committed
update
1 parent 03cd2bd commit 3614b54

File tree

8 files changed

+864
-223
lines changed

8 files changed

+864
-223
lines changed
-1 Bytes
Binary file not shown.

doc/pub/week9/ipynb/week9.ipynb

Lines changed: 222 additions & 222 deletions
Large diffs are not rendered by default.

doc/pub/week9/pdf/week9.pdf

0 Bytes
Binary file not shown.

doc/src/week9/programs/qft.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
def qft(n):
3+
4+
# Inputs n qubits and returns QFT matrix of size (2^n, 2^n)
5+
dim = 2 ** n # Dimension of the Hilbert space
6+
omega = np.exp(2j * np.pi / dim) # Primitive root of unity
7+
# Initialize QFT matrix
8+
QFT = np.zeros((dim, dim), dtype=complex)
9+
# Construct the QFT matrix
10+
for i in range(dim):
11+
for j in range(dim):
12+
QFT[i, j] = omega ** (i * j) / np.sqrt(dim)
13+
return QFT
14+
15+
def apply_qft(state):
16+
n = int(np.log2(len(state))) # Number of qubits
17+
qft_matrix = qft(n)
18+
return qft_matrix @ state
19+
# Example: 3-qubit system
20+
n_qubits = 4 # QFT on 3 qubits (8 dimensions)
21+
dim = 2 ** n_qubits
22+
23+
# Define an example quantum state (|5⟩ in computational basis)
24+
state = np.zeros(dim, dtype=complex)
25+
state[0] = 1 # |5⟩ = [0,0,0,0,0,1,0,0]
26+
27+
print("Initial State |5⟩:", state)
28+
# Apply QFT
29+
qft_state = apply_qft(state)
30+
print("\nState after QFT:")
31+
print(np.round(qft_state, 4)) # Rounded for better readability
32+
# Verify QFT is unitary (QFT * QFT† = I)
33+
qft_matrix = qft(n_qubits)
34+
identity = np.dot(qft_matrix, qft_matrix.conj().T)
35+
print("\nQFT * QFT† = Identity Matrix (Rounded):")
36+
print(np.round(identity, 4))
58.2 KB
Loading

0 commit comments

Comments
 (0)