pygsti.tools.matrixmod2

General matrix utilities. Some, but not all, are specific to matrices over the ints modulo 2.

Module Contents

Functions

dot_mod2(m1, m2)

Returns the product over the integers modulo 2 of two matrices.

multidot_mod2(mlist)

Returns the product over the integers modulo 2 of a list of matrices.

det_mod2(m)

Returns the determinant of a matrix over the integers modulo 2 (GL(n,2)).

matrix_directsum(m1, m2)

Returns the direct sum of two square matrices of integers.

inv_mod2(m)

Finds the inverse of a matrix over GL(n,2)

Axb_mod2(A, b)

Solves Ax = b over GF(2)

gaussian_elimination_mod2(a)

Gaussian elimination mod2 of a.

diagonal_as_vec(m)

Returns a 1D array containing the diagonal of the input square 2D array m.

strictly_upper_triangle(m)

Returns a matrix containing the strictly upper triangle of m and zeros elsewhere.

diagonal_as_matrix(m)

Returns a diagonal matrix containing the diagonal of m.

albert_factor(d, failcount=0)

Returns a matrix M such that d = M M.T for symmetric d, where d and M are matrices over [0,1] mod 2.

random_bitstring(n, p, failcount=0)

Constructs a random bitstring of length n with parity p

random_invertable_matrix(n, failcount=0)

Finds a random invertable matrix M over GL(n,2)

random_symmetric_invertable_matrix(n)

Creates a random, symmetric, invertible matrix from GL(n,2)

onesify(a, failcount=0, maxfailcount=100)

Returns M such that M a M.T has ones along the main diagonal

permute_top(a, i)

Permutes the first row & col with the i'th row & col

fix_top(a)

Computes the permutation matrix P such that the [1:t,1:t] submatrix of P a P is invertible.

proper_permutation(a)

Computes the permutation matrix P such that all [n:t,n:t] submatrices of P a P are invertible.

_check_proper_permutation(a)

Check to see if the matrix has been properly permuted.

pygsti.tools.matrixmod2.dot_mod2(m1, m2)

Returns the product over the integers modulo 2 of two matrices.

Parameters
  • m1 (numpy.ndarray) – First matrix

  • m2 (numpy.ndarray) – Second matrix

Returns

numpy.ndarray

pygsti.tools.matrixmod2.multidot_mod2(mlist)

Returns the product over the integers modulo 2 of a list of matrices.

Parameters

mlist (list) – A list of matrices.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.det_mod2(m)

Returns the determinant of a matrix over the integers modulo 2 (GL(n,2)).

Parameters

m (numpy.ndarray) – Matrix to take determinant of.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.matrix_directsum(m1, m2)

Returns the direct sum of two square matrices of integers.

Parameters
  • m1 (numpy.ndarray) – First matrix

  • m2 (numpy.ndarray) – Second matrix

Returns

numpy.ndarray

pygsti.tools.matrixmod2.inv_mod2(m)

Finds the inverse of a matrix over GL(n,2)

Parameters

m (numpy.ndarray) – Matrix to take inverse of.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.Axb_mod2(A, b)

Solves Ax = b over GF(2)

Parameters
  • A (numpy.ndarray) – Matrix to operate on.

  • b (numpy.ndarray) – Vector to operate on.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.gaussian_elimination_mod2(a)

Gaussian elimination mod2 of a.

Parameters

a (numpy.ndarray) – Matrix to operate on.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.diagonal_as_vec(m)

Returns a 1D array containing the diagonal of the input square 2D array m.

Parameters

m (numpy.ndarray) – Matrix to operate on.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.strictly_upper_triangle(m)

Returns a matrix containing the strictly upper triangle of m and zeros elsewhere.

Parameters

m (numpy.ndarray) – Matrix to operate on.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.diagonal_as_matrix(m)

Returns a diagonal matrix containing the diagonal of m.

Parameters

m (numpy.ndarray) – Matrix to operate on.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.albert_factor(d, failcount=0)

Returns a matrix M such that d = M M.T for symmetric d, where d and M are matrices over [0,1] mod 2.

The algorithm mostly follows the proof in “Orthogonal Matrices Over Finite Fields” by Jessie MacWilliams in The American Mathematical Monthly, Vol. 76, No. 2 (Feb., 1969), pp. 152-164

There is generally not a unique albert factorization, and this algorthm is randomized. It will general return a different factorizations from multiple calls.

Parameters
  • d (array-like) – Symmetric matrix mod 2.

  • failcount (int, optional) – UNUSED.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.random_bitstring(n, p, failcount=0)

Constructs a random bitstring of length n with parity p

Parameters
  • n (int) – Number of bits.

  • p (int) – Parity.

  • failcount (int, optional) – Internal use only.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.random_invertable_matrix(n, failcount=0)

Finds a random invertable matrix M over GL(n,2)

Parameters
  • n (int) – matrix dimension

  • failcount (int, optional) – Internal use only.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.random_symmetric_invertable_matrix(n)

Creates a random, symmetric, invertible matrix from GL(n,2)

Parameters

n (int) – Matrix dimension.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.onesify(a, failcount=0, maxfailcount=100)

Returns M such that M a M.T has ones along the main diagonal

Parameters
  • a (numpy.ndarray) – The matrix.

  • failcount (int, optional) – Internal use only.

  • maxfailcount (int, optional) – Maximum number of tries before giving up.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.permute_top(a, i)

Permutes the first row & col with the i’th row & col

Parameters
  • a (numpy.ndarray) – The matrix to act on.

  • i (int) – index to permute with first row/col.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.fix_top(a)

Computes the permutation matrix P such that the [1:t,1:t] submatrix of P a P is invertible.

Parameters

a (numpy.ndarray) – A symmetric binary matrix with ones along the diagonal.

Returns

numpy.ndarray

pygsti.tools.matrixmod2.proper_permutation(a)

Computes the permutation matrix P such that all [n:t,n:t] submatrices of P a P are invertible.

Parameters

a (numpy.ndarray) – A symmetric binary matrix with ones along the diagonal.

Returns

numpy.ndarray

pygsti.tools.matrixmod2._check_proper_permutation(a)

Check to see if the matrix has been properly permuted.

This should be redundent to what is already built into ‘fix_top’.

Parameters

a (numpy.ndarray) – A matrix.

Returns

bool