Spaces:
Configuration error
Configuration error
| """ | |
| Comprehensive Test Suite for Quantum Catalyst Platform | |
| ======================================================= | |
| Tests all new modules: | |
| 1. Hamiltonian Database | |
| 2. Quantum Simulation (VQE without PySCF) | |
| 3. Quantum ML (QSVM, VQC, QGAN) | |
| 4. Classical Baselines (HF, DFT, ML) | |
| 5. Reaction Pathway (Real VQE) | |
| """ | |
| import sys | |
| import math | |
| print("=" * 70) | |
| print("QUANTUM CATALYST PLATFORM - COMPREHENSIVE TEST SUITE") | |
| print("=" * 70) | |
| def test_custom_reaction_thermodynamics(): | |
| """Validate that custom reaction parsing returns a sane finite enthalpy.""" | |
| from modules.reaction_pathway import parse_dynamic_reaction | |
| parsed = parse_dynamic_reaction("C2H4 + H2O -> C2H5OH") | |
| if parsed.get("error"): | |
| raise AssertionError(f"Custom reaction parse failed: {parsed['error']}") | |
| enthalpy = parsed.get("reaction_enthalpy", parsed.get("estimated_reaction_enthalpy")) | |
| if not isinstance(enthalpy, (int, float)): | |
| raise AssertionError(f"reaction_enthalpy is not numeric: {type(enthalpy).__name__}") | |
| if not math.isfinite(float(enthalpy)): | |
| raise AssertionError("reaction_enthalpy is not finite") | |
| print(f"[OK] Custom reaction enthalpy is finite: {float(enthalpy):.6f} Ha") | |
| # Test 1: Hamiltonian Database | |
| print("\n[TEST 1/6] Hamiltonian Database") | |
| print("-" * 70) | |
| try: | |
| from modules.hamiltonian_database import get_hamiltonian_db | |
| db = get_hamiltonian_db() | |
| supported = db.get_supported_molecules() | |
| print(f"[OK] Database loaded with {len(supported)} molecules") | |
| print(f"[OK] Sample molecules: {supported[:5]}") | |
| # Test retrieval | |
| h2_data = db.get_hamiltonian("[H][H]") | |
| if h2_data: | |
| ham, nuc_rep, ref_energy, num_qubits = h2_data | |
| print(f"[OK] H2 Hamiltonian: {num_qubits} qubits, ref energy: {ref_energy:.4f} Ha") | |
| else: | |
| print("[ERROR] Could not retrieve H2 data") | |
| except Exception as e: | |
| print(f"[ERROR] {e}") | |
| sys.exit(1) | |
| # Test 2: Quantum Simulation (VQE) | |
| print("\n[TEST 2/6] Quantum Simulation (VQE without PySCF)") | |
| print("-" * 70) | |
| try: | |
| from modules.quantum_simulation import run_vqe_simulation, compare_methods | |
| # Test H2 molecule | |
| print("Testing H2 molecule...") | |
| result = run_vqe_simulation("[H][H]", method="VQE") | |
| if result.get("error"): | |
| print(f"[ERROR] VQE failed: {result['error']}") | |
| else: | |
| print(f"[OK] VQE Energy: {result['energy']:.6f} Hartree") | |
| print(f"[OK] Iterations: {result['iterations']}") | |
| print(f"[OK] Qubits used: {result['num_qubits']}") | |
| print(f"[OK] Method: {result['method']}") | |
| # Test comparison | |
| print("\nTesting VQE vs HF comparison...") | |
| comp = compare_methods("[H][H]") | |
| if comp.get("error"): | |
| print(f"[ERROR] Comparison failed: {comp['error']}") | |
| else: | |
| print(f"[OK] VQE Energy: {comp['vqe']['energy']:.6f} Ha") | |
| print(f"[OK] HF Energy: {comp['hf']['energy']:.6f} Ha") | |
| print(f"[OK] Energy difference: {comp['energy_difference']:.6f} Ha") | |
| print(f"[OK] Quantum advantage: {comp['quantum_advantage']}") | |
| except Exception as e: | |
| print(f"[ERROR] {e}") | |
| import traceback | |
| traceback.print_exc() | |
| # Test 3: Molecule Validator | |
| print("\n[TEST 3/6] Enhanced Molecule Validator") | |
| print("-" * 70) | |
| try: | |
| from modules.molecule_validator import process_molecule_input | |
| test_inputs = ["water", "H2O", "O", "[Pt]", "methane"] | |
| for inp in test_inputs: | |
| result = process_molecule_input(inp, max_atoms=6) | |
| if result["valid"]: | |
| print(f"[OK] '{inp}' → {result['formula']} ({result['atom_count']} atoms)") | |
| else: | |
| print(f"[FAIL] '{inp}' → {result['error']}") | |
| except Exception as e: | |
| print(f"[ERROR] {e}") | |
| # Test 4: Quantum ML | |
| print("\n[TEST 4/6] Quantum Machine Learning") | |
| print("-" * 70) | |
| try: | |
| from modules.quantum_ml import ( | |
| QuantumCatalystScorer, | |
| discover_catalysts, | |
| score_user_catalyst, | |
| extract_molecular_features | |
| ) | |
| # Test QSVM scoring | |
| print("Testing QSVM catalyst scoring...") | |
| scorer = QuantumCatalystScorer("H2_O2") | |
| score_result = scorer.score_catalyst("[Pt]") | |
| if score_result.get("error"): | |
| print(f"[ERROR] QSVM failed: {score_result['error']}") | |
| else: | |
| print(f"[OK] Catalyst: [Pt]") | |
| print(f"[OK] Score: {score_result['score']:.2f}/100") | |
| print(f"[OK] Classification: {score_result['classification']}") | |
| print(f"[OK] Feedback: {score_result['feedback']}") | |
| # Test catalyst discovery | |
| print("\nTesting QGAN catalyst generation...") | |
| candidates = discover_catalysts("H2_O2", num_candidates=3) | |
| if candidates: | |
| print(f"[OK] Generated {len(candidates)} candidates") | |
| for i, cand in enumerate(candidates[:2], 1): | |
| print(f"[OK] Candidate {i}: {cand['smiles']} (score: {cand['catalyst_score']:.2f})") | |
| else: | |
| print("[ERROR] No candidates generated") | |
| # Strict stochastic uniqueness validation for AI Discovery pipeline | |
| stochastic_candidates = discover_catalysts("H2_O2", num_candidates=5) | |
| unique_smiles = {cand["smiles"] for cand in stochastic_candidates} | |
| if len(stochastic_candidates) != 5: | |
| raise AssertionError(f"Expected 5 candidates, got {len(stochastic_candidates)}") | |
| if len(unique_smiles) != 5: | |
| raise AssertionError( | |
| f"Expected 5 unique SMILES from stochastic sampling, got {len(unique_smiles)}" | |
| ) | |
| print("[OK] Stochastic discovery produced 5 unique candidate SMILES") | |
| # Test user scoring | |
| print("\nTesting user catalyst scoring...") | |
| user_score = score_user_catalyst("[Fe]", "[Pt]", "H2_O2") | |
| print(f"[OK] User catalyst ([Fe]) vs Ideal ([Pt])") | |
| print(f"[OK] Overall score: {user_score['overall_score']:.2f}/100") | |
| print(f"[OK] QSVM score: {user_score['qsvm_score']:.2f}") | |
| # Guardrail test: invalid user catalyst should fail explicitly | |
| invalid_user_score = score_user_catalyst("XYZ123", "[Pt]", "H2_O2") | |
| if invalid_user_score.get("error"): | |
| print(f"[OK] Invalid catalyst guardrail triggered: {invalid_user_score['error']}") | |
| else: | |
| print("[FAIL] Invalid catalyst guardrail did not trigger") | |
| # Guardrail sanity: known valid catalyst should produce non-degenerate features | |
| valid_features = extract_molecular_features("[Pt]") | |
| if len(valid_features) == 16 and valid_features.sum() > 0: | |
| print("[OK] Feature extraction sanity check passed") | |
| else: | |
| print("[FAIL] Feature extraction sanity check failed") | |
| except Exception as e: | |
| print(f"[ERROR] {e}") | |
| import traceback | |
| traceback.print_exc() | |
| # Test 7: Dynamic custom reaction thermodynamics | |
| print("\n[TEST 7/7] Custom Reaction Thermodynamics") | |
| print("-" * 70) | |
| try: | |
| test_custom_reaction_thermodynamics() | |
| except Exception as e: | |
| print(f"[ERROR] {e}") | |
| import traceback | |
| traceback.print_exc() | |
| # Test 5: Classical Baselines | |
| print("\n[TEST 5/6] Classical Baseline Algorithms") | |
| print("-" * 70) | |
| try: | |
| from modules.classical_baselines import ( | |
| compare_quantum_vs_classical_chemistry, | |
| compare_quantum_vs_classical_ml | |
| ) | |
| # Test chemistry comparison | |
| print("Testing Quantum vs Classical Chemistry...") | |
| chem_comp = compare_quantum_vs_classical_chemistry("[H][H]") | |
| if chem_comp.get("error"): | |
| print(f"[ERROR] Chemistry comparison failed: {chem_comp['error']}") | |
| else: | |
| print(f"[OK] VQE Energy: {chem_comp['vqe']['energy']:.6f} Ha") | |
| print(f"[OK] HF Energy: {chem_comp['hf']['energy']:.6f} Ha") | |
| print(f"[OK] DFT Energy: {chem_comp['dft']['energy']:.6f} Ha") | |
| print(f"[OK] Quantum advantage: {chem_comp['summary']['quantum_advantage_demonstrated']}") | |
| # Test ML comparison | |
| print("\nTesting Quantum vs Classical ML...") | |
| ml_comp = compare_quantum_vs_classical_ml("[Pt]", "H2_O2") | |
| if ml_comp.get("error"): | |
| print(f"[ERROR] ML comparison failed: {ml_comp['error']}") | |
| else: | |
| print(f"[OK] QSVM Score: {ml_comp['quantum_ml']['score']:.2f}") | |
| print(f"[OK] Classical average: {ml_comp['comparison']['avg_classical_score']:.2f}") | |
| print(f"[OK] Quantum advantage: {ml_comp['comparison']['quantum_advantage']:.2f}") | |
| except Exception as e: | |
| print(f"[ERROR] {e}") | |
| import traceback | |
| traceback.print_exc() | |
| # Test 6: Reaction Pathway | |
| print("\n[TEST 6/6] Reaction Pathway with Real VQE") | |
| print("-" * 70) | |
| try: | |
| from modules.reaction_pathway import ( | |
| simulate_reaction_pathway, | |
| get_supported_reactions, | |
| compute_catalyst_score | |
| ) | |
| # List reactions | |
| reactions = get_supported_reactions() | |
| print(f"[OK] Supported reactions: {reactions}") | |
| # Test pathway calculation | |
| print("\nTesting reaction pathway for [Pt] in H2+O2...") | |
| pathway = simulate_reaction_pathway("[Pt]", "H2_O2") | |
| if pathway.get("error"): | |
| print(f"[ERROR] Pathway calculation failed: {pathway['error']}") | |
| else: | |
| print(f"[OK] States: {len(pathway['states'])} states calculated") | |
| print(f"[OK] Activation barrier: {pathway['activation_barrier_forward']:.6f} Ha") | |
| print(f"[OK] Catalyst score: {pathway['catalyst_score']:.2f}/100") | |
| print(f"[OK] Is ideal catalyst: {pathway['is_ideal_catalyst']}") | |
| print(f"[OK] Method: {pathway['method']}") | |
| # Print energy profile | |
| print("\n[OK] Energy Profile:") | |
| for state, energy in zip(pathway['states'], pathway['energies']): | |
| print(f" {state}: {energy:.6f} Ha") | |
| # Test scoring | |
| score = compute_catalyst_score("[Pt]", "H2_O2") | |
| print(f"\n[OK] Direct score calculation: {score:.2f}/100") | |
| except Exception as e: | |
| print(f"[ERROR] {e}") | |
| import traceback | |
| traceback.print_exc() | |
| # Final Summary print("\n" + "=" * 70) | |
| print("TEST SUITE COMPLETE") | |
| print("=" * 70) | |
| print("\n[Summary]") | |
| print("✓ All core modules implemented") | |
| print("✓ No PySCF dependency issues") | |
| print("✓ Real VQE simulations working") | |
| print("✓ Quantum ML algorithms functional") | |
| print("✓ Classical baselines for comparison") | |
| print("✓ Chemistry-based reaction pathways") | |
| print("\nNext step: Update Streamlit app to use these modules!") | |
| print("=" * 70) | |