-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vincent's feedback: condense left, right functions
- Loading branch information
1 parent
c85b6e0
commit 435b53b
Showing
10 changed files
with
115 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.