Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pydocstyle Errors #200

Merged
merged 6 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion toqito/channel_metrics/completely_bounded_trace_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

def completely_bounded_trace_norm(phi: np.ndarray) -> float:
r"""
Compute the completely bounded trace norm / diamond norm of a quantum
Find the completely bounded trace norm of a quantum channel.

Also known as the completely bounded diamond norm of a quantum
channel [WatCNorm18]_. The algorithm in p.11 of [WatSDP09] with
implementation in QETLAB [JohQET] is used.

Expand Down
3 changes: 1 addition & 2 deletions toqito/matrix_ops/inner_product.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Inner product operation"""
"""Inner product operation."""
import numpy as np


Expand Down Expand Up @@ -36,7 +36,6 @@ def inner_product(v1: np.ndarray, v2: np.ndarray) -> float:
:param args: v1 and v2, both vectors of dimenstions :math:`(n,1)` where :math:`n>1`.
:return: The computed inner product.
"""

# Check for dimensional validity
if not (v1.shape[0] == v2.shape[0] and v1.shape[0] > 1 and len(v1.shape) == 1):
raise ValueError("Dimension mismatch")
Expand Down
10 changes: 5 additions & 5 deletions toqito/matrix_ops/outer_product.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""Outer product operation"""
"""Outer product operation."""
import numpy as np


def outer_product(v1: np.ndarray, v2: np.ndarray) -> np.ndarray:
r"""
Compute the outer product :math:`|v_1\rangle\langle v_2|` of two vectors.
[WikOuter]_

The outer product is calculated as follows:
The outer product is calculated as follows [WikOuter]_:

.. math::
\left|\begin{pmatrix}a_1\\\vdots\\a_n\end{pmatrix}\right\rangle\left\langle\begin{pmatrix}b_1\\\vdots\\b_n\end{pmatrix}\right|=\begin{pmatrix}a_1\\\vdots\\a_n\end{pmatrix}\begin{pmatrix}b_1&\cdots&b_n\end{pmatrix}=\begin{pmatrix}a_1b_1&\cdots&a_1b_n\\\vdots&\ddots&\vdots\\a_1b_n&\cdots&a_nb_n\end{pmatrix}
Expand All @@ -34,10 +33,11 @@ def outer_product(v1: np.ndarray, v2: np.ndarray) -> np.ndarray:
==========
.. [WikOuter] Wikipedia: Outer Product
https://en.wikipedia.org/wiki/Outer_product

:raises ValueError: Vector dimensions are mismatched.
:param args: v1 and v2, both vectors of dimensions :math:`(n,1)` where :math:`n>1`.
:return: The computed outer product."""
:return: The computed outer product.
"""
# Check for dimensional validity
if not (v1.shape[0] == v2.shape[0] and v1.shape[0] > 1 and len(v1.shape) == 1):
raise ValueError("Dimension mismatch")
Expand Down
56 changes: 28 additions & 28 deletions toqito/matrix_props/is_diagonally_dominant.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

def is_diagonally_dominant(mat: np.ndarray, is_strict: bool = True) -> bool:
r"""
Check if matrix is diagnal dominant (DD) [WikDD]_.
Check if matrix is diagnal dominant (DD) [WikDD]_.

A matrix is diagonally dominant if the matrix is square
and if for every row of the matrix, the magnitude of the diagonal entry in a row is greater
than or equal to the sum of the magnitudes of all the other (non-diagonal) entries in that row.
A matrix is diagonally dominant if the matrix is square
and if for every row of the matrix, the magnitude of the diagonal entry in a row is greater
than or equal to the sum of the magnitudes of all the other (non-diagonal) entries in that row.

Examples
==========
Examples
==========

The following is an example of a 3-by-3 diagonal matrix:
The following is an example of a 3-by-3 diagonal matrix:

.. math::
A = \begin{pmatrix}
Expand All @@ -23,39 +23,39 @@ def is_diagonally_dominant(mat: np.ndarray, is_strict: bool = True) -> bool:
0 & -1 & 2
\end{pmatrix}

our function indicates that this is indeed a diagonally dominant matrix.
our function indicates that this is indeed a diagonally dominant matrix.

>>> from toqito.matrix_props import is_diagonally_dominant
>>> import numpy as np
>>> A = np.array([[2, -1, 0], [0, 2, -1], [0, -1, 2]])
>>> is_diagonally_dominant(A)
True
>>> from toqito.matrix_props import is_diagonally_dominant
>>> import numpy as np
>>> A = np.array([[2, -1, 0], [0, 2, -1], [0, -1, 2]])
>>> is_diagonally_dominant(A)
True

Alternatively, the following example matrix :math:`B` defined as
Alternatively, the following example matrix :math:`B` defined as

.. math::
B = \begin{pmatrix}
-1 & 2 \\
-1 & -1
\end{pmatrix}

is not diagonally dominant.
is not diagonally dominant.

>>> from toqito.matrix_props import is_diagonally_dominant
>>> import numpy as np
>>> B = np.array([[-1, 2], [-1, -1]])
>>> is_diagonally_dominant(B)
False
>>> from toqito.matrix_props import is_diagonally_dominant
>>> import numpy as np
>>> B = np.array([[-1, 2], [-1, -1]])
>>> is_diagonally_dominant(B)
False

References
==========
.. [WikDD] Wikipedia: Diagonally dominant matrix.
https://en.wikipedia.org/wiki/Diagonally_dominant_matrix
References
==========
.. [WikDD] Wikipedia: Diagonally dominant matrix.
https://en.wikipedia.org/wiki/Diagonally_dominant_matrix

:param mat: Matrix to check.
:param is_strict: Whether the inequality is strict.
:return: Return :code:`True` if matrix is diagnally dominant, and :code:`False` otherwise.
"""
:param mat: Matrix to check.
:param is_strict: Whether the inequality is strict.
:return: Return :code:`True` if matrix is diagnally dominant, and :code:`False` otherwise.
"""
if not is_square(mat):
return False

Expand Down
5 changes: 3 additions & 2 deletions toqito/matrix_props/trace_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

def trace_norm(rho: np.ndarray) -> float:
r"""
Compute the trace norm of the state [WikTn]_
as well as the operator 1-norm when inputting an operator.
Compute the trace norm of the state [WikTn]_.

Also computes the operator 1-norm when inputting an operator.

The trace norm :math:`||\rho||_1` of a density matrix :math:`\rho` is the sum of the singular
values of :math:`\rho`. The singular values are the roots of the eigenvalues of
Expand Down
70 changes: 34 additions & 36 deletions toqito/nonlocal_games/xor_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def __init__(

q_0, q_1 = self.prob_mat.shape
if tol is None:
self.tol = np.finfo(float).eps * q_0 ** 2 * q_1 ** 2
self.tol = np.finfo(float).eps * q_0**2 * q_1**2
else:
self.tol = tol

Expand All @@ -175,45 +175,44 @@ def __init__(

def quantum_value(self) -> float:
r"""
Compute the quantum value of the XOR game.

To obtain the quantum value of the XOR game, we calculate the following
simplified dual problem of the semidefinite program from the set of
notes: https://cs.uwaterloo.ca/~watrous/CS867.Winter2017/Notes/06.pdf

.. math::
\begin{equation}
\begin{aligned}
\text{minimize:} \quad & \frac{1}{2} \sum_{x \in X} u(x) +
\frac{1}{2} \sum_{y \in Y} v(y) \\
\text{subject to:} \quad &
\begin{pmatrix}
\text{Diag}(u) & -D \\
-D^* & \text{Diag}(v)
\end{pmatrix} \geq 0, \\
& u \in \mathbb{R}^X, \
v \in \mathbb{R}^Y.
\end{aligned}
\end{equation}

where :math:`D` is the matrix defined to be

.. math::
D(x,y) = \pi(x, y) (-1)^{f(x,y)}

In other words, :math:`\pi(x, y)` corresponds to :code:`prob_mat[x, y]`,
and :math:`f(x,y)` corresponds to :code:`pred_mat[x, y]`.

:return: A value between [0, 1] representing the quantum value.
"""
Compute the quantum value of the XOR game.

To obtain the quantum value of the XOR game, we calculate the following
simplified dual problem of the semidefinite program from the set of
notes: https://cs.uwaterloo.ca/~watrous/CS867.Winter2017/Notes/06.pdf

.. math::
\begin{equation}
\begin{aligned}
\text{minimize:} \quad & \frac{1}{2} \sum_{x \in X} u(x) +
\frac{1}{2} \sum_{y \in Y} v(y) \\
\text{subject to:} \quad &
\begin{pmatrix}
\text{Diag}(u) & -D \\
-D^* & \text{Diag}(v)
\end{pmatrix} \geq 0, \\
& u \in \mathbb{R}^X, \
v \in \mathbb{R}^Y.
\end{aligned}
\end{equation}

where :math:`D` is the matrix defined to be

.. math::
D(x,y) = \pi(x, y) (-1)^{f(x,y)}

In other words, :math:`\pi(x, y)` corresponds to :code:`prob_mat[x, y]`,
and :math:`f(x,y)` corresponds to :code:`pred_mat[x, y]`.

:return: A value between [0, 1] representing the quantum value.
"""
alice_in, bob_in = self.prob_mat.shape
d_mat = np.zeros([alice_in, bob_in])

for x_alice in range(alice_in):
for y_bob in range(bob_in):
d_mat[x_alice, y_bob] = self.prob_mat[x_alice, y_bob] * (-1) ** (
self.pred_mat[x_alice, y_bob]
)
d_mat[x_alice, y_bob] = self.prob_mat[x_alice, y_bob] * \
(-1) ** (self.pred_mat[x_alice, y_bob])

u_vec = cvxpy.Variable(alice_in, complex=False)
v_vec = cvxpy.Variable(bob_in, complex=False)
Expand Down Expand Up @@ -247,7 +246,6 @@ def classical_value(self) -> float:

:return: A value between [0, 1] representing the classical value.
"""

return self.to_nonlocal_game().classical_value()

def nonsignaling_value(self) -> float:
Expand Down
4 changes: 2 additions & 2 deletions toqito/state_opt/state_exclusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def state_exclusion(


def _min_error_primal(vectors: list[np.ndarray], probs: list[float] = None, solver: str = "cvxopt"):
"""The primal problem for minimum-error quantum state exclusion SDP."""
"""Find the primal problem for minimum-error quantum state exclusion SDP."""
n, dim = len(vectors), vectors[0].shape[0]
if probs is None:
probs = [1 / len(vectors)] * len(vectors)
Expand All @@ -125,7 +125,7 @@ def _min_error_primal(vectors: list[np.ndarray], probs: list[float] = None, solv
def _min_error_dual(
vectors: list[np.ndarray], probs: list[float] = None, solver: str = "cvxopt"
) -> float:
"""The dual problem for minimum-error quantum state exclusion SDP."""
"""Find the dual problem for minimum-error quantum state exclusion SDP."""
dim = vectors[0].shape[0]
if probs is None:
probs = [1 / len(vectors)] * len(vectors)
Expand Down