|
20 | 20 |
|
21 | 21 | # --- Compute and display the classical solution --- |
22 | 22 | print("Classical solution x (unnormalized):", x_classical) |
23 | | -We normalize \ket{b} so it becomes a valid quantum state . In our example A has eigenvalues \lambda_1=0.25,\lambda_2=0.75. The classical solution is x=A^{-1}b, which we compute and show for reference. |
| 23 | + |
24 | 24 | # Solve Ax = b classically for comparison |
25 | 25 | x_classical = np.linalg.solve(A, b) |
26 | 26 | print("Classical solution (unnormalized):", x_classical) |
27 | 27 |
|
28 | 28 | # Build the initial quantum state |00>_phase ⊗ |b>_main ⊗ |0>_ancilla |
29 | | -phase0 = np.zeros(4, dtype=complex); phase0[0] = 1.0 # |00> in 2-qubit space (dim=4) |
30 | | -anc0 = np.array([1.0+0j, 0+0j]) # single-qubit |0> |
| 29 | +phase0 = np.zeros(4, dtype=complex); |
| 30 | +phase0[0] = 1.0 # |00> in 2-qubit space (dim=4) |
| 31 | +anc0 = np.array([1.0+0j, 0+0j]) # single-qubit |0> |
31 | 32 | state = np.kron(phase0, np.kron(b, anc0)) |
32 | 33 |
|
33 | 34 | # Apply H on both phase qubits: (H⊗H⊗I⊗I) on 4-qubit state |
|
61 | 62 | # Compute eigenvalues and rotation angles |
62 | 63 | eigvals, _ = np.linalg.eigh(A) |
63 | 64 | lam1, lam2 = eigvals |
64 | | -C = lam1 # smallest eigenvalue = 0.25 |
| 65 | +C = lam1 # smallest eigenvalue = 0.25 |
65 | 66 | theta1 = 2*np.arcsin(C/lam1) |
66 | 67 | theta2 = 2*np.arcsin(C/lam2) |
67 | 68 |
|
68 | 69 | # Rotation matrices on ancilla |
69 | 70 | def R_y(angle): |
70 | | - return np.array([[np.cos(angle/2), -np.sin(angle/2)],[np.sin(angle/2), np.cos(angle/2)]], dtype=complex) |
| 71 | + return np.array([[np.cos(angle/2), -np.sin(angle/2)],[np.sin(angle/2), np.cos(angle/2)]], dtype=complex) |
71 | 72 | R1 = R_y(theta1) |
72 | 73 | R2 = R_y(theta2) |
73 | 74 |
|
|
0 commit comments