# Phase Estimation — estimate phase of T gate (pi/4)
from qiskit import QuantumCircuit
import math

qc = QuantumCircuit(4, 3)

# Eigenstate: |1>
qc.x(3)

# Counting qubits in superposition
qc.h([0, 1, 2])

# Controlled-U^(2^k)
qc.cp(math.pi / 4, 0, 3)  # T^1
qc.cp(math.pi / 2, 1, 3)  # T^2
qc.cp(math.pi, 2, 3)      # T^4

# Inverse QFT
qc.swap(0, 2)
qc.h(0)
qc.cp(-math.pi / 2, 1, 0)
qc.h(1)
qc.cp(-math.pi / 4, 2, 0)
qc.cp(-math.pi / 2, 2, 1)
qc.h(2)

qc.measure([0, 1, 2], [0, 1, 2])
