qplex.utils package

Submodules

qplex.utils.circuit_utils module

qplex.utils.circuit_utils.replace_params(circuit: str, params: ndarray) str[source]

Replaces parameter placeholders in a quantum circuit string with actual values.

This function takes a quantum circuit in the form of a string and replaces placeholders of the form ‘thetaX’ (where X is an integer index) with the corresponding values from a provided array of parameters. This is commonly used in parameterized quantum circuits for variational algorithms like VQE and QAOA, where gates depend on tunable parameters.

circuitstr

The quantum circuit as a string in which parameter placeholders of the form ‘thetaX’ (e.g., theta0, theta1) need to be replaced by actual numerical values.

paramsnp.ndarray

A 1-dimensional numpy array containing the numerical parameter values. Each index in the array corresponds to a specific placeholder in the circuit string. For example, params[0] replaces ‘theta0’, params[1] replaces ‘theta1’, and so on.

str

The quantum circuit string with all placeholders replaced by the corresponding values from the params array.

IndexError

If the circuit string contains a ‘thetaX’ placeholder where X is greater than or equal to the length of the params array, this will raise an IndexError because no corresponding parameter is available.

Given a circuit:

circuit = “ry(theta0) q[0];

rz(theta1) q[1]; “

And the parameters:

params = np.array([0.5, 1.2])

The result will be:

“ry(0.5) q[0];

rz(1.2) q[1]; “

qplex.utils.model_utils module

class qplex.utils.model_utils.ConstraintInfo(type: ConstraintType, parameters: Dict | None = None, additional_constraints: List[ConstraintType] | None = None)[source]

Bases: object

Stores information about detected constraints

additional_constraints: List[ConstraintType] | None = None
parameters: Dict | None = None
type: ConstraintType
qplex.utils.model_utils.get_model_constraint_info(model: Model) ConstraintInfo[source]

Analyzes a DOcplex model to determine constraint types.

Parameters:

model (Model) – The DOcplex model to analyze

Returns:

Information about detected constraints

Return type:

ConstraintInfo

qplex.utils.workflow_utils module

qplex.utils.workflow_utils.calculate_energy(counts: dict, shots: int, algorithm_instance)[source]

Calculates the energy (or cost function value) of a quantum solution.

This function computes the average energy of the quantum measurement results based on the provided counts (bitstrings and their frequencies). The energy is computed by evaluating the objective function for each sampled bitstring and taking the weighted average based on the counts.

Parameters:
  • counts (dict) – A dictionary of bitstrings (represented as strings of 0s and 1s) and their corresponding frequencies (or counts) from the quantum measurement results.

  • shots (int) – The total number of measurement shots, used to normalize the energy.

  • algorithm_instance (Algorithm) – The instance of the quantum algorithm (e.g., QAOA or VQE) being used. This instance contains the QUBO model, which is used to evaluate the objective function of the bitstrings.

Returns:

The average energy (or cost function value) of the quantum solution, normalized by the total number of shots.

Return type:

float

qplex.utils.workflow_utils.get_solution_from_counts(model: Model, optimal_counts: dict)[source]

Extracts the best solution from the optimal parameter counts obtained from a quantum algorithm’s execution.

This function takes the model and the counts of possible solutions (bitstrings) from a quantum run and extracts the best solution by selecting the most frequent bitstring and computing its objective value.

Parameters:
  • model (Model) – The optimization model that was solved, typically represented as a QUBO (Quadratic Unconstrained Binary Optimization) problem.

  • optimal_counts (dict) – A dictionary of bitstrings (represented as strings of 0s and 1s) and their corresponding frequencies or counts from the quantum measurement results.

Returns:

A dictionary containing the best solution with the following keys: - ‘solution’: A dictionary of variable names mapped to their binary

values (0 or 1) representing the optimal solution.

  • ’objective’: The computed objective value of the best solution.

Return type:

dict

Module contents