Overview¶
| Class | Description |
|---|---|
MathematicalProblemConverter | |
SampleResult | Result of a sample() execution. |
Classes¶
MathematicalProblemConverter [source]¶
class MathematicalProblemConverter(abc.ABC)Constructor¶
def __init__(self, instance: ommx.v1.Instance | BinaryModel) -> NoneAttributes¶
instanceoriginal_vartypespin_model
Methods¶
decode¶
def decode(self, samples: SampleResult[list[int]]) -> BinarySampleSet | ommx.v1.SampleSetDecode quantum measurement results.
The return type tracks the input that built this converter:
Built from an :class:
ommx.v1.Instance— returns an :class:ommx.v1.SampleSetevaluated against the original (un-penalized) instance, so feasibility, objective, and per-constraint violations are available through OMMX’s own API (.summary,.summary_with_constraints,.best_feasible,.feasible,.objectives).Built from a :class:
BinaryModel— returns a :class:BinarySampleSetwith samples in the model’s original vartype (BINARY 0/1 or SPIN ±1), energies, and shot counts.
Parameters:
| Name | Type | Description |
|---|---|---|
samples | SampleResult[list[int]] | Raw quantum measurement results from ExecutableProgram.sample(...).result(). |
Returns:
BinarySampleSet | ommx.v1.SampleSet — BinarySampleSet | ommx.v1.SampleSet: see method description.
See Also:
:meth:decode_to_binary_sampleset: always returns a
:class:BinarySampleSet. Use it when you need the
QUBO-domain (penalty-included) energy — e.g. to drive a
classical optimizer that must penalize infeasibility.
Example:
>>> # OMMX in → OMMX out
>>> converter = QAOAConverter(ommx_instance)
>>> exe = converter.transpile(QiskitTranspiler(), p=2)
>>> result = exe.sample(QiskitTranspiler().executor(),
... shots=1024,
... bindings={"gammas": gs, "betas": bs}).result()
>>> sample_set = converter.decode(result)
>>> sample_set.best_feasible.objectivedecode_to_binary_sampleset¶
def decode_to_binary_sampleset(self, samples: SampleResult[list[int]]) -> BinarySampleSetDecode samples into a :class:BinarySampleSet.
Always returns a :class:BinarySampleSet, regardless of whether
this converter was constructed with an :class:ommx.v1.Instance
or a :class:BinaryModel. Use this when you need:
The QUBO-domain
energy(penalty-included), e.g. as the cost driving a classical optimizer like COBYLA — :meth:decodeon OMMX-backed converters returns the un-penalized OMMX objective which won’t penalize infeasibility.The per-state
samples/num_occurrences/vartypeviews from :class:BinarySampleSet.
For most usage — feasibility, original-objective evaluation,
per-constraint diagnostics — prefer the polymorphic
:meth:decode, which returns an :class:ommx.v1.SampleSet for
OMMX-backed converters.
Parameters:
| Name | Type | Description |
|---|---|---|
samples | SampleResult[list[int]] | Raw quantum measurement results from ExecutableProgram.sample(...).result(). |
Returns:
BinarySampleSet — keyed by the SPIN model’s original variable
BinarySampleSet — indices (the QUBO variable IDs for OMMX-backed converters)
BinarySampleSet — in the converter’s original_vartype — BINARY for
BinarySampleSet — OMMX-backed converters, the :class:BinaryModel’s declared
BinarySampleSet — vartype otherwise.
get_cost_hamiltonian¶
def get_cost_hamiltonian(self) -> qm_o.HamiltonianConstruct the cost Hamiltonian.
Subclasses must implement this method to build the appropriate Hamiltonian for their specific algorithm (e.g., Pauli-Z for QAOA, QRAC-encoded for QRAO).
Returns:
qm_o.Hamiltonian — qm_o.Hamiltonian: The cost Hamiltonian.
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) -> NoneAttributes¶
results: list[tuple[T, int]] List of (value, count) tuples.shots: int Total number of shots executed.
Methods¶
most_common¶
def most_common(self, n: int = 1) -> list[tuple[T, int]]Return the n most common results.
Parameters:
| Name | Type | Description |
|---|---|---|
n | int | Number 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.