Optimal preparation of quantum states on an atom chip device