-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecomposition.py
More file actions
232 lines (167 loc) · 5.6 KB
/
Copy pathDecomposition.py
File metadata and controls
232 lines (167 loc) · 5.6 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import numpy as np
from cmath import *
import csv
from Classic_algebra import *
# Number of qubits
N = 3
# N-qubit gate U
U = np.zeros([2**N,2**N], complex)
for i in range (2**N):
for j in range(2**N):
U[i,j] = e**(2*pi*1j*i*j/2**N)/2**(N/2)
print("Your matrix is :")
print(U)
# Extract H matrix from gate
H = extract_hermitian_from_gate(U)
# All N-qubit Pauli operators
(pauli_names, pauli_matrices) = SU_2_N(N)
# Decomposition of H over the set of N-qubit operators
decomposition = [HS(H,pauli_matrix,N) for pauli_matrix in pauli_matrices]
#Showing results
#Showing [[O_i, alpha_i]]
decomposition_output = order(fusion_list(pauli_names, decomposition))
#Analysis
tokeep = []
n = len(decomposition_output)
for i in range(n-1,-1,-1):
x = decomposition_output[i]
if abs(x[1])> 10**(-10):
y = [x[0], x[1].real]
tokeep.append(y)
else :
break
print(tokeep)
#MUB inspired classification :
fichier = open("MUB_Pauli_order_"+str(N)+".csv", "rt")
MUBCSV = csv.reader(fichier,delimiter=";")
possible_partitioning = { i:[] for i in range(2**N+2)}
i = 0
for row in MUBCSV:
if i!=0:
partitioning = []
for base in row :
commutative_set = []
for x in tokeep :
if x[0] in base:
commutative_set.append(x)
partitioning.append(commutative_set)
k = len(partitioning)
possible_partitioning[k].append(partitioning)
else :
i+=1
best_index = 0
for k in range(1,2**N+2):
if len(possible_partitioning[k])!=0:
best_index = k
break
print("Les solutions permettant le partitionnement à plus faible coût sont :"+"\n")
index_solution = 0
for solution in possible_partitioning[best_index]:
index_solution+=1
print("Solution "+str(index_solution)+" : "+"\n")
index_gate = 0
print("Phase factor :")
print(solution[0][0])
for gate in solution :
if len(gate)!=1:
index_gate+=1
print("Décomposition Hamiltonien porte "+str(index_gate) + " : ")
print(gate[1:])
#First Iterative Classification :
cover = []
phase_factor = []
identity = ''
for _ in range(N):
identity+='I'
for pauli_operator_phase in tokeep :
(pauli_operator,phase) = (pauli_operator_phase[0],pauli_operator_phase[1])
if pauli_operator == identity:
phase_factor.append([pauli_operator_phase])
else :
n = len(cover)
fitting_subsets = []
for i in range(n) :
subset = cover[i]
commute_with_all = True
for operator in subset:
if not commute(pauli_operator, operator[0], N) :
commute_with_all = False
if commute_with_all :
fitting_subsets.append(i)
m = len(fitting_subsets)
if m == 0 :
cover.append([pauli_operator_phase])
else :
for index in fitting_subsets:
cover[index].append([pauli_operator, phase/m])
print("Decomposition using an iterative cover")
print(phase_factor+cover)
print("Number of gates :")
print(len(phase_factor+cover))
#Second Iterative Classification :
cover = []
phase_factor = []
identity = ''
for _ in range(N):
identity+='I'
for pauli_operator_phase in tokeep :
(pauli_operator,phase) = (pauli_operator_phase[0],pauli_operator_phase[1])
if pauli_operator == identity:
phase_factor.append([pauli_operator_phase])
else :
n = len(cover)
fitting_subsets = []
for i in range(n) :
subset = cover[i]
commute_with_all = True
for operator in subset:
if not commute(pauli_operator, operator[0], N) :
commute_with_all = False
if commute_with_all :
fitting_subsets.append(i)
m = len(fitting_subsets)
cover.append([pauli_operator_phase])
for index in fitting_subsets:
cover[index].append([pauli_operator, phase])
for pauli_operator_phase in tokeep :
(pauli_operator,phase) = (pauli_operator_phase[0],pauli_operator_phase[1])
if not pauli_operator == identity:
n = len(cover)
for i in range(n) :
subset = cover[i]
commute_with_all = True
if pauli_operator_phase in subset :
break
else :
for operator in subset:
if not commute(pauli_operator, operator[0], N) :
commute_with_all = False
if commute_with_all :
cover[i].append([pauli_operator, phase])
Final_cover = []
while len(cover)!=0:
weights = [norm(subset) for subset in cover]
m = max(weights)
n = len(weights)
for i in range(n):
if weights[i] == m:
best = i
break
best_subset = cover[i]
Final_cover.append(list(best_subset))
n = len(cover)
for i in range(n-1, -1, -1):
subset = cover[i]
m = len(subset)
todel= []
for j in range(m):
if subset[j] in best_subset :
todel.insert(0, j)
for j in todel:
del subset[j]
if len(subset)==0:
del cover[i]
print("Final_cover :")
print(phase_factor+Final_cover)
print("Number of gates :")
print(len(phase_factor+Final_cover))