A Variational Algorithm for Quantum Neural Networks