File size: 3,137 Bytes
9f8cf99 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import json
import matplotlib.pyplot as plt
import os
def plot_convergence():
# Load VQE history
try:
with open('vqe_history.json', 'r') as f:
vqe_history = json.load(f)
except FileNotFoundError:
print("VQE history not found.")
vqe_history = []
# Load GQE history
try:
with open('gqe_history.json', 'r') as f:
gqe_history = json.load(f)
except FileNotFoundError:
print("GQE history not found.")
gqe_history = []
# Exact ground energy for the 2-qubit "H2 minimal" Hamiltonian used by VQE/GQE.
# (Computed by diagonalizing the 4x4 matrix for the coefficients in `Hamiltonian::h2_minimal()`.)
h2_ground_state = -1.8572750302023795
# Convert VQE history (every cost eval) into a monotone "best-so-far" curve for easier comparison.
if vqe_history:
best = float("inf")
vqe_best = []
for e in vqe_history:
if e < best:
best = e
vqe_best.append(best)
else:
vqe_best = []
os.makedirs('docs/assets', exist_ok=True)
# --- VQE-only plot
if vqe_best:
plt.figure(figsize=(10, 5))
plt.plot(vqe_best, label='VQE best-so-far (Nelder-Mead)', color='blue', linestyle='-')
plt.axhline(y=h2_ground_state, color='red', linestyle='--', label='Exact Ground (2-qubit H2 minimal)')
plt.xlabel('Cost evaluations')
plt.ylabel('Energy (Hartree)')
plt.title('VQE Convergence (2-qubit H2 minimal)')
plt.legend()
plt.grid(True, which="both", ls="-", alpha=0.3)
plt.savefig('docs/assets/vqe_convergence.png', dpi=300)
print("Saved docs/assets/vqe_convergence.png")
# --- GQE-only plot
if gqe_history:
plt.figure(figsize=(10, 5))
plt.plot(gqe_history, label='GQE best (per generation)', color='green', linestyle='-')
plt.axhline(y=h2_ground_state, color='red', linestyle='--', label='Exact Ground (2-qubit H2 minimal)')
plt.xlabel('Generation')
plt.ylabel('Energy (Hartree)')
plt.title('GQE Convergence (2-qubit H2 minimal)')
plt.legend()
plt.grid(True, which="both", ls="-", alpha=0.3)
plt.savefig('docs/assets/gqe_convergence.png', dpi=300)
print("Saved docs/assets/gqe_convergence.png")
# --- Combined plot
plt.figure(figsize=(12, 6))
if vqe_best:
plt.plot(vqe_best, label='VQE best-so-far (Nelder-Mead)', color='blue', linestyle='-')
if gqe_history:
plt.plot(gqe_history, label='GQE best (per generation)', color='green', linestyle='-')
plt.axhline(y=h2_ground_state, color='red', linestyle='--', label='Exact Ground (2-qubit H2 minimal)')
plt.xlabel('Step (not directly comparable: evals vs generations)')
plt.ylabel('Energy (Hartree)')
plt.title('VQE vs. GQE Convergence (2-qubit H2 minimal)')
plt.legend()
plt.grid(True, which="both", ls="-", alpha=0.3)
plt.savefig('docs/assets/algorithm_comparison.png', dpi=300)
print("Saved docs/assets/algorithm_comparison.png")
if __name__ == "__main__":
plot_convergence()
|