CplexSolver Class Documentation
Overview
Section titled “Overview”The CplexSolver class implements a classical QUBO solver using IBM ILOG CPLEX. It extends the BaseClassicalSolver abstract base class and translates a QUBO (Quadratic Unconstrained Binary Optimization) instance into a CPLEX quadratic optimization model.
Initialization
Section titled “Initialization”class CplexSolver(BaseClassicalSolver): def __init__( self, instance: QUBOInstance, config: Optional[Dict[str, Any]] = None )- Parameters:
instance(QUBOInstance): QUBO problem containing a square coefficient matrix (torch.Tensor).config(Optional[Dict[str, Any]]): Dictionary supporting:cplex_maxtime(float, default600.0): Maximum solve time in seconds.cplex_log_path(str, default"solver.log"): Path for CPLEX log output.
Configuration Options
Section titled “Configuration Options”| Key | Type | Default | Description |
|---|---|---|---|
cplex_maxtime |
float |
600.0 |
Time limit for the CPLEX solver, in seconds. |
cplex_log_path |
str |
"solver.log" |
Log file path for CPLEX output streams. |
Configure the solver by passing a dict at instantiation:
config = { "cplex_maxtime": 300.0, "cplex_log_path": "cplex_run.log",}solver = CplexSolver(qubo_instance, config)solve() Method
Section titled “solve() Method”def solve(self) -> QUBOSolutionSolves the QUBO problem via CPLEX and returns a QUBOSolution containing bitstrings and costs.
Behavior
Section titled “Behavior”-
Validate Input Raises
ValueErrorifinstance.coefficientsisNone. -
Handle Empty Problem If the coefficient matrix has size zero, returns an empty
QUBOSolution. -
Convert to Sparse Format Calls
qubo_instance_to_sparsepairs(instance)to obtainList[cplex.SparsePair]for quadratic terms. -
Build CPLEX Model
- Instantiate
cplex.Cplex(). - Redirect log, error, warning, and result streams to the file at
cplex_log_path. - Set
timelimitparameter tocplex_maxtime. - Specify minimization objective.
- Add
Nbinary variables (types="B" * N). -
Assign quadratic objective via
objective.set_quadratic(sparsepairs). -
Solve Invoke
problem.solve(). -
Extract Results
- Retrieve variable values (
problem.solution.get_values()) and objective cost (get_objective_value()). -
Close the log file.
-
Format Output
- Build a
torch.Tensorfor bitstrings of shape(1, N), dtypefloat32. - Build a
torch.Tensorfor cost of shape(1,), dtypefloat32. - Return
QUBOSolution(bitstrings, costs).
Exceptions
Section titled “Exceptions”ValueError: Thrown if the QUBO instance has no coefficients.
Example Usage
Section titled “Example Usage”import torchfrom qubosolver import QUBOInstancefrom qubosolver.config import SolverConfig, ClassicalConfigfrom qubosolver.solver import QuboSolver
# Define a simple 2×2 QUBO matrix (identity)matrix = torch.eye(2)instance = QUBOInstance(coefficients=matrix)
# Prepare solver configurationcplex = ClassicalConfig( classical_solver_type="cplex", cplex_maxtime=120.0, cplex_log_path="cplex_run.log",)config = SolverConfig( classical=cplex, use_quantum=False)
# Directly obtain solution via dispatcherclassical_solver = QuboSolver(instance, config)solution = classical_solver.solve()
print("Bitstrings:", solution.bitstrings)print("Costs:", solution.costs)