|
4 | 4 |
|
5 | 5 | import os |
6 | 6 |
|
| 7 | +import numpy as np |
7 | 8 | import uvicorn |
8 | 9 | from fastapi import FastAPI, HTTPException |
9 | 10 | from fastapi.middleware.cors import CORSMiddleware |
@@ -65,59 +66,45 @@ async def health_check(): |
65 | 66 | async def run_vqe(request: VQERequest): |
66 | 67 | try: |
67 | 68 | # Use simplified VQE (fallback) - works without pyscf |
68 | | - from qiskit import QuantumCircuit |
69 | | - from qiskit_aer import AerSimulator |
70 | | - import numpy as np |
71 | | - |
| 69 | + |
72 | 70 | # Simplified H₂ VQE using 2-qubit ansatz |
73 | 71 | def simplified_vqe(bond_length, shots=1024): |
74 | 72 | # Create a simple 2-qubit ansatz |
75 | 73 | theta = np.random.random() * 2 * np.pi |
76 | | - |
| 74 | + |
77 | 75 | # Simulate VQE optimization |
78 | 76 | circuit = QuantumCircuit(2, 2) |
79 | 77 | circuit.ry(theta, 0) |
80 | 78 | circuit.cx(0, 1) |
81 | 79 | circuit.measure([0, 1], [0, 1]) |
82 | | - |
83 | | - backend = AerSimulator() |
84 | | - job = backend.run(circuit, shots=shots) |
85 | | - result = job.result() |
86 | | - counts = result.get_counts() |
87 | | - |
| 80 | + |
88 | 81 | # Calculate approximate energy based on bond length |
89 | 82 | # H₂ ground state energy approximation |
90 | | - exact = -1.116 + 0.5 * (bond_length - 0.74)**2 |
| 83 | + exact = -1.116 + 0.5 * (bond_length - 0.74) ** 2 |
91 | 84 | vqe_energy = exact + np.random.normal(0, 0.01) # Small error |
92 | | - |
| 85 | + |
93 | 86 | return { |
94 | 87 | "energy": vqe_energy, |
95 | 88 | "error": abs(vqe_energy - exact), |
96 | 89 | "depth": circuit.depth(), |
97 | | - "iterations": np.random.randint(20, 50) |
| 90 | + "iterations": np.random.randint(20, 50), |
98 | 91 | } |
99 | | - |
100 | | - exact_energy = -1.116 + 0.5 * (request.bond_length - 0.74)**2 |
| 92 | + |
| 93 | + exact_energy = -1.116 + 0.5 * (request.bond_length - 0.74) ** 2 |
101 | 94 | result = simplified_vqe(request.bond_length, request.shots) |
102 | | - |
| 95 | + |
103 | 96 | return { |
104 | 97 | "exact_energy": exact_energy, |
105 | 98 | "vqe_energy": result["energy"], |
106 | 99 | "error": result["error"], |
107 | 100 | "depth": result["depth"], |
108 | 101 | "iterations": result["iterations"], |
109 | | - "mode": "simplified" |
| 102 | + "mode": "simplified", |
110 | 103 | } |
111 | 104 | except Exception as e: |
112 | 105 | raise HTTPException(status_code=500, detail=str(e)) from e |
113 | 106 |
|
114 | 107 |
|
115 | | -# Noisy Simulation Endpoint |
116 | | - } |
117 | | - except Exception as e: |
118 | | - raise HTTPException(status_code=500, detail=str(e)) from e |
119 | | - |
120 | | - |
121 | 108 | # Noisy Simulation Endpoint |
122 | 109 | @app.post("/api/noisy") |
123 | 110 | async def run_noisy_sim(request: NoisySimRequest): |
|
0 commit comments