In Codercise P.3.1 of this node, we have to find the phase angle between the two values of the estimation window. As I understand, the solution steps are:
1- from qpe function, we find the largest two probablity values.
2- convert the two values to fractional binary.
3- convert the fractional binary values in step2 to decimal.
Using these steps , I got (0.0625, 0.03125)
as largest estimation values of U = np.array([[1, 0], [0, np.exp((2*np.pi*1j/7))]])
But this solution is not correct:
dev = qml.device("default.qubit", wires=10)
def fractional_binary_to_decimal(binary_fraction, wires):
return float(binary_fraction/ 2 ** len(wires))
def phase_window(probs, estimation_wires):
""" Given an array of probabilities, return the phase window of the
unitary's eigenvalue
Args:
probs (array[float]): Probabilities on the estimation wires.
estimation_wires (list[int]): List of estimation wires
Returns:
(float, float): the lower and upper bound of the phase
"""
##################
# YOUR CODE HERE #
##################
keys = [-1,-1]
values = [np.nextafter(0, 1) , np.nextafter(0, 1)]
def swap(values , keys):
tempv = values[1]
tempk = keys[1]
values[1] = values[0]
keys[1] = keys[0]
values[0] = tempv
keys[0] = tempk
#Step 1: find the two largest prob values
for i , item in zip(range(len(probs)) , probs):
if(item > values[0]):
values[0] = item
keys[0] = i
if( values[0] > values[1]):
swap(values , keys)
bounds = []
for j in range(len(keys)):
sum1 = 1/(2**( keys[j])) # step 2: convert them to fract binaries
bounds.append(fractional_binary_to_decimal(sum1, estimation_wires)) # step 3
bound_1 = bounds[1] # MOST LIKELY OUTCOME
bound_2 = bounds[0] # SECOND MOST LIKELY OUTCOME
return (bound_1, bound_2)
# Test your solution
# You can increase the number of estimation wires to a maximum of range(0, 9)
estimation_wires = range(0, 3)
# The target is set to the last qubit
target_wires = [9]
# Define the unitary
U = np.array([[1, 0], [0, np.exp((2*np.pi*1j/7))]])
probs = qpe(U, estimation_wires, target_wires)
print(probs)
print(phase_window(probs, estimation_wires))
# MODIFY TO TRUE AFTER TESTING YOUR SOLUTION
done = True