Partial Trace is not implemented in density package
Closed this issue · 1 comments
itsubaki commented
q/pkg/quantum/density/matrix.go
Lines 67 to 70 in de7ab9d
func (m *Matrix) PartialTrace() complex128 { | |
// TODO | |
return complex(0, 0) | |
} |
itsubaki commented
q/pkg/quantum/density/matrix.go
Lines 71 to 112 in 42e44a4
func (m *Matrix) PartialTrace(index int) *Matrix { | |
n := m.NumberOfBit() | |
f := fmt.Sprintf("%s%s%s", "%0", strconv.Itoa(n), "s") | |
out := matrix.Zero(number.Pow(2, n-1)) | |
d := m.Dimension() | |
for i := 0; i < d; i++ { | |
ibin := fmt.Sprintf(f, strconv.FormatInt(int64(i), 2)) | |
k, kk := fmt.Sprintf("%s%s", ibin[:index], ibin[index+1:]), string(ibin[index]) | |
for j := 0; j < d; j++ { | |
jbin := fmt.Sprintf(f, strconv.FormatInt(int64(j), 2)) | |
l, ll := fmt.Sprintf("%s%s", jbin[:index], jbin[index+1:]), string(jbin[index]) | |
if kk != ll { | |
continue | |
} | |
r := number.Must(strconv.ParseInt(k, 2, 0)) | |
c := number.Must(strconv.ParseInt(l, 2, 0)) | |
out[r][c] = out[r][c] + m.m[i][j] | |
// fmt.Printf("[%v][%v] = [%v][%v] + [%v][%v]\n", r, c, r, c, i, j) | |
// | |
// 4x4 explicit | |
// index -> 0 | |
// out[0][0] = m.m[0][0] + m.m[2][2] | |
// out[0][1] = m.m[0][1] + m.m[2][3] | |
// out[1][0] = m.m[1][0] + m.m[3][2] | |
// out[1][1] = m.m[1][1] + m.m[3][3] | |
// | |
// index -> 1 | |
// out[0][0] = m.m[0][0] + m.m[1][1] | |
// out[0][1] = m.m[0][2] + m.m[1][3] | |
// out[1][0] = m.m[2][0] + m.m[3][1] | |
// out[1][1] = m.m[2][2] + m.m[3][3] | |
} | |
} | |
return &Matrix{m: out} | |
} |