1
#This file is part of QuTIP.
3
# QuTIP is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
8
# QuTIP is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with QuTIP. If not, see <http://www.gnu.org/licenses/>.
16
# Copyright (C) 2011, Paul D. Nation & Robert J. Johansson
18
###########################################################################
20
import scipy.sparse as sp
21
from scipy.linalg import *
23
from list2ind import *
30
if isinstance(sel,int):
35
M=prod(asarray(drho).take(sel))
40
if prod(rho.dims[1]) == 1:
41
#print "ptrace for ket"
42
# XXX: temporary solution
46
# density matrix (operator)
48
#print "ptrace for operator"
50
perm = sp.lil_matrix(zeros((M*M,N*N)))
51
rest=setdiff1d(arange(len(drho)),asarray(sel)) #all elements in range(len(drho)) not in sel set
52
ilistsel=selct(sel,drho)
53
indsel=list2ind(ilistsel,drho)
54
ilistrest=selct(rest,drho)
55
indrest=list2ind(ilistrest,drho)
56
irest=(indrest-1)*N+indrest
63
for i in arange(len(col)):
64
perm[m-1,int(col[i][0])-1]=1
66
rws=prod(shape(rho.data))
69
#print "perm =", perm.shape
70
#print "rho =", rho.data.shape
75
if sp.issparse(rho.data):
76
rhdata=dot(perm,rho.data.tolil().reshape((rws,1)))
77
rhdata=rhdata.tolil().reshape((M,M))
78
rho1.data=rhdata.tocsr()
80
rhdata = perm * rho.data.reshape((rws,1))
81
rhdata=rhdata.reshape((M,M))
84
dims_kept0=asarray(rho.dims[0]).take(sel)
85
dims_kept1=asarray(rho.dims[0]).take(sel)
86
rho1.dims=[dims_kept0.tolist(),dims_kept1.tolist()]
87
rho1.shape=[prod(dims_kept0),prod(dims_kept1)]