HS.5.2 polynomial

The exercise is from the link above. We need to use QSP to estimate the \text{erf} function. I tried to solve that without viewing the hint by estimating the Chebyshev series of erf. I run this externally and copy the coef to the exercise

from numpy.polynomial.chebyshev import chebfit
def integrand(t):
    return 2/np.sqrt(np.pi) * np.exp(-t**2)

y = []
x = np.arange(-1, 1, 0.005)
for i in x:
    I, _ = quad(integrand, 0, i)
    y.append(I)

# do the chebyshev fit
coef = chebfit(x, y, deg=7)

Now I click the hint, and see we use Taylor expansion. That worked, but aren’t we supposed to use Chebysev as HS.5.1 instead of Taylor?

Hi @mchau , it’s great to see you back in the Forum!

It’s possible that more than one solution can give the right answer but we encourage you to use the method described in the Codercise. We will take a deeper look into this just to check that everything is working correctly but in the meantime I encourage you to try it out with the instructions from the exercise.

Hi @mchau, thanks for your excellent question!

There are a couple of things going on here. On one hand, the description for HS.5.2 does indeed suggest a “Taylor series expansion” of erf, which is indeed what we were looking for. I will elaborate in a moment.

On the other hand, HS.5.2 (and QSP generally) is ultimately agnostic to any polynomial basis you select so long as the final polynomial approximates erf. In principle, you could have used a Chebyshev polynomial with qp.poly_to_angles so long as you enforced definite parity (which the Taylor series for erf does automatically) and converted the Chebyshev polynomial coefficients to the monomial coefficients that qp.poly_to_angles expects. I.e., numpy.polynomial.chebyshev.cheb2poly(coef), then enforce parity, then feed into qp.poly_to_angles.

To clarify, HS.5.1 uses a Chebyshev polynomial because that is what Grover’s algorithm naturally produces (You will find that RZ_angles in HS.5.1 are all 0). However, HS.5.2 uses a Taylor series to approximate erf because it is a simple way of obtaining a definite parity polynomial.

I hope that helps!

Best wishes,

David