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

Add partial transpose function in quantum_info #9566

Merged
merged 31 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5ba4856
partial_transpose included
PayalSolanki2906 Feb 9, 2023
a93623a
included partial_transpose
PayalSolanki2906 Feb 9, 2023
1a162dd
included partial_transpose
PayalSolanki2906 Feb 9, 2023
a7cbffe
included partial_transpose
PayalSolanki2906 Feb 9, 2023
95d99a5
included partial_transpose
PayalSolanki2906 Feb 9, 2023
ea9f8f0
included partial_transpose
PayalSolanki2906 Feb 10, 2023
2ff5294
included partial_transpose
PayalSolanki2906 Feb 10, 2023
ae400f1
included partial_transpose
PayalSolanki2906 Feb 10, 2023
28ba225
included partial_transpose
PayalSolanki2906 Feb 10, 2023
91cc3c1
included partial_transpose
PayalSolanki2906 Feb 10, 2023
d03e70e
included partial_transpose
PayalSolanki2906 Feb 10, 2023
fc1d655
included partial_transpose
PayalSolanki2906 Feb 10, 2023
a2c8a05
included test for partial transpose
PayalSolanki2906 Feb 10, 2023
35882a4
included test for partial transpose
PayalSolanki2906 Feb 10, 2023
3934c8c
included test for partial transpose
PayalSolanki2906 Feb 10, 2023
2a22078
added docstring
PayalSolanki2906 Feb 10, 2023
788f960
included DensityMatrix(rho1)
PayalSolanki2906 Feb 10, 2023
a3a16fb
changed rho1
PayalSolanki2906 Feb 10, 2023
c498c77
addition of release note
PayalSolanki2906 Feb 10, 2023
68f1ef6
Merge branch 'main' into pp-parttr
PayalSolanki2906 Feb 11, 2023
8e2684a
Merge branch 'main' into pp-parttr
PayalSolanki2906 Feb 14, 2023
934941f
Merge branch 'pp-parttr' of https://github.com/PayalSolanki2906/qiski…
PayalSolanki2906 Feb 14, 2023
3832e74
fix
PayalSolanki2906 Feb 16, 2023
11d5adc
fix
PayalSolanki2906 Feb 16, 2023
0ab74ea
fir partial_transpose
PayalSolanki2906 Feb 16, 2023
d944e23
Update utils.py
PayalSolanki2906 Feb 16, 2023
4f0a8c8
Merge branch 'main' into pp-parttr
ikkoham Apr 12, 2023
84bd450
refactor and add tests
ikkoham Apr 12, 2023
e7c53ee
Merge pull request #1 from ikkoham/pp-parttr
PayalSolanki2906 Apr 13, 2023
5edcf20
Merge branch 'main' into pp-parttr
PayalSolanki2906 Apr 13, 2023
1b92492
Merge branch 'main' into pp-parttr
PayalSolanki2906 Apr 15, 2023
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
19 changes: 19 additions & 0 deletions qiskit/quantum_info/states/densitymatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,3 +814,22 @@ def to_statevector(self, atol=None, rtol=None):

psi = evecs[:, np.argmax(evals)] # eigenvectors returned in columns.
return Statevector(psi)

def partial_transpose(self, qargs):
"""Return partially transposed density matrix.

Args:
qargs (list): The subsystems to be transposed.

Returns:
DensityMatrix: The partially transposed density matrix.
"""
arr = self._data.reshape(self._op_shape.tensor_shape)
qargs = len(self._op_shape.dims_l()) - 1 - np.array(qargs)
n = len(self.dims())
lst = list(range(2 * n))
for i in 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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

features:
- |
The partial transpose operation has been integrated into the quantum_info module, allowing for partial transposition of matrices.
This operation is key in detecting entanglement between bipartite quantum system.
35 changes: 26 additions & 9 deletions test/python/quantum_info/states/test_densitymatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@

"""Tests for DensityMatrix quantum state class."""

import unittest
import logging
from ddt import ddt, data
import unittest

import numpy as np
from ddt import data, ddt
from numpy.testing import assert_allclose

from qiskit.test import QiskitTestCase
from qiskit import QiskitError
from qiskit import QuantumRegister, QuantumCircuit
from qiskit.circuit.library import HGate, QFT

from qiskit.quantum_info.random import random_unitary, random_density_matrix, random_pauli
from qiskit.quantum_info.states import DensityMatrix, Statevector
from qiskit import QiskitError, QuantumCircuit, QuantumRegister
from qiskit.circuit.library import QFT, HGate
from qiskit.quantum_info.operators.operator import Operator
from qiskit.quantum_info.operators.symplectic import Pauli, SparsePauliOp
from qiskit.quantum_info.random import random_density_matrix, random_pauli, random_unitary
from qiskit.quantum_info.states import DensityMatrix, Statevector
from qiskit.test import QiskitTestCase

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -1202,6 +1201,24 @@ def test_drawings(self):
with self.subTest(msg=f"draw('{drawtype}')"):
dm.draw(drawtype)

def test_density_matrix_partial_transpose(self):
"""Test partial_transpose function on density matrices"""
with self.subTest(msg="separable"):
rho = DensityMatrix.from_label("10+")
rho1 = np.zeros((8, 8), complex)
rho1[4, 4] = 0.5
rho1[4, 5] = 0.5
rho1[5, 4] = 0.5
rho1[5, 5] = 0.5
self.assertEqual(rho.partial_transpose([0, 1]), DensityMatrix(rho1))
self.assertEqual(rho.partial_transpose([0, 2]), DensityMatrix(rho1))

with self.subTest(msg="entangled"):
rho = DensityMatrix([[0, 0, 0, 0], [0, 0.5, -0.5, 0], [0, -0.5, 0.5, 0], [0, 0, 0, 0]])
rho1 = DensityMatrix([[0, 0, 0, -0.5], [0, 0.5, 0, 0], [0, 0, 0.5, 0], [-0.5, 0, 0, 0]])
self.assertEqual(rho.partial_transpose([0]), DensityMatrix(rho1))
self.assertEqual(rho.partial_transpose([1]), DensityMatrix(rho1))

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