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

Overview

FunctionDescription
binaryCreate a binary variable expression.
spinCreate a spin variable expression.
ClassDescription
BinaryExpr
BinaryModel
BinarySampleSet
VarType

Functions

binary [source]

def binary(index: int) -> BinaryExpr[VarType]

Create a binary variable expression.

Parameters:

NameTypeDescription
indexintThe index of the binary variable.

Returns:

BinaryExpr[VarType] — BinaryExpr[VarType.BINARY]: The binary variable expression.


spin [source]

def spin(index: int) -> BinaryExpr[VarType]

Create a spin variable expression.

Parameters:

NameTypeDescription
indexintThe index of the spin variable.

Returns:

BinaryExpr[VarType] — BinaryExpr[VarType.SPIN]: The spin variable expression.

Classes

BinaryExpr [source]

class BinaryExpr(Generic[VT])

Constructor

def __init__(
    self,
    vartype: VT = VarType.BINARY,
    constant: float = 0.0,
    coefficients: dict[tuple[int, ...], float] = dict(),
) -> None

Attributes

Methods

copy
def copy(self) -> BinaryExpr[VT]

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]

VarType [source]

class VarType(enum.StrEnum)

Attributes


qamomile.optimization.binary_model.expr

Overview

FunctionDescription
binaryCreate a binary variable expression.
is_close_zeroCheck if a given floating-point value is close to zero within a small tolerance.
spinCreate a spin variable expression.
ClassDescription
BinaryExpr
VarType

Functions

binary [source]

def binary(index: int) -> BinaryExpr[VarType]

Create a binary variable expression.

Parameters:

NameTypeDescription
indexintThe index of the binary variable.

Returns:

BinaryExpr[VarType] — BinaryExpr[VarType.BINARY]: The binary variable expression.


is_close_zero [source]

def is_close_zero(value: float, abs_tol = 1e-15) -> bool

Check if a given floating-point value is close to zero within a small tolerance.

Parameters:

NameTypeDescription
valuefloatThe floating-point value to check.

Returns:

bool — True if the value is close to zero, False otherwise.


spin [source]

def spin(index: int) -> BinaryExpr[VarType]

Create a spin variable expression.

Parameters:

NameTypeDescription
indexintThe index of the spin variable.

Returns:

BinaryExpr[VarType] — BinaryExpr[VarType.SPIN]: The spin variable expression.

Classes

BinaryExpr [source]

class BinaryExpr(Generic[VT])
Constructor
def __init__(
    self,
    vartype: VT = VarType.BINARY,
    constant: float = 0.0,
    coefficients: dict[tuple[int, ...], float] = dict(),
) -> None
Attributes
Methods
copy
def copy(self) -> BinaryExpr[VT]

VarType [source]

class VarType(enum.StrEnum)
Attributes

qamomile.optimization.binary_model.model

Overview

FunctionDescription
is_close_zeroCheck if a given floating-point value is close to zero within a small tolerance.
normalize_by_abs_maxNormalize the BinaryExpr by its absolute maximum coefficient.
normalize_by_factorNormalize the BinaryExpr by a given factor.
normalize_by_rmsNormalize coefficients by the root mean square.
ClassDescription
BinaryExpr
BinaryModel
BinarySampleSet
SampleResultResult of a sample() execution.
VarType

Functions

is_close_zero [source]

def is_close_zero(value: float, abs_tol = 1e-15) -> bool

Check if a given floating-point value is close to zero within a small tolerance.

Parameters:

NameTypeDescription
valuefloatThe floating-point value to check.

Returns:

bool — True if the value is close to zero, False otherwise.


normalize_by_abs_max [source]

def normalize_by_abs_max(model: BinaryExpr, replace: bool = False) -> BinaryExpr

Normalize the BinaryExpr by its absolute maximum coefficient.

Parameters:

NameTypeDescription
modelBinaryExprThe binary expression to be normalized.

Returns:

BinaryExpr — The normalized binary expression.


normalize_by_factor [source]

def normalize_by_factor(model: BinaryExpr, factor: float, replace: bool = False) -> BinaryExpr

Normalize the BinaryExpr by a given factor.

Parameters:

NameTypeDescription
modelBinaryExprThe binary expression to be normalized.
factorfloatThe normalization factor.

Returns:

BinaryExpr — The normalized binary expression.


normalize_by_rms [source]

def normalize_by_rms(expr: BinaryExpr, replace: bool = False) -> BinaryExpr

Normalize coefficients by the root mean square.

The coefficients for normalized is defined as:

