import os
import time
import requests
API_KEY = os.environ["QCOS_API_KEY"]
BASE_URL = "https://api.softquantus.com"
HEADERS = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
# Bond lengths to sweep (Ångströms)
bond_lengths = [0.5, 0.6, 0.7, 0.74, 0.8, 0.9, 1.0, 1.2, 1.5, 2.0]
energies = []
for r_angstrom in bond_lengths:
resp = requests.post(
f"{BASE_URL}/api/v1/algorithms/vqe",
headers=HEADERS,
json={
"molecule": {
"formula": "H2",
"geometry": [
{"atom": "H", "x": 0.0, "y": 0.0, "z": 0.0},
{"atom": "H", "x": 0.0, "y": 0.0, "z": r_angstrom}
],
"basis": "sto-3g",
"charge": 0,
"spin_multiplicity": 1
},
"backend": "statevector_simulator",
"shots": 8192,
"ansatz": "UCCSD",
"optimizer": "COBYLA",
"max_iterations": 150
}
)
resp.raise_for_status()
job_id = resp.json()["job_id"]
# Poll
while True:
r = requests.get(f"{BASE_URL}/api/v1/jobs/{job_id}", headers=HEADERS).json()
if r["status"] in ("completed", "failed"):
break
time.sleep(3)
energy = r["result"]["ground_state_energy_hartree"]
energies.append((r_angstrom, energy))
print(f" r = {r_angstrom:.2f} Å → E = {energy:.6f} Hartree")
# Find equilibrium geometry
eq = min(energies, key=lambda x: x[1])
print(f"\nEquilibrium bond length: {eq[0]:.2f} Å")
print(f"Ground state energy: {eq[1]:.6f} Hartree")
print(f"Dissociation energy: {energies[-1][1] - eq[1]:.6f} Hartree")