Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Differentiating autograd.numpy.linalg.norm gives incorrect results #612

Open
mudit2812 opened this issue Nov 21, 2023 · 0 comments
Open
Labels
bug This item is a bug that needs to be investigated or fixed

Comments

@mudit2812
Copy link

This came to be known due to an issue on the PennyLane GitHub. Consider the following function:

import autograd
import autograd.numpy as np

def get_sv(theta, gamma=0):
    coeff = np.array([((1 + np.exp(-1.j * theta)) / 2) * (np.sqrt(1 - gamma)) , (1 - np.exp(-1.j * theta)) / 2]) / (1 - gamma * (np.cos(theta)**2))
    x = coeff / np.sqrt(np.sum(np.real(coeff) ** 2 + np.imag(coeff) ** 2))
    res = np.real(x) ** 2 + np.imag(x) ** 2
    return res

x = np.array(10.)
autograd.jacobian(get_sv, 0)(10.0)
array([ 0.27201056, -0.27201056])

The norm of coeff is 1.0, so I get the same jacobian if I remove the normalization step. However, if I use np.linalg.norm instead, I get a different jacobian:

def get_sv(theta, gamma=0):
    coeff = np.array([((1 + np.exp(-1.j * theta)) / 2) * (np.sqrt(1 - gamma)) , (1 - np.exp(-1.j * theta)) / 2]) / (1 - gamma * (np.cos(theta)**2))
    x = coeff / np.linalg.norm(coeff)
    res = np.real(x) ** 2 + np.imag(x) ** 2
    return res

x = np.array(10.)
autograd.jacobian(get_sv, 0)(10.0)
array([0.34547   , 0.56747525])

System info:

Platform info:           macOS-13.6.1-arm64-arm-64bit
Python version:          3.10.12
Autograd version:        1.6.2
Numpy version:           1.26.1
Scipy version:           1.11.1
@agriyakhetarpal agriyakhetarpal added the bug This item is a bug that needs to be investigated or fixed label Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This item is a bug that needs to be investigated or fixed
Projects
None yet
Development

No branches or pull requests

2 participants