<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def compile(self, program: Program, to_native_gates: bool = True, optimize: bool = True) -> Union[BinaryExecutableResponse, PyQuilExecutableResponse]: """ A high-level interface to program compilation. Compilation currently consists of two stages. Please see the :py:class:`AbstractCompiler` docs for more information. This function does all stages of compilation. Right now both ``to_native_gates`` and ``optimize`` must be either both set or both unset. More modular compilation passes may be available in the future. :param program: A Program :param to_native_gates: Whether to compile non-native gates to native gates. :param optimize: Whether to optimize programs to reduce the number of operations. :return: An executable binary suitable for passing to :py:func:`QuantumComputer.run`. """

        flags = [to_native_gates, optimize]
        assert all(flags) or all(not f for f in flags), "Must turn quilc all on or all off"
        quilc = all(flags)

        if quilc:
            nq_program = self.compiler.quil_to_native_quil(program)
        else:
            nq_program = program
        binary = self.compiler.native_quil_to_executable(nq_program)
        return binary