Utilities

Utility functions for quantum computing operations have been organized into specialized modules based on their functionality.

Result Processing Utilities

The divi.backends._results_processing module provides functions for processing quantum measurement results.

Public functions are available directly from the divi.backends module:

convert_counts_to_probs(counts, shots)[source]

Convert raw counts to probability distributions.

Parameters:
  • counts (dict[str, dict[str, int]]) – The counts to convert to probabilities.

  • shots (int) – The number of shots.

Returns:

The probability distributions.

Return type:

dict[str, dict[str, float]]

reverse_dict_endianness(probs_dict)[source]

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

Return type:

dict[str, dict[str, float]]

Hamiltonian Utilities

The divi.qprog._hamiltonians module provides functions for working with Hamiltonians, including QUBO conversion and Hamiltonian manipulation.

convert_hamiltonian_to_pauli_string(hamiltonian, n_qubits)[source]

Convert a PennyLane Operator to a semicolon-separated string of Pauli operators.

Each term in the Hamiltonian is represented as a string of Pauli letters (‘I’, ‘X’, ‘Y’, ‘Z’), one per qubit. Multiple terms are separated by semicolons.

Parameters:
  • hamiltonian (qml.operation.Operator) – The PennyLane Operator (e.g., Hamiltonian, PauliZ) to convert.

  • n_qubits (int) – Number of qubits to represent in the string.

Returns:

The Hamiltonian as a semicolon-separated string of Pauli operators.

Return type:

str

Raises:

ValueError – If an unknown Pauli operator is encountered or wire index is out of range.

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 (npt.NDArray[np.float64] | 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}")

Expectation Value Computation

The divi.qprog._expectation module provides internal functions for efficiently computing expectation values from quantum measurement results. These functions are primarily used internally by variational quantum algorithms.

Note

The functions in this module are internal implementation details and are not part of the public API. They are documented here for completeness but should not be used directly by end users.