qplex.solvers package

Submodules

qplex.solvers.base_solver module

class qplex.solvers.base_solver.Solver[source]

Bases: ABC

Abstract base class for a quantum solver.

This class defines the necessary methods for interacting with a quantum backend provider, including solving problems, parsing inputs and responses, and selecting the appropriate backend.

abstract property backend: str

Abstract property for the backend.

Returns:

The name of the backend being used by the solver.

abstract parse_input(input_form) Any[source]

Parses the input model or problem formulation into a format suitable for solving.

Parameters:
  • input_form – The input model or problem formulation, which could

  • representation. (be a QModel instance or a string)

Returns:

An object or data structure that the solver can use to perform the optimization.

abstract parse_response(response: Any) Dict[source]

Parses the response received from the backend solver.

Parameters:
  • response – The raw response from the backend. The format of this

  • implementation. (response depends on the specific backend and solver)

Returns:

A dictionary containing the parsed response, which typically includes the solution and any relevant metadata or metrics.

abstract select_backend(qubits: int) Any[source]

Selects and configures the appropriate backend based on the number of qubits required.

Parameters:

qubits – The minimum number of qubits needed for the backend.

Returns:

The selected backend that meets the qubit requirement. This may involve initialization or configuration specific to the quantum provider.

abstract solve(formulation) Dict[source]

Solves the given problem formulation and returns the solution.

Parameters:

formulation – The problem formulation to be solved. This could be a model instance or a string representation of the problem.

Returns:

A dictionary containing the solution to the problem. The structure of this dictionary depends on the specific implementation and problem.

qplex.solvers.braket_solver module

class qplex.solvers.braket_solver.BraketSolver(shots: int, backend: str, device_parameters)[source]

Bases: Solver

A quantum solver for Braket that can execute quantum circuits on AWS Braket’s devices or local simulators.

shots

The number of shots for the quantum experiment.

Type:

int

_backend

The name of the backend to be used, which can be a Braket device or a local simulator.

Type:

str

property backend

Abstract property for the backend.

Returns:

The name of the backend being used by the solver.

parse_input(circuit: str) Program[source]

Converts a circuit string to an OpenQASMProgram, replacing ‘cx’ with ‘cnot’.

Parameters:

circuit (str) – The quantum circuit as an OpenQASM string.

Returns:

An OpenQASMProgram instance with the modified circuit.

Return type:

OpenQASMProgram

parse_response(response: Any) dict[source]

Parses the response from the backend to extract measurement counts.

Parameters:

response (Any) – The raw response from the backend.

Returns:

A dictionary with the measurement counts.

Return type:

dict

select_backend(qubits: int) Any[source]

Selects the appropriate backend based on the number of qubits and the specified backend name.

Parameters:

qubits (int) – The minimum number of qubits required.

Returns:

The selected backend, which could be an AWS device or a local simulator.

Return type:

Any

solve(model: str) dict[source]

Solves the given problem formulation using the specified backend.

Parameters:

model (str) – The quantum circuit as an OpenQASM string to be executed.

Returns:

A dictionary containing the measurement counts from the backend.

Return type:

dict

qplex.solvers.dwave_solver module

class qplex.solvers.dwave_solver.DWaveSolver(token, time_limit, num_reads, topology, embedding, backend)[source]

Bases: Solver

A solver for D-Wave quantum systems capable of handling various model types, including constrained quadratic models (CQM), discrete quadratic models (DQM), and binary quadratic models (BQM).

property backend

Abstract property for the backend.

Returns:

The name of the backend being used by the solver.

parse_constraint(constraint) QuadraticModel[source]

Convert a constraint into a D-Wave compatible QuadraticModel.

Parameters:

constraint (Any) – The constraint to be converted.

Returns:

The D-Wave model representing the constraint.

Return type:

QuadraticModel

parse_input(model) -> (typing.Any, <class 'str'>)[source]

Convert the input model into a D-Wave compatible model.

Parameters:

model (QModel) – The model to be parsed, which includes problem constraints and objectives.

