core.converters.converter#
qamomile/core/converter.py
This module defines the QuantumConverter abstract base class for converting between different problem representations and quantum models in Qamomile.
The QuantumConverter class provides a framework for encoding classical optimization problems into quantum representations (e.g., Ising models) and decoding quantum computation results back into classical problem solutions.
Key Features:
Conversion between classical optimization problems and Ising models.
Abstract methods for generating cost Hamiltonians and decoding results.
Integration with QuantumSDKTranspiler for SDK-specific result handling.
Usage:
Developers implementing specific quantum conversion strategies should subclass QuantumConverter and implement the abstract methods. The class is designed to work with jijmodeling for problem representation and various quantum SDKs through the QuantumSDKTranspiler interface.
Example
class QAOAConverter(QuantumConverter):
def get_cost_hamiltonian(self) -> qm_o.Hamiltonian:
# Implementation for generating QAOA cost Hamiltonian
...
Attributes#
Classes#
Enumeration for relaxation methods used in quantum problem conversion. |
|
Abstract base class for quantum problem converters in Qamomile. |
Module Contents#
- ResultType#
- class RelaxationMethod#
Bases:
enum.Enum
Enumeration for relaxation methods used in quantum problem conversion.
- AugmentedLagrangian#
Augmented Lagrangian method for PUBO conversion.
- SquaredPenalty#
Squared penalty method for PUBO conversion.
- AugmentedLagrangian = 'AugmentedLagrangian'#
- SquaredPenalty = 'SquaredPenalty'#
- class QuantumConverter(instance: ommx.v1.Instance, relax_method: RelaxationMethod = RelaxationMethod.SquaredPenalty, normalize_ising: Literal['abs_max', 'rms'] | None = None)#
Bases:
abc.ABC
Abstract base class for quantum problem converters in Qamomile.
This class provides methods for encoding classical optimization problems into quantum representations (e.g., Ising models) and decoding quantum computation results back into classical problem solutions.
- compiled_instance#
The compiled instance of the optimization problem.
- pubo_builder#
The PUBO (Polynomial Unconstrained Binary Optimization) builder.
- _ising#
Cached Ising model representation.
- Type:
Optional[IsingModel]
- get_ising()#
Retrieve or compute the Ising model representation.
- ising_encode()#
Encode the problem into an Ising model.
- get_cost_hamiltonian()#
Abstract method to get the cost Hamiltonian.
- decode()#
Decode quantum computation results into a SampleSet.
- decode_bits_to_sampleset()#
Abstract method to convert BitsSampleSet to SampleSet.
Initialize the QuantumConverter.
This method initializes the converter with the compiled instance of the optimization problem
- Parameters:
instance (ommx.v1.Instance) – an orginal instance to be converted.
relax_method (RelaxationMethod) – The relaxation method for PUBO conversion. Defaults to RelaxationMethod.SquaredPenalty.
normalize_ising (Literal["abs_max", "rms"] | None) – The normalization method for the Ising Hamiltonian. Available options: - “abs_max”: Normalize by absolute maximum value - “rms”: Normalize by root mean square Defaults to None.
- original_instance: ommx.v1.Instance#
- int2varlabel: dict[int, str]#
- normalize_ising = None#
- instance_to_qubo(multipliers: dict[str, float] | None = None, detail_parameters: dict[str, dict[tuple[int, Ellipsis], float]] | None = None) tuple[dict[tuple[int, int], float], float] #
Convert the instance to QUBO format.
This method converts the optimization problem instance into a QUBO (Quadratic Unconstrained Binary Optimization) representation, which is suitable for quantum computation.
- Args:
multipliers (Optional[dict[str, float]]): Multipliers for constraint terms. detail_parameters (Optional[dict[str, dict[tuple[int, …], float]]]):
Detailed parameters for the encoding process.
- Note:
$min_x f(x)$~s.t. $g_{s, i}(x) = 0~
- orall s, i$ is converted to
$min_x f(x) + sum_{s in { ext{‘const1’}, cdots}} A_s sum_i lambda_i g_i(x)$.
where $A_s$ is the multiplier for constraint $s$ and $lambda_i$ is the detailed parameter for constraint $s$ with subscripts $i$.
- Returns:
tuple[dict[int, float], float]: A tuple containing the QUBO dictionary and the constant term.
- Example:
- get_ising() qamomile.core.ising_qubo.IsingModel #
Get the Ising model representation of the problem.
- Returns:
The Ising model representation.
- Return type:
- ising_encode(multipliers: dict[str, float] | None = None, detail_parameters: dict[str, dict[tuple[int, Ellipsis], float]] | None = None) qamomile.core.ising_qubo.IsingModel #
Encode the problem to an Ising model.
This method converts the problem from QUBO (Quadratic Unconstrained Binary Optimization) to Ising model representation.
- Parameters:
multipliers (Optional[dict[str, float]]) – Multipliers for constraint terms.
detail_parameters (Optional[dict[str, dict[tuple[int, ...], float]]]) – Detailed parameters for the encoding process.
- Returns:
The encoded Ising model.
- Return type:
- abstract get_cost_hamiltonian() qamomile.core.operator.Hamiltonian #
Abstract method to get the cost Hamiltonian for the quantum problem.
This method should be implemented in subclasses to define how the cost Hamiltonian is constructed for specific quantum algorithms.
- Returns:
The cost Hamiltonian for the quantum problem.
- Return type:
qm_o.Hamiltonian
- Raises:
NotImplementedError – If the method is not implemented in the subclass.
- decode(transpiler: qamomile.core.transpiler.QuantumSDKTranspiler[ResultType], result: ResultType) ommx.v1.SampleSet #
Decode quantum computation results into a SampleSet.
This method uses the provided transpiler to convert SDK-specific results into a BitsSampleSet, then calls decode_bits_to_sampleset to produce the final SampleSet.
- Parameters:
transpiler (QuantumSDKTranspiler[ResultType]) – The transpiler for the specific quantum SDK.
result (ResultType) – The raw result from the quantum computation.
- Returns:
The decoded results as a SampleSet.
- Return type:
ommx.v1.SampleSet
- decode_bits_to_sampleset(bitssampleset: qamomile.core.bitssample.BitsSampleSet) ommx.v1.SampleSet #
Decode a BitArraySet to a SampleSet.
This method converts the quantum computation results (bitstrings) into a format that represents solutions to the original optimization problem.
- Parameters:
bitarray_set (qm_c.BitArraySet) – The set of bitstring results from quantum computation.
- Returns:
The decoded results as a SampleSet.
- Return type:
ommx.v1.SampleSet