-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterative_tomography.py
230 lines (143 loc) · 6.45 KB
/
iterative_tomography.py
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
'''
This is a implementation of
'Iterative algorithm for reconstruction of entangled states(10.1103/PhysRevA.63.040303)'
'Diluted maximum-likelihood algorithm for quantum tomography(10.1103/PhysRevA.75.042108)'
for quantum state tomography.
This is for two qubits system.
'''
import numpy as np
from numpy import array, kron, trace, identity
import scipy
from scipy.linalg import sqrtm, funm
from datetime import datetime, timedelta
"""
definition of base:
bH (numpy.array (2*2))
bV ( " )
bD ( " )
bR ( " )
bL ( " )
"""
bH = array([[1,0],[0,0]])
bV = array([[0,0],[0,1]])
bD = array([[1/2,1/2],[1/2,1/2]])
bR = array([[1/2,1j/2],[-1j/2,1/2]])
bL = array([[1/2,-1j/2],[1j/2,1/2]])
"""
matrix of bases for two qubits:
bases (numpy.array ((4*4)*16))
------------------
A order of this bases array is one of most important things in calculation.
So you must match each other between this and data set.
"""
bases =array(
[
kron(bH,bH),kron(bH,bV),kron(bV,bV),kron(bV,bH),
kron(bR,bH),kron(bR,bV),kron(bD,bV),kron(bD,bH),
kron(bD,bR),kron(bD,bD),kron(bR,bD),kron(bH,bD),
kron(bV,bD),kron(bV,bL),kron(bH,bL),kron(bR,bL)
]
)
"""
Get Experimental Datas
"""
def getDatasFromFile(fileOfExperimentalDatas, numberOfQubits):
"""
getDatasFromFile(fileOfExperimentalDatas, numberOfQubits):
This function is getting datas from file of experiment consequense,
and return matrix (np.array (numberOfQubits*numberOfQubits)) of them.
"""
matrixOfExperimentalDatas = np.zeros([numberOfQubits,numberOfQubits], dtype=np.complex)
# TODO: modify matrixOfExperimentalDatas by given data file.
return matrixOfExperimentalDatas
"""
Iterative Algorithm
"""
def doIterativeAlgorithm(maxNumberOfIteration, listOfExperimentalDatas, initialDensityMatrix):
"""
doIterativeAlgorithm():
This function is to do iterative algorithm(10.1103/PhysRevA.63.040303) to a set of datas given from a experiment.
This recieve two variables (maxNumberOfIteration, listAsExperimentalDatas),
and return most likely estimated density matrix (np.array).
First quantum state matrix for this algorithm is a identity matrix.
--------------------------------------------------------------------------------------------------------------
Return:
most likely estimated density matrix(np.array), time difference(datetime.timedelta)
"""
iter = 0
dimH = 4
epsilon = 1000 # about 1000 is the best number of epsilon at first
TolFun = 10e-11
endDiff = 10e-10
diff = 100
traceDistance = 100
dataList = listOfExperimentalDatas
totalCountOfData = sum(dataList)
nDataList = dataList / totalCountOfData # nDataList is a list of normarized datas
# densityMatrix = identity(dimH) # Input Density Matrix in Diluted MLE (Identity)
densityMatrix = initialDensityMatrix
startTime = datetime.now() #Timestamp
while traceDistance > TolFun and iter <= maxNumberOfIteration:
# while diff > endDiff and iter <= maxNumberOfIteration:
probList = [trace(bases[i] @ densityMatrix) for i in range(16)]
nProbList = probList / sum(probList)
rotationMatrix = sum([(nDataList[i] / probList[i])*bases[i] for i in range(16)])
""" Normalization of Matrices for Measurement Bases """
U = np.linalg.inv(sum(bases)) / sum(probList)
rotationMatrixLeft = (identity(dimH) + epsilon * U @ rotationMatrix) / (1 + epsilon)
rotationMatrixRight = (identity(dimH) + epsilon * rotationMatrix @ U) / (1 + epsilon)
""" Calculation of updated density matrix """
modifiedDensityMatrix = rotationMatrixLeft @ densityMatrix @ rotationMatrixRight / trace(rotationMatrixLeft @ densityMatrix @ rotationMatrixRight)
eigValueArray, eigVectors = np.linalg.eig(densityMatrix - modifiedDensityMatrix)
traceDistance = sum(np.absolute(eigValueArray)) / 2
""" Update Likelihood Function, and Compared with older one """
LikelihoodFunction = sum([nDataList[i]*np.log(nProbList[i]) for i in range(16)])
probList = [trace(bases[i] @ modifiedDensityMatrix) for i in range(16)]
nProbList = probList / sum(probList)
modifiedLikelihoodFunction = sum([nDataList[i] * np.log(nProbList[i]) for i in range(16)])
diff = modifiedLikelihoodFunction - LikelihoodFunction
""" Show Progress of Calculation """
progress = 100 * iter / maxNumberOfIteration
if progress % 5 == 0:
msg = "Progress of calculation: " + str(int(progress)) + "%"
print(msg)
iter += 1
""" Check Increasing of Likelihood Function """
if diff < 0:
epsilon = epsilon * 0.1 # about 0.1 is the best
continue
""" Update Density Matrix """
densityMatrix = modifiedDensityMatrix.copy()
endTime = datetime.now() #Timestamp
""" Check That Max Iteration Number was proper """
if iter >= maxNumberOfIteration:
print("----------------------------------------------")
print("Iteration time reached max iteration number.")
print("The number of iteration times is too small.")
print("----------------------------------------------")
endIterationTimes = iter
emsg = "Iteration was '" + str(endIterationTimes) + "' times."
print(emsg)
return modifiedDensityMatrix, endTime - startTime
"""
Calculate Fidelity
"""
def calculateFidelity(idealDensityMatrix, estimatedDensityMatrix):
"""
calculateFidelity(idealDensityMatrix, estimatedDensityMatrix):
"""
fidelity = np.real(trace(idealDensityMatrix @ estimatedDensityMatrix))
return fidelity
if __name__ == "__main__":
listOfExperimentalDatas = array(list(map(float, input().split())))
sTime = datetime.now()
maxNumberOfIteration = 10000000
initialDensityMatrix = identity(4)
estimatedDensityMatrix, timeDifference = doIterativeAlgorithm(maxNumberOfIteration, listOfExperimentalDatas, initialDensityMatrix)
# idealDensityMatrix = array([[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 1]]) / 2
# fidelity = calculateFidelity(idealDensityMatrix, estimatedDensityMatrix)
print(estimatedDensityMatrix)
# print("Fidelity is " + str(fidelity))
endTime = datetime.now()
allTimeDifference = endTime - sTime
print("Time of calculation: ",allTimeDifference)