Utilities

The divi.utils module provides utility functions for quantum computing operations, including QUBO conversion, data serialization, and helper functions.

Core Utilities

reverse_dict_endianness(probs_dict)[source]

Reverse endianness of all bitstrings in a dictionary of probability distributions.

Return type:

dict[str, dict[str, float]]

convert_qubo_matrix_to_pennylane_ising(qubo_matrix)[source]

Convert a QUBO matrix to an Ising Hamiltonian in PennyLane format.

The conversion follows the mapping from QUBO variables x_i ∈ {0,1} to Ising variables σ_i ∈ {-1,1} via the transformation x_i = (1 - σ_i)/2. This transforms a QUBO minimization problem into an equivalent Ising minimization problem.

The function handles both dense NumPy arrays and sparse SciPy matrices efficiently. If the input matrix is neither symmetric nor upper triangular, it will be symmetrized automatically with a warning.

Parameters:

qubo_matrix (np.ndarray | sps.spmatrix) – The QUBO matrix Q where the objective is to minimize x^T Q x. Can be a dense NumPy array or a sparse SciPy matrix (any format). Should be square and either symmetric or upper triangular.

Returns:

A tuple containing:
  • Ising Hamiltonian as a PennyLane operator (sum of Pauli Z terms)

  • Constant offset term to be added to energy calculations

Return type:

tuple[qml.operation.Operator, float]

Raises:

UserWarning – If the QUBO matrix is neither symmetric nor upper triangular.

Example

>>> import numpy as np
>>> qubo = np.array([[1, 2], [0, 3]])
>>> hamiltonian, offset = convert_qubo_matrix_to_pennylane_ising(qubo)
>>> print(f"Offset: {offset}")