VIATEUR-AI commited on
Commit
e983e5c
·
verified ·
1 Parent(s): e2a35b3

# ===== app.py ===== # Full interactive H2 Quantum Simulator + 3D molecule (modern Qiskit) # --- Standard math --- import numpy as np # --- Graphing & UI --- import plotly.graph_objects as go import gradio as gr # --- Qiskit (modern second_q API) --- from qiskit_nature.second_q.drivers import PySCFDriver from qiskit_nature.second_q.problems import ElectronicStructureProblem from qiskit_nature.second_q.converters import QubitConverter from qiskit_nature.second_q.mappers 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 # ---------------------------- # Compute H2 ground state energy using VQE # ---------------------------- def h2_ground_state_energy(bond_length): bond_length = float(bond_length) 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 # ---------------------------- # Create 3D molecule plot # ---------------------------- 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 # ---------------------------- # Generate energy curve over bond lengths # ---------------------------- def h2_energy_curve(dummy): bond_lengths = np.linspace(0.4, 2.0, 10) energies = [h2_ground_state_energy(bl) for bl in bond_lengths] 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 # ---------------------------- # Full interface: single bond length + 3D molecule # ---------------------------- 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 # --- Gradio interfaces --- 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 simulation of H2. Enter bond length to compute energy." ) 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 Å." ) demo = gr.TabbedInterface( [iface, energy_curve_iface], ["Single Bond Length", "Energy Curve"] ) # Launch app demo.launch()

Browse files
Files changed (1) hide show
  1. app.py +47 -27
app.py CHANGED
@@ -1,28 +1,31 @@
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
 
@@ -49,7 +52,9 @@ def h2_ground_state_energy(bond_length):
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
 
@@ -59,9 +64,17 @@ def h2_3d_plot(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 Å',
@@ -69,18 +82,20 @@ def h2_3d_plot(bond_length):
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 (Å)",
@@ -88,23 +103,24 @@ def h2_energy_curve(dummy):
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"),
@@ -113,6 +129,10 @@ energy_curve_iface = gr.Interface(
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)
 
 
 
 
 
1
+ # ===== app.py =====
2
+ # Full interactive H2 Quantum Simulator + 3D molecule (modern Qiskit)
 
 
3
 
4
+ # --- Standard math ---
5
  import numpy as np
6
+
7
+ # --- Graphing & UI ---
8
  import plotly.graph_objects as go
9
  import gradio as gr
10
 
11
+ # --- Qiskit (modern second_q API) ---
12
+ from qiskit_nature.second_q.drivers import PySCFDriver
13
+ from qiskit_nature.second_q.problems import ElectronicStructureProblem
14
+ from qiskit_nature.second_q.converters import QubitConverter
15
+ from qiskit_nature.second_q.mappers import JordanWignerMapper
16
+
17
  from qiskit.algorithms import VQE
18
  from qiskit.algorithms.optimizers import COBYLA
19
  from qiskit.circuit.library import TwoLocal
20
  from qiskit.utils import QuantumInstance
21
  from qiskit import Aer
22
 
23
+ # ----------------------------
24
+ # Compute H2 ground state energy using VQE
25
+ # ----------------------------
26
  def h2_ground_state_energy(bond_length):
27
  bond_length = float(bond_length)
28
 
 
29
  driver = PySCFDriver(atom=f"H 0 0 0; H 0 0 {bond_length}", basis='sto3g')
30
  problem = ElectronicStructureProblem(driver)
31
 
 
52
  energy = result.eigenvalue.real
53
  return energy
54
 
55
+ # ----------------------------
56
+ # Create 3D molecule plot
57
+ # ----------------------------
58
  def h2_3d_plot(bond_length):
59
  bond_length = float(bond_length)
60
 
 
64
 
65
  fig = go.Figure()
66
  # Atoms
67
+ fig.add_trace(go.Scatter3d(
68
+ x=x, y=y, z=z,
69
+ mode='markers',
70
+ marker=dict(size=10, color='red')
71
+ ))
72
  # Bond
73
+ fig.add_trace(go.Scatter3d(
74
+ x=x, y=y, z=z,
75
+ mode='lines',
76
+ line=dict(color='blue', width=5)
77
+ ))
78
 
79
  fig.update_layout(scene=dict(
80
  xaxis_title='X Å',
 
82
  zaxis_title='Z Å',
83
  aspectmode='data'
84
  ))
85
+
86
  return fig
87
 
88
+ # ----------------------------
89
+ # Generate energy curve over bond lengths
90
+ # ----------------------------
91
  def h2_energy_curve(dummy):
92
  bond_lengths = np.linspace(0.4, 2.0, 10)
93
+ energies = [h2_ground_state_energy(bl) for bl in bond_lengths]
 
 
 
94
 
95
  fig = go.Figure()
96
+ fig.add_trace(go.Scatter(
97
+ x=bond_lengths, y=energies, mode='lines+markers'
98
+ ))
99
  fig.update_layout(
100
  title="H2 Ground State Energy vs Bond Length",
101
  xaxis_title="Bond Length (Å)",
 
103
  )
104
  return fig
105
 
106
+ # ----------------------------
107
+ # Full interface: single bond length + 3D molecule
108
+ # ----------------------------
109
  def full_interface(bond_length):
110
  bond_length_float = float(bond_length)
111
  energy = h2_ground_state_energy(bond_length_float)
112
  mol_fig = h2_3d_plot(bond_length_float)
 
113
  return f"Ground state energy: {energy:.6f} Hartree", mol_fig
114
 
115
+ # --- Gradio interfaces ---
116
  iface = gr.Interface(
117
  fn=full_interface,
118
  inputs=gr.Textbox(label="Bond Length Å", placeholder="Enter e.g., 0.74"),
119
  outputs=[gr.Textbox(label="Ground State Energy"), gr.Plot(label="3D Molecule")],
120
  title="H2 Quantum Simulator",
121
+ description="Interactive 3D molecule + VQE simulation of H2. Enter bond length to compute energy."
122
  )
123
 
 
124
  energy_curve_iface = gr.Interface(
125
  fn=h2_energy_curve,
126
  inputs=gr.Textbox(label="Dummy Input", placeholder="Press submit"),
 
129
  description="Shows ground state energy of H2 over bond lengths 0.4–2.0 Å."
130
  )
131
 
132
+ demo = gr.TabbedInterface(
133
+ [iface, energy_curve_iface],
134
+ ["Single Bond Length", "Energy Curve"]
135
+ )
136
+
137
+ # Launch app
138
+ demo.launch()