microquantum.problems.optimization¶
Generic combinatorial optimization problem abstraction.
An OptimizationProblem describes an objective over binary
variables without encoding how it is optimized. It supports two
equivalent, interchangeable views of the same problem:
an objective function
objective(bits) -> floatover binary vectors,an Ising Hamiltonian (spin variables)
cost_hamiltonian().
Classmethods OptimizationProblem.from_qubo() and
OptimizationProblem.from_ising() construct problems from the QUBO /
Ising formulations used by combinatorial-optimization tooling, so the same
problem object stays usable by algorithms (QAOA) as well as classical
brute-force evaluation.
Module Contents¶
- class microquantum.problems.optimization.OptimizationProblem[source]¶
Bases:
microquantum.problems.base.ProblemMinimize an objective over
num_variablesbinary variables.- Variables:
num_variables – Number of binary variables.
objective – Optional
objective(bits) -> floatover a binary vector. Required unlessisingis provided.ising – Optional spin-representation Hamiltonian (
PauliSum).sampler – Optional
sampler(num_variables) -> bitsused by algorithms to sample candidate solutions classically.
- objective: Callable[[numpy.ndarray], float] | None = None[source]¶
- sampler: Callable[[int], numpy.ndarray] | None = None[source]¶
- classmethod from_ising(pauli_sum, *, name='optimization')[source]¶
Build a problem whose Ising cost Hamiltonian is
pauli_sum.- Parameters:
pauli_sum (Any)
name (str)
- Return type:
- classmethod from_qubo(qubo, *, name='optimization')[source]¶
Build a problem from a
QUBOProblem.The objective is the QUBO energy and the Ising view is derived via the standard QUBO -> Ising mapping.
- Parameters:
qubo (Any)
name (str)
- Return type:
- cost_hamiltonian()[source]¶
Return the Ising cost Hamiltonian (
PauliSum).- Raises:
ValueError – If the problem was defined purely by a binary objective and carries no Ising view.
- Return type:
Any
- energy(bits)[source]¶
Evaluate the objective (or the Ising Hamiltonian) on a bit vector.
- Parameters:
bits (numpy.ndarray) – Binary vector of shape (num_variables,).
- Raises:
ValueError – If the vector has the wrong length.
- Return type:
- encode_spins(bits)[source]¶
Map a binary vector to spin values
s = 1 - 2 * x.- Parameters:
bits (numpy.ndarray)
- Return type:
- sample(seed=None)[source]¶
Sample a candidate bit vector classically.
Uses the problem’s
samplerwhen provided; otherwise a uniform random bit vector (seedapplied).- Parameters:
seed (Optional[int])
- Return type: