Skip to content
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
13 changes: 6 additions & 7 deletions toqito/matrix_ops/tensor_comb.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@ def tensor_comb(
raise ValueError("k must be less than or equal to the number of states for injective sequences.")

# Generate sequences based on the selected mode.
match mode:
case "injective":
sequences = list(itertools.permutations(range(len(states)), k))
case "non-injective":
sequences = list(itertools.product(range(len(states)), repeat=k))
case "diagonal":
sequences = [(i,) * k for i in range(len(states))]
if mode == "injective":
sequences = list(itertools.permutations(range(len(states)), k))
elif mode == "non-injective":
sequences = list(itertools.product(range(len(states)), repeat=k))
else: # mode == "diagonal"
sequences = [(i,) * k for i in range(len(states))]

sequences_of_states = {}
for seq in sequences:
Expand Down
36 changes: 36 additions & 0 deletions toqito/matrix_ops/tests/test_tensor_comb.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@
[(0, 0, 0), (1, 1, 1)],
{(0, 0, 0): np.array([1, 0, 0, 0, 0, 0, 0, 0]), (1, 1, 1): np.array([0, 0, 0, 0, 0, 0, 0, 1])},
),
# diagonal mode with k = 2.
(
[np.array([1, 0]), np.array([0, 1])],
2,
"diagonal",
[(0, 0), (1, 1)],
{
(0, 0): np.array([1, 0, 0, 0]),
(1, 1): np.array([0, 0, 0, 1]),
},
),
],
)
def test_tensor_comb(states, k, mode, expected_comb_keys, expected_comb):
Expand Down Expand Up @@ -123,6 +134,31 @@ def test_tensor_comb(states, k, mode, expected_comb_keys, expected_comb):
),
},
),
# diagonal mode with k = 2.
(
[np.array([1, 0]), np.array([0, 1])],
2,
"diagonal",
[(0, 0), (1, 1)],
{
(0, 0): np.array(
[
[1, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
]
),
(1, 1): np.array(
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1],
]
),
},
),
],
)
def test_tensor_comb_density_matrix(states, k, mode, expected_comb_keys, expected_rho):
Expand Down
17 changes: 17 additions & 0 deletions toqito/matrix_props/tests/test_is_absolutely_k_incoherent.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,20 @@ def fake_solve(self, *args, **kwargs):
monkeypatch.setattr(cp.Problem, "solve", fake_solve)
# Expect True as the final result.
assert is_absolutely_k_incoherent(mat, 3) is True


def test_k_equals_n_minus_one_true(monkeypatch):
"""When k = n - 1 and the SDP succeeds, the function returns True."""
mat = np.diag([0.6, 0.2, 0.2, 0.0])

def fake_solve(self, *args, **kwargs):
return 1.0

monkeypatch.setattr(cp.Problem, "solve", fake_solve)
assert is_absolutely_k_incoherent(mat, 3) is True


def test_k_equals_n_minus_one_large_eigenvalue():
"""When k = n - 1 and the largest eigenvalue exceeds the bound, return False immediately."""
mat = np.diag([0.9, 0.05, 0.05, 0.0])
assert is_absolutely_k_incoherent(mat, 3) is False