|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +from scipy.optimize import curve_fit |
| 4 | + |
| 5 | +# --- Quantum Gates and Helpers --- |
| 6 | + |
| 7 | +I = np.eye(2) |
| 8 | +X = np.array([[0, 1], [1, 0]]) |
| 9 | +Z = np.array([[1, 0], [0, -1]]) |
| 10 | + |
| 11 | +def kron_n(*ops): |
| 12 | + result = ops[0] |
| 13 | + for op in ops[1:]: |
| 14 | + result = np.kron(result, op) |
| 15 | + return result |
| 16 | + |
| 17 | +def GHZ_state(n): |
| 18 | + state = np.zeros(2**n, dtype=complex) |
| 19 | + state[0] = 1 |
| 20 | + state[-1] = 1 |
| 21 | + return state / np.sqrt(2) |
| 22 | + |
| 23 | +def expm(matrix): |
| 24 | + eigvals, eigvecs = np.linalg.eigh(matrix) |
| 25 | + return eigvecs @ np.diag(np.exp(eigvals)) @ eigvecs.conj().T |
| 26 | + |
| 27 | +def evolve_magnetic(state, B_t, t, gamma, n): |
| 28 | + phi = gamma * B_t * t |
| 29 | + U = np.eye(1, dtype=complex) |
| 30 | + for _ in range(n): |
| 31 | + U = np.kron(U, expm(-1j * phi * Z / 2)) |
| 32 | + return U @ state |
| 33 | + |
| 34 | +def measure_in_X_basis(state, n): |
| 35 | + Xn = kron_n(*([X] * n)) |
| 36 | + return np.real(np.vdot(state, Xn @ state)) |
| 37 | + |
| 38 | +# --- Sensing Parameters --- |
| 39 | + |
| 40 | +n = 3 # number of entangled qubits |
| 41 | +gamma = 1.0 # gyromagnetic ratio |
| 42 | +true_B0 = 0.8 # true unknown amplitude |
| 43 | +omega = 1.0 # known frequency |
| 44 | +times = np.linspace(0.1, 5, 200) # sensing times |
| 45 | + |
| 46 | +# --- Simulate Sensor Signal --- |
| 47 | + |
| 48 | +def B_field(t, B0): return B0 * np.sin(omega * t) |
| 49 | + |
| 50 | +expectations = [] |
| 51 | +for t in times: |
| 52 | + Bt = B_field(t, true_B0) |
| 53 | + psi = GHZ_state(n) |
| 54 | + psi_t = evolve_magnetic(psi, Bt, t, gamma, n) |
| 55 | + expectations.append(measure_in_X_basis(psi_t, n)) |
| 56 | + |
| 57 | +expectations = np.array(expectations) |
| 58 | + |
| 59 | +# --- Step 1: Optimal Measurement Time --- |
| 60 | + |
| 61 | +grad = np.gradient(expectations, times) |
| 62 | +optimal_time = times[np.argmax(np.abs(grad))] |
| 63 | + |
| 64 | +print(f"🧠 Optimal measurement time: t = {optimal_time:.3f} s (max |d⟨Xⁿ⟩/dt|)") |
| 65 | + |
| 66 | +# --- Step 2: Estimate Unknown B₀ --- |
| 67 | + |
| 68 | +# Fit model: cos(γ n B₀ sin(ω t) * t) |
| 69 | +def model(t, B0_est): |
| 70 | + return np.cos(gamma * n * B0_est * np.sin(omega * t) * t) |
| 71 | + |
| 72 | +B0_fit, _ = curve_fit(model, times, expectations, p0=[0.5]) |
| 73 | +print(f"🔍 Estimated B₀: {B0_fit[0]:.3f} (true: {true_B0})") |
| 74 | + |
| 75 | +plt.figure(figsize=(10, 5)) |
| 76 | +plt.plot(times, expectations, label="Quantum Sensor Signal", color='blue') |
| 77 | +plt.plot(times, model(times, B0_fit[0]), '--', label=f"Fitted Model (B₀ ≈ {B0_fit[0]:.3f})", color='red') |
| 78 | +plt.axvline(optimal_time, color='green', linestyle=':', label=f"Optimal t = {optimal_time:.2f}") |
| 79 | +plt.xlabel("Time [s]") |
| 80 | +plt.ylabel(r"$\langle X^{\otimes n} \rangle$") |
| 81 | +plt.title(f"Quantum Sensor with GHZ({n}) State — Estimating $B_0$") |
| 82 | +plt.legend() |
| 83 | +plt.grid(True) |
| 84 | +plt.tight_layout() |
| 85 | +plt.show() |
0 commit comments