# Phase Estimation — estimate phase of T gate (pi/4)
import cirq
import numpy as np

q = cirq.LineQubit.range(4)
circuit = cirq.Circuit([
    # Eigenstate: |1>
    cirq.X(q[3]),
    # Counting qubits in superposition
    cirq.H.on_each(q[0], q[1], q[2]),
    # Controlled-U^(2^k)
    cirq.CZPowGate(exponent=1/4)(q[0], q[3]),
    cirq.CZPowGate(exponent=1/2)(q[1], q[3]),
    cirq.CZPowGate(exponent=1)(q[2], q[3]),
    # Inverse QFT
    cirq.SWAP(q[0], q[2]),
    cirq.H(q[0]),
    cirq.CZPowGate(exponent=-1/2)(q[1], q[0]),
    cirq.H(q[1]),
    cirq.CZPowGate(exponent=-1/4)(q[2], q[0]),
    cirq.CZPowGate(exponent=-1/2)(q[2], q[1]),
    cirq.H(q[2]),
    cirq.measure(q[0], q[1], q[2], key="result"),
])
