Dense Hermitian matrix wrapper with Pauli decomposition via FWHT.
Overview¶
| Function | Description |
|---|---|
fwht_pauli_coefficients | Compute real Pauli coefficients for a Hermitian matrix via FWHT. |
is_close_zero | Check if a given floating-point value is close to zero within a small tolerance. |
is_power_of_two | Check whether n is a positive power of two. |
| Class | Description |
|---|---|
HermitianMatrix | Dense Hermitian operator on n qubits (a 2**n x 2**n matrix). |
Functions¶
fwht_pauli_coefficients [source]¶
def fwht_pauli_coefficients(matrix: np.ndarray, *, imag_tol: float = 1e-10) -> tuple[np.ndarray, int]Compute real Pauli coefficients for a Hermitian matrix via FWHT.
Parameters:
| Name | Type | Description |
|---|---|---|
matrix | np.ndarray | Dense finite (N, N) Hermitian array with N = 2**n. |
imag_tol | float | Maximum tolerated absolute imaginary residue in any coefficient. Defaults to 1e-10. |
Returns:
tuple[np.ndarray, int] — tuple[np.ndarray, int]: Pair (coeffs, num_qubits) where coeffs
is an (N, N) float64 array indexed by
(x_mask, z_mask).
Raises:
ValueError— If the matrix fails the generic complex decomposition validation or produces an imaginary coefficient larger thanimag_tol.
is_close_zero [source]¶
def is_close_zero(value: float, abs_tol: float = 1e-15) -> boolCheck if a given floating-point value is close to zero within a small tolerance.
Parameters:
| Name | Type | Description |
|---|---|---|
value | float | The floating-point value to check. |
abs_tol | float | Absolute tolerance passed to :func:math.isclose. Defaults to 1e-15. |
Returns:
bool — True if the value is close to zero, False otherwise.
is_power_of_two [source]¶
def is_power_of_two(n: int) -> boolCheck whether n is a positive power of two.
Parameters:
| Name | Type | Description |
|---|---|---|
n | int | Integer to test. |
Returns:
bool — True if n is a power of two, False otherwise.
Classes¶
HermitianMatrix [source]¶
class HermitianMatrixDense Hermitian operator on n qubits (a 2**n x 2**n matrix).
Wraps a NumPy array and validates on construction that it is 2D square,
has a power-of-two dimension, and is Hermitian up to atol. The
Hermitian check can be skipped via validate=False for internal use
(for example, when the caller already knows a result is Hermitian by
construction).
The main entry point for downstream quantum code is
:meth:to_hamiltonian, which returns the exact Pauli decomposition as
a :class:qamomile.observable.Hamiltonian via the Fast Walsh-Hadamard
Transform in O(n * 4**n) time.
Example:
>>> import numpy as np
>>> from qamomile.linalg import HermitianMatrix
>>> X = np.array([[0, 1], [1, 0]], dtype=complex)
>>> Z = np.array([[1, 0], [0, -1]], dtype=complex)
>>> H = HermitianMatrix(np.kron(X, Z) + 0.5 * np.kron(Z, np.eye(2)))
>>> H.num_qubits
2
>>> ham = H.to_hamiltonian()Constructor¶
def __init__(
self,
matrix: np.ndarray,
*,
validate: bool = True,
atol: float = 1e-10,
) -> NoneAttributes¶
matrix: np.ndarray Return a read-only view of the underlying array.num_qubits: int Number of qubits (log2of the matrix dimension).shape: tuple[int, int] Shape of the underlying matrix as(2**n, 2**n).
Methods¶
to_hamiltonian¶
def to_hamiltonian(self, *, tol: float = 1e-12) -> HamiltonianReturn the exact Pauli decomposition as a Hamiltonian.
Uses FWHT internally. Pauli terms with |coeff| <= tol are
dropped. The all-identity component is accumulated into
:attr:qamomile.observable.Hamiltonian.constant. Qubit 0
corresponds to the least-significant bit of the matrix’s
computational-basis indices.
Parameters:
| Name | Type | Description |
|---|---|---|
tol | float | Absolute tolerance below which a Pauli coefficient is treated as zero and omitted from the result. |
Returns:
Hamiltonian — class:~qamomile.observable.Hamiltonian containing the
Hamiltonian — Pauli decomposition.