qldpc.math

Miscellaneous mathematical and linear algebra methods.

Attributes

DenseIntegerArray

DenseIntegerArrayType

IntegerArray

SparseIntegerArray

Functions

block_matrix(→ numpy.typing.NDArray[numpy.int_])

Build a block matrix.

first_nonzero_cols(→ numpy.typing.NDArray[numpy.int_])

Get the first nonzero column for every row in a matrix.

get_dual_basis(→ galois.FieldArray)

Construct a dual basis, for which dual_basis @ basis.T = identity_matrix.

get_orthonormal_basis(→ galois.FieldArray | None)

An orthonormal basis for the row space of a matrix over a finite field.

log_choose(→ float)

Natural logarithm of (n choose k) = n! / ( k! * (n-k)! ).

op_to_string(→ stim.PauliString)

Convert an integer array that represents a Pauli string into a stim.PauliString.

string_to_op(→ numpy.typing.NDArray[numpy.int_])

Convert a stim.PauliString into an integer array, inverting qldpc.math.op_to_string.

symplectic_conjugate(→ DenseIntegerArrayType)

Take symplectic vectors to their duals.

symplectic_gram_schmidt(→ tuple[galois.FieldArray, ...)

Reduce vectors to symplectic hyperbolic pairs and a symplectic radical.

symplectic_weight(→ numpy.typing.NDArray[numpy.int_])

The symplectic weight of vectors.

Module Contents

qldpc.math.block_matrix(blocks: collections.abc.Sequence[collections.abc.Sequence[numpy.typing.NDArray[numpy.generic] | int | object]]) → numpy.typing.NDArray[numpy.int_][source]

Build a block matrix.

Literal 0 entries are replaced by zero matrices, and literal 1 entries are replaced by an identity matrix (padded below and to the right with zeros, if necessary).

qldpc.math.first_nonzero_cols(matrix: numpy.typing.NDArray[numpy.generic] | collections.abc.Sequence[numpy.typing.NDArray[numpy.generic]]) → numpy.typing.NDArray[numpy.int_][source]

Get the first nonzero column for every row in a matrix.

If all columns are zero in a particular row, the column value is set to the number of columns.

If the input matrix has more than two dimensions, return an output vector of length matrix.shape[0], where output[r] is the first “column” c for which np.any(matrix[r, c]) is nonzero; (or set output[r] = matrix.shape[1] if not np.any(matrix[r])).

qldpc.math.get_dual_basis(basis: galois.FieldArray, *, validate: bool = True) → galois.FieldArray[source]

Construct a dual basis, for which dual_basis @ basis.T = identity_matrix.

The rows of basis must be linearly independent, and it must have at least as many columns as rows; a dual basis exists only in that case.

Parameters:
  • basis – A full-row-rank matrix with at least as many columns as rows, whose rows form the basis to dualize.

  • validate – If True (default), check the precondition above and raise a ValueError when it fails. Pass False to skip the check when the precondition is already guaranteed.

qldpc.math.get_orthonormal_basis(matrix: galois.FieldArray, *, promise_full_rank: bool = False) → galois.FieldArray | None[source]

An orthonormal basis for the row space of a matrix over a finite field.

Given a matrix whose rows span a subspace V of GF(q)^n, return a matrix L whose rows are a basis for V with L @ L.T = identity – that is, a basis of V whose vectors each have self-overlap v @ v = 1 and are pairwise orthogonal under the dot product. If V has no such basis, return None.

The rows of the matrix may be linearly dependent; they are first reduced to a basis of V. Pass promise_full_rank=True to skip this reduction when the rows are already independent.

An orthonormal basis for V exists if and only if (a) V is nondegenerate, meaning no nonzero vector of V is orthogonal to all of V, and (b), according to the field’s characteristic:

  • over a field of characteristic 2, some vector of V has nonzero self-overlap v @ v;

  • over a field of odd characteristic, an even number of the vectors in any mutually orthogonal basis of V have a self-overlap with no square root in GF(q).

The construction is a variant of Gram-Schmidt orthogonalization; the characteristic-2 case follows Algorithm 1 and Lemma 2 of https://arxiv.org/abs/2503.19790.

qldpc.math.log_choose(n: int, k: int) → float[source]

Natural logarithm of (n choose k) = n! / ( k! * (n-k)! ).

qldpc.math.op_to_string(op: numpy.typing.NDArray[numpy.int_]) → stim.PauliString[source]

Convert an integer array that represents a Pauli string into a stim.PauliString.

The (first, second) half the array indicates the support of (X, Z) Paulis.

qldpc.math.string_to_op(string: stim.PauliString, num_qubits: int | None = None) → numpy.typing.NDArray[numpy.int_][source]

Convert a stim.PauliString into an integer array, inverting qldpc.math.op_to_string.

The (first, second) half the array indicates the support of (X, Z) Paulis.

qldpc.math.symplectic_conjugate(vectors: DenseIntegerArrayType) → DenseIntegerArrayType[source]

Take symplectic vectors to their duals.

The symplectic conjugate of a Pauli string swaps its X and Z support, and multiplies its Z sector by -1, taking Q = [Q_x|Q_z] -> [Q_z|-Q_x], such that the symplectic inner product between Pauli strings P and Q is ⟨P,Q⟩_s = P_x @ Q_z - P_z @ Q_x = P @ symplectic_conjugate(Q).

qldpc.math.symplectic_gram_schmidt(vectors: galois.FieldArray, *, promise_full_rank: bool = False) → tuple[galois.FieldArray, galois.FieldArray][source]

Reduce vectors to symplectic hyperbolic pairs and a symplectic radical.

The rows of vectors span a subspace V of GF(q)^(2n) equipped with the symplectic inner product ⟨a, b⟩_s = a @ symplectic_conjugate(b) (see symplectic_conjugate). Return a pair (hyperbolic, radical):

  • hyperbolic has shape (2m, 2n) and holds m mutually orthogonal hyperbolic pairs. Its rows are ordered [b_0, ..., b_{m-1}, c_0, ..., c_{m-1}], so that hyperbolic @ symplectic_conjugate(hyperbolic).T is the block matrix [[0, I], [-I, 0]]: ⟨b_i, c_j⟩_s = δ_ij and all other products vanish.

  • radical spans the symplectic radical of V – the vectors of V that are orthogonal to all of V. Its rows are isotropic and orthogonal to every row of hyperbolic and radical.

Together the rows of hyperbolic and radical form a basis for V. The rows of vectors may be linearly dependent; they are first reduced to a basis of V. Pass promise_full_rank=True to skip this reduction when the rows are already independent; passing it for dependent rows leaves the dependent directions in radical as spurious (possibly zero) rows, though the hyperbolic pairs stay correct.

Because the symplectic form is alternating, ⟨v, v⟩_s = 0 for every vector in every characteristic, so – unlike get_orthonormal_basis – there is no unit-vector case: the construction peels off one hyperbolic pair at a time and collects the leftover radical.

qldpc.math.symplectic_weight(vectors: numpy.typing.NDArray[numpy.int_]) → numpy.typing.NDArray[numpy.int_][source]

The symplectic weight of vectors.

The symplectic weight of a Pauli string is the number of qudits that it addresses nontrivially. Returns one weight per input vector (a 0-dimensional array for a single vector).

qldpc.math.DenseIntegerArray
qldpc.math.DenseIntegerArrayType
qldpc.math.IntegerArray
qldpc.math.SparseIntegerArray