Skip to content

Commit 3ba85db

Browse files
committed
Update code.py
1 parent 872d561 commit 3ba85db

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

doc/Programs/HHL/code.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020

2121
# --- Compute and display the classical solution ---
2222
print("Classical solution x (unnormalized):", x_classical)
23-
We normalize \ket{b} so it becomes a valid quantum stateIn our example A has eigenvalues \lambda_1=0.25,\lambda_2=0.75The classical solution is x=A^{-1}b, which we compute and show for reference.
23+
2424
# Solve Ax = b classically for comparison
2525
x_classical = np.linalg.solve(A, b)
2626
print("Classical solution (unnormalized):", x_classical)
2727

2828
# 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>
3132
state = np.kron(phase0, np.kron(b, anc0))
3233

3334
# Apply H on both phase qubits: (H⊗H⊗I⊗I) on 4-qubit state
@@ -61,13 +62,13 @@
6162
# Compute eigenvalues and rotation angles
6263
eigvals, _ = np.linalg.eigh(A)
6364
lam1, lam2 = eigvals
64-
C = lam1  # smallest eigenvalue = 0.25
65+
C = lam1 # smallest eigenvalue = 0.25
6566
theta1 = 2*np.arcsin(C/lam1)
6667
theta2 = 2*np.arcsin(C/lam2)
6768

6869
# Rotation matrices on ancilla
6970
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)
7172
R1 = R_y(theta1)
7273
R2 = R_y(theta2)
7374

0 commit comments

Comments
 (0)