Skip to content

Commit

Permalink
vincent's feedback: condense left, right functions
Browse files Browse the repository at this point in the history
  • Loading branch information
purva-thakre committed Aug 3, 2024
1 parent c85b6e0 commit 435b53b
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 231 deletions.
4 changes: 2 additions & 2 deletions docs/refs.bib
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ @misc{WikiDiagDom

}

@misc{WikiDoublyStichasticMatrix,
@misc{WikiDoublyStochasticMatrix,
author = "Wikipedia",
title = "Doubly Stochastic Matrix",
howpublished = {https://en.wikipedia.org/wiki/Doubly_stochastic_matrix}
Expand Down Expand Up @@ -1397,7 +1397,7 @@ @misc{WikiSymMat

}

@misc{WikiStichasticMatrix,
@misc{WikiStochasticMatrix,
author = "Wikipedia",
title = "Stochastic Matrix",
howpublished = {https://en.wikipedia.org/wiki/Stochastic_matrix}
Expand Down
4 changes: 2 additions & 2 deletions toqito/matrix_props/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
from toqito.matrix_props.is_doubly_nonnegative import is_doubly_nonnegative
from toqito.matrix_props.is_positive import is_positive
from toqito.matrix_props.positive_semidefinite_rank import positive_semidefinite_rank
from toqito.matrix_props.is_right_stochastic import is_right_stochastic
from toqito.matrix_props.is_left_stochastic import is_left_stochastic
from toqito.matrix_props.is_stochastic import is_stochastic
from toqito.matrix_props.is_doubly_stochastic import is_doubly_stochastic

42 changes: 9 additions & 33 deletions toqito/matrix_props/is_doubly_stochastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,38 @@

import numpy as np

from toqito.matrix_props import is_left_stochastic, is_right_stochastic
from toqito.matrix_props import is_stochastic


def is_doubly_stochastic(mat: np.ndarray) -> bool:
r"""Verify matrix is doubly stochastic.
A matrix is doubly stochastic if it is also a right and left stochastic matrix.
:cite:`WikiStichasticMatrix, WikiDoublyStichasticMatrix`.
:cite:`WikiStochasticMatrix, WikiDoublyStochasticMatrix`.
See Also
========
is_right_stochastic
is_left_stochastic
is_stochastic
Examples
========
The elements an identity matrix and a Pauli X matrix are nonnegative where both the rows and columns sum up to 1.
The same cannot be said about a Pauli Z matrix.
.. math::
Id = \begin{pmatrix}
1 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0\\
0 & 0 & 1 & 0 & 0\\
0 & 0 & 0 & 1 & 0\\
0 & 0 & 0 & 0 & 1\\
\end{pmatrix}
The elements of an identity matrix and a Pauli-X matrix are nonnegative where both the rows and columns sum up to 1.
The same cannot be said about a Pauli-Z matrix.
>>> import numpy as np
>>> from toqito.matrix_props import is_doubly_stochastic
>>> id_mat = np.eye(5)
>>> is_doubly_stochastic(id_mat)
>>> is_doubly_stochastic(np.eye(5))
True
.. math::
PauliX = \begin{pmatrix}
0 & 1 \\
1 & 0\\
\end{pmatrix}
>>> from toqito.matrices import pauli
>>> from toqito.matrix_props import is_doubly_stochastic
>>> x_mat = pauli("X")
>>> is_doubly_stochastic(x_mat)
>>> is_doubly_stochastic(pauli("X"))
True
.. math::
PauliY = \begin{pmatrix}
1 & 0 \\
0 & -1\\
\end{pmatrix}
>>> from toqito.matrices import pauli
>>> from toqito.matrix_props import is_doubly_stochastic
>>> z_mat = pauli("Z")
>>> is_doubly_stochastic(z_mat)
>>> is_doubly_stochastic(pauli("Z"))
False
Expand All @@ -69,7 +45,7 @@ def is_doubly_stochastic(mat: np.ndarray) -> bool:
:param rho: Matrix of interest
"""
if is_left_stochastic(mat) and is_right_stochastic(mat):
if is_stochastic(mat, "right") and is_stochastic(mat, "left"):
return True

return False
72 changes: 0 additions & 72 deletions toqito/matrix_props/is_left_stochastic.py

This file was deleted.

74 changes: 0 additions & 74 deletions toqito/matrix_props/is_right_stochastic.py

This file was deleted.

70 changes: 70 additions & 0 deletions toqito/matrix_props/is_stochastic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""Check is a matrix is stochastic."""

import numpy as np

from toqito.matrix_props import is_nonnegative, is_square


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:
raise TypeError("Invalid stochastic matrix type provided.")

4 changes: 1 addition & 3 deletions toqito/matrix_props/tests/test_is_doubly_stochastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import pytest

from toqito.matrices import cyclic_permutation_matrix, pauli
from toqito.matrix_props import is_doubly_stochastic, is_left_stochastic, is_right_stochastic
from toqito.matrix_props import is_doubly_stochastic


@pytest.mark.parametrize("test_input", [(np.identity(3)), (cyclic_permutation_matrix(4)), (pauli("X"))])
def test_true(test_input):
"""Check if function identifies right stochastic matrix correctly."""
assert is_left_stochastic(test_input)
assert is_right_stochastic(test_input)
assert is_doubly_stochastic(test_input)


Expand Down
23 changes: 0 additions & 23 deletions toqito/matrix_props/tests/test_is_left_stochastic.py

This file was deleted.

22 changes: 0 additions & 22 deletions toqito/matrix_props/tests/test_is_right_stochastic.py

This file was deleted.

Loading

0 comments on commit 435b53b

Please sign in to comment.