Spaces:
Running
on
Zero
Running
on
Zero
Molecule3D displays for some molecules but not all, bug where clicking on H2 breaks drop down
Browse files- app.py +4 -6
- quantum_utils.py +3 -2
- visualization.py +26 -16
app.py
CHANGED
@@ -173,8 +173,8 @@ def create_interface():
|
|
173 |
choices=list(molecules.keys()),
|
174 |
value=list(molecules.keys())[0],
|
175 |
label="Select Molecule",
|
176 |
-
|
177 |
-
|
178 |
)
|
179 |
|
180 |
# Get scale range from selected molecule
|
@@ -212,12 +212,10 @@ def create_interface():
|
|
212 |
label="VQE Convergence",
|
213 |
show_label=True
|
214 |
)
|
215 |
-
with gr.Column(scale=1):
|
216 |
-
# Create MolGallery3D component with more space
|
217 |
molecule_viz = MolGallery3D(
|
218 |
columns=1,
|
219 |
-
height=600, #
|
220 |
-
width=600, # Added explicit width
|
221 |
label="3D Molecule Viewer",
|
222 |
automatic_rotation=True
|
223 |
)
|
|
|
173 |
choices=list(molecules.keys()),
|
174 |
value=list(molecules.keys())[0],
|
175 |
label="Select Molecule",
|
176 |
+
interactive=True,
|
177 |
+
allow_custom_value=False # Prevent text input
|
178 |
)
|
179 |
|
180 |
# Get scale range from selected molecule
|
|
|
212 |
label="VQE Convergence",
|
213 |
show_label=True
|
214 |
)
|
215 |
+
with gr.Column(scale=1, min_width=600): # Set minimum width for the column
|
|
|
216 |
molecule_viz = MolGallery3D(
|
217 |
columns=1,
|
218 |
+
height=600, # Keep increased height
|
|
|
219 |
label="3D Molecule Viewer",
|
220 |
automatic_rotation=True
|
221 |
)
|
quantum_utils.py
CHANGED
@@ -218,7 +218,7 @@ def expand_geometry(geometry_template: List[List[Any]], scale_factor: float) ->
|
|
218 |
# Edits in run_vqe_simulation to use the new approach
|
219 |
# and a CUDA-Q optimizer
|
220 |
# ----------------------------------------------------------
|
221 |
-
@spaces.GPU(duration=
|
222 |
def run_vqe_simulation(molecule_data: Dict[str, Any],
|
223 |
scale_factor: float) -> Dict[str, Any]:
|
224 |
"""
|
@@ -243,7 +243,9 @@ def run_vqe_simulation(molecule_data: Dict[str, Any],
|
|
243 |
- 'message': (str) status or error message
|
244 |
"""
|
245 |
logging.info(f"Starting VQE simulation for {molecule_data['name']} with scale factor {scale_factor}")
|
|
|
246 |
|
|
|
247 |
# Ensure the correct GPU/CPU target is set
|
248 |
setup_target()
|
249 |
|
@@ -266,7 +268,6 @@ def run_vqe_simulation(molecule_data: Dict[str, Any],
|
|
266 |
# Create the UCCSD ansatz kernel
|
267 |
qubit_count = 2 * molecule_data['spatial_orbitals']
|
268 |
electron_count = molecule_data['electron_count']
|
269 |
-
optimizer_max_iterations = 10
|
270 |
|
271 |
# Compute how many UCCSD parameters we need to optimize
|
272 |
parameter_count = cudaq.kernels.uccsd_num_parameters(electron_count, qubit_count)
|
|
|
218 |
# Edits in run_vqe_simulation to use the new approach
|
219 |
# and a CUDA-Q optimizer
|
220 |
# ----------------------------------------------------------
|
221 |
+
@spaces.GPU(duration=30)
|
222 |
def run_vqe_simulation(molecule_data: Dict[str, Any],
|
223 |
scale_factor: float) -> Dict[str, Any]:
|
224 |
"""
|
|
|
243 |
- 'message': (str) status or error message
|
244 |
"""
|
245 |
logging.info(f"Starting VQE simulation for {molecule_data['name']} with scale factor {scale_factor}")
|
246 |
+
optimizer_max_iterations = 30
|
247 |
|
248 |
+
# Set up the CUDA-Q target
|
249 |
# Ensure the correct GPU/CPU target is set
|
250 |
setup_target()
|
251 |
|
|
|
268 |
# Create the UCCSD ansatz kernel
|
269 |
qubit_count = 2 * molecule_data['spatial_orbitals']
|
270 |
electron_count = molecule_data['electron_count']
|
|
|
271 |
|
272 |
# Compute how many UCCSD parameters we need to optimize
|
273 |
parameter_count = cudaq.kernels.uccsd_num_parameters(electron_count, qubit_count)
|
visualization.py
CHANGED
@@ -154,9 +154,11 @@ def create_molecule_viewer(molecule_id: str, scale_factor: float) -> MolGallery3
|
|
154 |
|
155 |
molecule_data = molecules[molecule_id]
|
156 |
|
157 |
-
# Special handling for
|
158 |
if molecule_id == "H2":
|
159 |
mol = Chem.MolFromSmiles("[H][H]")
|
|
|
|
|
160 |
elif molecule_id == "H2O":
|
161 |
mol = Chem.MolFromSmiles("O")
|
162 |
mol = Chem.AddHs(mol)
|
@@ -168,22 +170,30 @@ def create_molecule_viewer(molecule_id: str, scale_factor: float) -> MolGallery3
|
|
168 |
return None
|
169 |
mol = Chem.AddHs(mol)
|
170 |
|
|
|
|
|
|
|
|
|
171 |
# Generate 3D coordinates with distance matrix
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
|
|
|
|
|
|
|
|
187 |
|
188 |
except Exception as e:
|
189 |
logger.error(f"Failed to create molecule viewer: {e}")
|
|
|
154 |
|
155 |
molecule_data = molecules[molecule_id]
|
156 |
|
157 |
+
# Special handling for different molecules
|
158 |
if molecule_id == "H2":
|
159 |
mol = Chem.MolFromSmiles("[H][H]")
|
160 |
+
if mol is None:
|
161 |
+
mol = Chem.MolFromSmiles("[HH]") # Alternative SMILES for H2
|
162 |
elif molecule_id == "H2O":
|
163 |
mol = Chem.MolFromSmiles("O")
|
164 |
mol = Chem.AddHs(mol)
|
|
|
170 |
return None
|
171 |
mol = Chem.AddHs(mol)
|
172 |
|
173 |
+
if mol is None:
|
174 |
+
logger.error(f"Failed to create molecule for {molecule_id}")
|
175 |
+
return None
|
176 |
+
|
177 |
# Generate 3D coordinates with distance matrix
|
178 |
+
try:
|
179 |
+
AllChem.EmbedMolecule(mol, randomSeed=42)
|
180 |
+
|
181 |
+
# Scale the molecule coordinates
|
182 |
+
conf = mol.GetConformer()
|
183 |
+
positions = conf.GetPositions()
|
184 |
+
centroid = np.mean(positions, axis=0)
|
185 |
+
|
186 |
+
for i in range(mol.GetNumAtoms()):
|
187 |
+
pos = positions[i]
|
188 |
+
scaled_pos = centroid + (pos - centroid) * scale_factor
|
189 |
+
conf.SetAtomPosition(i, scaled_pos)
|
190 |
+
|
191 |
+
logger.info(f"Successfully created 3D coordinates for {molecule_id}")
|
192 |
+
return [mol] # Return the molecule as a list as expected by MolGallery3D
|
193 |
+
|
194 |
+
except Exception as e:
|
195 |
+
logger.error(f"Failed to generate 3D coordinates: {e}")
|
196 |
+
return None
|
197 |
|
198 |
except Exception as e:
|
199 |
logger.error(f"Failed to create molecule viewer: {e}")
|