<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def sample(program: Union[circuits.Circuit, schedules.Schedule], *, noise: devices.NoiseModel = devices.NO_NOISE, param_resolver: Optional[study.ParamResolver] = None, repetitions: int = 1, dtype: Type[np.number] = np.complex64) -> study.TrialResult: """Simulates sampling from the given circuit or schedule. Args: program: The circuit or schedule to sample from. noise: Noise model to use while running the simulation. param_resolver: Parameters to run with the program. repetitions: The number of samples to take. dtype: The `numpy.dtype` used by the simulation. Typically one of `numpy.complex64` or `numpy.complex128`. Favors speed over precision by default, i.e. uses `numpy.complex64`. """


    # State vector simulation is much faster, but only works if no randomness.
    if noise == devices.NO_NOISE and protocols.has_unitary(program):
        return sparse_simulator.Simulator(dtype=dtype).run(
            program=program,
            param_resolver=param_resolver,
            repetitions=repetitions)

    return density_matrix_simulator.DensityMatrixSimulator(
        dtype=dtype, noise=noise).run(program=program,
                                      param_resolver=param_resolver,
                                      repetitions=repetitions)