Overview¶
| Class | Description |
|---|---|
LocalSearch | Local search energy minimizer for :class:BinaryModel. |
LocalSearchMethod | Strategy for picking which bit to flip at each local-search step. |
Classes¶
LocalSearch [source]¶
class LocalSearchLocal search energy minimizer for :class:BinaryModel.
Greedily flips single bits that strictly lower the model’s energy
(calc_energy), so lower is better — the search terminates at a
local minimum where no single-bit flip further decreases energy.
Supports arbitrary-order Ising / HUBO models. Internally converts the model to SPIN representation (±1) for the search, then converts the result back to the original vartype.
Accepts either a :class:BinaryModel directly or an
:class:ommx.v1.Instance, matching the other optimization converters.
An ommx instance is lowered to a BINARY model via
:meth:ommx.v1.Instance.to_hubo, preserving higher-order terms so
that HUBO objectives survive the handoff. When an ommx instance is
used as input, :meth:run returns an :class:ommx.v1.Solution
evaluated against that instance; otherwise it returns a
:class:BinarySampleSet.
Parameters:
| Name | Type | Description |
|---|---|---|
model | ommx.v1.Instance | BinaryModel | The problem to minimize. Either a :class:BinaryModel (any vartype, any order) or an :class:ommx.v1.Instance. |
Raises:
TypeError— If model is neither a BinaryModel nor an ommx.v1.Instance.
Constructor¶
def __init__(self, model: ommx.v1.Instance | BinaryModel) -> NoneAttributes¶
num_bits: int Number of bits the local search flips, including any slack bits.
Methods¶
run¶
def run(
self,
initial_state: Sequence[int] | np.ndarray | ommx.v1.Solution,
max_iter: int = -1,
method: str | LocalSearchMethod = LocalSearchMethod.BEST,
) -> BinarySampleSet | ommx.v1.SolutionRun local search starting from initial_state.
Minimizes the model’s energy: only strictly energy-decreasing flips are accepted, so the returned state is a local minimum (lower is better), not necessarily the global optimum.
Parameters:
| Name | Type | Description |
|---|---|---|
initial_state | Sequence[int] | np.ndarray | ommx.v1.Solution | Variable values in the model’s vartype domain (±1 for SPIN, 0/1 for BINARY). Accepts any int sequence (list, tuple), a 1-D :class:numpy.ndarray, or an :class:ommx.v1.Solution (e.g. the output of another solver — values are read from solution.state.entries and mapped onto the internal BinaryModel via the original decision-variable IDs; slack bits introduced by :meth:ommx.v1.Instance.to_hubo on constrained instances are defaulted to 0). Sequence / ndarray length must equal :attr:num_bits; for constrained :class:ommx.v1.Instance inputs this includes slack bits introduced by :meth:ommx.v1.Instance.to_hubo. |
max_iter | int | Maximum iterations (-1 for unlimited). |
method | str | LocalSearchMethod | Flip-selection strategy. Either a :class:LocalSearchMethod member or its string value ("best" / "first"). |
Returns:
BinarySampleSet | ommx.v1.Solution — The energy-minimized state. If this LocalSearch was
BinarySampleSet | ommx.v1.Solution — constructed from an :class:ommx.v1.Instance, the return
BinarySampleSet | ommx.v1.Solution — value is an :class:ommx.v1.Solution evaluated against that
BinarySampleSet | ommx.v1.Solution — instance (carrying objective, feasibility, and constraint
BinarySampleSet | ommx.v1.Solution — values). Otherwise it is a :class:BinarySampleSet with a
BinarySampleSet | ommx.v1.Solution — single sample in the model’s original vartype.
Raises:
ValueError— If method is not recognized, initial_state has wrong length or invalid values, max_iter is less than -1, or initial_state is an :class:ommx.v1.Solutionthat does not cover the instance’s used decision variables (orLocalSearchwas constructed from a :class:BinaryModel, so there is no instance for the Solution to be interpreted against).
LocalSearchMethod [source]¶
class LocalSearchMethod(enum.StrEnum)Strategy for picking which bit to flip at each local-search step.
Both variants only accept strictly improving flips; the difference is in how the next flip is chosen.
Attributes¶
BESTFIRST
qamomile.optimization.post_process.local_search¶
Local search post-processing for binary optimization models.
Provides first-improvement and best-improvement local search algorithms
that operate on :class:BinaryModel instances (both SPIN and BINARY).
Overview¶
| Class | Description |
|---|---|
BinaryModel | |
BinarySampleSet | |
LocalSearch | Local search energy minimizer for :class:BinaryModel. |
LocalSearchMethod | Strategy for picking which bit to flip at each local-search step. |
VarType |
Classes¶
BinaryModel [source]¶
class BinaryModel(Generic[VT])Constructor¶
def __init__(self, expr: BinaryExpr[VT]) -> NoneAttributes¶
coefficients: dict[tuple[int, ...], float] All coefficients as a single flat dictionary using sequential indices.constant: floathigher: dict[tuple[int, ...], float]index_new_to_originindex_origin_to_newlinear: dict[int, float]num_bits: intorderquad: dict[tuple[int, int], float]vartype: VT
Methods¶
calc_energy¶
def calc_energy(self, state: list[int]) -> floatCalculate the energy for a given variable assignment.
Parameters:
| Name | Type | Description |
|---|---|---|
state | list[int] | Variable values indexed by sequential (zero-origin) indices. For SPIN: values must be +1 or -1. For BINARY: values must be 0 or 1. |
Returns:
float — The energy value.
Raises:
ValueError— If state values are invalid for the vartype.
change_vartype¶
def change_vartype(self, vartype: VarType) -> 'BinaryModel'decode_from_sampleresult¶
def decode_from_sampleresult(self, result: SampleResult[list[int]]) -> BinarySampleSet[VT]Decode quantum measurement results into samples with energies.
Converts raw measurement bitstrings (0/1 from quantum hardware) into the model’s variable domain and calculates energies.
Parameters:
| Name | Type | Description |
|---|---|---|
result | SampleResult[list[int]] | Measurement results with sequential bit indices |
Returns:
BinarySampleSet[VT] — BinarySampleSet with samples in expression domain, using original indices
from_hubo¶
@classmethod
def from_hubo(
cls,
hubo: dict[tuple[int, ...], float],
constant: float = 0.0,
simplify: bool = False,
) -> 'BinaryModel'Create a BINARY BinaryModel from HUBO coefficients.
Parameters:
| Name | Type | Description |
|---|---|---|
hubo | dict[tuple[int, ...], float] | HUBO coefficients mapping index tuples to values. Index tuples are sorted and deduplicated. Duplicate indices in a single term (e.g., (0, 0, 2)) emit a warning and are normalized to unique indices ((0, 2)). Empty tuples (()) are accumulated into the constant term. |
constant | float | Constant offset term. |
simplify | bool | If True, remove near-zero coefficients. |
Returns:
'BinaryModel' — BinaryModel with BINARY vartype.
from_ising¶
@classmethod
def from_ising(
cls,
linear: dict[int, float],
quad: dict[tuple[int, int], float],
constant: float = 0.0,
simplify: bool = False,
) -> 'BinaryModel'from_qubo¶
@classmethod
def from_qubo(
cls,
qubo: dict[tuple[int, int], float],
constant: float = 0.0,
simplify: bool = False,
) -> 'BinaryModel'normalize_by_abs_max¶
def normalize_by_abs_max(self, replace: bool = False) -> BinaryModel[VT]Normalize the BinaryModel by its absolute maximum coefficient.
Returns:
BinaryModel[VT] — BinaryModel[VT]: The normalized binary model.
normalize_by_factor¶
def normalize_by_factor(self, factor: float, replace: bool = False) -> BinaryModel[VT]Normalize the BinaryModel by a given factor.
Parameters:
| Name | Type | Description |
|---|---|---|
factor | float | The normalization factor. |
Returns:
BinaryModel[VT] — BinaryModel[VT]: The normalized binary model.
normalize_by_rms¶
def normalize_by_rms(self, replace: bool = False) -> BinaryModel[VT]Normalize the BinaryModel by its root mean square.
Returns:
BinaryModel[VT] — BinaryModel[VT]: The normalized binary model.
BinarySampleSet [source]¶
class BinarySampleSet(Generic[VT])Constructor¶
def __init__(
self,
samples: list[dict[int, int]],
num_occurrences: list[int],
energy: list[float],
vartype: VT = VarType.BINARY,
) -> NoneAttributes¶
energy: list[float]num_occurrences: list[int]samples: list[dict[int, int]]vartype: VT
Methods¶
energy_mean¶
def energy_mean(self) -> floatlowest¶
def lowest(self) -> tuple[dict[int, int], float, int]LocalSearch [source]¶
class LocalSearchLocal search energy minimizer for :class:BinaryModel.
Greedily flips single bits that strictly lower the model’s energy
(calc_energy), so lower is better — the search terminates at a
local minimum where no single-bit flip further decreases energy.
Supports arbitrary-order Ising / HUBO models. Internally converts the model to SPIN representation (±1) for the search, then converts the result back to the original vartype.
Accepts either a :class:BinaryModel directly or an
:class:ommx.v1.Instance, matching the other optimization converters.
An ommx instance is lowered to a BINARY model via
:meth:ommx.v1.Instance.to_hubo, preserving higher-order terms so
that HUBO objectives survive the handoff. When an ommx instance is
used as input, :meth:run returns an :class:ommx.v1.Solution
evaluated against that instance; otherwise it returns a
:class:BinarySampleSet.
Parameters:
| Name | Type | Description |
|---|---|---|
model | ommx.v1.Instance | BinaryModel | The problem to minimize. Either a :class:BinaryModel (any vartype, any order) or an :class:ommx.v1.Instance. |
Raises:
TypeError— If model is neither a BinaryModel nor an ommx.v1.Instance.
Constructor¶
def __init__(self, model: ommx.v1.Instance | BinaryModel) -> NoneAttributes¶
num_bits: int Number of bits the local search flips, including any slack bits.
Methods¶
run¶
def run(
self,
initial_state: Sequence[int] | np.ndarray | ommx.v1.Solution,
max_iter: int = -1,
method: str | LocalSearchMethod = LocalSearchMethod.BEST,
) -> BinarySampleSet | ommx.v1.SolutionRun local search starting from initial_state.
Minimizes the model’s energy: only strictly energy-decreasing flips are accepted, so the returned state is a local minimum (lower is better), not necessarily the global optimum.
Parameters:
| Name | Type | Description |
|---|---|---|
initial_state | Sequence[int] | np.ndarray | ommx.v1.Solution | Variable values in the model’s vartype domain (±1 for SPIN, 0/1 for BINARY). Accepts any int sequence (list, tuple), a 1-D :class:numpy.ndarray, or an :class:ommx.v1.Solution (e.g. the output of another solver — values are read from solution.state.entries and mapped onto the internal BinaryModel via the original decision-variable IDs; slack bits introduced by :meth:ommx.v1.Instance.to_hubo on constrained instances are defaulted to 0). Sequence / ndarray length must equal :attr:num_bits; for constrained :class:ommx.v1.Instance inputs this includes slack bits introduced by :meth:ommx.v1.Instance.to_hubo. |
max_iter | int | Maximum iterations (-1 for unlimited). |
method | str | LocalSearchMethod | Flip-selection strategy. Either a :class:LocalSearchMethod member or its string value ("best" / "first"). |
Returns:
BinarySampleSet | ommx.v1.Solution — The energy-minimized state. If this LocalSearch was
BinarySampleSet | ommx.v1.Solution — constructed from an :class:ommx.v1.Instance, the return
BinarySampleSet | ommx.v1.Solution — value is an :class:ommx.v1.Solution evaluated against that
BinarySampleSet | ommx.v1.Solution — instance (carrying objective, feasibility, and constraint
BinarySampleSet | ommx.v1.Solution — values). Otherwise it is a :class:BinarySampleSet with a
BinarySampleSet | ommx.v1.Solution — single sample in the model’s original vartype.
Raises:
ValueError— If method is not recognized, initial_state has wrong length or invalid values, max_iter is less than -1, or initial_state is an :class:ommx.v1.Solutionthat does not cover the instance’s used decision variables (orLocalSearchwas constructed from a :class:BinaryModel, so there is no instance for the Solution to be interpreted against).
LocalSearchMethod [source]¶
class LocalSearchMethod(enum.StrEnum)Strategy for picking which bit to flip at each local-search step.
Both variants only accept strictly improving flips; the difference is in how the next flip is chosen.
Attributes¶
BESTFIRST
VarType [source]¶
class VarType(enum.StrEnum)Attributes¶
BINARYSPIN