VIATEUR-AI commited on
Commit
c9c6353
·
verified ·
1 Parent(s): 087fadd

# 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)

Browse files
Files changed (1) hide show
  1. app.py +118 -0
app.py CHANGED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ # Full interactive H2 Quantum Simulator + 3D molecule
3
+
4
+ # Install dependencies: gradio, plotly, qiskit, qiskit-nature, matplotlib
5
+
6
+ import numpy as np
7
+ import plotly.graph_objects as go
8
+ import gradio as gr
9
+
10
+ # Qiskit imports
11
+ from qiskit_nature.drivers import PySCFDriver
12
+ from qiskit_nature.problems.second_quantization.electronic import ElectronicStructureProblem
13
+ from qiskit_nature.converters.second_quantization import QubitConverter
14
+ from qiskit_nature.mappers.second_quantization import JordanWignerMapper
15
+ from qiskit.algorithms import VQE
16
+ from qiskit.algorithms.optimizers import COBYLA
17
+ from qiskit.circuit.library import TwoLocal
18
+ from qiskit.utils import QuantumInstance
19
+ from qiskit import Aer
20
+
21
+ # ----- Function to compute ground state energy of H2 using VQE -----
22
+ def h2_ground_state_energy(bond_length):
23
+ bond_length = float(bond_length)
24
+
25
+ # Define H2 molecule
26
+ driver = PySCFDriver(atom=f"H 0 0 0; H 0 0 {bond_length}", basis='sto3g')
27
+ problem = ElectronicStructureProblem(driver)
28
+
29
+ # Hamiltonian
30
+ second_q_ops = problem.second_q_ops()
31
+ main_op = second_q_ops[0]
32
+
33
+ # Map to qubits
34
+ qubit_converter = QubitConverter(mapper=JordanWignerMapper())
35
+ qubit_op = qubit_converter.convert(main_op)
36
+
37
+ # Ansatz & optimizer
38
+ ansatz = TwoLocal(qubit_op.num_qubits, 'ry', 'cz', reps=2)
39
+ optimizer = COBYLA(maxiter=100)
40
+
41
+ # Quantum instance
42
+ backend = Aer.get_backend('statevector_simulator')
43
+ qi = QuantumInstance(backend)
44
+
45
+ # Run VQE
46
+ vqe = VQE(ansatz=ansatz, optimizer=optimizer, quantum_instance=qi)
47
+ result = vqe.compute_minimum_eigenvalue(qubit_op)
48
+
49
+ energy = result.eigenvalue.real
50
+ return energy
51
+
52
+ # ----- Function to create 3D molecule plot using plotly -----
53
+ def h2_3d_plot(bond_length):
54
+ bond_length = float(bond_length)
55
+
56
+ x = [0, 0]
57
+ y = [0, 0]
58
+ z = [0, bond_length]
59
+
60
+ fig = go.Figure()
61
+ # Atoms
62
+ fig.add_trace(go.Scatter3d(x=x, y=y, z=z, mode='markers', marker=dict(size=10, color='red')))
63
+ # Bond
64
+ fig.add_trace(go.Scatter3d(x=x, y=y, z=z, mode='lines', line=dict(color='blue', width=5)))
65
+
66
+ fig.update_layout(scene=dict(
67
+ xaxis_title='X Å',
68
+ yaxis_title='Y Å',
69
+ zaxis_title='Z Å',
70
+ aspectmode='data'
71
+ ))
72
+ return fig
73
+
74
+ # ----- Function to generate energy curve over range of bond lengths -----
75
+ def h2_energy_curve(dummy):
76
+ bond_lengths = np.linspace(0.4, 2.0, 10)
77
+ energies = []
78
+ for bl in bond_lengths:
79
+ energy = h2_ground_state_energy(bl)
80
+ energies.append(energy)
81
+
82
+ fig = go.Figure()
83
+ fig.add_trace(go.Scatter(x=bond_lengths, y=energies, mode='lines+markers'))
84
+ fig.update_layout(
85
+ title="H2 Ground State Energy vs Bond Length",
86
+ xaxis_title="Bond Length (Å)",
87
+ yaxis_title="Energy (Hartree)"
88
+ )
89
+ return fig
90
+
91
+ # ----- Combine all into one interface -----
92
+ def full_interface(bond_length):
93
+ bond_length_float = float(bond_length)
94
+ energy = h2_ground_state_energy(bond_length_float)
95
+ mol_fig = h2_3d_plot(bond_length_float)
96
+
97
+ return f"Ground state energy: {energy:.6f} Hartree", mol_fig
98
+
99
+ iface = gr.Interface(
100
+ fn=full_interface,
101
+ inputs=gr.Textbox(label="Bond Length Å", placeholder="Enter e.g., 0.74"),
102
+ outputs=[gr.Textbox(label="Ground State Energy"), gr.Plot(label="3D Molecule")],
103
+ title="H2 Quantum Simulator",
104
+ description="Interactive 3D molecule + VQE Quantum simulation of H2. Enter bond length to compute energy and see molecule."
105
+ )
106
+
107
+ # Add separate energy curve tab
108
+ energy_curve_iface = gr.Interface(
109
+ fn=h2_energy_curve,
110
+ inputs=gr.Textbox(label="Dummy Input", placeholder="Press submit"),
111
+ outputs=gr.Plot(label="Energy Curve"),
112
+ title="H2 Energy Curve",
113
+ description="Shows ground state energy of H2 over bond lengths 0.4–2.0 Å."
114
+ )
115
+
116
+ # Launch all interfaces as Tabs
117
+ demo = gr.TabbedInterface([iface, energy_curve_iface], ["Single Bond Length", "Energy Curve"])
118
+ demo.launch(share=True)