|
| 1 | +import pandas as pd |
| 2 | +import numpy as np |
| 3 | +from sklearn.linear_model import LinearRegression |
| 4 | +import openpyxl |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +# Quantum imports |
| 8 | +from qiskit import QuantumCircuit, Aer, execute |
| 9 | + |
| 10 | +# Path to Aura workbook |
| 11 | +AURA_FILE = Path("../data/Aura.xlsl") |
| 12 | + |
| 13 | +# ---------- AI + SIMULATION PART ---------- |
| 14 | + |
| 15 | +def load_data(sheet_name="SimulationInput"): |
| 16 | + return pd.read_excel(AURA_FILE, sheet_name=sheet_name) |
| 17 | + |
| 18 | +def run_simulation(params_df, iterations=1000): |
| 19 | + results = [] |
| 20 | + for _, row in params_df.iterrows(): |
| 21 | + base_growth = row["growth_rate"] |
| 22 | + population = row["initial_population"] |
| 23 | + |
| 24 | + sims = [] |
| 25 | + for _ in range(iterations): |
| 26 | + pop = population |
| 27 | + for _ in range(row["years"]): |
| 28 | + pop = pop * (1 + base_growth + np.random.normal(0, 0.01)) |
| 29 | + sims.append(pop) |
| 30 | + results.append({ |
| 31 | + "scenario": row["scenario"], |
| 32 | + "expected_population": np.mean(sims), |
| 33 | + "std_dev": np.std(sims) |
| 34 | + }) |
| 35 | + return pd.DataFrame(results) |
| 36 | + |
| 37 | +def train_ai_model(results_df, params_df): |
| 38 | + X = params_df[["growth_rate", "years"]].values |
| 39 | + y = results_df["expected_population"].values |
| 40 | + model = LinearRegression().fit(X, y) |
| 41 | + |
| 42 | + preds = model.predict(X) |
| 43 | + results_df["ai_prediction"] = preds |
| 44 | + return results_df, model |
| 45 | + |
| 46 | +def export_results(df, sheet_name): |
| 47 | + with pd.ExcelWriter(AURA_FILE, engine="openpyxl", mode="a", if_sheet_exists="replace") as writer: |
| 48 | + df.to_excel(writer, sheet_name=sheet_name, index=False) |
| 49 | + |
| 50 | +# ---------- QUANTUM PART ---------- |
| 51 | + |
| 52 | +def load_quantum_instructions(sheet_name="QuantumInput"): |
| 53 | + """ |
| 54 | + Expected sheet format: |
| 55 | + | qubits | gate | target | param | |
| 56 | + | 2 | h | 0 | | |
| 57 | + | 2 | cx | 0,1 | | |
| 58 | + | 2 | rx | 0 | 1.57 | |
| 59 | + """ |
| 60 | + return pd.read_excel(AURA_FILE, sheet_name=sheet_name) |
| 61 | + |
| 62 | +def build_and_run_quantum(qdf, shots=1024): |
| 63 | + num_qubits = int(qdf["qubits"].max()) # assume first row has total qubits |
| 64 | + qc = QuantumCircuit(num_qubits) |
| 65 | + |
| 66 | + for _, row in qdf.iterrows(): |
| 67 | + gate = str(row["gate"]).lower() |
| 68 | + targets = str(row["target"]).split(",") |
| 69 | + param = row.get("param", None) |
| 70 | + |
| 71 | + if gate == "h": |
| 72 | + qc.h(int(targets[0])) |
| 73 | + elif gate == "x": |
| 74 | + qc.x(int(targets[0])) |
| 75 | + elif gate == "rx" and pd.notna(param): |
| 76 | + qc.rx(float(param), int(targets[0])) |
| 77 | + elif gate == "cx": |
| 78 | + qc.cx(int(targets[0]), int(targets[1])) |
| 79 | + |
| 80 | + qc.measure_all() |
| 81 | + |
| 82 | + backend = Aer.get_backend("qasm_simulator") |
| 83 | + job = execute(qc, backend, shots=shots) |
| 84 | + counts = job.result().get_counts() |
| 85 | + return counts |
| 86 | + |
| 87 | +def format_quantum_results(counts): |
| 88 | + df = pd.DataFrame(list(counts.items()), columns=["state", "counts"]) |
| 89 | + return df |
| 90 | + |
| 91 | +# ---------- MAIN ---------- |
| 92 | + |
| 93 | +def main(): |
| 94 | + # AI + Simulation |
| 95 | + print("Loading simulation data...") |
| 96 | + params_df = load_data() |
| 97 | + print("Running Monte Carlo simulation...") |
| 98 | + results_df = run_simulation(params_df) |
| 99 | + print("Training AI model...") |
| 100 | + results_df, _ = train_ai_model(results_df, params_df) |
| 101 | + export_results(results_df, "SimulationResults") |
| 102 | + |
| 103 | + # Quantum |
| 104 | + print("Loading quantum instructions...") |
| 105 | + qdf = load_quantum_instructions() |
| 106 | + print("Building and running quantum circuit...") |
| 107 | + counts = build_and_run_quantum(qdf) |
| 108 | + q_results = format_quantum_results(counts) |
| 109 | + export_results(q_results, "QuantumResults") |
| 110 | + |
| 111 | + print("✅ Demo complete: AI + Simulation + Quantum results written to Aura.xlsl") |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments