Skip to content

Commit

Permalink
reduced state fix (#395)
Browse files Browse the repository at this point in the history
* added lines to change the type from np.ndarray to list in `reduced_state`

* added an explanation to CHANGELOG.md

* added test

* added the related PR number to CHANGELOG.md
  • Loading branch information
RyosukeNORO authored Jul 9, 2024
1 parent 0e51518 commit 087743d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

* Add the calculation method of `takagi` when the matrix is diagonal. [(#394)](https://github.com/XanaduAI/thewalrus/pull/394)

* Add the lines for avoiding the comparison of np.ndarray and list. [(#395)](https://github.com/XanaduAI/thewalrus/pull/395)

### Documentation

### Contributors
Expand Down
3 changes: 3 additions & 0 deletions thewalrus/symplectic.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ def reduced_state(mu, cov, modes):
"""
N = len(mu) // 2

if type(modes) == np.ndarray:
modes = modes.tolist()

if modes == list(range(N)):
# reduced state is full state
return mu, cov
Expand Down
9 changes: 9 additions & 0 deletions thewalrus/tests/test_symplectic.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,15 @@ def test_tms(self, hbar, tol):
assert np.allclose(res[0], expected[0], atol=tol, rtol=0)
assert np.allclose(res[1], expected[1], atol=tol, rtol=0)

def test_ndarray(self, hbar, tol):
"""Test numpy.ndarray in the third argument of `reduced_state` is converted to list correctly"""
mu, cov = symplectic.vacuum_state(4, hbar=hbar)
res = symplectic.reduced_state(mu, cov, np.array([0, 1, 2, 3]))
expected = np.zeros([8]), np.identity(8) * hbar / 2

assert np.allclose(res[0], expected[0], atol=tol, rtol=0)
assert np.allclose(res[1], expected[1], atol=tol, rtol=0)


class TestLossChannel:
"""Tests for the loss channel"""
Expand Down

0 comments on commit 087743d

Please sign in to comment.