Spaces:
Build error
Build error
Fetching metadata from the HF Docker repository...
- 1.52 kB initial commit
- 262 Bytes initial commit
- 4.87 kB # ===== Ultimate Quantum Chemistry Platform (Starter) ===== # Python 3.11 + Qiskit stable + Gradio + Plotly import numpy as np import plotly.graph_objects as go import gradio as gr from qiskit_nature.second_q.drivers import PySCFDriver from qiskit_nature.second_q.problems import ElectronicStructureProblem from qiskit_nature.second_q.mappers import JordanWignerMapper from qiskit.algorithms.minimum_eigensolvers import VQE from qiskit.algorithms.optimizers import COBYLA from qiskit.circuit.library import TwoLocal from qiskit.primitives import Estimator # stable Qiskit 1.12 # ---------------------------- # Molecules Library # ---------------------------- molecules = { "H2": {"atoms": ["H", "H"], "coords": [(0,0,0), (0,0,0.74)]}, "LiH": {"atoms": ["Li", "H"], "coords": [(0,0,0), (0,0,1.6)]}, "BeH2": {"atoms": ["Be","H","H"], "coords": [(0,0,0), (0,0,1.3), (0,0,-1.3)]} } # ---------------------------- # Quantum ground state energy # ---------------------------- def compute_ground_state_energy(mol_name, bond_length): bond_length = float(bond_length) if mol_name == "H2": atom_str = f"H 0 0 0; H 0 0 {bond_length}" elif mol_name == "LiH": atom_str = f"Li 0 0 0; H 0 0 {bond_length}" elif mol_name == "BeH2": # symmetrically along z-axis atom_str = f"Be 0 0 0; H 0 0 {bond_length}; H 0 0 {-bond_length}" else: raise ValueError("Molecule not supported") driver = PySCFDriver(atom=atom_str, basis="sto3g") problem = ElectronicStructureProblem(driver) hamiltonian = problem.second_q_ops()[0] qubit_op = JordanWignerMapper().map(hamiltonian) ansatz = TwoLocal(qubit_op.num_qubits, "ry", "cz", reps=2) vqe = VQE(estimator=Estimator(), ansatz=ansatz, optimizer=COBYLA(maxiter=80)) result = vqe.compute_minimum_eigenvalue(qubit_op) energy = result.eigenvalue.real return energy # ---------------------------- # 3D molecule plot # ---------------------------- def molecule_3d_plot(mol_name, bond_length): bond_length = float(bond_length) if mol_name == "H2": coords = [(0,0,0),(0,0,bond_length)] elif mol_name == "LiH": coords = [(0,0,0),(0,0,bond_length)] elif mol_name == "BeH2": coords = [(0,0,0),(0,0,bond_length),(0,0,-bond_length)] else: coords = [] x, y, z = zip(*coords) fig = go.Figure() fig.add_trace(go.Scatter3d( x=x, y=y, z=z, mode="markers", marker=dict(size=8, color="red") )) fig.add_trace(go.Scatter3d( x=x, y=y, z=z, mode="lines", line=dict(color="blue", width=5) )) fig.update_layout(scene=dict(aspectmode="data")) return fig # ---------------------------- # Energy curve precompute (for 0.4 - 2.0 Å) # ---------------------------- bond_grid = np.linspace(0.4, 2.0, 12) energy_curves = {} for mol in molecules: energy_curves[mol] = [compute_ground_state_energy(mol, bl) for bl in bond_grid] # ---------------------------- # Update UI # ---------------------------- def update_ui(mol_name, bond_length): bond_length = float(bond_length) energy = compute_ground_state_energy(mol_name, bond_length) mol_fig = molecule_3d_plot(mol_name, bond_length) curve_fig = go.Figure() curve_fig.add_trace(go.Scatter( x=bond_grid, y=energy_curves[mol_name], mode="lines+markers", name="Energy Curve" )) curve_fig.add_trace(go.Scatter( x=[bond_length], y=[energy], mode="markers", marker=dict(size=12, color="green"), name="Current Energy" )) min_idx = int(np.argmin(energy_curves[mol_name])) curve_fig.add_trace(go.Scatter( x=[bond_grid[min_idx]], y=[energy_curves[mol_name][min_idx]], mode="markers", marker=dict(size=14, color="orange"), name="Equilibrium" )) curve_fig.update_layout( xaxis_title="Bond Length (Å)", yaxis_title="Energy (Hartree)" ) text = f"Ground state energy: {energy:.6f} Hartree\nEquilibrium bond length ≈ {bond_grid[min_idx]:.2f} Å" return text, mol_fig, curve_fig # ---------------------------- # Gradio App # ---------------------------- with gr.Blocks() as demo: gr.Markdown("# 🧬 Ultimate Quantum Chemistry Platform") mol_dropdown = gr.Dropdown(list(molecules.keys()), value="H2", label="Select Molecule") bond_slider = gr.Slider(0.4, 2.0, value=0.74, step=0.01, label="Bond Length (Å)") energy_box = gr.Textbox(label="Energy", lines=2) mol_plot = gr.Plot(label="3D Molecule") curve_plot = gr.Plot(label="Energy Curve") mol_dropdown.change(update_ui, [mol_dropdown, bond_slider], [energy_box, mol_plot, curve_plot]) bond_slider.change(update_ui, [mol_dropdown, bond_slider], [energy_box, mol_plot, curve_plot]) demo.launch()
- 114 Bytes Update requirements.txt