Implementation of quantum gates and preparation of entangled states in cavity QED with cold trapped ions