title: Pytorch Autograd with complex fourier transforms gives wrong results
body: <p>I am trying to implement a real valued cost function that evaluates a complex input in frequency space with pytorch &amp; autograd since I am interested in the gradients of the cost function w.r.t. the input. When I compare the autograd results with the derivative that I computed by hand (with Wirtinger calculus) I get a different result. I'm not sure where I made the mistake, whether it is in my implementation or in my own derivation of the gradient.</p>
  <p>The cost function and its derivative by hand looks like this:
  <a href="https://i.stack.imgur.com/fE2on.png" rel="nofollow noreferrer">Formula of the cost function</a></p>
  <p>My implementation is here</p>
  <pre><code>def f_derivative_by_hand(f):
      f = torch.tensor(f, dtype=torch.complex128)
      ftilde = torch.fft.fft(f)
      absf = torch.abs(ftilde)
      f2 = absf**2
      C = torch.trapz(f2).numpy()
      grads = 2 * torch.fft.ifft((ftilde)).numpy()
      return C, grads
  
  def f_derivative_autograd(f):
      f = torch.tensor(f, dtype=torch.complex128, requires_grad=True)
      ftilde = torch.fft.fft(f)
      f2 = torch.abs(ftilde)**2
      C = torch.trapz(f2)
      C.backward()
      grads = f.grad
      return C.detach().numpy(), grads.detach().numpy()
  </code></pre>
  <p>When I use some data and evaluate it by both functions, the gradients of the implementation with automatic differentiation is tilted in comparison (note that I normalized the plotted arrays):
  <a href="https://i.stack.imgur.com/725gH.png" rel="nofollow noreferrer">Autograd and derivative by hand comparison</a></p>
  <p>I suspect there could also be something wrong with the automatic differentiation of fft though since if I remove the fourier transform from the cost function and integrate the function in real space, both implementations match exactly except at the edges (again normalized):
  <a href="https://i.stack.imgur.com/Yebju.png" rel="nofollow noreferrer">No FFT autograd and derivative by hand</a></p>
  <p>It would be fantasic if someone could help me figure out what is wrong!</p>