Skip to content

Commit

Permalink
linear method: handle variation = 0 or 1
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsung committed Mar 20, 2024
1 parent f7d5170 commit 7842299
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
8 changes: 3 additions & 5 deletions python/ffsim/optimize/linear_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,16 @@ def minimize_linear_method(
""" # noqa: E501
if regularization < 0:
raise ValueError(f"regularization must be nonnegative. Got {regularization}.")
if not 0 < variation < 1:
raise ValueError(
f"variation must be strictly between 0 and 1. Got {variation}."
)
if not 0 <= variation <= 1:
raise ValueError(f"variation must be between 0 and 1. Got {variation}.")
if maxiter < 1:
raise ValueError(f"maxiter must be at least 1. Got {maxiter}.")

if optimize_hyperparameters_args is None:
optimize_hyperparameters_args = dict(method="L-BFGS-B")

regularization_param = math.sqrt(regularization)
variation_param = math.atanh(2 * variation - 1)
variation_param = math.atanh(2 * min(1 - 1e-8, max(1e-8, variation)) - 1)
params = x0.copy()
converged = False
intermediate_result = OptimizeResult(
Expand Down
4 changes: 2 additions & 2 deletions tests/optimize/linear_method_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ def callback(intermediate_result):
)
with pytest.raises(ValueError, match="variation"):
result = ffsim.optimize.minimize_linear_method(
params_to_vec, x0=x0, hamiltonian=hamiltonian, variation=0
params_to_vec, x0=x0, hamiltonian=hamiltonian, variation=-0.1
)
with pytest.raises(ValueError, match="variation"):
result = ffsim.optimize.minimize_linear_method(
params_to_vec, x0=x0, hamiltonian=hamiltonian, variation=1
params_to_vec, x0=x0, hamiltonian=hamiltonian, variation=1.1
)
with pytest.raises(ValueError, match="maxiter"):
result = ffsim.optimize.minimize_linear_method(
Expand Down

0 comments on commit 7842299

Please sign in to comment.