1
$\begingroup$

Im trying to put the QFT circuit in my qiskit circuit but im having trouble:

import numpy as np
# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, transpile, Aer, IBMQ
from qiskit.tools.jupyter import *
from qiskit.visualization import *
from ibm_quantum_widgets import *
from math import sqrt
from qiskit.quantum_info import DensityMatrix
from qiskit.circuit.library import QFT

# Loading your IBM Q account(s)
provider = IBMQ.load_account()

qc = QuantumCircuit(1,1)

init = [sqrt(3/4),-sqrt(1/4)]
qc.initialize(init,0)
qc.h(0)
qc.x(0)
qc.h(0)
qc.QFT(0)

display(qc.draw())


s0 = DensityMatrix(qc)
sim = Aer.get_backend('unitary_simulator')
job = transpile(qc,sim)
state = sim.run(job).result().get_unitary()
print(qc.qasm())
$\endgroup$
1
$\begingroup$

The way you imported the packages, including the QFT circuit is correct. You just didn't execute it correctly.

When you call QFT, it creates a circuit that perform QFT on its own. To add this circuit to your original circuit, you can just use +. Also the to perform a QFT on 1 qubit, you should use QFT(1) instead of QFT(0).

So your circuit without QFT is:

qc = QuantumCircuit(1,1)

init = [sqrt(3/4),-sqrt(1/4)]
qc.initialize(init,0)
qc.h(0)
qc.x(0)
qc.h(0)
display(qc.draw())
     ┌──────────────────────────┐┌───┐┌───┐┌───┐
q_0: ┤ initialize(0.86603,-0.5) ├┤ H ├┤ X ├┤ H ├
     └──────────────────────────┘└───┘└───┘└───┘
c: 1/═══════════════════════════════════════════
                                                

Now to add on the 1-qubit QFT operation which is just the Hadamard gate, you can do it as:

qc = QuantumCircuit(1,1)
init = [sqrt(3/4),-sqrt(1/4)]
qc.initialize(init,0)
qc.h(0)
qc.x(0)
qc.h(0)
qc = qc + QFT(1)
display( qc.draw() )

     ┌──────────────────────────┐┌───┐┌───┐┌───┐┌───┐
q_0: ┤ initialize(0.86603,-0.5) ├┤ H ├┤ X ├┤ H ├┤ H ├
     └──────────────────────────┘└───┘└───┘└───┘└───┘
c: 1/════════════════════════════════════════════════
                                                     

As you can see, the Hadamard gate is now added to the circuit.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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