qplex.commons package

Submodules

qplex.commons.algorithm_factory module

class qplex.commons.algorithm_factory.AlgorithmConfig(algorithm: AlgorithmType, penalty: float, seed: int, p: int | None = None, mixer: Any | None = None, layers: int | None = None, ansatz: Any | None = None)[source]

Bases: object

Configuration dataclass for gate-based quantum algorithms.

This class encapsulates the parameters needed to configure and instantiate quantum algorithms for combinatorial optimization problems.

algorithm

The type of quantum algorithm to use.

Type:

AlgorithmType

penalty

The penalty coefficient for constraint violations in the Hamiltonian.

Type:

float

seed

Random seed for reproducibility of results.

Type:

int

p

Number of layers in the QAOA circuit. Only used for QAOA and QAOAnsatz algorithms.

Type:

int | None

mixer

Custom mixer operator for the QAOA framework. Only used for QAOA and QAOAnsatz algorithms.

Type:

Any | None

layers

Number of layers in the VQE ansatz circuit. Only used for VQE algorithm.

Type:

int | None

ansatz

Custom ansatz circuit for VQE. If None, uses the default hardware-efficient ansatz.

Type:

Any | None

algorithm: AlgorithmType
ansatz: Any | None = None
layers: int | None = None
mixer: Any | None = None
p: int | None = None
penalty: float
seed: int
class qplex.commons.algorithm_factory.AlgorithmFactory[source]

Bases: object

Factory class for creating quantum algorithm instances.

This class implements the Factory pattern to instantiate appropriate quantum algorithms based on the provided configuration. It handles the creation of QAOA, QAOAnsatz, and VQE algorithm objects with their respective parameters.

The factory determines the appropriate mixer operator for optimization problems when using QAOAnsatz, and ensures all necessary parameters are properly configured for each algorithm type.

classmethod get_algorithm(model, config: AlgorithmConfig) Algorithm[source]

Creates and returns a quantum algorithm instance based on configuration.

Parameters:

model: The optimization model to be solved, typically a DOcplex model

instance containing the problem formulation.

config (AlgorithmConfig): Configuration object specifying the algorithm

type and its parameters.

Returns:

Algorithm: An instance of the specified quantum algorithm configured

according to the provided parameters.

Raises:

ValueError: If the specified algorithm type is not supported.

class qplex.commons.algorithm_factory.AlgorithmType(value)[source]

Bases: Enum

Supported gate-based quantum algorithms.

This enum defines the gate-based quantum algorithms that can be used to solve optimization problems in QPLEX. It currently supports QAOA (Quantum Approximate Optimization Algorithm), QAOAnsatz (Quantum Alternating Operator Ansatz), and VQE (Variational Quantum Eigensolver).

QAOA = 'qaoa'
QAO_ANSATZ = 'qao-ansatz'
VQE = 'vqe'

qplex.commons.optimization_callback module

class qplex.commons.optimization_callback.OptimizationCallback(user_callback: Callable[[ndarray], None] | None = None, verbose: bool = True)[source]

Bases: object

A class to track the optimization process and provide an optional customizable callback.

This class allows tracking the current iteration and parameter values during the optimization process using the callback mechanism of scipy.optimize.minimize. Additionally, users can provide a custom callback function to extend or modify the behavior during each iteration.

Attributes:

iterationint

Tracks the current iteration count of the optimization process.

user_callbackOptional[Callable[[np.ndarray], None]]

A user-defined callback function that accepts the current parameters xk and is executed at each iteration. This function allows for customized behavior during optimization.

Methods:

__call__(xk: np.ndarray):

The method called at each iteration during the optimization process. It prints the current iteration number and parameters, and invokes the user-defined callback if provided.

class qplex.commons.optimization_callback.OptimizationHistory(iterations: ~typing.List[int] = <factory>, parameters: ~typing.List[~numpy.ndarray] = <factory>, objectives: ~typing.List[float] = <factory>, timestamps: ~typing.List[~datetime.datetime] = <factory>, metadata: ~typing.Dict[str, ~typing.Any] = <factory>)[source]

Bases: object

Tracks optimization progress

iterations: List[int]
metadata: Dict[str, Any]
objectives: List[float]
parameters: List[ndarray]
timestamps: List[datetime]

qplex.commons.solver_factory module

class qplex.commons.solver_factory.BraketConfig(backend: str, shots: int | None, device_parameters: Dict[str, Any] = None)[source]

Bases: object

backend: str
device_parameters: Dict[str, Any] = None
shots: int | None
class qplex.commons.solver_factory.DWaveConfig(backend: str, time_limit: int | None = None, num_reads: int = 100, topology: str = 'pegasus', embedding: Any | None = None)[source]

Bases: object

backend: str
embedding: Any | None = None
num_reads: int = 100
time_limit: int | None = None
topology: str = 'pegasus'
class qplex.commons.solver_factory.IMBQConfig(backend: str, shots: int | None, optimization_level: int = 1)[source]

Bases: object

backend: str
optimization_level: int = 1
shots: int | None
class qplex.commons.solver_factory.ProviderConfig(backend: str, shots: int | None, provider_options: Dict[str, Any] | None = None)[source]

Bases: object

backend: str
provider_options: Dict[str, Any] | None = None
shots: int | None
class qplex.commons.solver_factory.ProviderType(value)[source]

Bases: Enum

An enumeration.

BRAKET = 'braket'
DWAVE = 'd-wave'
IBMQ = 'ibmq'
class qplex.commons.solver_factory.SolverFactory[source]

Bases: object

A factory class for creating quantum solvers based on the specified provider.

classmethod get_solver(provider: ProviderType, quantum_api_tokens: dict, config: ProviderConfig) Solver[source]

Return a solver based on the specified provider and available tokens.

Parameters:
  • provider (ProviderType) – The quantum provider (e.g., ‘d-wave’, ‘ibmq’, ‘braket’).

  • quantum_api_tokens (dict) – A dictionary containing API tokens for various quantum providers.

  • config (ProviderConfig) – A ProviderConfig instance containing the configuration for the provider.

Returns:

solver – An instance of the appropriate solver based on the specified provider.

Return type:

Solver

Raises:
  • RuntimeError – If the necessary credentials for the specified provider are missing.

  • ValueError – If the specified provider is not recognized.

Module contents

This module provides different utility classes.