Refactor the code in quantum matrices

This aims at re factoring the code and I would like to further extend this to create a group.