Demonstration of an Algorithm for Quantum State Generation in Polarization-Encoding QKD Systems