Returns:

A tuple containing the parsed model and the model type as a string.

Return type:

tuple

parse_objective(model, parsed_model) Any[source]

Convert the objective function into the format required by D-Wave.

Parameters:
  • model (QModel) – The original model containing the objective function.

  • parsed_model – The D-Wave model to which the objective function will be added.

Returns:

The D-Wave model with the objective function set.

Return type:

Any

parse_response(response: Any) Dict[str, Any][source]

Parse the response from the D-Wave solver to extract solution.

Parameters:

response (Any) – The raw response from the D-Wave solver.

Returns:

A dictionary with ‘objective’ and ‘solution’ keys.

Return type:

dict

select_backend(parsed_model, model_type) Any[source]

Select the appropriate backend for the given model type. This method chooses the correct D-Wave sampler or hybrid solver based on the specified backend and the model type. It handles scenarios where the provided backend is incompatible with the model type by switching to a compatible hybrid solver.

Parameters:
  • parsed_model (Any) – The parsed model to be solved. This is used to determine backend compatibility.

  • model_type (str) – The type of the model, represented as a value from VAR_TYPE. It determines whether the model is a constrained quadratic model (CQM), discrete quadratic model (DQM), or binary quadratic model (BQM).

Returns:

An instance of the selected sampler, which could be a hybrid solver (e.g., LeapHybridCQMSampler, LeapHybridDQMSampler, or LeapHybridBQMSampler) or a DWaveSampler with an appropriate embedding composite.

Return type:

Any

Raises:

ValueError – If the specified backend (self._backend) is unsupported.

solve(model) Dict[str, Any][source]

Solve the given problem formulation using a D-Wave solver.

Parameters:

model (QModel) – The model to be solved, which includes quantum API tokens and the problem specification.

Returns:

A dictionary containing the solution with ‘objective’ and ‘solution’ keys.

Return type:

dict

qplex.solvers.ibmq_solver module

class qplex.solvers.ibmq_solver.IBMQSolver(token: str, shots: int, backend: str, optimization_level: int)[source]

Bases: Solver

Solver for IBMQ. Can execute circuits on IBM backend or local simulators.

shots

The number of shots for the quantum experiment.

Type:

int

_backend

The name of the backend to be used, which can be an IBMQ device or a local simulator.

Type:

str

service

The Qiskit runtime service instance for interacting with IBMQ’s backend.

Type:

QiskitRuntimeService

optimization_level

The desired optimization level for the Qiskit circuit.

Type:

int

property backend

Returns the currently selected backend name.

Returns:

The name of the backend.

Return type:

str

parse_input(circuit: str) QuantumCircuit[source]

Converts a circuit string to a Qiskit QuantumCircuit object.

Parameters:

circuit (str) – The quantum circuit as an OpenQASM string.

Returns:

The quantum circuit object.

Return type:

qiskit.QuantumCircuit

parse_response(response: dict) dict[source]

Parses the response from the backend to extract measurement counts.

Parameters:

response (dict) – The raw response from the backend.

Returns:

A dictionary with the measurement counts.

Return type:

dict

run(qc, sampler)[source]

Executes the given quantum circuit using the provided sampler.

Parameters:
  • qc (QuantumCircuit) – The quantum circuit to run.

  • sampler (Sampler) – The sampler instance to use for executing the circuit.

Returns:

A dictionary with the raw measurement counts.

Return type:

dict

select_backend(qubits: int) BackendV2[source]

Selects the appropriate backend based on the number of qubits and the specified backend name.

Parameters:

qubits (int) – The number of qubits in the quantum circuit.

Returns:

The selected backend, which could be an IBMQ device or a local simulator.

Return type:

Any

solve(model: str) dict[source]

Solves the given problem formulation using the specified backend.

Parameters:

model (str) – The quantum circuit as an OpenQASM string to be executed.

Returns:

A dictionary containing the measurement counts from the backend.

Return type:

dict

Module contents

This module provides the different solvers.