simulated-bifurcation-algorithm icon indicating copy to clipboard operation
simulated-bifurcation-algorithm copied to clipboard

Force backend computation dtype to float

Open bqth29 opened this issue 1 year ago • 1 comments

💬 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 which is used by the SB optimizer can only be defined with float32 (default) 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, a new dtype argument must be passed to indicate the dtype of the Ising model that will be generated. When calling QuadraticPolynomial::optimize, QuadraticPolynomial::maximize or QuadraticPolynomial::minimize, a dtype argument must also be provided to indicate the backend dtype (for equivalent Ising model and thus SB optimizer computations).

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 as the optimization dtype. If not provided, float32 will be used.

We concluded that two dtype parameters are not useful for sb.maximize and sb.minimize since the model is only used for optimization and not retrieved in any manner. Thus, using the computation dtype as the model's dtype is acceptable. If users really want to create the model with a specific dtype, they can first build it with build_model and then call one of three optimization methods.

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

  • [x] The code matches the styling rules
  • [x] The new code is covered by relevant tests
  • [x] Documentation was added

🚀 New features

None.

🐞 Bug fixes

None.

📣 Supplementary information

None.

bqth29 avatar Apr 20 '24 15:04 bqth29

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 100.00%. Comparing base (6194a9e) to head (9ff455d). Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #64   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           36        36           
  Lines         1600      1612   +12     
=========================================
+ Hits          1600      1612   +12     

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

codecov[bot] avatar Apr 20 '24 15:04 codecov[bot]

Overriden by #78

bqth29 avatar Sep 29 '24 20:09 bqth29