code: def _get_flipped_protoquil_program(program: Program) -> Program:
      
      program = program.copy()
      to_measure = []
      while len(program) > 0:
          inst = program.instructions[-1]
          if isinstance(inst, Measurement):
              program.pop()
              to_measure.append((inst.qubit, inst.classical_reg))
          else:
              break
  
      program += Pragma('PRESERVE_BLOCK')
      for qu, addr in to_measure[::-1]:
          program += RX(pi, qu)
      program += Pragma('END_PRESERVE_BLOCK')
  
      for qu, addr in to_measure[::-1]:
          program += Measurement(qubit=qu, classical_reg=addr)
  
      return program
docs: For symmetrization, generate a program where X gates are added before measurement.
  
      Forest is picky about where the measure instructions happen. It has to be at the end!