Ciro Santilli OurBigBook.com  Sponsor 中国独裁统治 China Dictatorship 新疆改造中心、六四事件、法轮功、郝海东、709大抓捕、2015巴拿马文件 邓家贵、低端人口、西藏骚乱
This is an example of the qiskit.circuit.library.QFT implementation of the Quantum Fourier transform function which is documented at: docs.quantum.ibm.com/api/qiskit/0.44/qiskit.circuit.library.QFT
Output:
init: [1, 0, 0, 0, 0, 0, 0, 0]
qc
     ┌──────────────────────────────┐┌──────┐
q_0: ┤0                             ├┤0     ├
     │                              ││      │
q_1: ┤1 Initialize(1,0,0,0,0,0,0,0) ├┤1 QFT ├
     │                              ││      │
q_2: ┤2                             ├┤2     ├
     └──────────────────────────────┘└──────┘
transpiled qc
     ┌──────────────────────────────┐                                     ┌───┐   
q_0: ┤0                             ├────────────────────■────────■───────┤ H ├─X─
     │                              │              ┌───┐ │        │P(π/2) └───┘ │ 
q_1: ┤1 Initialize(1,0,0,0,0,0,0,0) ├──────■───────┤ H ├─┼────────■─────────────┼─
     │                              │┌───┐ │P(π/2) └───┘ │P(π/4)                │ 
q_2: ┤2                             ├┤ H ├─■─────────────■──────────────────────X─
     └──────────────────────────────┘└───┘
Statevector([0.35355339+0.j, 0.35355339+0.j, 0.35355339+0.j,
             0.35355339+0.j, 0.35355339+0.j, 0.35355339+0.j,
             0.35355339+0.j, 0.35355339+0.j],
            dims=(2, 2, 2))

init: [0.0, 0.35355339059327373, 0.5, 0.3535533905932738, 6.123233995736766e-17, -0.35355339059327373, -0.5, -0.35355339059327384]
Statevector([ 7.71600526e-17+5.22650714e-17j,
              1.86749130e-16+7.07106781e-01j,
             -6.10667421e-18+6.10667421e-18j,
              1.13711443e-16-1.11022302e-16j,
              2.16489014e-17-8.96726857e-18j,
             -5.68557215e-17-1.11022302e-16j,
             -6.10667421e-18-4.94044770e-17j,
             -3.30200457e-16-7.07106781e-01j],
            dims=(2, 2, 2))
So this also serves as a more interesting example of quantum compilation, mapping the QFT gate to Qiskit Aer primitives.
If we don't transpile in this example, then running blows up with:
qiskit_aer.aererror.AerError: 'unknown instruction: QFT'
The second input is:
and the output of that approximately:
[0, 1j/sqrt(2), 0, 0, 0, 0, 0, 1j/sqrt(2)]
which can be defined simply as the normalized DFT of the input quantum state vector.
From this we see that the Quantum Fourier transform is equivalent to a direct discrete Fourier transform on the quantum state vector, related: physics.stackexchange.com/questions/110073/how-to-derive-quantum-fourier-transform-from-discrete-fourier-transform-dft
qiskit/qft.py
#!/usr/bin/env python

import math

from qiskit import QuantumCircuit, transpile
from qiskit.circuit.library import QFT
from qiskit_aer import Aer, AerSimulator

n = 3
N = 2**n

def test(init, print_qc=False):
    qc = QuantumCircuit(n)
    qc.initialize(init)
    qft = QFT(num_qubits=n).to_gate()
    qc.append(qft, qargs=range(3))
    print(f'init: {init}')
    if print_qc:
        print('qc')
        print(qc)
    qc = transpile(qc, AerSimulator())
    if print_qc:
        print('transpiled qc')
        print(qc)
    print(Aer.get_backend('statevector_simulator').run(qc, shots=1).result().get_statevector())
    print()

test([1] + [0] * (N - 1), print_qc=True)
test([math.sin(i * 2 * math.pi / N)/2 for i in range(N)])

Ancestors

  1. Qiskit example
  2. Qiskit
  3. Quantum programming framework
  4. Quantum software
  5. Quantum computing
  6. Quantum information
  7. Information
  8. Computer
  9. Information technology
  10. Area of technology
  11. Technology
  12. Ciro Santilli's Homepage