bqth29 / simulated-bifurcation-algorithm

Python CPU/GPU implementation of the Simulated Bifurcation (SB) algorithm to solve quadratic optimization problems (QUBO, Ising, TSP, optimal asset allocations for a portfolio, etc.).
MIT License
103 stars 25 forks source link

Force backend dtype to float #62

Closed bqth29 closed 4 months ago

bqth29 commented 5 months ago

šŸ’¬ Pull Request Description

Based on #41, a reflexion was carried out regarding the difference between the models' dtype and the computation dtype.

Because the oscillators in the SB backend are in [-1, 1], the backend computation dtype must be a float. Besides, some key PyTorch methods are not available for float16 so only float32 and float64 are considered.

Thus, the core Ising model and the SB optimizer can only run on float32 or float64 dtype, any other dtype will raise a ValueError.

However, QuadraticPolynomials can still be of any dtype. When such a polynomial is converted to an Ising model:

Once the optimal spins are retrieved by the polynomial at the end of the optimization and converted to integer values according to the optimization domain, the tensors are converted to the polynomial's dtype.

When passing the dtype parameter to the sb.maximize/sb.minimize functions, the dtype will be used as the QuadraticPolynomial's dtype and by extension as the optimization dtype if the dtype id float (otherwise, it will be float32).

Finally, for Polynomials, if the dtype and/or device parameters are set to None, the default dtype and/or device of PyTorch will be used.

āœ”ļø Check list

šŸš€ New features

None.

šŸž Bug fixes

None.

šŸ“£ Supplementary information

None.

codecov[bot] commented 5 months ago

Codecov Report

Attention: Patch coverage is 98.61111% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 99.93%. Comparing base (54cf452) to head (01b3802).

Files Patch % Lines
src/simulated_bifurcation/check_dtype.py 87.50% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #62 +/- ## =========================================== - Coverage 100.00% 99.93% -0.07% =========================================== Files 36 38 +2 Lines 1600 1634 +34 =========================================== + Hits 1600 1633 +33 - Misses 0 1 +1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.