I'm currently going through the lab problems for the Qiskit course here. I'm trying to finish lab set number 3 on QPE but I can't seem to get the desired output, even from the solution notebook. I'm trying to estimate the phase of the following unitary:
$$ U_{\theta} = \left(\begin{matrix} 1&0\\ 0&e^{2\pi i \theta} \end{matrix}\right)$$
Specifically I want to implement a circuit for the case $\theta=0.5$ and I am using a 5 qubit estimation. Here is the code for the circuit:
import numpy as np
from qiskit.circuit.library import QFT
from qiskit import QuantumCircuit
pi = np.pi
def initialize_qubits(given_circuit, measurement_qubits, target_qubit):
given_circuit.h(measurement_qubits)
given_circuit.x(target_qubit)
def unitary_operator_exponent(given_circuit, control_qubit, target_qubit, theta, exponent):
given_circuit.cp(2*pi*theta*exponent, control_qubit, target_qubit)
def apply_iqft(given_circuit, measurement_qubits, n):
given_circuit.append(QFT(n).inverse(), measurement_qubits)
def qpe_program(n, theta):
qc = QuantumCircuit(n+1, n)
# Initialize the qubits
initialize_qubits(qc, range(n), n)
qc.barrier()
# Apply the controlled unitary operators in sequence
for x in range(n):
exponent = 2**(n-x-1)
unitary_operator_exponent(qc, x, n, theta, exponent)
qc.barrier()
# Apply the inverse quantum Fourier transform
apply_iqft(qc, range(n), n)
# Measure all qubits
qc.measure(range(n), range(n))
return qc
n = 5; theta = 0.5
mycircuit = qpe_program(n, theta)
mycircuit.draw(output='mpl')
This is a fairly standard phase estimation circuit and I don't see why it's not working (especially since it's in the solution manual).
The problem is when I run a simulator on this and get counts, the state $|{11111}\rangle$ shows up in half the instances and I can't for the life of me find out why (it should just be $|{00001}\rangle$ plus some small error). Here is the simulation code:
from qiskit import Aer, execute
simulator = Aer.get_backend('qasm_simulator')
counts = execute(mycircuit, backend=simulator, shots=1000).result().get_counts(mycircuit)
from qiskit.visualization import plot_histogram
plot_histogram(counts)
Here's a screenshot of the circuit and then the simulation results:
Notes: Running on Python v3.9.7 and Qiskit v0.29