This paper considers right-invariant and controllable driftless quantum systems with state X(t) evolving on the unitary group U(n) and m inputs u = (u 1 , . . . , u m ). The T -sampling stabilisation problem is introduced and solved: given any initial condition X 0 and any goal state X goal , find a control law u = u(X, t) such that lim j →∞ X(jT ) = X goal for the closed-loop system. The purpose is to generate arbitrary quantum gates corresponding to X goal . This is achieved by the tracking of T -periodic reference trajectories (X a (t), u a (t)) of the quantum system that pass by X goal using the framework of Coron's return method. The T -periodic reference trajectories X a (t) are generated by applying controls u a (t) that are a sum of a finite number M of harmonics of sin(2πt/T ), whose amplitudes are parameterised by a vector a. The main result establishes that, for M big enough, X(jT ) exponentially converges towards X goal for almost all fixed a, with explicit and completely constructive control laws. This paper also establishes a stochastic version of this deterministic control law. The key idea is to randomly choose a different parameter vector of control amplitudes a = a j at each t = jT , and keeping it fixed for t ∈ [jT , (j + 1)T ). It is shown in the paper that X(jT ) exponentially converges towards X goal almost surely. Simulation results have indicated that the convergence speed of X(jT ) may be significantly improved with such stochastic technique. This is illustrated in the generation of the C-NOT quantum logic gate on U(4).