-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
473 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# (C) Copyright IBM 2024. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Callable | ||
|
||
import numpy as np | ||
from scipy.optimize import OptimizeResult | ||
from scipy.sparse.linalg import LinearOperator | ||
|
||
from ffsim import states | ||
|
||
|
||
class WrappedCallable: | ||
"""Callable wrapper used to count function calls.""" | ||
|
||
def __init__( | ||
self, func: Callable[[np.ndarray], np.ndarray], optimize_result: OptimizeResult | ||
): | ||
self.func = func | ||
self.optimize_result = optimize_result | ||
|
||
def __call__(self, x: np.ndarray) -> np.ndarray: | ||
self.optimize_result.nfev += 1 | ||
return self.func(x) | ||
|
||
|
||
class WrappedLinearOperator: | ||
"""LinearOperator wrapper used to count LinearOperator applications.""" | ||
|
||
def __init__(self, linop: LinearOperator, optimize_result: OptimizeResult): | ||
self.linop = linop | ||
self.optimize_result = optimize_result | ||
|
||
def __matmul__(self, other: np.ndarray): | ||
if len(other.shape) == 1: | ||
self.optimize_result.nlinop += 1 | ||
else: | ||
_, n = other.shape | ||
self.optimize_result.nlinop += n | ||
return self.linop @ other | ||
|
||
def __rmatmul__(self, other: np.ndarray): | ||
if len(other.shape) == 1: | ||
self.optimize_result.nlinop += 1 | ||
else: | ||
n, _ = other.shape | ||
self.optimize_result.nlinop += n | ||
return other @ self.linop | ||
|
||
|
||
def gradient_finite_diff( | ||
params_to_vec: Callable[[np.ndarray], np.ndarray], | ||
theta: np.ndarray, | ||
index: int, | ||
epsilon: float, | ||
) -> np.ndarray: | ||
"""Return the gradient of one of the components of a function. | ||
Given a function that maps a vector of "parameters" to an output vector, return | ||
the gradient of one of the parameter components. | ||
Args: | ||
params_to_vec: Function that maps a parameter vector to an output vector. | ||
theta: The parameters at which to evaluate the gradient. | ||
index: The index of the parameter to take the gradient of. | ||
epsilon: Finite difference step size. | ||
Returns: | ||
The gradient of the desired parameter component. | ||
""" | ||
unit = states.one_hot(len(theta), index, dtype=float) | ||
plus = theta + epsilon * unit | ||
minus = theta - epsilon * unit | ||
return (params_to_vec(plus) - params_to_vec(minus)) / (2 * epsilon) | ||
|
||
|
||
def orthogonal_jacobian_finite_diff( | ||
params_to_vec: Callable[[np.ndarray], np.ndarray], | ||
theta: np.ndarray, | ||
vec: np.ndarray, | ||
epsilon: float, | ||
) -> np.ndarray: | ||
"""Return the "orthogonal" Jacobian matrix of a function. | ||
Rather than the true Jacobian, the columns of the returned matrix contain only | ||
the components of the gradients orthogonal to a given input vector. | ||
Args: | ||
params_to_vec: Function that maps a parameter vector to an output vector. | ||
theta: The parameters at which to evaluate the Jacobian. | ||
vec: The vector to use for computing the orthogonal component of the gradient. | ||
epsilon: Finite difference step size. | ||
Returns: | ||
The "orthogonal" Jacobian matrix. | ||
""" | ||
jac = np.zeros((len(vec), len(theta)), dtype=complex) | ||
for i in range(len(theta)): | ||
grad = gradient_finite_diff(params_to_vec, theta, i, epsilon) | ||
# Subtract the component of the gradient along the vector | ||
grad -= np.vdot(vec, grad) * vec | ||
jac[:, i] = grad | ||
return jac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.