0
$\begingroup$

I tried to create a PoC that QFT and classical (np.fft) are the same; however, the result confuses me. I use the same input for both QFT and np.fft. I used simulator circuit and directly measured the vector so there shouldn't be any noise. Here is my code.

import qiskit
from qiskit.providers.aer.extensions.snapshot_statevector import *
import numpy as np
import matplotlib.pyplot as plt
import math

sample_norm = [0.5, 0, 0.5, 0, 0.5, 0, 0.5, 0]
n = len(sample_norm)
q = int(math.log(n,2))

# Start the QFT circuit
circuit = qiskit.QuantumCircuit(q)
circuit.initialize(sample_norm, range(q))
circuit.snapshot_statevector('init') # This vector is correct
circuit += qiskit.circuit.library.QFT(num_qubits=q, do_swaps=False, approximation_degree=0)
circuit.snapshot_statevector('qft')
circuit.measure_all()

aer_sim = qiskit.Aer.get_backend('qasm_simulator')
qobj = qiskit.assemble(circuit)
result = aer_sim.run(qobj, shots=1).result()
init_vec = result.data()["snapshots"]["statevector"]["init"][0]
qft_vec = result.data()["snapshots"]["statevector"]["qft"][0]

PSD_q = np.real(qft_vec * np.conj(qft_vec))

# Classical np.fft
f = sample_norm
fhat = np.fft.fft(f,n)
PSD = np.real(fhat * np.conj(fhat) / n)

plt.plot(PSD_q, color='r', linewidth=2, label='QTF')
plt.plot(PSD, color='c', linestyle='dashed', linewidth=2, label='npFFT')
plt.xlim(0, n)
plt.legend()
plt.show()

The result:

Text

I understand that there may be different in scaling but the result doesn't look like an effect from scaling. I have also tried with do_swaps=True/False but it doesn't help either. The vectors I got from the snapshot also don't look similar. (And not just the conjugate part)

eps = 0.000000001
qft_vec.real[np.abs(qft_vec.real) < eps] = 0
qft_vec.imag[np.abs(qft_vec.imag) < eps] = 0
fhat.real[np.abs(fhat.real) < eps] = 0
fhat.imag[np.abs(fhat.imag) < eps] = 0

qft_vec
array([0.70710678+0.j       , 0.1767767 +0.4267767j,
       0.        +0.j       , 0.1767767 +0.0732233j,
       0.        +0.j       , 0.1767767 -0.0732233j,
       0.        +0.j       , 0.1767767 -0.4267767j])

fhat
array([2.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 2.+0.j, 0.+0.j, 0.+0.j, 0.+0.j])

I'm actually a beginner in both QFT and FFT but I have read many sources, the result from both QFT and FFT should be similar? Even the matrix is only different in the complex conjugate part. Qiskit QFT matrix does not match with DFT matrix

Any help is appreciated.

$\endgroup$

1 Answer 1

0
$\begingroup$

I tested your code with different vectors like $[0.3, 0.4, 0.3, 0.4, 0.3, 0.4, 0.3, 0.4]$ and $[0.4, 0, 0.5, 0.3, 0.4, 0, 0.5, 0.3]$ and it worked without any issue. Upgrading Qiskit to the latest version fixes this issue.

I have also tried with do_swaps=True/False but it doesn't help either.

You need to set do_swaps to True.

$\endgroup$
5
  • $\begingroup$ I have already tried that. It looks bit better for some init_vector and worse for the others so I don't think this is the cause? Here is the result from vector [0.5,0.5,0,0,0.5,0.5,0,0] when do_swaps=True: imgur.com/1rORMU5 $\endgroup$
    – Bankde
    Commented Feb 21, 2022 at 15:00
  • $\begingroup$ I tested your code with this vector also and it worked perfectly fine! Then I tried other vectors like [0.3, 0.4, 0.3, 0.4, 0.3, 0.4, 0.3, 0.4] and [0.4, 0, 0.5, 0.3, 0.4, 0, 0.5, 0.3]. Again, it worked without any issue. What's your Qiskit version? $\endgroup$ Commented Feb 21, 2022 at 15:22
  • $\begingroup$ It's qiskit 0.24.0 Wow, I didn't think this would be the case so I have forgotten about the qiskit version; it's very old already. Just updated to 0.34.2. I'm now getting error on statevector API, let me fix that and I will check the result again. $\endgroup$
    – Bankde
    Commented Feb 21, 2022 at 15:39
  • $\begingroup$ After upgrade you need to replace qobj = qiskit.assemble(circuit) with qobj = qiskit.transpile(circuit, aer_sim). The remaining are warnings not errors. $\endgroup$ Commented Feb 21, 2022 at 15:47
  • $\begingroup$ You're right about the qiskit version. I am now getting the correct result on every vector. Could you please add this to the original answer? I will mark yours as correct answer. Thank you so much. $\endgroup$
    – Bankde
    Commented Feb 21, 2022 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.