forked from peterkirton/permutations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations_test.py
More file actions
169 lines (111 loc) · 4.9 KB
/
Copy pathpermutations_test.py
File metadata and controls
169 lines (111 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 25 20:00:09 2024
@author: lukas
"""
from numpy import array, where
from math import factorial
def degeneracy_outer_invariant(outer1, outer2, inner):
xi = outer1+ 2*outer2
# print(xi)
# print('inner',inner)
deg = 1
for i in range(4):
l = where(xi==i)[0]
# print('loop',i)
# print(l)
if len(l) == 0:
continue
sub_inner = inner[l]
# print('subinner',sub_inner)
s = sum(sub_inner)
factor = factorial(len(l)) / (factorial(s)*factorial(len(l)-s))
# print('factor',factor)
deg = deg * factor
print(int(deg))
def degeneracy_outer_invariant1(outer1, outer2, inner):
""" calculate how many distinct permutations there are of the spins (outer1, inner)
and (inner, outer2), which leave outer1 and outer2 invariant. """
from itertools import permutations
from numpy import array
perms = [inner]
for p in permutations(range(len(inner))):
inner_cp = array([inner[i] for i in p])
if(any(all(existing_list == inner_cp) for existing_list in perms)):
continue
outer1_cp = array([outer1[i] for i in p])
outer2_cp = array([outer2[i] for i in p])
# print(outer1_cp, inner_cp, outer2_cp)
if (all(outer1_cp == outer1) and all(outer2_cp == outer2)):
perms.append(inner_cp)
return len(perms)
def degeneracy_outer_invariant_inner2(outer1,outer2,inner1,inner2):
""" For sigma^- lindblad operator. Calculate the number of permutations,
that leave outer1 and outer2 invariant, but produce a new combined state
(inner1, inner2)"""
from itertools import permutations
from numpy import array, concatenate
perms = [concatenate((inner1, inner2))]
for p in permutations(range(len(inner1))):
inner1_cp = array([inner1[i] for i in p])
inner2_cp = array([inner2[i] for i in p])
inner_total = concatenate((inner1_cp, inner2_cp))
if(any(all(existing_list == inner_total) for existing_list in perms)):
continue
outer1_cp = array([outer1[i] for i in p])
outer2_cp = array([outer2[i] for i in p])
if (all(outer1_cp == outer1) and all(outer2_cp == outer2)):
perms.append(concatenate((inner1_cp, inner2_cp)))
return len(perms)
def degeneracy_permutation(left, right):
""" Calculate the degeneracy due to simultaneous permutation of left and
right spin indices"""
from math import factorial
from numpy import where
xi = left + 2*right
deg = factorial(len(xi))
for i in range(4):
l = len(where(xi==i)[0])
deg = deg / factorial(l)
return deg
def align_ones(left, right):
from numpy import where, setdiff1d, intersect1d, copy
# check indices, where the arrays have ones
idx1 = where(left == 1)[0]
idx2 = where(right == 1)[0]
# find common indices and remove them, because they are already in order
common_elements = intersect1d(idx1,idx2)
idx_ones1 = setdiff1d(idx1, common_elements)
idx_ones2 = setdiff1d(idx2, common_elements)
# go through idx_ones1 and permute elements, such that ones align
left_cp = copy(left)
for i in range(len(idx_ones1)):
left_cp[idx_ones2[i]] = left[idx_ones1[i]]
left_cp[idx_ones1[i]] = left[idx_ones2[i]]
return 0
def degeneracy_gamma_changing_block(outer1, outer2, inner1, inner2):
"""Find simultaneous permutation of inner1 and inner2, such that all but one
spin index align, and in exactly the same positions"""
from itertools import permutations
from numpy import array, concatenate, where, not_equal
if sum(outer1) - sum(inner1) != 1 or sum(outer2) - sum(inner2) != 1:
return -1
perms = []
for p in permutations(range(len(inner1))):
inner1_cp = array([inner1[i] for i in p])
inner2_cp = array([inner2[i] for i in p])
if(any(all(existing_list == concatenate((inner1_cp, inner2_cp))) for existing_list in perms)):
continue
# check, where they align
notequal1 = where(not_equal(inner1_cp, outer1))[0]
notequal2 = where(not_equal(inner2_cp, outer2))[0]
if len(notequal1) == 1 and len(notequal2) == 1:
if notequal1[0] == notequal2[0]:
perms.append(concatenate((inner1_cp, inner2_cp)))
return len(perms)
outer1 = array([1,1])
outer2 = array([1,1])
inner1 = array([1,0])
inner2 = array([1,0])
print(degeneracy_gamma_changing_block(outer1, outer2, inner1, inner2))
# print(degeneracy_outer_invariant_inner2(outer1, outer2, inner1, inner2))