HS.5.1 Polynomial Error

Hi everyone,

I’m trying to implement a QSP sequence that reproduces the polynomial for two iterations of Grover’s search:

P(x) = 16x^5 - 20x^3 + 5x

I’ve double-checked the encoding and signs, and everything seems consistent with the theory but I’m getting the following error when I run my code:

Error: Your polynomial isn't correct.

Here is the full code I`m usuing:

target_poly = [0, 5, 0, -20, 0, 16] # Coefficients of the polynomial ordered from lowest to highest power

RZ_angles = qml.poly_to_angles(target_poly, "QSP") # Get the QSP angles from PennyLane

a_array = np.linspace(-1,1,200) # Array of 'a' values (overlaps) to evaluate the polynomial over

dev = qml.device("default.qubit", wires=1)

@qml.qnode(dev)

def qsp(): 
    """
    This quantum function implements a QSP sequence for Grovers search 
    and returns an array that contains the information to extract the polynomial

    """
    for k in range(len(RZ_angles) - 1, 0, -1):
        qml.RZ(-2*RZ_angles[k], wires=0)
        qml.RX(2 * np.arccos(a), wires=0)

    qml.RZ(-2*RZ_angles[0], wires=0)
        
    return qml.state()

P_a_QSP = []
for val in a_array:
    a = val  
    P_a_QSP.append(qsp()[0]) 
P_a_QSP = np.array(P_a_QSP)

Any idea what might be wrong?

Thanks in advance!

Hi!
thank you for your question.
Your answer is almost right. This is a matter of slicing the result of qsp() correctly.
Try to print what the function output and see how you can extract an array of the polynomial (recall to select the real part). Taking a look at this piece of documentation might be useful.
Let me know if this works.