Skip to content

Fix binary_intercept and fit_line validation on unbounded search - #1141

Open
davrens101 wants to merge 1 commit into
quantumlib:mainfrom
davrens101:fix-sinter-fit-line
Open

davrens101 wants to merge 1 commit into
quantumlib:mainfrom
davrens101:fix-sinter-fit-line

Conversation

@davrens101

Copy link
Copy Markdown

Summary of Changes

In sinter._probability_util:

  1. binary_intercept did not cast step to float before doubling it in while ...: step *= 2. When step is passed as an integer (e.g., -1 or 1 in fit_line_slope and fit_line_y_at_x), repeated doubling grows arbitrarily large as a Python arbitrary-precision integer rather than overflowing to float('inf'). When passed to np.isinf(step), NumPy 2.x raises:

    TypeError: ufunc 'isinf' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
    

    Furthermore, evaluating func(start_x + step) with massive values can raise OverflowError: (34, 'Result too large') before reaching the termination condition.

    Fix: Cast step = float(step) so math.isinf(step) cleanly catches float overflow, and catch OverflowError during evaluation to raise ValueError("Failed.") as intended when no intercept exists.

  2. In fit_line_slope and fit_line_y_at_x, max_extra_squared_error must be non-negative. A negative value causes target_y = base_cost + max_extra_squared_error < base_cost. Because base_cost is the global minimum of the sum of squared errors, target_y can never be achieved, causing an infinite doubling loop. Added input validation checking max_extra_squared_error >= 0.

Before

>>> import sinter
>>> sinter.fit_line_slope(xs=[1, 2, 3], ys=[2, 4, 6], max_extra_squared_error=-0.5)
TypeError: ufunc 'isinf' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

After

>>> import sinter
>>> sinter.fit_line_slope(xs=[1, 2, 3], ys=[2, 4, 6], max_extra_squared_error=-0.5)
ValueError: max_extra_squared_error=-0.5 < 0

Unit tests added to _probability_util_test.py verifying the validation and failure conditions.

Signed-off-by: Davis Rens <166565828+davrens101@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant