Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

qamomile.optimization.post_process

Overview

ClassDescription
LocalSearchLocal search energy minimizer for :class:BinaryModel.
LocalSearchMethodStrategy for picking which bit to flip at each local-search step.

Classes

LocalSearch [source]

class LocalSearch

Local 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:

NameTypeDescription
modelommx.v1.Instance | BinaryModelThe problem to minimize. Either a :class:BinaryModel (any vartype, any order) or an :class:ommx.v1.Instance.

Raises:

Constructor

def __init__(self, model: ommx.v1.Instance | BinaryModel) -> None

Attributes

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.Solution

Run 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:

NameTypeDescription
initial_stateSequence[int] | np.ndarray | ommx.v1.SolutionVariable 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_iterintMaximum iterations (-1 for unlimited).
methodstr | LocalSearchMethodFlip-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:


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


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

ClassDescription
BinaryModel
BinarySampleSet
LocalSearchLocal search energy minimizer for :class:BinaryModel.
LocalSearchMethodStrategy 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]) -> None
Attributes
Methods
calc_energy
def calc_energy(self, state: list[int]) -> float

Calculate the energy for a given variable assignment.

Parameters:

NameTypeDescription
statelist[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:

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:

NameTypeDescription
resultSampleResult[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:

NameTypeDescription
hubodict[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.
constantfloatConstant offset term.
simplifyboolIf 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:

NameTypeDescription
factorfloatThe 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,
) -> None
Attributes
Methods
energy_mean
def energy_mean(self) -> float
lowest
def lowest(self) -> tuple[dict[int, int], float, int]

LocalSearch [source]

class LocalSearch

Local 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:

NameTypeDescription
modelommx.v1.Instance | BinaryModelThe problem to minimize. Either a :class:BinaryModel (any vartype, any order) or an :class:ommx.v1.Instance.

Raises:

Constructor
def __init__(self, model: ommx.v1.Instance | BinaryModel) -> None
Attributes
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.Solution

Run 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:

NameTypeDescription
initial_stateSequence[int] | np.ndarray | ommx.v1.SolutionVariable 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_iterintMaximum iterations (-1 for unlimited).
methodstr | LocalSearchMethodFlip-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:


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

VarType [source]

class VarType(enum.StrEnum)
Attributes