///|
fn simulator_random(seed : UInt64?) -> @random.Rand {
  match seed {
    Some(value) =>
      @random.Rand::chacha8(
        seed=Bytes::makei(32, fn(i) { (value >> (i % 8 * 8)).to_byte() }),
      )
    None => @random.Rand()
  }
}

///|
priv enum SamplingPlan {
  Terminal(Array[Int32])
  Shots(Int32)
}

///|
/// Analyze the program and determine its measurement sampling strategy.
///
/// Returns the measured wires in instruction order and whether all
/// measurements are terminal. A program with non-measurement instructions
/// after its first measurement requires independent execution for each shot.
fn sampling_plan(
  program : @circuit.Program,
  repetitions : Int32,
  sample_channels~ : Bool = false,
) -> SamplingPlan raise @error.QError {
  if repetitions < 0 {
    raise @error.QError::InvalidState("repetitions must be nonnegative")
  }

  let targets : Array[Int32] = []
  let mut measuring = false
  let mut terminal = true
  let mut width = 0
  for instruction in program.instructions() {
    let count = instruction.record_width()
    if count > 2147483647 - width {
      raise @error.SizeLimit(2147483647)
    }
    width += count
  }

  if width == 0 {
    raise @error.QError::InvalidState("run requires a measurement")
  }
  if repetitions > 2147483647 / width {
    raise @error.QError::SizeLimit(2147483647)
  }

  for instruction in program.instructions(expand_loops=true) {
    guard instruction is Operation(op) && op.effect().try_conditional() is None else {
      terminal = false
      break
    }
    if op.effect().measurement_key() is Some(_) {
      measuring = true
      for wire in op.targets() {
        targets.push(wire)
      }
    } else if measuring ||
      (sample_channels && op.effect().try_channel() is Some(_)) {
      terminal = false
      break
    }
  }

  if terminal {
    Terminal(targets)
  } else {
    Shots(width)
  }
}

///|
/// Sample the final distribution without collapsing or rerunning the state.
fn sample_terminal(
  probabilities : Array[Double],
  program : @circuit.Program,
  targets : Array[Int32],
  repetitions : Int32,
  random : @random.Rand,
) -> SamplerResult {
  let mut total = 0.0
  for i in 0.. Int32 {
  let mut lo = 0
  let mut hi = probabilities.length() - 1
  while lo < hi {
    let mid = lo + (hi - lo) / 2
    if draw < probabilities[mid] {
      hi = mid
    } else {
      lo = mid + 1
    }
  }
  lo
}

///|
/// Restart execution for each shot when a measurement affects later gates.
fn[S : SimulationState] sample_shots(
  engine : Simulator[S],
  program : @circuit.Program,
  repetitions : Int32,
  width : Int32,
) -> SamplerResult raise @error.QError {
  let records = @maths.Array3::make(repetitions, 1, width, 0)
  for shot in 0..