@@ -1036,7 +1036,7 @@ <h2 id="applying-to-all-qubits">Applying to all qubits </h2>
10361036
10371037<!-- !split --> < br > < br > < br > < br > < br > < br > < br > < br > < br > < br >
10381038< h2 id ="qft-example-codes "> QFT example codes </ h2 >
1039-
1039+ < p > This code sets up the QFT for \( n \) qubits. </ p >
10401040
10411041<!-- code=text (!bc pycode) typeset with pygments style "default" -->
10421042< div class ="cell border-box-sizing code_cell rendered ">
@@ -1046,39 +1046,22 @@ <h2 id="qft-example-codes">QFT example codes </h2>
10461046 < div class ="highlight " style ="background: #f8f8f8 ">
10471047 < pre style ="line-height: 125%; "> import numpy as np
10481048def qft(n):
1049- Creates the Quantum Fourier Transform (QFT) matrix for n qubits.
1050-
1051- Parameters:
1052- - n: Number of qubits (QFT acts on 2^n dimensions)
10531049
1054- Returns:
1055- - QFT matrix of size (2^n, 2^n)
1056- """
1050+ # Inputs n qubits and returns QFT matrix of size (2^n, 2^n)
10571051 dim = 2 ** n # Dimension of the Hilbert space
10581052 omega = np.exp(2j * np.pi / dim) # Primitive root of unity
1059-
10601053 # Initialize QFT matrix
10611054 QFT = np.zeros((dim, dim), dtype=complex)
1062-
10631055 # Construct the QFT matrix
10641056 for i in range(dim):
10651057 for j in range(dim):
10661058 QFT[i, j] = omega ** (i * j) / np.sqrt(dim)
1067-
10681059 return QFT
10691060
10701061def apply_qft(state):
1071- """
1072- Applies QFT to a given quantum state vector.
1073- Parameters:
1074- - state: Input state vector (1D NumPy array)
1075- Returns:
1076- - Transformed state vector after QFT
1077- """
10781062 n = int(np.log2(len(state))) # Number of qubits
10791063 qft_matrix = qft(n)
1080- return np.dot(qft_matrix, state)
1081-
1064+ return qft_matrix @ state
10821065# Example: 3-qubit system
10831066n_qubits = 3 # QFT on 3 qubits (8 dimensions)
10841067dim = 2 ** n_qubits
0 commit comments