BinaryOptimizationProblem¶
- class BinaryOptimizationProblem(problem, *, hamiltonian_builder='native', quadratization_strength=None, decomposer=None, composer=None)[source]¶
Bases:
QAOAProblemGeneric QUBO or HUBO problem for QAOA.
Wraps a binary optimization problem expressed as either a quadratic form (QUBO) or higher-order polynomial (HUBO), normalizes it to a canonical
BinaryPolynomialProblem, and exposes the QAOA building blocks: cost Hamiltonian (via Ising conversion), standard X-mixer, and ground-state initial superposition.Accepted inputs (anything
dimod.BinaryQuadraticModelordimod.BinaryPolynomialaccepts, plus matrices):np.ndarray/scipy.sparse.spmatrix— square QUBO matrix.dimod.BinaryQuadraticModel— quadratic form with named variables.dictwith tuple keys — HUBO terms, e.g.{(0,): -1.0, (0, 1, 2): 2.0}.dimod.BinaryPolynomial— polynomial of arbitrary degree.
Ising-conversion strategies selected via
hamiltonian_builder:"native"(default): translates each polynomial term into a Pauli-Z product. Exact, but high-degree terms produce many-body interactions that some simulators handle slowly."quadratized": introduces auxiliary qubits and penalty terms so every interaction becomes two-body. Penalty magnitude isquadratization_strength;Nonepicks2 * max(|hubo coeff|).
Optionally accepts a
dimod.hybriddecomposer/composer pair to enable partitioned solving viadecompose(). Without one, the decomposition-related methods raiseRuntimeError.- Parameters:
problem (
list|ndarray|spmatrix|BinaryQuadraticModel|Mapping[tuple[Hashable,...],float] |BinaryPolynomial) – QUBO matrix, BQM, HUBO dict, or BinaryPolynomial.hamiltonian_builder (
Literal['native','quadratized']) –"native"(default) or"quadratized".quadratization_strength (
float|None) – Penalty strength for the quadratized builder.None(default) auto-picks2 * max(|hubo coeff|). Ignored whenhamiltonian_builder="native". The auto default is sized against a single worst-case term and may under-penalise dense HUBOs where many constraints can be violated simultaneously; pass an explicit value (or raise the multiplier onQuadratizedIsingConverter) for such instances.decomposer (
ProblemDecomposer|None) – Optionalhybrid.traits.ProblemDecomposerthat enablesdecompose().composer (
SubsamplesComposer|None) – Optionalhybrid.traits.SubsamplesComposerfor recombining sub-solutions; defaults tohybrid.SplatComposer.
Examples
>>> import numpy as np >>> from divi.qprog.problems import BinaryOptimizationProblem >>> Q = np.array([[-1.0, 2.0], [0.0, -1.0]]) >>> problem = BinaryOptimizationProblem(Q) >>> problem.cost_hamiltonian # ready for QAOA
Attributes Summary
The normalized
BinaryPolynomialProblem.Cost Hamiltonian derived from the Ising conversion of the QUBO/HUBO.
Decode a measurement bitstring to the original variable assignment.
Constant offset from Ising conversion, added back to expectation values.
Encoding metadata from the Ising conversion (e.g. quadratization aux info).
Standard X-mixer over all qubits in the Ising encoding.
The original QUBO/HUBO input passed at construction time.
Methods Summary
Partition the problem using the configured
hybriddecomposer.evaluate_global_solution(solution)Energy of a global bit assignment under the underlying BQM.
extend_solution(current_solution, prog_id, ...)Splice a sub-problem's decoded bits into the global solution.
Number of variables in the global solution vector.
postprocess_candidates(candidates, *[, strict])Run global candidate solutions through the configured composer.
Attributes Documentation
- canonical_problem¶
The normalized
BinaryPolynomialProblem.
- cost_hamiltonian¶
Cost Hamiltonian derived from the Ising conversion of the QUBO/HUBO.
- decode_fn¶
Decode a measurement bitstring to the original variable assignment.
For problems built from named variables (e.g. a BQM with non-integer keys), the result is a
dictmapping the original variable names to their bit values. For integer-indexed problems, returns the encoding’s raw bitstring projection.
- loss_constant¶
Constant offset from Ising conversion, added back to expectation values.
- metadata¶
Encoding metadata from the Ising conversion (e.g. quadratization aux info).
- mixer_hamiltonian¶
Standard X-mixer over all qubits in the Ising encoding.
- raw_problem¶
The original QUBO/HUBO input passed at construction time.
Methods Documentation
- decompose()[source]¶
Partition the problem using the configured
hybriddecomposer.Each non-trivial partition becomes its own
BinaryOptimizationProblemkeyed by(name, size). Partitions with no interactions are tracked internally and skipped during composition.- Raises:
ValueError – If no decomposer was provided at construction.
- Return type:
- evaluate_global_solution(solution)[source]¶
Energy of a global bit assignment under the underlying BQM.
- Raises:
RuntimeError – If no decomposer was provided at construction.
- Return type:
- extend_solution(current_solution, prog_id, candidate_decoded)[source]¶
Splice a sub-problem’s decoded bits into the global solution.
Returns a new list with values at positions corresponding to
prog_id’s variables overwritten bycandidate_decoded.
- initial_solution_size()[source]¶
Number of variables in the global solution vector.
Equals the number of variables in the underlying BQM. Only defined when a decomposer was provided at construction.
- Raises:
RuntimeError – If no decomposer was provided.
- Return type: