Skip to content
Open
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
6 changes: 3 additions & 3 deletions toqito/state_ops/schmidt_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def schmidt_decomposition(
r"""Compute the Schmidt decomposition of a bipartite vector [@wikipediaschmidt].

Args:
rho: A bipartite quantum state to compute the Schmidt decomposition of.
rho: A bipartite state vector (flat, row, or column) or an operator.
dim: An array consisting of the dimensions of the subsystems (default gives subsystems
equal dimensions).
k_param: How many terms of the Schmidt decomposition should be computed (default is 0).
Expand Down Expand Up @@ -63,13 +63,13 @@ def schmidt_decomposition(
return _operator_schmidt_decomposition(rho, dim, k_param)

if dim is None:
dim = np.round(np.sqrt(len(rho)))
dim = np.round(np.sqrt(rho.size))
if isinstance(dim, list):
dim = np.array(dim)

# Allow the user to enter a single number for `dim`.
if isinstance(dim, (int, float, np.integer, np.floating)):
dim = np.array([dim, len(rho) / dim])
dim = np.array([dim, rho.size / dim])
dim[1] = np.round(dim[1])

# Otherwise, use lots of Schmidt coefficients.
Expand Down
10 changes: 10 additions & 0 deletions toqito/state_ops/tests/test_schmidt_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,13 @@ def test_operator_schmidt_decomposition_scalar_dim(dim):
calculated = schmidt_decomposition(rho, dim)
for calc, exp in zip(calculated, expected):
np.testing.assert_allclose(calc, exp)


@pytest.mark.parametrize("dim", [None, 2, [2, 2]])
def test_schmidt_decomposition_row_vector(dim):
"""A row vector has the same Schmidt decomposition as a column vector."""
vec = np.array([[1, 0, 0, 1j]]) / np.sqrt(2)
singular_vals, left, right = schmidt_decomposition(vec, dim)
reconstructed = sum(singular_vals[i, 0] * np.kron(left[:, i], right[:, i]) for i in range(len(singular_vals)))
np.testing.assert_allclose(singular_vals.ravel(), [1 / np.sqrt(2)] * 2)
np.testing.assert_allclose(reconstructed, vec.ravel())
6 changes: 3 additions & 3 deletions toqito/state_props/schmidt_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def schmidt_rank(rho: np.ndarray, dim: int | list[int] | np.ndarray | None = Non
determined as the number of Schmidt coefficients larger than `tol`.

Args:
rho: A bipartite vector or matrix to have its Schmidt rank computed.
rho: A bipartite state vector (flat, row, or column) or an operator.
dim: A 1-by-2 vector or matrix.

Returns:
Expand Down Expand Up @@ -88,12 +88,12 @@ def schmidt_rank(rho: np.ndarray, dim: int | list[int] | np.ndarray | None = Non
return _operator_schmidt_rank(rho, dim)

# Otherwise, compute the Schmidt rank for the vector.
slv = int(np.round(np.sqrt(len(rho))))
slv = int(np.round(np.sqrt(rho.size)))

if dim is None:
dim = slv
if isinstance(dim, int):
dim = np.array([dim, len(rho) / dim], dtype=int)
dim = np.array([dim, rho.size / dim], dtype=int)
dim[1] = np.round(dim[1])

return np.linalg.matrix_rank(np.reshape(rho, dim))
Expand Down
7 changes: 7 additions & 0 deletions toqito/state_props/tests/test_schmidt_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,10 @@ def test_operator_schmidt_rank_unequal_dimensions(vec, dims, expected_result):
"""Operator Schmidt rank is correct when the two subsystems have different dimensions."""
rho = vec @ vec.conj().T
assert schmidt_rank(rho, dims) == expected_result


@pytest.mark.parametrize("dim", [None, 2, [2, 2]])
def test_schmidt_rank_row_vector(dim):
"""The shape of a state vector does not change its Schmidt rank."""
vec = np.array([[1, 0, 0, 1j]]) / np.sqrt(2)
assert schmidt_rank(vec, dim) == 2
Loading