Optical switches based on magnesium lanthanide alloy hydrides