quara.utils.matrix_util module

utility package about matrix.

allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)[source]
Return type

bool

calc_conjugate(x, v)[source]

calculates conjugate of matrices.

Parameters
  • x (np.ndarray) – parameter x.

  • v (np.ndarray) – parameter v.

Returns

\(x v x^T\)

Return type

np.ndarray

calc_covariance_mat(q, n)[source]

calculates covariance matrix of vector q.

Parameters
  • q (np.ndarray) – vector.

  • n (int) – number of data.

Returns

covariance matrix is \(\frac{1}{n} (diag(q) - q \cdot q^T)\)

Return type

np.ndarray

calc_covariance_mat_total(empi_dists)[source]

calculates covariance matrix of total empirical distributions.

Parameters

empi_dists (List[Tuple[int, np.ndarray]]) – list of empirical distributions. each empirical distribution is a tuple of (data_num, empirical distribution).

Returns

covariance matrix of total empirical distributions.

Return type

np.ndarray

calc_direct_sum(matrices)[source]

calculates direct sum of matrices.

Parameters

matrices (List[np.ndarray]) – matrices to calculate direct sum.

Returns

direct sum.

Return type

np.ndarray

Raises
  • ValueErrormatrices don’t consist of matrices(dim=2).

  • ValueErrormatrices don’t consist of square matrices.

calc_fisher_matrix(prob_dist, grad_prob_dist, eps=None)[source]

calculates Fisher matrix.

Parameters
  • prob_dist (np.ndarray) – probability distribution.

  • grad_prob_dist (List[np.ndarray]) – list of gradient of probability distribution. the length of list is the size of probability distribution. the size of np.ndarray is the number of variables.

  • eps (float, optional) – a parameter to avoid divergence about the inverse of probability, by default 1e-8

Returns

Fisher matrix.

Return type

np.ndarray

Raises
  • ValueError – some elements of prob_dist are not between 0 and 1.

  • ValueError – the sum of prob_dist is not 1.

  • ValueError – the size of prob_dist and grad_prob_dist are not equal.

  • ValueError – eps is not a positive number.

calc_fisher_matrix_total(prob_dists, grad_prob_dists, weights, eps=None)[source]

calculates total Fisher matrix.

Parameters
  • prob_dists (List[np.ndarray]) – list of probability distribution.

  • grad_prob_dists (List[List[np.ndarray]]) – list of list of gradient of probability distribution.

  • weights (List[float]) – list of weight.

  • eps (float, optional) – a parameter to avoid divergence about the inverse of probability, by default 1e-8

Returns

total Fisher matrix.

Return type

np.ndarray

Raises
  • ValueError – size of prob_dists, grad_prob_dists and weights are not equal

  • ValueError – some weights are not non-nagative number.

calc_left_inv(matrix)[source]

calculates left inverse matrix.

Parameters

matrix (np.ndarray) – matrix to calculate left inverse matrix.

Returns

left inverse matrix.

Return type

np.ndarray

Raises

ValueErrormatrix is not full rank.

calc_mat_from_vector_adjoint(vec)[source]
Parameters

vec (numpy.ndarray) –

Return type

numpy.ndarray

calc_mse_prob_dists(xs_list, ys_list)[source]

calculates MSE(Mean Squared Error) of ‘list of xs’ and ‘list of ys’.

MSE is a sum of each MSE. Assume xs_list = [xs0, xs1] and ys_list = [ys0, ys1], returns ‘MSE of xs0 and ys0’ + ‘MSE of xs1 and ys1’.

Parameters
  • xs_list (List[List[np.ndarray]]) – a list of list of ndarray.

  • ys_list (List[List[np.ndarray]]) – a list of list of ndarray.

Returns

MSE of xs_list and ys_list.

Return type

np.float64

calc_permutation_matrix(system_order, size_list)[source]

calculate permutation matrix.

permutation matrix can reorder the system order to [0, 1,…, n].

Parameters
  • system_order (List[int]) – system_order before permutation.

  • size_list (List[int]) – size of systems.

Returns

permutation matrix.

Return type

np.ndarray

calc_se(xs, ys)[source]

calculates Squared Error of xs and ys.

Parameters
  • xs (List[np.ndarray]) – a list of ndarray.

  • ys (List[np.ndarray]) – a list of ndarray.

Returns

Squared Error of xs and ys.

Return type

np.float64

convert_list_by_permutation_matrix(old_list, permutation_matrix)[source]

converts list by permutation_matrix.

this function executes “permutation_matrix @ old_list”-like operation. for example, if old_list = [a, b] and permutation_matrix = np.array([[0, 1], [1, 0]]), then this function returns [b, a].

Parameters
  • old_list (List) – a list before permutation.

  • permutation_matrix (np.ndarray) – permutation_matrix to permutate a list.

Returns

[description]

Return type

List

eig(matrix)[source]
Parameters

matrix (Union[scipy.sparse.csr.csr_matrix, numpy.ndarray]) –

Return type

Tuple[numpy.ndarray]

flatten(matrix)[source]
Return type

numpy.ndarray

is_hermitian(matrix, atol=None)[source]

returns whether the matrix is Hermitian.

Parameters
  • matrix (np.ndarray) – input matrix.

  • atol (float, optional) – the absolute tolerance parameter, uses get_atol() by default.

Returns

True where matrix is Hermitian, False otherwise.

Return type

bool

is_positive_semidefinite(matrix, atol=None)[source]

Returns whether the matrix is positive semidifinite.

Parameters
  • matrix (np.ndarray) – input matrix.

  • atol (float, optional) – the absolute tolerance parameter, uses get_atol() by default.

Returns

True where matrix is positive semidifinite, False otherwise.

Return type

bool

is_real(matrix, atol=None)[source]

returns whether the matrix is real.

Parameters
  • matrix (np.ndarray) – input matrix.

  • atol (float, optional) – the absolute tolerance parameter, uses get_atol() by default.

Returns

True where matrix is real, False otherwise.

Return type

bool

is_symmetric(matrix, atol=None)[source]

returns whether the matrix is symmetric.

Parameters
  • matrix (np.ndarray) – input matrix.

  • atol (float, optional) – the absolute tolerance parameter, uses get_atol() by default.

Returns

True where matrix is symmetric, False otherwise.

Return type

bool

is_tp(matrix, dim, atol=None)[source]

returns whether the matrix is TP. if \(\mathrm{Tr}_1[\text{matrix}] = I_2\), we think the matrix is TP. dim is a size of \(I_2\).

Parameters
  • matrix (np.ndarray) – input matrix.

  • dim (int) – dim of partial trace.

  • atol (float, optional) – the absolute tolerance parameter, uses get_atol() by default. returns True, if absolute(identity matrix - partial trace) <= atol. otherwise returns False.

Returns

True where matrix is TP, False otherwise.

Return type

bool

is_unitary(matrix, atol=None)[source]

returns whether the matrix is unitary.

Parameters
  • matrix (np.ndarray) – input matrix.

  • atol (float, optional) – the absolute tolerance parameter, uses get_atol() by default.

Returns

True where matrix is unitary, False otherwise.

Return type

bool

isclose(a, b, rtol=1e-05, atol=1e-08)[source]
Return type

bool

kron(a, b)[source]
partial_trace1(matrix, dim_Y)[source]

calculates partial trace \(\mathrm{Tr}_1[X \otimes Y] := \mathrm{Tr}[X]Y\).

Parameters
  • matrix (np.ndarray) – input matrix.

  • dim_Y (int) – dim of Y.

Returns

partial trace.

Return type

np.ndarray

replace_prob_dist(prob_dist, eps=None)[source]
Parameters
  • prob_dist (numpy.ndarray) –

  • eps (Optional[float]) –

Return type

numpy.ndarray

toarray(matrix)[source]
truncate_and_normalize(matrix, eps=None)[source]

truncates entries smaller than eps and normalizes to the matrix whose sum of each row is 1.

Parameters
  • matrix (np.ndarray) – the matrix to be truncated and normalized.

  • eps (float, optional) – threshold to truncate, uses get_atol() by default.

Returns

truncated and normalized matrix

Return type

np.array

truncate_computational_fluctuation(matrix, eps=None)[source]

truncates the computational fluctuation (real part) of the matrix entries.

Parameters
  • matrix (np.ndarray) – matrix to truncate the computational fluctuation.

  • eps (float, optional) – threshold to truncate, by default get_atol()

Returns

truncated matrix.

Return type

np.float64

truncate_hs(hs, eps_truncate_imaginary_part=None, is_zero_imaginary_part_required=True)[source]

truncate HS matrix to a real matrix.

Parameters
  • hs (np.ndarray) – HS matrix to truncate.

  • eps_truncate_imaginary_part (float, optional) – threshold to truncate imaginary part, by default get_atol()

  • is_zero_imaginary_part_required (bool, optional) – whether the imaginary part should be truncated to zero, by default True

Returns

truncated real matrix.

Return type

np.ndarray

Raises

ValueErroris_zero_imaginary_part_required == True and some imaginary parts of entries of matrix != 0.

truncate_imaginary_part(matrix, eps=None)[source]

truncates the imaginary part of the matrix entries.

Parameters
  • matrix (np.ndarray) – matrix to truncate the imaginary part.

  • eps (float, optional) – threshold to truncate, by default get_atol()

Returns

truncated matrix.

Return type

np.float64

vdot(a, b)[source]
Parameters
  • a (Union[scipy.sparse.csr.csr_matrix, numpy.ndarray]) –

  • b (Union[scipy.sparse.csr.csr_matrix, numpy.ndarray]) –

Return type

float

where_not_zero(matrix)[source]
Return type

Tuple[List[int], List[int]]