def calc_fidelity(sequence, Op, Ugoal):
  # Op will be function that return operator matrix
  # Ugoal 2x2 unitary matrix
  # sequence = [1 2 3 4];
  # return = fidelity

  # example:
  # sequence = [1 4 2 4 5];
  # Uapprox = Op(1) * Op(4) * Op(2) * Op(4) * Op(5);

  # create identity matrix
  Uapprox = np.eye(len(Ugoal))

  for k in range(len(sequence)):
    Uapprox = Op.M[sequence[k]] * Uapprox

  # M.getH() returns the complex conjugate of self
  result = (len(Ugoal) / 1.0) * abs(np.trace(Ugoal * Uapprox.getH()))

  return result
dataset/ETHPy150Open jansel/opentuner/examples/unitary/cla_func.py/calc_fidelity/swapped operands of `/`