Quantum Gate Pattern Recognition and Circuit Optimization for Scientific Applications