An Algorithm For Quantum Circuit Optimization