The paper describes a technique, based on quaternion algebra, to implement the control loop for the inertial stabilization of the line of sight of an optical system. The paper includes a theoretical description of the method and the adopted mathematical tools, thus indicating the meaning of the symbols used. Particular attention is paid to the specific use made of quaternions in the algorithms. The parameters of the control loop, used in the verification of the technique, are detailed in order to provide a better understanding of the problem. The control loop has been extensively tested in simulation and in practice, and it has provided good and appropriate results.