Skip to content

Commit

Permalink
Fix output of DensityMatrix.partial_transpose to match input dimens…
Browse files Browse the repository at this point in the history
…ions (#10163) (#10215)

* (fix) change output of partial_transpose to return DensityMatrix matching input dimensions

* (test) check output dims match input dims when using DensityMatrix.partial_transpose

* (docs) add bugfix reno

* lint changes

* Fix up release note

---------

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit 6f29d7b)

Co-authored-by: Diego Emilio Serrano <65074936+diemilio@users.noreply.github.com>
  • Loading branch information
mergify[bot] and diemilio authored Jun 6, 2023
1 parent aa6066c commit d1b8c5d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qiskit/quantum_info/states/densitymatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,4 +832,4 @@ def partial_transpose(self, qargs):
lst[i], lst[i + n] = lst[i + n], lst[i]
rho = np.transpose(arr, lst)
rho = np.reshape(rho, self._op_shape.shape)
return DensityMatrix(rho)
return DensityMatrix(rho, dims=self.dims())
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fixed the dimensions of the output density matrix from :meth:`.DensityMatrix.partial_transpose`
so they match the dimensions of the corresponding input density matrix.
9 changes: 9 additions & 0 deletions test/python/quantum_info/states/test_densitymatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,15 @@ def test_density_matrix_partial_transpose(self):
self.assertEqual(rho.partial_transpose([0]), DensityMatrix(rho1))
self.assertEqual(rho.partial_transpose([1]), DensityMatrix(rho1))

with self.subTest(msg="dims(3,3)"):
mat = np.zeros((9, 9))
mat1 = np.zeros((9, 9))
mat[8, 0] = 1
mat1[0, 8] = 1
rho = DensityMatrix(mat, dims=(3, 3))
rho1 = DensityMatrix(mat1, dims=(3, 3))
self.assertEqual(rho.partial_transpose([0, 1]), rho1)

def test_clip_probabilities(self):
"""Test probabilities are clipped to [0, 1]."""
dm = DensityMatrix([[1.1, 0], [0, 0]])
Expand Down

0 comments on commit d1b8c5d

Please sign in to comment.