Skip to content

Commit

Permalink
fix sphinx build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
purva-thakre committed Aug 3, 2024
1 parent 435b53b commit 6c3e039
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 61 deletions.
3 changes: 2 additions & 1 deletion toqito/matrix_props/is_doubly_stochastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def is_doubly_stochastic(mat: np.ndarray) -> bool:
.. bibliography::
:filter: docname in docnames
:param rho: Matrix of interest
:param mat: Matrix of interest
:return: Returns :code:`True` if the matrix is right and left stochastic, :code:`False` otherwise.
"""
if is_stochastic(mat, "right") and is_stochastic(mat, "left"):
Expand Down
121 changes: 61 additions & 60 deletions toqito/matrix_props/is_stochastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,66 @@


def is_stochastic(mat: np.ndarray, mat_type: str) -> bool:
r"""Verify matrix is right or left stochastic.
When the nonnegative elements in a row of a square matrix sum up to 1, the matrix is right stochastic and if the
columns sum up to 1, the matrix is left stochastic. :cite:WikiStochasticMatrix.
See Also
========
is_doubly_stochastic
Examples
========
The elements of an identity matrix and a Pauli-X matrix are nonnegative and the rows sum up to 1. The same cannot be
said about a Pauli-Z or a Pauli-Y matrix.
>>> import numpy as np
>>> from toqito.matrix_props import is_stochastic
>>> is_stochastic(np.eye(5), "right")
True
>>> is_stochastic(np.eye(5), "left")
True
>>> from toqito.matrices import pauli
>>> from toqito.matrix_props import is_stochastic
>>> is_stochastic(pauli("X"), "left")
True
>>> is_stochastic(pauli("X"), "right")
True
>>> from toqito.matrices import pauli
>>> from toqito.matrix_props import is_stochastic
>>> is_stochastic(pauli("Z"), "right")
False
>>> is_stochastic(pauli("Z"), "left")
False
References
==========
.. bibliography::
:filter: docname in docnames
:param rho: Matrix of interest
:param mat_type: Type of stochastic matrix.
"left" for left stochastic matrix and "right" for right stochastic matrix.
"""
if mat_type == "left":
if is_square(mat) and is_nonnegative(mat) and np.all(np.sum(mat, axis=0) == 1.0):
return True

return False

elif mat_type == "right":
if is_square(mat) and is_nonnegative(mat) and np.all(np.sum(mat, axis=1) == 1.0):
return True

return False
else:
r"""Verify matrix is right or left stochastic.
When the nonnegative elements in a row of a square matrix sum up to 1, the matrix is right stochastic and if the
columns sum up to 1, the matrix is left stochastic. :cite:`WikiStochasticMatrix`.
See Also
========
is_doubly_stochastic
Examples
========
The elements of an identity matrix and a Pauli-X matrix are nonnegative and the rows sum up to 1. The same cannot be
said about a Pauli-Z or a Pauli-Y matrix.
>>> import numpy as np
>>> from toqito.matrix_props import is_stochastic
>>> is_stochastic(np.eye(5), "right")
True
>>> is_stochastic(np.eye(5), "left")
True
>>> from toqito.matrices import pauli
>>> from toqito.matrix_props import is_stochastic
>>> is_stochastic(pauli("X"), "left")
True
>>> is_stochastic(pauli("X"), "right")
True
>>> from toqito.matrices import pauli
>>> from toqito.matrix_props import is_stochastic
>>> is_stochastic(pauli("Z"), "right")
False
>>> is_stochastic(pauli("Z"), "left")
False
References
==========
.. bibliography::
:filter: docname in docnames
:param mat: Matrix of interest
:param mat_type: Type of stochastic matrix.
:code:`"left"` for left stochastic matrix and :code:`"right"` for right stochastic matrix.
:return: Returns :code:`True` if the matrix is right or left stochastic, :code:`False` otherwise.
:raises TypeError: If something other than :code:`"left"` or :code:`"right"` is used for :code:`mat_type`
"""
if mat_type == "left":
if is_square(mat) and is_nonnegative(mat) and np.all(np.sum(mat, axis=0) == 1.0):
return True
return False

elif mat_type == "right":
if is_square(mat) and is_nonnegative(mat) and np.all(np.sum(mat, axis=1) == 1.0):
return True

return False
else:
raise TypeError("Invalid stochastic matrix type provided.")

0 comments on commit 6c3e039

Please sign in to comment.