run method

Map<String, int> run({
  1. int shots = 1000,
})

Implementation

Map<String, int> run({int shots = 1000}) {
  Matrix initialState = Matrix(_dim, 1);
  initialState.set(0,0, Complex(1, 0));
  Matrix finalState = unitary.mul(initialState);

  List<double> CDF = [];
  List<String> labels = [];
  for (int i = 0; i < _dim; i++) {
    double prev = 0;
    if (i > 0) {
      prev = CDF[i - 1];
    }
    Complex probability = finalState.get(i, 0);
    CDF.add(prev + (probability.mul(probability).abs()));
    labels.add(_bintostring(_tobin(i)));
  }

  SplayTreeMap<String, int> counts = SplayTreeMap<String, int>();
  var random = Random();
  loopThroughAllShots: for (int i = 0; i < shots; i++) {
    double a = random.nextDouble();
    for (int i = 0; i < _dim; i++) {
      if (a < CDF[i]) {
        int? prevCount = counts[labels[i]];
        if (prevCount == null) {
          prevCount = 0;
        }
        counts[labels[i]] = prevCount + 1;
        continue loopThroughAllShots;
      }
    }
  }

  return counts;
}