Quantum state preparation and nonunitary evolution with diagonal operators