W=1Ek{u1,,uk}(wu1,...,uk(k))2++1E1u(wu(1))2W = \sqrt{\frac{1}{\lvert E_k \rvert} \sum_{\{u_1, \dots, u_k\}} (w_{u_1,...,u_k}^{(k)})^2 + \cdots + \frac{1}{\lvert E_1 \rvert} \sum_u (w_u^{(1)})^2}

where w are coefficients and their subscriptions imply a term to be applied. E_i are the number of i-th order terms. We normalize the Ising Hamiltonian as

H~=1W(C+iwiZi++i0,,ikwi0,,ikZi0Zik)\tilde{H} = \frac{1}{W} \left( C + \sum_i w_i Z_i + \cdots + \sum_{i_0, \dots, i_k} w_{i_0, \dots, i_k} Z_{i_0}\dots Z_{i_k} \right)

This method is proposed in [Sureshbabu2024parametersettingin]

Classes

BinaryExpr [source]

class BinaryExpr(Generic[VT])
Constructor
def __init__(
    self,
    vartype: VT = VarType.BINARY,
    constant: float = 0.0,
    coefficients: dict[tuple[int, ...], float] = dict(),
) -> None
Attributes
Methods
copy
def copy(self) -> BinaryExpr[VT]

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]

SampleResult [source]

class SampleResult(Generic[T])

Result of a sample() execution.

Contains results as a list of (value, count) tuples.

Example:

result.results  # [(0.25, 500), (0.75, 500)]
Constructor
def __init__(self, results: list[tuple[T, int]], shots: int) -> None
Attributes
Methods
most_common
def most_common(self, n: int = 1) -> list[tuple[T, int]]

Return the n most common results.

Parameters:

NameTypeDescription
nintNumber of results to return.

Returns:

list[tuple[T, int]] — List of (result, count) tuples sorted by count descending.

probabilities
def probabilities(self) -> list[tuple[T, float]]

Return probability distribution over results.

Returns:

list[tuple[T, float]] — List of (value, probability) tuples.


VarType [source]

class VarType(enum.StrEnum)
Attributes

qamomile.optimization.binary_model.normalize

Overview

FunctionDescription
normalize_by_abs_maxNormalize the BinaryExpr by its absolute maximum coefficient.
normalize_by_factorNormalize the BinaryExpr by a given factor.
normalize_by_rmsNormalize coefficients by the root mean square.
ClassDescription
BinaryExpr

Functions

normalize_by_abs_max [source]

def normalize_by_abs_max(model: BinaryExpr, replace: bool = False) -> BinaryExpr

Normalize the BinaryExpr by its absolute maximum coefficient.

Parameters:

NameTypeDescription
modelBinaryExprThe binary expression to be normalized.

Returns:

BinaryExpr — The normalized binary expression.


normalize_by_factor [source]

def normalize_by_factor(model: BinaryExpr, factor: float, replace: bool = False) -> BinaryExpr

Normalize the BinaryExpr by a given factor.

Parameters:

NameTypeDescription
modelBinaryExprThe binary expression to be normalized.
factorfloatThe normalization factor.

Returns:

BinaryExpr — The normalized binary expression.


normalize_by_rms [source]

def normalize_by_rms(expr: BinaryExpr, replace: bool = False) -> BinaryExpr

Normalize coefficients by the root mean square.

The coefficients for normalized is defined as:

W=1Ek{u1,,uk}(wu1,...,uk(k))2++1E1u(wu(1))2W = \sqrt{\frac{1}{\lvert E_k \rvert} \sum_{\{u_1, \dots, u_k\}} (w_{u_1,...,u_k}^{(k)})^2 + \cdots + \frac{1}{\lvert E_1 \rvert} \sum_u (w_u^{(1)})^2}

where w are coefficients and their subscriptions imply a term to be applied. E_i are the number of i-th order terms. We normalize the Ising Hamiltonian as

H~=1W(C+iwiZi++i0,,ikwi0,,ikZi0Zik)\tilde{H} = \frac{1}{W} \left( C + \sum_i w_i Z_i + \cdots + \sum_{i_0, \dots, i_k} w_{i_0, \dots, i_k} Z_{i_0}\dots Z_{i_k} \right)

This method is proposed in [Sureshbabu2024parametersettingin]

Classes

BinaryExpr [source]

class BinaryExpr(Generic[VT])
Constructor
def __init__(
    self,
    vartype: VT = VarType.BINARY,
    constant: float = 0.0,
    coefficients: dict[tuple[int, ...], float] = dict(),
) -> None
Attributes
Methods
copy
def copy(self) -> BinaryExpr[VT]

qamomile.optimization.binary_model.sampleset

Overview

ClassDescription
BinarySampleSet
VarType

Classes

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]

VarType [source]

class VarType(enum.StrEnum)
Attributes