Refactor: Replace if-elif chain with optimizer config dict #470
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors the benchmarking script by replacing the 80+ line if-elif chain for optimizer selection with a centralized, maintainable configuration system. The change improves code readability, maintainability, and makes it easier to add new optimizers in the future.
Problem
The current benchmarking script (
benchmarking_lsbbo_2.py
) contains a very long if-elif chain (80+ lines) for optimizer selection:This approach has several issues:
Solution
1. Centralized Configuration
Replaced the if-elif chain with a dictionary-based configuration system:
2. Dynamic Import Function
Created a clean function to handle optimizer loading:
3. Improved Error Handling
Added proper validation and helpful error messages:
4. Type Safety
Added comprehensive type hints throughout the code for better IDE support and catch potential issues early.
Key Benefits
Backward Compatibility
Testing
Added comprehensive GitHub Actions workflow that tests:
Code Quality Improvements
Files Changed
tutorials/benchmarking_lsbbo_2.py
- Direct refactoring of the original file.github/workflows/test-refactoring.yml
- Comprehensive test suiteFuture Enhancements
This refactoring lays the groundwork for future improvements:
However, this PR focuses solely on the structural improvement to keep changes focused and reviewable.
Testing Instructions: