Repository: qutip/qutip

Issue:
Negativity function does not work
### Bug Description

The function `negativity() ` is currently broken because of a broken import of `partial_transpose()`.   This is probably an easy fix and good first issue, but more importantly we need a test for this function, and anything else that currently is not tested in `entropy.py`.

Also, might be useful to either properly support kets in negativity (i think it just assumes at the moment the input state is a density operator, and incorrectly acts on kets), or give a warning if a state is provided, and not a density operator.

### Code to Reproduce the Bug

```shell
import numpy as np
from qutip import *

test = (tensor(basis(2,0),basis(2,1)) + tensor(basis(2,1),basis(2,0)))/np.sqrt(2)
negativity(test * test.dag(),0)
```


### Code Output

```shell
123 """
    124 Compute the negativity for a multipartite quantum system described
    125 by the density matrix rho. The subsys argument is an index that
   (...)
    130     Experimental.
    131 """
    132 mask = [idx == subsys for idx, n in enumerate(rho.dims[0])]
--> 133 rho_pt = partial_transpose(rho, mask)
    135 if method == 'tracenorm':
    136     N = ((rho_pt.dag() * rho_pt).sqrtm().tr().real - 1)/2.0

TypeError: 'module' object is not callable
```


### Expected Behaviour

should be = 0.5

### Your Environment

```shell
QuTiP Version:      5.0.0.dev0+c745ace
Numpy Version:      1.26.3
Scipy Version:      1.11.4
Cython Version:     3.0.8
Matplotlib Version: 3.8.0
Python Version:     3.12.1
Number of CPUs:     28
BLAS Info:          Generic
INTEL MKL Ext:      False
Platform Info:      Linux (x86_64)
```


### Additional Context

_No response_


Discussion:
@nwlambert I love to work on this issue.

thanks @vikas-chaudhary-2802 that would be great
@nwlambert 
Unable to run the test cases in (entropy.py) it show this error 
<img width="850" alt="Screenshot 2024-03-20 at 2 48 54 AM" src="https://github.com/qutip/qutip/assets/147247481/710869a8-79e6-4941-b6d6-2ad0cf9fd76d">
Can you please suggest me the solution to solve it ?

@vikas-chaudhary-2802 It looks like something went wrong with the installation. Make sure that you really installed the master branch and are testing it.
Sorry @vikas-chaudhary-2802 I didn't notice your question last week. As boxi said, might be a bad install or you are using the wrong version.  The ''how to contribute to qutip'' section of this tutorial might be useful for setting things up https://colab.research.google.com/drive/1RcgX7oEzGjzPAF8Ryus54Q5UmyMddmLA?usp=sharing

Resolving patch:
diff --git a/qutip/entropy.py b/qutip/entropy.py
--- a/qutip/entropy.py
+++ b/qutip/entropy.py
@@ -4,7 +4,8 @@
 
 from numpy import conj, e, inf, imag, inner, real, sort, sqrt
 from numpy.lib.scimath import log, log2
-from . import (ptrace, ket2dm, tensor, sigmay, partial_transpose,
+from .partial_transpose import partial_transpose
+from . import (ptrace, tensor, sigmay, ket2dm,
                expand_operator)
 from .core import data as _data
 
@@ -129,6 +130,8 @@ def negativity(rho, subsys, method='tracenorm', logarithmic=False):
 
         Experimental.
     """
+    if rho.isket or rho.isbra:
+        rho = ket2dm(rho)
     mask = [idx == subsys for idx, n in enumerate(rho.dims[0])]
     rho_pt = partial_transpose(rho, mask)
 
@@ -140,6 +143,7 @@ def negativity(rho, subsys, method='tracenorm', logarithmic=False):
     else:
         raise ValueError("Unknown method %s" % method)
 
+# Return the negativity value (or its logarithm if specified)
     if logarithmic:
         return log2(2 * N + 1)
     else:


Test patch: