5.78 kB
VIATEUR-AI's picture
# app.py # Full interactive H2 Quantum Simulator + 3D molecule # Install dependencies: gradio, plotly, qiskit, qiskit-nature, matplotlib import numpy as np import plotly.graph_objects as go import gradio as gr # Qiskit imports from qiskit_nature.drivers import PySCFDriver from qiskit_nature.problems.second_quantization.electronic import ElectronicStructureProblem from qiskit_nature.converters.second_quantization import QubitConverter from qiskit_nature.mappers.second_quantization import JordanWignerMapper from qiskit.algorithms import VQE from qiskit.algorithms.optimizers import COBYLA from qiskit.circuit.library import TwoLocal from qiskit.utils import QuantumInstance from qiskit import Aer # ----- Function to compute ground state energy of H2 using VQE ----- def h2_ground_state_energy(bond_length): bond_length = float(bond_length) # Define H2 molecule driver = PySCFDriver(atom=f"H 0 0 0; H 0 0 {bond_length}", basis='sto3g') problem = ElectronicStructureProblem(driver) # Hamiltonian second_q_ops = problem.second_q_ops() main_op = second_q_ops[0] # Map to qubits qubit_converter = QubitConverter(mapper=JordanWignerMapper()) qubit_op = qubit_converter.convert(main_op) # Ansatz & optimizer ansatz = TwoLocal(qubit_op.num_qubits, 'ry', 'cz', reps=2) optimizer = COBYLA(maxiter=100) # Quantum instance backend = Aer.get_backend('statevector_simulator') qi = QuantumInstance(backend) # Run VQE vqe = VQE(ansatz=ansatz, optimizer=optimizer, quantum_instance=qi) result = vqe.compute_minimum_eigenvalue(qubit_op) energy = result.eigenvalue.real return energy # ----- Function to create 3D molecule plot using plotly ----- def h2_3d_plot(bond_length): bond_length = float(bond_length) x = [0, 0] y = [0, 0] z = [0, bond_length] fig = go.Figure() # Atoms fig.add_trace(go.Scatter3d(x=x, y=y, z=z, mode='markers', marker=dict(size=10, color='red'))) # Bond fig.add_trace(go.Scatter3d(x=x, y=y, z=z, mode='lines', line=dict(color='blue', width=5))) fig.update_layout(scene=dict( xaxis_title='X Γ…', yaxis_title='Y Γ…', zaxis_title='Z Γ…', aspectmode='data' )) return fig # ----- Function to generate energy curve over range of bond lengths ----- def h2_energy_curve(dummy): bond_lengths = np.linspace(0.4, 2.0, 10) energies = [] for bl in bond_lengths: energy = h2_ground_state_energy(bl) energies.append(energy) fig = go.Figure() fig.add_trace(go.Scatter(x=bond_lengths, y=energies, mode='lines+markers')) fig.update_layout( title="H2 Ground State Energy vs Bond Length", xaxis_title="Bond Length (Γ…)", yaxis_title="Energy (Hartree)" ) return fig # ----- Combine all into one interface ----- def full_interface(bond_length): bond_length_float = float(bond_length) energy = h2_ground_state_energy(bond_length_float) mol_fig = h2_3d_plot(bond_length_float) return f"Ground state energy: {energy:.6f} Hartree", mol_fig iface = gr.Interface( fn=full_interface, inputs=gr.Textbox(label="Bond Length Γ…", placeholder="Enter e.g., 0.74"), outputs=[gr.Textbox(label="Ground State Energy"), gr.Plot(label="3D Molecule")], title="H2 Quantum Simulator", description="Interactive 3D molecule + VQE Quantum simulation of H2. Enter bond length to compute energy and see molecule." ) # Add separate energy curve tab energy_curve_iface = gr.Interface( fn=h2_energy_curve, inputs=gr.Textbox(label="Dummy Input", placeholder="Press submit"), outputs=gr.Plot(label="Energy Curve"), title="H2 Energy Curve", description="Shows ground state energy of H2 over bond lengths 0.4–2.0 Γ…." ) # Launch all interfaces as Tabs demo = gr.TabbedInterface([iface, energy_curve_iface], ["Single Bond Length", "Energy Curve"]) demo.launch(share=True)
c9c6353 verified