import numpy as np
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import execute
from qiskit import BasicAer
π = np.pi

q = QuantumRegister(3, 'q')
c = ClassicalRegister(1, 'c')
qft = QuantumCircuit(q, c)
qft.h(q[0])
qft.cu1(π/2, q[1], q[0])
qft.h(q[1])
qft.cu1(π/4, q[2], q[0])
qft.cu1(π/2, q[2], q[1])
qft.h(q[2]);

from qiskit.tools.visualization import circuit_drawer
circuit_drawer(qft)

qpe = QuantumCircuit(q, c)
qpe.h(q[0])
qpe.h(q[1]);

# Controlled-U0
qpe.cu3(-π / 2, -π / 2, π / 2, q[1], q[2])
qpe.cu1(3 * π / 4, q[1], q[2])
qpe.cx(q[1], q[2])
qpe.cu1(3 * π / 4, q[1], q[2])
qpe.cx(q[1], q[2])
# Controlled-U1
qpe.cx(q[0], q[2]);

qpe.swap(q[0], q[1])
qpe.h(q[1])
qpe.cu1(-π / 2, q[0], q[1])
qpe.h(q[0]);

circuit_drawer(qpe)