Codebook: Changing Perspectives

Ask here about the “Changing Perspectives” Codebook topic from the “Quantum Fourier Transform” module.

I get Error: the output of your function isn’t the right length. Can someone help me understand what I did wrong ? Here is my code
x = len(poly_a) + len(poly_b) + 1

n = nearest_power_of_2(x)

poly_a = np.pad(poly_a, (0, n - len(poly_a)), 'constant')
poly_b = np.pad(poly_b, (0, n - len(poly_b)), 'constant')

val_rep_a = np.fft.fft(poly_a)
val_rep_b = np.fft.fft(poly_b)

mult = val_rep_a * val_rep_b

coef_rep = np.fft.ifft(mult)

return coef_rep[:x]

Thanks

Hi @yoshypdf,

I see several things happening. First, double check your value for ‘x’. Are you sure you need to add one? Or instead do something else?

Also, note that the Codercise doesn’t ask you to use np.fft.fft. It asks you to use coefficients_to_values and values_to_coefficients which are the functions you had from before.

This can help you simplify the second half of your code a lot and arrive at the answer.

I hope this helps!

We need degA + degB + 1 points to interpolate hence ‘x’ should be
x = len(poly_a) + len(poly_b) - 1
For coefficients_to_values and values_to_coefficients there are defined as np.fft.fft and np.fft.ifft so I don’t think it changes anything ( I tried both ).
Now that I have the correct value for x, I get a new error that I don’t understand the origin Error: operands could not be broadcast together with shapes (3,) (4,) .
When I try my code on personal examples it seems to work fine, I wonder which inputs make it fail.

Hi @yoshypdf,

You’re right. Make sure to check your return statement. You’re asked to return all of the coefficients, not a subset of them. I hope this helps!

The error message was not clear but your response helped me. Thank you !

1 Like

Thanks for your feedback @yoshypdf .

In this case it’s hard to tailor the error message to this specific scenario. But hopefully if someone runs into the same issue they’ll be able to find this thread and quickly debug their code :smiley: .

Thanks for posting your question here!

1 Like