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

Upated Indexing from 1 -> 0 #602

Merged
merged 18 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions toqito/perms/perfect_matchings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,27 @@ def perfect_matchings(num: list[int] | int | np.ndarray) -> np.ndarray:

Examples
==========

This is an example of how to generate all perfect matchings of the numbers 1, 2, 3, 4.
This is an example of how to generate all perfect matchings of the numbers 0, 1, 2, 3.

>>> from toqito.perms import perfect_matchings
>>> perfect_matchings(4)
array([[1, 2, 3, 4],
[1, 3, 2, 4],
[1, 4, 3, 2]])
array([[0, 1, 2, 3],
[0, 2, 1, 3],
[0, 3, 2, 1]])

References
==========
.. bibliography::
:filter: docname in docnames

:param num: Either an even integer, indicating that you would like all perfect matchings of the
integers 1,2, ... N, or a `list` or `np.array` containing an even number of distinct
integers 0, 1, ... N-1, or a `list` or `np.array` containing an even number of distinct
entries, indicating that you would like all perfect matchings of those entries.
:return: An array containing all valid perfect matchings of size :code:`num`.

"""
if isinstance(num, int):
num = np.arange(1, num + 1)
num = np.arange(num)
if isinstance(num, list):
num = np.array(num)

Expand Down Expand Up @@ -73,3 +72,4 @@ def perfect_matchings(num: list[int] | int | np.ndarray) -> np.ndarray:
matchings = np.vstack((matchings, s_vec))

return matchings

38 changes: 20 additions & 18 deletions toqito/perms/tests/test_perfect_matchings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
def test_perfect_matchings_num_4():
"""All perfect matchings of size 4."""
res = perfect_matchings(4)
expected_res = np.array([[1, 2, 3, 4], [1, 3, 2, 4], [1, 4, 3, 2]])
print(res)
anushkrishnav marked this conversation as resolved.
Show resolved Hide resolved
expected_res = np.array([[0, 1, 2, 3], [0, 2, 1, 3], [0, 3, 2, 1]])
bool_mat = np.isclose(res, expected_res)
np.testing.assert_equal(np.all(bool_mat), True)


def test_perfect_matchings_num_4_list():
"""All perfect matchings of size 4 with input as list."""
res = perfect_matchings([1, 2, 3, 4])
expected_res = np.array([[1, 2, 3, 4], [1, 3, 2, 4], [1, 4, 3, 2]])
res = perfect_matchings([0, 1, 2, 3])
expected_res = np.array([[0, 1, 2, 3], [0, 2, 1, 3], [0, 3, 2, 1]])
bool_mat = np.isclose(res, expected_res)
np.testing.assert_equal(np.all(bool_mat), True)

Expand All @@ -32,24 +33,25 @@ def test_perfect_matchings_odd():
def test_perfect_matchings_num_6():
"""All perfect matchings of size 6."""
res = perfect_matchings(6)
print(res)
anushkrishnav marked this conversation as resolved.
Show resolved Hide resolved

expected_res = np.array(
[
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 5, 4, 6],
[1, 2, 3, 6, 5, 4],
[1, 3, 2, 4, 5, 6],
[1, 3, 2, 5, 4, 6],
[1, 3, 2, 6, 5, 4],
[1, 4, 3, 2, 5, 6],
[1, 4, 3, 5, 2, 6],
[1, 4, 3, 6, 5, 2],
[1, 5, 3, 4, 2, 6],
[1, 5, 3, 2, 4, 6],
[1, 5, 3, 6, 2, 4],
[1, 6, 3, 4, 5, 2],
[1, 6, 3, 5, 4, 2],
[1, 6, 3, 2, 5, 4],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 4, 3, 5],
[0, 1, 2, 5, 4, 3],
[0, 2, 1, 3, 4, 5],
[0, 2, 1, 4, 3, 5],
[0, 2, 1, 5, 4, 3],
[0, 3, 2, 1, 4, 5],
[0, 3, 2, 4, 1, 5],
[0, 3, 2, 5, 4, 1],
[0, 4, 2, 3, 1, 5],
[0, 4, 2, 1, 3, 5],
[0, 4, 2, 5, 1, 3],
[0, 5, 2, 3, 4, 1],
[0, 5, 2, 4, 3, 1],
[0, 5, 2, 1, 4, 3]
]
)
bool_mat = np.isclose(res, expected_res)
Expand Down
1 change: 0 additions & 1 deletion toqito/states/brauer.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def brauer(dim: int, p_val: int) -> np.ndarray:
matchings = perfect_matchings(2 * p_val)
num_matchings = matchings.shape[0]
state = np.zeros((dim ** (2 * p_val), num_matchings))
matchings = matchings - 1

# Turn these perfect matchings into the corresponding states.
for i in range(num_matchings):
Expand Down
Loading