from qiskit import *
from qiskit.visualization import *
from qiskit.circuit.library import QFT
from numpy import *
backend = BasicAer.get_backend('qasm_simulator')
peT = QuantumCircuit(3)
peT.add_register(QuantumRegister(1,'qr'))
peT.x(3)
<qiskit.circuit.instructionset.InstructionSet at 0x7f424103fe80>
for qubit in range(3):
peT.h(qubit)
peT.draw()
┌───┐
q_0: ┤ H ├
├───┤
q_1: ┤ H ├
├───┤
q_2: ┤ H ├
├───┤
qr_0: ┤ X ├
└───┘
repetitions = 1
for counting_qubit in range(3):
for i in range(repetitions):
peT.cp(pi/4, counting_qubit, 3)
repetitions *= 2
peT.draw()
┌───┐
q_0: ┤ H ├─■─────────────────────────────────────────────────────────────
├───┤ │
q_1: ┤ H ├─┼────────■────────■───────────────────────────────────────────
├───┤ │ │ │
q_2: ┤ H ├─┼────────┼────────┼────────■────────■────────■────────■───────
├───┤ │P(π/4) │P(π/4) │P(π/4) │P(π/4) │P(π/4) │P(π/4) │P(π/4)
qr_0: ┤ X ├─■────────■────────■────────■────────■────────■────────■───────
└───┘
qftinv = QFT(3, inverse=True)
qftinv.draw()
┌───┐
q_0: ─X─┤ H ├─■──────────────■───────────────────────
│ └───┘ │P(-π/2) ┌───┐ │
q_1: ─┼───────■────────┤ H ├─┼─────────■─────────────
│ └───┘ │P(-π/4) │P(-π/2) ┌───┐
q_2: ─X──────────────────────■─────────■────────┤ H ├
└───┘
qftinv.add_register(QuantumRegister(1,'qr'))
qftinv.add_register(ClassicalRegister(3))
qftinv.draw()
┌───┐
q_0: ─X─┤ H ├─■──────────────■───────────────────────
│ └───┘ │P(-π/2) ┌───┐ │
q_1: ─┼───────■────────┤ H ├─┼─────────■─────────────
│ └───┘ │P(-π/4) │P(-π/2) ┌───┐
q_2: ─X──────────────────────■─────────■────────┤ H ├
└───┘
qr_0: ────────────────────────────────────────────────
c0: 3/════════════════════════════════════════════════
peT = peT + qftinv
/tmp/ipykernel_9164/1987650100.py:1: DeprecationWarning: The QuantumCircuit.__add__() method is being deprecated.Use the compose() method which is more flexible w.r.t circuit register compatibility. peT = peT + qftinv /home/al/.local/lib/python3.9/site-packages/qiskit/circuit/quantumcircuit.py:869: DeprecationWarning: The QuantumCircuit.combine() method is being deprecated. Use the compose() method which is more flexible w.r.t circuit register compatibility. return self.combine(rhs)
peT.draw()
┌───┐ »
q_0: ┤ H ├─■──────────────────────────────────────────────────────────────X─»
├───┤ │ │ »
q_1: ┤ H ├─┼────────■────────■────────────────────────────────────────────┼─»
├───┤ │ │ │ │ »
q_2: ┤ H ├─┼────────┼────────┼────────■────────■────────■────────■────────X─»
├───┤ │P(π/4) │P(π/4) │P(π/4) │P(π/4) │P(π/4) │P(π/4) │P(π/4) »
qr_0: ┤ X ├─■────────■────────■────────■────────■────────■────────■──────────»
└───┘ »
c0: 3/═══════════════════════════════════════════════════════════════════════»
»
« ┌───┐
« q_0: ┤ H ├─■──────────────■───────────────────────
« └───┘ │P(-π/2) ┌───┐ │
« q_1: ──────■────────┤ H ├─┼─────────■─────────────
« └───┘ │P(-π/4) │P(-π/2) ┌───┐
« q_2: ─────────────────────■─────────■────────┤ H ├
« └───┘
«qr_0: ─────────────────────────────────────────────
«
«c0: 3/═════════════════════════════════════════════
«
for i in range(3):
peT.measure(i,i)
peT.draw(output='mpl')
t_peT = transpile(peT, backend)
peTobj = assemble(t_peT, shots = 2048)
results = backend.run(peTobj).result()
plot_histogram(results.get_counts())