├── .gitignore ├── .idea ├── .name ├── encodings.xml ├── misc.xml ├── modules.xml ├── physics.iml └── vcs.xml ├── AnimatedScatter.py ├── CRW.gif ├── QDS-VS-DW.gif ├── QW.gif ├── QWithDiffShift.py ├── QWithDiffShiftTest.py ├── Quantum Walk in Graph K5.nb ├── README.md ├── classicalRW.py ├── classicalRWMat.py ├── quantumRW.py ├── quantumRWMat.py └── quantumRWTest.py /.gitignore: -------------------------------------------------------------------------------- 1 | /QWM_100.png 2 | /QDS_1.png 3 | /QDS_2.png 4 | /QDS_4.png 5 | /QDS_5.png 6 | /QDS_3.png 7 | /QW_50.txt 8 | /QW_25.txt 9 | /QW_85.txt 10 | /QW_50.png 11 | /QW_20.png 12 | /QW_55.txt 13 | /QW_80.txt 14 | /QW_100.txt 15 | /QW_20.txt 16 | /QW_70.png 17 | /QW_30.txt 18 | /QW_95.txt 19 | /QW_30.png 20 | /QW_90.txt 21 | /QW_60.txt 22 | /QW_90.png 23 | /QW_5.txt 24 | /QW_60.png 25 | /QW_15.txt 26 | /QW_65.txt 27 | /QW_70.txt 28 | /QW_35.txt 29 | /QW_40.png 30 | /QW_10.png 31 | /QW_40.txt 32 | /QW_10.txt 33 | /QW_45.txt 34 | /QW_100.png 35 | /QW_49.txt 36 | /QW_80.png 37 | /QW_75.txt 38 | /CRW_100.png 39 | /CRW_40.png 40 | /CRW_70.png 41 | /CRW_30.png 42 | /CRW_80.png 43 | /CRW_20.png 44 | /CRW_60.png 45 | /CRW_90.png 46 | /CRW_10.png 47 | /CRW_50.png 48 | 49 | # Created by .ignore support plugin (hsz.mobi) 50 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | physics -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/physics.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /AnimatedScatter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | __author__ = 'levitan' 4 | 5 | import matplotlib.pyplot as plt 6 | import matplotlib.animation as animation 7 | from numpy import * 8 | import QWithDiffShift as QDS 9 | import time 10 | 11 | 12 | class AnimatedScatterQW(object): 13 | """An animated scatter plot using matplotlib.animations.FuncAnimation.""" 14 | 15 | def __init__(self, X0, X1, steps, shiftGateNum, node): 16 | self.X0 = X0 17 | self.X1 = X1 18 | self.steps = steps 19 | self.shiftGateNum = shiftGateNum 20 | self.node = node 21 | self.step = 1 22 | self.linedata = zeros([self.node, 1]) 23 | self.distri = self.distribution() 24 | 25 | # Setup the figure and axes... 26 | self.fig = plt.figure() 27 | self.ax1 = plt.subplot(211) 28 | plt.title('Quantum Walk in Cycle Graph,view in cycle') 29 | self.step_text1 = self.ax1.text(-1.8, 1.2, '') 30 | self.step_text1.set_text('') 31 | self.ax2 = plt.subplot(212) 32 | # self.ax2_axis=self.ax2.axis([1, self.node, 0, 1]) 33 | # self.ax2_axis.set_axis() 34 | plt.title('Quantum Walk in Cycle Graph,position 0 probability') 35 | # plt.title('Quantum Walk in Cycle Graph,position probability by step') 36 | # Then setup FuncAnimation. 37 | self.ani = animation.FuncAnimation(self.fig, self.update, frames=200, interval=500, 38 | init_func=self.setup_plot, blit=True) 39 | 40 | def setup_plot(self): 41 | """Initial drawing of the scatter plot.""" 42 | data = next(self.distri) 43 | # temp=array([data[:,2]]).T 44 | # print temp 45 | # self.linedata=column_stack((self.linedata,temp)) 46 | # print self.linedata 47 | x = data[:, 0] 48 | y = data[:, 1] 49 | d = data[:, 2] 50 | self.ax1.axis([-4.5, 4.5, -1.5, 1.5]) 51 | self.scat = self.ax1.scatter(x, y, s=d, c='red', marker='o', animated=True) 52 | 53 | self.ax2.axis([1, 5000, 0, 1]) 54 | self.line, = self.ax2.plot([], []) 55 | # For FuncAnimation's sake, we need to return the artist we'll be using 56 | # Note that it expects a sequence of artists, thus the trailing comma. 57 | return self.scat, self.line, 58 | 59 | def distribution(self): 60 | """Generate a quantum walk distribution in Cycle Graph . """ 61 | step = 1 62 | radius = 1 63 | node = self.node 64 | data = zeros([node, 3], float) 65 | Filename = 'Data/' + time.strftime('%Y%m%d-%H-%M-%S') + '.txt' 66 | distrFlie = open(Filename, 'w+') 67 | for j in range(node): 68 | data[j, 0] = sin(j * 2 * math.pi / node) * radius 69 | data[j, 1] = cos(j * 2 * math.pi / node) * radius 70 | while True: 71 | distribution = QDS.QWCicleDistribution(self.X0, self.X1, step, self.shiftGateNum, node) 72 | # data[:,2]= QDS.ciclePosiTrans(distribution, step, node) 73 | data[:, 2] = distribution 74 | # write distribution to txt file 75 | distrFlie.write(str(step) + '\t') 76 | for x in range(shape(distribution)[0]): 77 | distrFlie.write(str(distribution[x]) + '\t') 78 | distrFlie.write('\n') 79 | self.step = step 80 | print ('step: %s') % step 81 | step += 1 82 | yield data 83 | distrFlie.close() 84 | 85 | def update(self, i): 86 | """Update the scatter plot.""" 87 | data = next(self.distri) 88 | # Set x and y data... 89 | temp = array([data[:, 2]]).T 90 | # print temp 91 | self.linedata = column_stack((self.linedata, temp)) 92 | m, n = shape(self.linedata) 93 | # print self.linedata 94 | self.scat.set_offsets(data[:, :2]) 95 | # self.line.set_offsets(data[:,:2]) 96 | # Set sizes... 97 | self.scat._sizes = data[:, 2] * 200 98 | # self.line._sizes = data[:,2]*500 99 | lineResize = self.linedata.copy() 100 | lineResize.resize(1, self.step) 101 | # print lineResize[0] 102 | x = arange(1, self.step + 1) 103 | y = lineResize 104 | # print x,y 105 | self.line.set_data(x, y) 106 | 107 | self.step_text1.set_text('Setp:%s' % self.step) 108 | # self.ax2.axis([1, self.step, 0, 1]) 109 | # self.ax2_axis.set_axis([[1, self.step, 0, 1]]) 110 | # self.scat.set_title(self.Title) 111 | # Set colors.. 112 | # self.scat.set_array(data[3]) 113 | 114 | # We need to return the updated artist for FuncAnimation to draw.. 115 | # Note that it expects a sequence of artists, thus the trailing comma. 116 | return self.scat, self.line, self.step_text1, 117 | 118 | def show(self): 119 | plt.show() 120 | 121 | 122 | if __name__ == '__main__': 123 | a = AnimatedScatterQW(1, 0, 100, 3, 8) 124 | a.show() 125 | -------------------------------------------------------------------------------- /CRW.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daihui/QuantumWalkSimulation/56b664f2de5bc06425ee15aee97d95522ae8fcfe/CRW.gif -------------------------------------------------------------------------------- /QDS-VS-DW.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daihui/QuantumWalkSimulation/56b664f2de5bc06425ee15aee97d95522ae8fcfe/QDS-VS-DW.gif -------------------------------------------------------------------------------- /QW.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daihui/QuantumWalkSimulation/56b664f2de5bc06425ee15aee97d95522ae8fcfe/QW.gif -------------------------------------------------------------------------------- /QWithDiffShift.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | __author__ = 'levitan' 4 | 5 | """ 6 | This is matrix release for 1D quantum walk with different shift simulation(graph). 7 | 基本过程:有一个初始量子态,coin是一个幺正变换,一次游走即coin对量子态变换一次 8 | 然后根据量子态walk一步,或几步(根据shift而定)得到一个位置概率分布,如此反复。 9 | """ 10 | 11 | from numpy import * 12 | from matplotlib import pyplot as plt 13 | import time 14 | import pylab as py 15 | from mpl_toolkits.mplot3d import Axes3D 16 | from matplotlib import animation 17 | 18 | # ------------------------------------------- 19 | # Quantum walk in line with different shift 20 | # ------------------------------------------- 21 | 22 | 23 | # 初始化量子态,是一个2*1维矩阵 24 | def initQuanStat(X0, X1): 25 | initQuanStat = zeros([2, 1], complex) 26 | initQuanStat[0][0] = X0 27 | initQuanStat[1][0] = X1 28 | return initQuanStat 29 | 30 | 31 | # 定义1D hadamard coin 32 | def hadamardCoin(): 33 | hadamardCoin = array([[1 / sqrt(2), 1 / sqrt(2)], [1 / sqrt(2), -1 / sqrt(2)]], complex) 34 | return hadamardCoin 35 | 36 | 37 | # 定义1D phase coin 38 | def phaseCoin(Psi): 39 | phaseCoin = array([[1, 0], [0, exp(1j * Psi)]], complex) 40 | return phaseCoin 41 | 42 | # 根据shift的最大值和走了的步数定义空位置矩阵 43 | def initPositionMap(steps, shiftNum, shiftGateNum): 44 | stepNum = 0 45 | for j in range(0, shiftGateNum): 46 | stepNum += power(2, j) 47 | dimension = stepNum * (steps - 1) + 1 48 | for i in range(0, shiftNum): 49 | dimension += power(2, i) 50 | positionMap = zeros([dimension, 2, 1], complex) 51 | return positionMap 52 | 53 | 54 | # 对量子态经行coin变换 55 | def coinOperator(coin, positionMap): 56 | dimension = shape(positionMap)[0] 57 | for i in range(dimension): 58 | positionMap[i] = dot(coin, positionMap[i]) 59 | return positionMap 60 | 61 | 62 | # 根据量子态进行位置变换,相当于一次walk 63 | def shiftOperator(positionMap, step, shiftGateNum): 64 | for shiftNum in range(1, shiftGateNum + 1): 65 | newPositionMap = initPositionMap(step, shiftNum, shiftGateNum) 66 | coin = hadamardCoin() 67 | coinMap = coinOperator(coin, positionMap) 68 | for i in range(shape(coinMap)[0]): 69 | newPositionMap[i][0][0] += coinMap[i][0][0] 70 | newPositionMap[i + power(2, shiftNum - 1)][1][0] += coinMap[i][1][0] 71 | positionMap = newPositionMap 72 | return newPositionMap 73 | 74 | 75 | # quantum walk 的整体封装,返回位置分布的量子态 76 | def quantumWalk(X0, X1, steps, shiftGateNum): 77 | initPositionMap = zeros([1, 2, 1], complex) 78 | initPositionMap[0] = initQuanStat(X0, X1) 79 | for step in range(1, steps + 1): 80 | positionMap = initPositionMap 81 | initPositionMap = shiftOperator(positionMap, step, shiftGateNum) 82 | return initPositionMap 83 | 84 | 85 | # 计算QW的位置分布 86 | def QWDistribution(X0, X1, steps, shiftGateNum): 87 | positionMap = quantumWalk(X0, X1, steps, shiftGateNum) 88 | dimension = shape(positionMap)[0] 89 | distribution = zeros([dimension], dtype=float) 90 | sum = 0.0 91 | for i in range(dimension): 92 | distribution[i] = float(positionMap[i][0][0].real ** 2 + positionMap[i][0][0].imag ** 2 + \ 93 | positionMap[i][1][0].real ** 2 + positionMap[i][1][0].imag ** 2) 94 | sum += distribution[i] 95 | print('sum: %s') % sum 96 | return distribution 97 | 98 | 99 | # 将结果写到文本文档 100 | def writeQWtoArray(distribution): 101 | Filename = 'Data/' + time.strftime('%Y%m%d-%H-%M-%S') + '.txt' 102 | distrFlie = open(Filename, 'w+') 103 | for x in range(shape(distribution)[0]): 104 | distrFlie.write(str(distribution[x]) + '\n') 105 | distrFlie.close() 106 | 107 | 108 | # 画出位置概率分布图 109 | def PlotX(distribution, step, figName): 110 | plt.plot(distribution) 111 | plt.title('The Distribution of %s Quantum Walk with Different Shift') % step 112 | plt.xlabel('Started from the 0') 113 | plt.ylabel('Probability') 114 | # plt.show() 115 | plt.savefig('Fig/' + str(figName) + str(step) + '.png') 116 | plt.close() 117 | 118 | 119 | # 画出QDS与QW的概率分布对比图 120 | def PlotComp(distributionQDS, distributionQW, step, figName): 121 | QDS, = plt.plot(distributionQDS, 'r') 122 | QW, = plt.plot(distributionQW, 'b') 123 | plt.axis([0, 7 * step, 0, 0.27]) 124 | plt.legend([QDS, QW, ], ['QDS', 'QW']) 125 | plt.title('The Compare between QDS and QW') 126 | plt.xlabel('X Position (started from the center)') 127 | plt.ylabel('Probability') 128 | # plt.show() 129 | plt.savefig('Fig/' + str(figName) + str(step) + '.png') 130 | plt.close() 131 | 132 | 133 | # ------------------------------------------- 134 | # Quantum walk in cicle with different shift 135 | # ------------------------------------------- 136 | def initQuanStatAverPosiCicle(X0, X1, Node): 137 | initQuanStat = zeros([Node, 2, 1], complex) 138 | for node in range(Node): 139 | initQuanStat[node][0][0] = X0 * 1 / sqrt(Node) 140 | initQuanStat[node][1][0] = X1 * 1 / sqrt(Node) 141 | return initQuanStat 142 | 143 | def positionCicleMap(node): 144 | positionCicleMap = zeros([node, 2, 1], complex) 145 | return positionCicleMap 146 | 147 | 148 | # 根据量子态进行位置变换,相当于一次walk 149 | def shiftCicleOperator(positionMap, step, shiftGateNum, node): 150 | for shiftNum in range(1, shiftGateNum + 1): 151 | newPositionMap = positionCicleMap(node) 152 | coin = hadamardCoin() 153 | coinMap = coinOperator(coin, positionMap) 154 | for i in range(node): 155 | newPositionMap[i][0][0] += coinMap[i][0][0] 156 | if (i + power(2, shiftNum - 1) >= node): 157 | newPositionMap[i + power(2, shiftNum - 1) - node][1][0] += coinMap[i][1][0] 158 | # print('loop') 159 | else: 160 | newPositionMap[i + power(2, shiftNum - 1)][1][0] += coinMap[i][1][0] 161 | positionMap = newPositionMap 162 | return newPositionMap 163 | 164 | 165 | # 加入phase coin 版本, 修正移位操作,实现complete graph 166 | def shiftCicleWithPhaseOperator(positionMap, step, shiftGateNum, node, Psi, phaseGate): 167 | # 先整体走一步 168 | moveOne = positionMap.copy() 169 | for i in range(1, node): 170 | positionMap[i] = moveOne[i - 1] 171 | positionMap[0] = moveOne[node - 1] 172 | 173 | # 根据延时门再各自走对应步数 174 | for shiftNum in range(1, shiftGateNum + 1): 175 | newPositionMap = positionCicleMap(node) 176 | HadamardCoin = hadamardCoin() 177 | PhaseCoin = phaseCoin(Psi) 178 | coinMap = coinOperator(HadamardCoin, positionMap) 179 | if shiftNum == phaseGate: 180 | newMap = coinMap.copy() 181 | coinMap = coinOperator(PhaseCoin, newMap) 182 | print 'gate:%s phase coin' % shiftNum 183 | for i in range(node): 184 | newPositionMap[i][0][0] += coinMap[i][0][0] 185 | if (i + power(2, shiftNum - 1) >= node): 186 | newPositionMap[i + power(2, shiftNum - 1) - node][1][0] += coinMap[i][1][0] 187 | # print('loop') 188 | else: 189 | newPositionMap[i + power(2, shiftNum - 1)][1][0] += coinMap[i][1][0] 190 | positionMap = newPositionMap 191 | return newPositionMap 192 | 193 | 194 | # phase coin ,complete graph with loop 195 | def shiftCicleWithPhaseLoopOperator(positionMap, step, shiftGateNum, node, Psi, phaseGate): 196 | # 根据延时门各自走对应步数 197 | for shiftNum in range(1, shiftGateNum + 1): 198 | newPositionMap = positionCicleMap(node) 199 | HadamardCoin = hadamardCoin() 200 | PhaseCoin = phaseCoin(Psi) 201 | coinMap = coinOperator(HadamardCoin, positionMap) 202 | if shiftNum == phaseGate: 203 | newMap = coinMap.copy() 204 | coinMap = coinOperator(PhaseCoin, newMap) 205 | print 'gate:%s phase coin' % shiftNum 206 | for i in range(node): 207 | newPositionMap[i][0][0] += coinMap[i][0][0] 208 | if (i + power(2, shiftNum - 1) >= node): 209 | newPositionMap[i + power(2, shiftNum - 1) - node][1][0] += coinMap[i][1][0] 210 | # print('loop') 211 | else: 212 | newPositionMap[i + power(2, shiftNum - 1)][1][0] += coinMap[i][1][0] 213 | positionMap = newPositionMap 214 | return newPositionMap 215 | 216 | 217 | # search position with marked coin ,complete graph with loop for 2 gate,4 node 218 | def shiftCicleWithPhaseLoopSearchOperator(positionMap, step, shiftGateNum, node, Psi, phaseGate): 219 | # 根据延时门各自走对应步数 220 | for shiftNum in range(1, shiftGateNum + 1): 221 | newPositionMap = positionCicleMap(node) 222 | HadamardCoin = hadamardCoin() 223 | # PhaseCoin = phaseCoin(Psi) 224 | coinMap = coinOperator(HadamardCoin, positionMap) 225 | if shiftNum == phaseGate: 226 | # print 'gate:%s phase coin' % shiftNum 227 | newPositionMap[0][0][0] += coinMap[0][0][0] 228 | newPositionMap[4][1][0] += coinMap[0][1][0] * exp(1j * Psi) 229 | newPositionMap[4][0][0] += coinMap[4][0][0] 230 | newPositionMap[0][1][0] += coinMap[4][1][0] * exp(1j * Psi) 231 | for i in range(1, node): 232 | if i == 4: 233 | print i 234 | else: 235 | newPositionMap[i][0][0] += coinMap[i][0][0] 236 | if (i + power(2, shiftNum - 1) >= node): 237 | newPositionMap[i + power(2, shiftNum - 1) - node][1][0] += coinMap[i][1][0] 238 | # print('loop') 239 | else: 240 | newPositionMap[i + power(2, shiftNum - 1)][1][0] += coinMap[i][1][0] 241 | else: 242 | for i in range(node): 243 | newPositionMap[i][0][0] += coinMap[i][0][0] 244 | if (i + power(2, shiftNum - 1) >= node): 245 | newPositionMap[i + power(2, shiftNum - 1) - node][1][0] += coinMap[i][1][0] 246 | # print('loop') 247 | else: 248 | newPositionMap[i + power(2, shiftNum - 1)][1][0] += coinMap[i][1][0] 249 | positionMap = newPositionMap 250 | return newPositionMap 251 | 252 | 253 | # search position with marked point 0,实现complete graph 254 | def shiftCicleWithPhaseSearchOperator(positionMap, step, shiftGateNum, node, Psi, phaseGate): 255 | # 先整体走一步 256 | moveOne = positionMap.copy() 257 | for i in range(1, node): 258 | positionMap[i] = moveOne[i - 1] 259 | positionMap[0] = moveOne[node - 1] 260 | 261 | # 根据延时门再各自走对应步数 262 | for shiftNum in range(1, shiftGateNum + 1): 263 | newPositionMap = positionCicleMap(node) 264 | HadamardCoin = hadamardCoin() 265 | # PhaseCoin = phaseCoin(Psi) 266 | coinMap = coinOperator(HadamardCoin, positionMap) 267 | for i in range(node): 268 | newPositionMap[i][0][0] += coinMap[i][0][0] 269 | if (i + power(2, shiftNum - 1) >= node): 270 | newPositionMap[i + power(2, shiftNum - 1) - node][1][0] += coinMap[i][1][0] 271 | # print('loop') 272 | else: 273 | newPositionMap[i + power(2, shiftNum - 1)][1][0] += coinMap[i][1][0] 274 | positionMap = newPositionMap 275 | # 标记点0,附加一个Psi的相位 276 | newPositionMap[0] = newPositionMap[0] * exp(1j * Psi) 277 | return newPositionMap 278 | 279 | 280 | # quantum walk 的整体封装,返回位置分布的量子态 281 | def quantumWalkCicle(X0, X1, steps, shiftGateNum, node): 282 | print 'begin' 283 | initPositionMap = zeros([node, 2, 1], complex) 284 | initPositionMap[0] = initQuanStat(X0, X1) 285 | for step in range(1, steps + 1): 286 | positionMap = initPositionMap 287 | initPositionMap = shiftCicleOperator(positionMap, step, shiftGateNum, node) 288 | return initPositionMap 289 | 290 | 291 | ''' 292 | Release 1, for animate plotting 293 | ''' 294 | # 计算概率分布 295 | def QWCicleDistribution(X0, X1, steps, shiftGateNum, node): 296 | positionMap = quantumWalkCicle(X0, X1, steps, shiftGateNum, node) 297 | dimension = shape(positionMap)[0] 298 | distribution = zeros([dimension], dtype=float) 299 | sum = 0.0 300 | for i in range(dimension): 301 | distribution[i] = float(positionMap[i][0][0].real ** 2 + positionMap[i][0][0].imag ** 2 + \ 302 | positionMap[i][1][0].real ** 2 + positionMap[i][1][0].imag ** 2) 303 | sum += distribution[i] 304 | print('sum: %s') % sum 305 | return distribution 306 | 307 | 308 | ''' 309 | Release 2 : for write data to txt file 310 | ''' 311 | # quantum walk in cycle graph release for write data to txt file(直接将每一步的态与分布写到文件保存) 312 | def QWCicleDistrWrite(X0, X1, steps, shiftGateNum, node): 313 | print 'begin' 314 | initPositionMap = zeros([node, 2, 1], complex) 315 | initPositionMap[0] = initQuanStat(X0, X1) 316 | DsitriFilename = 'Data/' + time.strftime('%Y%m%d-%H-%M-%S_') + 'Position' + str(shiftGateNum) + \ 317 | str(node) + str(steps) + '.txt' 318 | StateFilename = 'Data/' + time.strftime('%Y%m%d-%H-%M-%S_') + 'State' + str(shiftGateNum) + \ 319 | str(node) + str(steps) + '.txt' 320 | distrFile = open(DsitriFilename, 'w+') 321 | stateFile = open(StateFilename, 'w+') 322 | for step in range(1, steps + 1): 323 | print 'step: %s' % step 324 | positionMap = initPositionMap 325 | initPositionMap = shiftCicleOperator(positionMap, step, shiftGateNum, node) 326 | dimension = shape(initPositionMap)[0] 327 | distribution = zeros([dimension], dtype=float) 328 | sum = 0.0 329 | for i in range(dimension): 330 | distribution[i] = float(initPositionMap[i][0][0].real ** 2 + initPositionMap[i][0][0].imag ** 2 + \ 331 | initPositionMap[i][1][0].real ** 2 + initPositionMap[i][1][0].imag ** 2) 332 | sum += distribution[i] 333 | stateFile.write(str(initPositionMap[i][0][0]) + str(initPositionMap[i][1][0]) + '\t') 334 | distrFile.write(str(distribution[i]) + '\t') 335 | print('sum: %s') % sum 336 | distrFile.write('\n') 337 | stateFile.write('\n') 338 | stateFile.close() 339 | distrFile.close() 340 | print 'finish' 341 | 342 | 343 | ''' 344 | Release 3 : add phase coin and modified for complete graph 345 | ''' 346 | # quantum walk in cycle graph release for phase coin (添加phase coin,对shift operator进行了修正) 347 | def QWCicleWithPhaseDistrWrite(X0, X1, steps, shiftGateNum, node, Psi, gate): 348 | print 'begin' 349 | initPositionMap = zeros([node, 2, 1], complex) 350 | initPositionMap[0] = initQuanStat(X0, X1) 351 | # initPositionMap = initQuanStatAverPosiCicle(X0, X1,node) 352 | PositionPsiFilename = 'Data/K5_X0' + str(X0) + '_X1' + str(X1) + '_' + str(shiftGateNum) + '-' + str( 353 | node) + '_Steps' + str(steps) + '.txt' 354 | PosiFile = open(PositionPsiFilename, 'a') 355 | # DsitriFilename = 'Data/' + time.strftime('%Y%m%d-%H-%M-%S') + '_Position_' + str(shiftGateNum) + \ 356 | # str(node) + str(steps) + '.txt' 357 | # StateFilename = 'Data/' + time.strftime('%Y%m%d-%H-%M-%S') + '_State_' + str(shiftGateNum) + \ 358 | # str(node) + str(steps) + '.txt' 359 | # distrFile = open(DsitriFilename, 'w+') 360 | # stateFile = open(StateFilename, 'w+') 361 | # infoS = 'State: X0 %s X1 %s, steps: %s, shiftGateNum: %s, node: %s, Psi: %s, gate: %s\n' % ( 362 | # X0, X1, steps, shiftGateNum, node, Psi, gate) 363 | # infoD = 'State: X0 %s X1 %s, steps: %s, shiftGateNum: %s, node: %s, Psi: %s, gate: %s \n' % ( 364 | # X0, X1, steps, shiftGateNum, node, Psi, gate) 365 | # stateFile.write(infoS) 366 | # distrFile.write(infoD) 367 | for step in range(1, steps + 1): 368 | print 'step: %s' % step 369 | positionMap = initPositionMap 370 | initPositionMap = shiftCicleWithPhaseOperator(positionMap, step, shiftGateNum, node, Psi, gate) 371 | dimension = shape(initPositionMap)[0] 372 | distribution = zeros([dimension], dtype=float) 373 | sum = 0.0 374 | PosiFile.write(str(Psi) + '\t') 375 | PosiFile.write(str(step) + '\t') 376 | for i in range(dimension): 377 | distribution[i] = float(initPositionMap[i][0][0].real ** 2 + initPositionMap[i][0][0].imag ** 2 + \ 378 | initPositionMap[i][1][0].real ** 2 + initPositionMap[i][1][0].imag ** 2) 379 | sum += distribution[i] 380 | PosiFile.write(str(distribution[i]) + '\t') 381 | # stateFile.write(str(initPositionMap[i][0][0]) + str(initPositionMap[i][1][0]) + '\t') 382 | # distrFile.write(str(distribution[i]) + '\t') 383 | PosiFile.write('\n') 384 | print('sum: %s') % sum 385 | # distrFile.write('\n') 386 | # stateFile.write('\n') 387 | # stateFile.close() 388 | # distrFile.close() 389 | PosiFile.close() 390 | print 'finish' 391 | 392 | 393 | ''' 394 | Release 4 : add phase coin and modified for complete graph with loop 395 | ''' 396 | 397 | 398 | # quantum walk in cycle graph with loop release for phase coin 399 | def QWCicleWithPhaseLoopDistrWrite(X0, X1, steps, shiftGateNum, node, Psi, gate): 400 | print 'begin' 401 | initPositionMap = zeros([node, 2, 1], complex) 402 | initPositionMap[0] = initQuanStat(X0, X1) 403 | #initPositionMap=initQuanStatAverPosiCicle(X0,X1,node) 404 | DsitriFilename = 'Data/' + time.strftime('%Y%m%d-%H-%M-%S') + '_Position_' + str(shiftGateNum) + \ 405 | str(node) + str(steps) + '.txt' 406 | StateFilename = 'Data/' + time.strftime('%Y%m%d-%H-%M-%S') + '_State_' + str(shiftGateNum) + \ 407 | str(node) + str(steps) + '.txt' 408 | distrFile = open(DsitriFilename, 'w+') 409 | stateFile = open(StateFilename, 'w+') 410 | infoS = 'State: X0 %s X1 %s, steps: %s, shiftGateNum: %s, node: %s, Psi: %s, gate: %s\n' % ( 411 | X0, X1, steps, shiftGateNum, node, Psi, gate) 412 | infoD = 'State: X0 %s X1 %s, steps: %s, shiftGateNum: %s, node: %s, Psi: %s, gate: %s \n' % ( 413 | X0, X1, steps, shiftGateNum, node, Psi, gate) 414 | stateFile.write(infoS) 415 | distrFile.write(infoD) 416 | for step in range(1, steps + 1): 417 | print 'step: %s' % step 418 | positionMap = initPositionMap 419 | initPositionMap = shiftCicleWithPhaseLoopOperator(positionMap, step, shiftGateNum, node, Psi, gate) 420 | dimension = shape(initPositionMap)[0] 421 | distribution = zeros([dimension], dtype=float) 422 | sum = 0.0 423 | for i in range(dimension): 424 | distribution[i] = float(initPositionMap[i][0][0].real ** 2 + initPositionMap[i][0][0].imag ** 2 + \ 425 | initPositionMap[i][1][0].real ** 2 + initPositionMap[i][1][0].imag ** 2) 426 | sum += distribution[i] 427 | stateFile.write(str(initPositionMap[i][0][0]) + str(initPositionMap[i][1][0]) + '\t') 428 | distrFile.write(str(distribution[i]) + '\t') 429 | print('sum: %s') % sum 430 | distrFile.write('\n') 431 | stateFile.write('\n') 432 | stateFile.close() 433 | distrFile.close() 434 | print 'finish' 435 | 436 | 437 | ''' 438 | Release 5 : add phase coin and modified for complete graph with loop, write position 439 | probability file with Psi 440 | ''' 441 | 442 | 443 | # quantum walk in cycle graph with loop release for phase coin 444 | def QWCicleWithPhaseLoopDistrWritePosi(X0, X1, steps, shiftGateNum, node, Psi, gate): 445 | print 'begin' 446 | initPositionMap = zeros([node, 2, 1], complex) 447 | # initPositionMap[0] = initQuanStat(X0, X1) 448 | initPositionMap = initQuanStatAverPosiCicle(X0, X1, node) 449 | PositionPsiFilename = 'Data/Loop_X0' + str(X0) + '_X1' + str(X1) + '_' + str(shiftGateNum) + '-' + str( 450 | node) + '_Steps' + str(steps) + 'Aver.txt' 451 | PosiFile = open(PositionPsiFilename, 'a') 452 | for step in range(1, steps + 1): 453 | print 'step: %s' % step 454 | positionMap = initPositionMap 455 | initPositionMap = shiftCicleWithPhaseLoopOperator(positionMap, step, shiftGateNum, node, Psi, gate) 456 | dimension = shape(initPositionMap)[0] 457 | distribution = zeros([dimension], dtype=float) 458 | sum = 0.0 459 | for i in range(dimension): 460 | distribution[i] = float(initPositionMap[i][0][0].real ** 2 + initPositionMap[i][0][0].imag ** 2 + \ 461 | initPositionMap[i][1][0].real ** 2 + initPositionMap[i][1][0].imag ** 2) 462 | sum += distribution[i] 463 | if i == 0: 464 | PosiFile.write(str(Psi) + '\t') 465 | PosiFile.write(str(step) + '\t') 466 | PosiFile.write(str(distribution[i]) + '\n') 467 | print('sum: %s') % sum 468 | PosiFile.close() 469 | print 'finish' 470 | 471 | 472 | ''' 473 | Release 6 : Search one position by marked in complete graph with loop 474 | ''' 475 | 476 | 477 | # quantum walk search in cycle graph with loop 478 | def QWCicleWithPhaseLoopSearch(X0, X1, steps, shiftGateNum, node, Psi, gate): 479 | print 'begin' 480 | initPositionMap = zeros([node, 2, 1], complex) 481 | initPositionMap[0] = initQuanStat(X0, X1) 482 | # initPositionMap=initQuanStatAverPosiCicle(X0,X1,node) 483 | # DsitriFilename = 'Data/' + time.strftime('%Y%m%d-%H-%M-%S') + '_Search_Position_' + str(shiftGateNum) + \ 484 | # str(node) + str(steps) + '.txt' 485 | # StateFilename = 'Data/' + time.strftime('%Y%m%d-%H-%M-%S') + '_Search_State_' + str(shiftGateNum) + \ 486 | # str(node) + str(steps) + '.txt' 487 | # distrFile = open(DsitriFilename, 'w+') 488 | # stateFile = open(StateFilename, 'w+') 489 | # infoS = 'Search State: X0 %s X1 %s, steps: %s, shiftGateNum: %s, node: %s, Psi: %s, gate: %s\n' % ( 490 | # X0, X1, steps, shiftGateNum, node, Psi, gate) 491 | # infoD = 'Search State: X0 %s X1 %s, steps: %s, shiftGateNum: %s, node: %s, Psi: %s, gate: %s \n' % ( 492 | # X0, X1, steps, shiftGateNum, node, Psi, gate) 493 | # stateFile.write(infoS) 494 | # distrFile.write(infoD) 495 | PositionPsiFilename = 'Data/Loop_Search_X0' + str(X0) + '_X1' + str(X1) + '_' + str(shiftGateNum) + '-' + str( 496 | node) + '_Steps' + str(steps) + '.txt' 497 | PosiFile = open(PositionPsiFilename, 'a') 498 | for step in range(1, steps + 1): 499 | print 'step: %s' % step 500 | positionMap = initPositionMap 501 | initPositionMap = shiftCicleWithPhaseLoopSearchOperator(positionMap, step, shiftGateNum, node, Psi, gate) 502 | dimension = shape(initPositionMap)[0] 503 | distribution = zeros([dimension], dtype=float) 504 | sum = 0.0 505 | for i in range(dimension): 506 | distribution[i] = float(initPositionMap[i][0][0].real ** 2 + initPositionMap[i][0][0].imag ** 2 + \ 507 | initPositionMap[i][1][0].real ** 2 + initPositionMap[i][1][0].imag ** 2) 508 | sum += distribution[i] 509 | if i == 1: 510 | PosiFile.write(str(Psi) + '\t') 511 | PosiFile.write(str(step) + '\t') 512 | PosiFile.write(str(distribution[i]) + '\n') 513 | # stateFile.write(str(initPositionMap[i][0][0]) + str(initPositionMap[i][1][0]) + '\t') 514 | # distrFile.write(str(distribution[i]) + '\t') 515 | print('sum: %s') % sum 516 | # distrFile.write('\n') 517 | # stateFile.write('\n') 518 | # stateFile.close() 519 | # distrFile.close() 520 | PosiFile.close() 521 | print 'finish' 522 | 523 | 524 | ''' 525 | Release 7 : Search one position by marked in complete graph 526 | ''' 527 | 528 | 529 | # quantum walk in cycle graph release for phase coin (添加phase coin,对shift operator进行了修正) 530 | def QWCicleWithPhaseSearch(X0, X1, steps, shiftGateNum, node, Psi, gate): 531 | print 'begin' 532 | initPositionMap = zeros([node, 2, 1], complex) 533 | # initPositionMap[0] = initQuanStat(X0, X1) 534 | initPositionMap = initQuanStatAverPosiCicle(X0, X1, node) 535 | PositionPsiFilename = 'Data/Search mark_0 X0' + str(X0) + '_X1' + str(X1) + '_' + str(shiftGateNum) + '-' + str( 536 | node) + '_Steps' + str(steps) + 'Aver.txt' 537 | PosiFile = open(PositionPsiFilename, 'a') 538 | for step in range(1, steps + 1): 539 | print 'step: %s' % step 540 | positionMap = initPositionMap 541 | initPositionMap = shiftCicleWithPhaseSearchOperator(positionMap, step, shiftGateNum, node, Psi, gate) 542 | dimension = shape(initPositionMap)[0] 543 | distribution = zeros([dimension], dtype=float) 544 | sum = 0.0 545 | PosiFile.write(str(Psi) + '\t') 546 | PosiFile.write(str(step) + '\t') 547 | for i in range(dimension): 548 | distribution[i] = float(initPositionMap[i][0][0].real ** 2 + initPositionMap[i][0][0].imag ** 2 + \ 549 | initPositionMap[i][1][0].real ** 2 + initPositionMap[i][1][0].imag ** 2) 550 | sum += distribution[i] 551 | PosiFile.write(str(distribution[i]) + '\t') 552 | print('sum: %s') % sum 553 | PosiFile.write('\n') 554 | PosiFile.close() 555 | print 'finish' 556 | 557 | # 对位置列表进行位移,使原点保持不变 558 | #TODO 位置变换有点问题,暂时不用 559 | def ciclePosiTrans(positionMap, steps, shiftGateNum): 560 | node = shape(positionMap)[0] 561 | positionMapTrans = zeros([node], float) 562 | initNode = (power(2, shiftGateNum) - 1) * steps % node 563 | for i in range(node): 564 | positionMapTrans[i] = positionMap[(initNode + i) % node] 565 | return positionMapTrans 566 | 567 | 568 | # 画出2D位置分布图 569 | def PlotCicle(distribution, steps, shiftGateNum, figName): 570 | node = shape(distribution)[0] 571 | radius = 1 572 | x = zeros([node], float) 573 | y = zeros([node], float) 574 | for i in range(node): 575 | x[i] = sin(i * 2 * math.pi / node) * radius 576 | y[i] = cos(i * 2 * math.pi / node) * radius 577 | 578 | plt.figure(1, figsize=(20, 9)) 579 | ax1 = plt.subplot(121) 580 | plt.sca(ax1) 581 | plt.title('Distribution of %s steps Quantum Walk in Cicle' % steps) 582 | plt.xlabel('X Position') 583 | plt.ylabel('Y Position') 584 | # plt.imshow(positionMap) 585 | # plt.axis([0, 2 * int(radius) + 3, 0, 2 * int(radius) + 3]) 586 | # plt.grid(True) 587 | plt.scatter(x, y, distribution * 2000, c='r', marker='o') 588 | plt.axis([-1.5, 1.5, -1.5, 1.5]) 589 | plt.subplot(122) 590 | plt.plot(distribution) 591 | plt.axis([0, node - 1, 0, 1]) 592 | plt.title('Distribution of %s steps QW with Different Shift' % steps) 593 | plt.xlabel('Started from 0') 594 | plt.ylabel('Probability') 595 | # plt.show() 596 | plt.savefig('Fig/' + str(figName) + str(steps) + '_' + str(shiftGateNum) + '.png') 597 | plt.close() 598 | -------------------------------------------------------------------------------- /QWithDiffShiftTest.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | __author__ = 'levitan' 4 | 5 | from numpy import * 6 | from matplotlib import pyplot as plt 7 | import QWithDiffShift as QDS 8 | 9 | 10 | # distribution=QDS.QWDistribution(1/sqrt(2),1j/sqrt(2),700,1) 11 | # QDS.PlotX(distribution) 12 | 13 | def QDSPlot(X0, X1, steps, shiftGateNum): 14 | for step in range(1, steps + 1): 15 | distributionQDS = QDS.QWDistribution(X0, X1, step, shiftGateNum) 16 | distributionQW = QDS.QWDistribution(X0, X1, 7 * step, 1) 17 | QDS.PlotComp(distributionQDS, distributionQW, step, 'QDS-VS-QW-H') 18 | 19 | 20 | def QDSCPlot(X0, X1, steps, shiftGateNum, node): 21 | for step in range(1, steps + 1): 22 | distribution = QDS.QWCicleDistribution(X0, X1, step, shiftGateNum, node) 23 | distributionTrans = QDS.ciclePosiTrans(distribution, step, shiftGateNum) 24 | QDS.PlotX(distributionTrans, step, 'QDS_CT') 25 | 26 | # QDSPlot(1/sqrt(2),1j/sqrt(2),15,3) 27 | # QDSCPlot(1/sqrt(2),1j/sqrt(2),15,3,50) 28 | 29 | def QDSCiclePlot(X0, X1, steps, shiftGateNum, node): 30 | for step in range(1, steps + 1): 31 | distribution = QDS.QWCicleDistribution(X0, X1, step, shiftGateNum, node) 32 | distributionTrans = QDS.ciclePosiTrans(distribution, step, shiftGateNum) 33 | QDS.PlotCicle(distributionTrans, step, shiftGateNum, 'QDSCicle_K4_H_') 34 | 35 | 36 | def QDSCicleContourfPlot(Filename, walkSteps, PiSteps, node, point): 37 | # x,y=ogrid[0:50:PiSteps,1:200:walkSteps] 38 | x = arange(0, 2 * pi, 2 * pi / PiSteps) 39 | y = arange(0, walkSteps, 1) 40 | v = linspace(0, 1, 50, endpoint=True) 41 | # print x,shape(x) 42 | file = open(Filename, 'r') 43 | dataList = zeros([walkSteps * PiSteps, node]) 44 | i = 0 45 | for line in file.readlines(): 46 | for j in range(node): 47 | dataList[i][j] = line.split('\t')[2 + j] 48 | # print dataList[i] 49 | i += 1 50 | # print dataList[2] 51 | z = zeros([walkSteps, PiSteps], float) 52 | for yi in range(walkSteps): 53 | for xi in range(PiSteps): 54 | z[yi][xi] = float(dataList[xi * walkSteps + yi][point]) 55 | file.close() 56 | plt.figure() 57 | plt.title('Distribution of %s steps Quantum Walk in Cicle with different phase' % walkSteps) 58 | plt.xlabel('Phase') 59 | plt.ylabel('Step') 60 | plt.contourf(x, y, z, v, cmap=plt.cm.jet) 61 | x = plt.colorbar(ticks=v) 62 | print x 63 | plt.show() 64 | 65 | 66 | # QDSCiclePlot(1, 0, 10, 2, 4) 67 | # QDSPlot(1 , 0, 10, 3) 68 | # QDS.QWCicleDistrWrite(1, 0, 2000, 3, 8) 69 | # QDS.QWCicleWithPhaseDistrWrite(1/sqrt(2), 1/sqrt(2), 400, 2, 5, 0, 2) 70 | # QDS.QWCicleWithPhaseLoopDistrWrite(1/sqrt(2), 1j/sqrt(2), 200, 2, 4, pi, 2) 71 | # QDS.QWCicleWithPhaseLoopSearch(1/sqrt(2), 1/sqrt(2), 200, 3, 8, pi, 3) 72 | 73 | # for i in range(51): 74 | # QDS.QWCicleWithPhaseLoopSearch(1, 0, 200, 3, 8, 2*pi*i/50, 3) 75 | 76 | # for i in range(51): 77 | # QDS.QWCicleWithPhaseLoopDistrWritePosi(1/sqrt(2), 1/sqrt(2), 200, 2, 4, 2*pi*i/50, 2) 78 | 79 | # for i in range(51): 80 | # QDS.QWCicleWithPhaseDistrWrite(1/sqrt(2), 1/sqrt(2), 200, 2, 5, 2*pi*i/50, 2) 81 | 82 | # for i in range(51): 83 | # QDS.QWCicleWithPhaseSearch(1/sqrt(2), 1/sqrt(2), 200, 2, 4, 2*pi*i/50, 2) 84 | 85 | QDSCicleContourfPlot('Data/Search mark_0 X00.707106781187_X10.707106781187_2-4_Steps200Aver.txt', 200, 51,4,2) 86 | -------------------------------------------------------------------------------- /Quantum Walk in Graph K5.nb: -------------------------------------------------------------------------------- 1 | (* Content-type: application/vnd.wolfram.mathematica *) 2 | 3 | (*** Wolfram Notebook File ***) 4 | (* http://www.wolfram.com/nb *) 5 | 6 | (* CreatedBy='Mathematica 8.0' *) 7 | 8 | (*CacheID: 234*) 9 | (* Internal cache information: 10 | NotebookFileLineBreakTest 11 | NotebookFileLineBreakTest 12 | NotebookDataPosition[ 157, 7] 13 | NotebookDataLength[ 25108, 672] 14 | NotebookOptionsPosition[ 24822, 658] 15 | NotebookOutlinePosition[ 25167, 673] 16 | CellTagsIndexPosition[ 25124, 670] 17 | WindowFrame->Normal*) 18 | 19 | (* Beginning of Notebook Content *) 20 | Notebook[{ 21 | Cell[BoxData[ 22 | RowBox[{ 23 | RowBox[{"(*", 24 | RowBox[{ 25 | RowBox[{ 26 | RowBox[{"--", 27 | RowBox[{"--", 28 | RowBox[{"--", 29 | RowBox[{"--", 30 | RowBox[{"--", 31 | RowBox[{"--", 32 | RowBox[{"--", 33 | RowBox[{"--", 34 | RowBox[{"--", 35 | RowBox[{"--", 36 | RowBox[{"--", 37 | RowBox[{"--", 38 | RowBox[{"--", 39 | RowBox[{"--", 40 | RowBox[{"--", 41 | RowBox[{"--", 42 | RowBox[{"--", 43 | RowBox[{"--", 44 | RowBox[{"--", 45 | RowBox[{"--", 46 | RowBox[{"--", 47 | RowBox[{"--", 48 | RowBox[{"--", 49 | RowBox[{ 50 | "-", "\[IndentingNewLine]", 51 | "Quantum"}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}], 52 | " ", "Walk", " ", "Matrix", " ", 53 | RowBox[{"Simulation", " ", ":", " ", 54 | RowBox[{"Graph", " ", 55 | RowBox[{"K5", "\[IndentingNewLine]", "--"}], 56 | RowBox[{"--", 57 | RowBox[{"--", 58 | RowBox[{"--", 59 | RowBox[{"--", 60 | RowBox[{"--", 61 | RowBox[{"--", 62 | RowBox[{"--", 63 | RowBox[{"--", 64 | RowBox[{"-", 65 | RowBox[{"-", 66 | RowBox[{"-", 67 | RowBox[{"--", 68 | RowBox[{"--", 69 | RowBox[{"--", 70 | RowBox[{"--", 71 | RowBox[{"--", 72 | RowBox[{"--", 73 | RowBox[{"--", 74 | RowBox[{"--", 75 | RowBox[{"-", 76 | RowBox[{"--", 77 | RowBox[{"--", 78 | RowBox[{"-", 79 | RowBox[{"-", 80 | RowBox[{"--", 81 | RowBox[{"--", 82 | RowBox[{"--", 83 | RowBox[{"--", 84 | RowBox[{"--", 85 | RowBox[{"-", "\[IndentingNewLine]", 86 | RowBox[{"Shift", ":", " ", 87 | RowBox[{ 88 | "Shift", " ", 89 | "Matrix"}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}] 90 | }]}]}]}]}]}]}]}]}]}], ",", " ", 91 | RowBox[{"Coin", ":", " ", 92 | RowBox[{"Coin", " ", "Matrix"}]}], ",", " ", 93 | RowBox[{"\[Psi]", ":", 94 | RowBox[{"Quantum", " ", "State"}]}], ",", " ", 95 | RowBox[{"Posi", ":", 96 | RowBox[{"Position", " ", "State"}]}]}], "\[IndentingNewLine]", "*)"}], 97 | "\[IndentingNewLine]", "\[IndentingNewLine]", 98 | RowBox[{ 99 | RowBox[{ 100 | RowBox[{"\[Psi]", "=", 101 | RowBox[{ 102 | RowBox[{"{", 103 | RowBox[{ 104 | RowBox[{"{", "1", "}"}], ",", 105 | RowBox[{"{", "0", "}"}]}], "}"}], "//", "MatrixForm"}]}], " ", ";"}], 106 | "\[IndentingNewLine]", 107 | RowBox[{ 108 | RowBox[{"Posi", "=", 109 | RowBox[{ 110 | RowBox[{"{", 111 | RowBox[{ 112 | RowBox[{"{", "1", "}"}], ",", 113 | RowBox[{"{", "0", "}"}], ",", 114 | RowBox[{"{", "0", "}"}], ",", 115 | RowBox[{"{", "0", "}"}], ",", 116 | RowBox[{"{", "0", "}"}]}], "}"}], "//", "MatrixForm"}]}], ";"}], 117 | "\[IndentingNewLine]", 118 | RowBox[{ 119 | RowBox[{"ShiftMoveOne", "=", 120 | RowBox[{ 121 | RowBox[{"Transpose", "[", 122 | RowBox[{"{", 123 | RowBox[{ 124 | RowBox[{"{", 125 | RowBox[{ 126 | "0", ",", "1", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 127 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 128 | RowBox[{"{", 129 | RowBox[{ 130 | "0", ",", "0", ",", "1", ",", "0", ",", "0", ",", "0", ",", "0", 131 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 132 | RowBox[{"{", 133 | RowBox[{ 134 | "0", ",", "0", ",", "0", ",", "1", ",", "0", ",", "0", ",", "0", 135 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 136 | RowBox[{"{", 137 | RowBox[{ 138 | "0", ",", "0", ",", "0", ",", "0", ",", "1", ",", "0", ",", "0", 139 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 140 | RowBox[{"{", 141 | RowBox[{ 142 | "1", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 143 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 144 | RowBox[{"{", 145 | RowBox[{ 146 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "1", 147 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 148 | RowBox[{"{", 149 | RowBox[{ 150 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 151 | ",", "1", ",", "0", ",", "0"}], "}"}], ",", 152 | RowBox[{"{", 153 | RowBox[{ 154 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 155 | ",", "0", ",", "1", ",", "0"}], "}"}], ",", 156 | RowBox[{"{", 157 | RowBox[{ 158 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 159 | ",", "0", ",", "0", ",", "1"}], "}"}], ",", 160 | RowBox[{"{", 161 | RowBox[{ 162 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "1", ",", "0", 163 | ",", "0", ",", "0", ",", "0"}], "}"}]}], "}"}], "]"}], "//", 164 | "MatrixForm"}]}], ";"}], "\[IndentingNewLine]", 165 | RowBox[{ 166 | RowBox[{"Shift1", "=", 167 | RowBox[{ 168 | RowBox[{"Transpose", "[", 169 | RowBox[{"{", 170 | RowBox[{ 171 | RowBox[{"{", 172 | RowBox[{ 173 | "1", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 174 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 175 | RowBox[{"{", 176 | RowBox[{ 177 | "0", ",", "1", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 178 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 179 | RowBox[{"{", 180 | RowBox[{ 181 | "0", ",", "0", ",", "1", ",", "0", ",", "0", ",", "0", ",", "0", 182 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 183 | RowBox[{"{", 184 | RowBox[{ 185 | "0", ",", "0", ",", "0", ",", "1", ",", "0", ",", "0", ",", "0", 186 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 187 | RowBox[{"{", 188 | RowBox[{ 189 | "0", ",", "0", ",", "0", ",", "0", ",", "1", ",", "0", ",", "0", 190 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 191 | RowBox[{"{", 192 | RowBox[{ 193 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "1", 194 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 195 | RowBox[{"{", 196 | RowBox[{ 197 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 198 | ",", "1", ",", "0", ",", "0"}], "}"}], ",", 199 | RowBox[{"{", 200 | RowBox[{ 201 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 202 | ",", "0", ",", "1", ",", "0"}], "}"}], ",", 203 | RowBox[{"{", 204 | RowBox[{ 205 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 206 | ",", "0", ",", "0", ",", "1"}], "}"}], ",", 207 | RowBox[{"{", 208 | RowBox[{ 209 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "1", ",", "0", 210 | ",", "0", ",", "0", ",", "0"}], "}"}]}], "}"}], "]"}], "//", 211 | "MatrixForm"}]}], ";"}], "\[IndentingNewLine]", 212 | RowBox[{ 213 | RowBox[{"Shift2", "=", 214 | RowBox[{ 215 | RowBox[{"Transpose", "[", 216 | RowBox[{"{", 217 | RowBox[{ 218 | RowBox[{"{", 219 | RowBox[{ 220 | "1", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 221 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 222 | RowBox[{"{", 223 | RowBox[{ 224 | "0", ",", "1", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 225 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 226 | RowBox[{"{", 227 | RowBox[{ 228 | "0", ",", "0", ",", "1", ",", "0", ",", "0", ",", "0", ",", "0", 229 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 230 | RowBox[{"{", 231 | RowBox[{ 232 | "0", ",", "0", ",", "0", ",", "1", ",", "0", ",", "0", ",", "0", 233 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 234 | RowBox[{"{", 235 | RowBox[{ 236 | "0", ",", "0", ",", "0", ",", "0", ",", "1", ",", "0", ",", "0", 237 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 238 | RowBox[{"{", 239 | RowBox[{ 240 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 241 | ",", "1", ",", "0", ",", "0"}], "}"}], ",", 242 | RowBox[{"{", 243 | RowBox[{ 244 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 245 | ",", "0", ",", "1", ",", "0"}], "}"}], ",", 246 | RowBox[{"{", 247 | RowBox[{ 248 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", 249 | ",", "0", ",", "0", ",", "1"}], "}"}], ",", 250 | RowBox[{"{", 251 | RowBox[{ 252 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "1", ",", "0", 253 | ",", "0", ",", "0", ",", "0"}], "}"}], ",", 254 | RowBox[{"{", 255 | RowBox[{ 256 | "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "1", 257 | ",", "0", ",", "0", ",", "0"}], "}"}]}], "}"}], "]"}], "//", 258 | "MatrixForm"}]}], ";"}], "\[IndentingNewLine]", 259 | RowBox[{ 260 | RowBox[{"HadamardCoin1", "=", 261 | RowBox[{ 262 | TagBox[ 263 | RowBox[{"(", "\[NoBreak]", GridBox[{ 264 | { 265 | FractionBox["1", 266 | SqrtBox["2"]], "0", "0", "0", "0", 267 | FractionBox["1", 268 | SqrtBox["2"]], "0", "0", "0", "0"}, 269 | {"0", 270 | FractionBox["1", 271 | SqrtBox["2"]], "0", "0", "0", "0", 272 | FractionBox["1", 273 | SqrtBox["2"]], "0", "0", "0"}, 274 | {"0", "0", 275 | FractionBox["1", 276 | SqrtBox["2"]], "0", "0", "0", "0", 277 | FractionBox["1", 278 | SqrtBox["2"]], "0", "0"}, 279 | {"0", "0", "0", 280 | FractionBox["1", 281 | SqrtBox["2"]], "0", "0", "0", "0", 282 | FractionBox["1", 283 | SqrtBox["2"]], "0"}, 284 | {"0", "0", "0", "0", 285 | FractionBox["1", 286 | SqrtBox["2"]], "0", "0", "0", "0", 287 | FractionBox["1", 288 | SqrtBox["2"]]}, 289 | { 290 | FractionBox["1", 291 | SqrtBox["2"]], "0", "0", "0", "0", 292 | RowBox[{"-", 293 | FractionBox["1", 294 | SqrtBox["2"]]}], "0", "0", "0", "0"}, 295 | {"0", 296 | FractionBox["1", 297 | SqrtBox["2"]], "0", "0", "0", "0", 298 | RowBox[{"-", 299 | FractionBox["1", 300 | SqrtBox["2"]]}], "0", "0", "0"}, 301 | {"0", "0", 302 | FractionBox["1", 303 | SqrtBox["2"]], "0", "0", "0", "0", 304 | RowBox[{"-", 305 | FractionBox["1", 306 | SqrtBox["2"]]}], "0", "0"}, 307 | {"0", "0", "0", 308 | FractionBox["1", 309 | SqrtBox["2"]], "0", "0", "0", "0", 310 | RowBox[{"-", 311 | FractionBox["1", 312 | SqrtBox["2"]]}], "0"}, 313 | {"0", "0", "0", "0", 314 | FractionBox["1", 315 | SqrtBox["2"]], "0", "0", "0", "0", 316 | RowBox[{"-", 317 | FractionBox["1", 318 | SqrtBox["2"]]}]} 319 | }, 320 | GridBoxAlignment -> { 321 | "Columns" -> {{Center}}, "ColumnsIndexed" -> {}, 322 | "Rows" -> {{Baseline}}, "RowsIndexed" -> {}}, 323 | GridBoxSpacings -> {"Columns" -> { 324 | Offset[0.27999999999999997`], { 325 | Offset[0.7]}, 326 | Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { 327 | Offset[0.2], { 328 | Offset[0.4]}, 329 | Offset[0.2]}, "RowsIndexed" -> {}}], "\[NoBreak]", ")"}], 330 | Function[BoxForm`e$, 331 | MatrixForm[BoxForm`e$]]], "//", "MatrixForm"}]}], ";"}], 332 | "\[IndentingNewLine]", 333 | RowBox[{ 334 | RowBox[{"HadamardCoin2", "=", 335 | RowBox[{ 336 | TagBox[ 337 | RowBox[{"(", "\[NoBreak]", GridBox[{ 338 | { 339 | FractionBox["1", 340 | SqrtBox["2"]], "0", "0", "0", "0", 341 | FractionBox["1", 342 | SqrtBox["2"]], "0", "0", "0", "0"}, 343 | {"0", 344 | FractionBox["1", 345 | SqrtBox["2"]], "0", "0", "0", "0", 346 | FractionBox["1", 347 | SqrtBox["2"]], "0", "0", "0"}, 348 | {"0", "0", 349 | FractionBox["1", 350 | SqrtBox["2"]], "0", "0", "0", "0", 351 | FractionBox["1", 352 | SqrtBox["2"]], "0", "0"}, 353 | {"0", "0", "0", 354 | FractionBox["1", 355 | SqrtBox["2"]], "0", "0", "0", "0", 356 | FractionBox["1", 357 | SqrtBox["2"]], "0"}, 358 | {"0", "0", "0", "0", 359 | FractionBox["1", 360 | SqrtBox["2"]], "0", "0", "0", "0", 361 | FractionBox["1", 362 | SqrtBox["2"]]}, 363 | { 364 | FractionBox["1", 365 | SqrtBox["2"]], "0", "0", "0", "0", 366 | RowBox[{"-", 367 | FractionBox["1", 368 | SqrtBox["2"]]}], "0", "0", "0", "0"}, 369 | {"0", 370 | FractionBox["1", 371 | SqrtBox["2"]], "0", "0", "0", "0", 372 | RowBox[{"-", 373 | FractionBox["1", 374 | SqrtBox["2"]]}], "0", "0", "0"}, 375 | {"0", "0", 376 | FractionBox["1", 377 | SqrtBox["2"]], "0", "0", "0", "0", 378 | RowBox[{"-", 379 | FractionBox["1", 380 | SqrtBox["2"]]}], "0", "0"}, 381 | {"0", "0", "0", 382 | FractionBox["1", 383 | SqrtBox["2"]], "0", "0", "0", "0", 384 | RowBox[{"-", 385 | FractionBox["1", 386 | SqrtBox["2"]]}], "0"}, 387 | {"0", "0", "0", "0", 388 | FractionBox["1", 389 | SqrtBox["2"]], "0", "0", "0", "0", 390 | RowBox[{"-", 391 | FractionBox["1", 392 | SqrtBox["2"]]}]} 393 | }, 394 | GridBoxAlignment -> { 395 | "Columns" -> {{Center}}, "ColumnsIndexed" -> {}, 396 | "Rows" -> {{Baseline}}, "RowsIndexed" -> {}}, 397 | GridBoxSpacings -> {"Columns" -> { 398 | Offset[0.27999999999999997`], { 399 | Offset[0.7]}, 400 | Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { 401 | Offset[0.2], { 402 | Offset[0.4]}, 403 | Offset[0.2]}, "RowsIndexed" -> {}}], "\[NoBreak]", ")"}], 404 | Function[BoxForm`e$, 405 | MatrixForm[BoxForm`e$]]], "//", "MatrixForm"}]}], ";"}], 406 | "\[IndentingNewLine]", 407 | RowBox[{ 408 | RowBox[{"I2", "=", 409 | RowBox[{ 410 | RowBox[{"{", 411 | RowBox[{ 412 | RowBox[{"{", 413 | RowBox[{"1", ",", "0"}], "}"}], ",", 414 | RowBox[{"{", 415 | RowBox[{"0", ",", "1"}], "}"}]}], "}"}], "//", "MatrixForm"}]}], 416 | ";"}], "\[IndentingNewLine]", 417 | RowBox[{ 418 | RowBox[{"I5", "=", 419 | RowBox[{ 420 | RowBox[{"{", 421 | RowBox[{ 422 | RowBox[{"{", 423 | RowBox[{"1", ",", "0", ",", "0", ",", "0", ",", "0"}], "}"}], ",", 424 | RowBox[{"{", 425 | RowBox[{"0", ",", "1", ",", "0", ",", "0", ",", "0"}], "}"}], ",", 426 | RowBox[{"{", 427 | RowBox[{"0", ",", "0", ",", "1", ",", "0", ",", "0"}], "}"}], ",", 428 | RowBox[{"{", 429 | RowBox[{"0", ",", "0", ",", "0", ",", "1", ",", "0"}], "}"}], ",", 430 | RowBox[{"{", 431 | RowBox[{"0", ",", "0", ",", "0", ",", "0", ",", "1"}], "}"}]}], 432 | "}"}], "//", "MatrixForm"}]}], ";"}], "\[IndentingNewLine]", 433 | RowBox[{ 434 | RowBox[{ 435 | RowBox[{"PhaseCoin", "[", "Psi_", "]"}], ":=", 436 | RowBox[{ 437 | TagBox[ 438 | RowBox[{"(", "\[NoBreak]", GridBox[{ 439 | {"1", "0", "0", "0", "0", "0", "0", "0", "0", "0"}, 440 | {"0", "1", "0", "0", "0", "0", "0", "0", "0", "0"}, 441 | {"0", "0", "1", "0", "0", "0", "0", "0", "0", "0"}, 442 | {"0", "0", "0", "1", "0", "0", "0", "0", "0", "0"}, 443 | {"0", "0", "0", "0", "1", "0", "0", "0", "0", "0"}, 444 | {"0", "0", "0", "0", "0", 445 | SuperscriptBox["\[ExponentialE]", 446 | RowBox[{"\[ImaginaryI]", " ", "Psi"}]], "0", "0", "0", "0"}, 447 | {"0", "0", "0", "0", "0", "0", 448 | SuperscriptBox["\[ExponentialE]", 449 | RowBox[{"\[ImaginaryI]", " ", "Psi"}]], "0", "0", "0"}, 450 | {"0", "0", "0", "0", "0", "0", "0", 451 | SuperscriptBox["\[ExponentialE]", 452 | RowBox[{"\[ImaginaryI]", " ", "Psi"}]], "0", "0"}, 453 | {"0", "0", "0", "0", "0", "0", "0", "0", 454 | SuperscriptBox["\[ExponentialE]", 455 | RowBox[{"\[ImaginaryI]", " ", "Psi"}]], "0"}, 456 | {"0", "0", "0", "0", "0", "0", "0", "0", "0", 457 | SuperscriptBox["\[ExponentialE]", 458 | RowBox[{"\[ImaginaryI]", " ", "Psi"}]]} 459 | }, 460 | GridBoxAlignment -> { 461 | "Columns" -> {{Center}}, "ColumnsIndexed" -> {}, 462 | "Rows" -> {{Baseline}}, "RowsIndexed" -> {}}, 463 | GridBoxSpacings -> {"Columns" -> { 464 | Offset[0.27999999999999997`], { 465 | Offset[0.7]}, 466 | Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { 467 | Offset[0.2], { 468 | Offset[0.4]}, 469 | Offset[0.2]}, "RowsIndexed" -> {}}], "\[NoBreak]", ")"}], 470 | Function[BoxForm`e$, 471 | MatrixForm[BoxForm`e$]]], "//", "MatrixForm"}]}], ";"}], 472 | "\[IndentingNewLine]", 473 | RowBox[{ 474 | RowBox[{"\[Phi]", "=", 475 | RowBox[{ 476 | TagBox[ 477 | RowBox[{"(", "\[NoBreak]", GridBox[{ 478 | {"1"}, 479 | {"0"}, 480 | {"0"}, 481 | {"0"}, 482 | {"0"}, 483 | {"0"}, 484 | {"0"}, 485 | {"0"}, 486 | {"0"}, 487 | {"0"} 488 | }, 489 | GridBoxAlignment -> { 490 | "Columns" -> {{Center}}, "ColumnsIndexed" -> {}, 491 | "Rows" -> {{Baseline}}, "RowsIndexed" -> {}}, 492 | GridBoxSpacings -> {"Columns" -> { 493 | Offset[0.27999999999999997`], { 494 | Offset[0.7]}, 495 | Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { 496 | Offset[0.2], { 497 | Offset[0.4]}, 498 | Offset[0.2]}, "RowsIndexed" -> {}}], "\[NoBreak]", ")"}], 499 | Function[BoxForm`e$, 500 | MatrixForm[BoxForm`e$]]], "//", "MatrixForm"}]}], ";"}], 501 | "\[IndentingNewLine]", 502 | RowBox[{"(*", 503 | RowBox[{ 504 | "\:5b9a\:4e49K5\:7684\:53d8\:6362\:77e9\:9635U", ",", 505 | "\:4ee5\:53ca\:5e26\:76f8\:4f4dcoin\:7684UPhase"}], "*)"}], 506 | "\[IndentingNewLine]", 507 | RowBox[{ 508 | RowBox[{"U", "=", 509 | RowBox[{ 510 | RowBox[{ 511 | "ShiftMoveOne", ".", "Shift2", ".", "HadamardCoin2", ".", "Shift1", ".", 512 | "HadamardCoin1"}], "//", "MatrixForm"}]}], ";"}], 513 | "\[IndentingNewLine]", 514 | RowBox[{ 515 | RowBox[{ 516 | RowBox[{"UPhase", "[", "Psi_", "]"}], ":=", 517 | RowBox[{ 518 | RowBox[{"ShiftMoveOne", ".", 519 | RowBox[{"PhaseCoin", "[", "Psi", "]"}], ".", "Shift2", ".", 520 | "HadamardCoin2", ".", "Shift1", ".", "HadamardCoin1"}], "//", 521 | "MatrixForm"}]}], ";"}], "\[IndentingNewLine]", 522 | RowBox[{"(*", 523 | RowBox[{ 524 | RowBox[{"\:5b9a\:4e49QuantumWalkK5", " ", "\:51fd\:6570"}], "\:ff0c", 525 | "\:53c2\:6570\:4e3a\:6b65\:6570", "\:ff0c", 526 | RowBox[{ 527 | "\:7ed9\:51fa\:4f4d\:7f6e\:6001", "\[IndentingNewLine]", 528 | "\:5b9a\:4e49QuantumWalkK5Phase", " ", "\:51fd\:6570"}], "\:ff0c", 529 | "\:53c2\:6570\:4e3a\:6b65\:6570", "\:ff0c", "\:53ca\:76f8\:4f4dPsi", ",", 530 | " ", 531 | RowBox[{ 532 | RowBox[{"marked", " ", "line", " ", "\:4e3a"}], "+", 533 | RowBox[{"2", "\:7ed9\:51fa\:4f4d\:7f6e\:6001"}]}]}], 534 | "\[IndentingNewLine]", "*)"}], "\[IndentingNewLine]", 535 | RowBox[{"Clear", "[", "QuantumWalkK5", "]"}], "\[IndentingNewLine]", 536 | RowBox[{"Clear", "[", "QuantumWalkK5Phase", "]"}], "\[IndentingNewLine]", 537 | RowBox[{ 538 | RowBox[{ 539 | RowBox[{"QuantumWalkK5", "[", "0", "]"}], "=", 540 | RowBox[{ 541 | TagBox[ 542 | RowBox[{"(", "\[NoBreak]", GridBox[{ 543 | {"1"}, 544 | {"0"}, 545 | {"0"}, 546 | {"0"}, 547 | {"0"}, 548 | {"0"}, 549 | {"0"}, 550 | {"0"}, 551 | {"0"}, 552 | {"0"} 553 | }, 554 | GridBoxAlignment -> { 555 | "Columns" -> {{Center}}, "ColumnsIndexed" -> {}, 556 | "Rows" -> {{Baseline}}, "RowsIndexed" -> {}}, 557 | GridBoxSpacings -> {"Columns" -> { 558 | Offset[0.27999999999999997`], { 559 | Offset[0.7]}, 560 | Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { 561 | Offset[0.2], { 562 | Offset[0.4]}, 563 | Offset[0.2]}, "RowsIndexed" -> {}}], "\[NoBreak]", ")"}], 564 | Function[BoxForm`e$, 565 | MatrixForm[BoxForm`e$]]], "//", "MatrixForm"}]}], ";"}], 566 | "\[IndentingNewLine]", "\[IndentingNewLine]", 567 | RowBox[{ 568 | RowBox[{ 569 | RowBox[{"QuantumWalkK5", "[", "steps_", "]"}], ":=", " ", 570 | RowBox[{ 571 | RowBox[{"QuantumWalkK5", "[", "steps", "]"}], "=", 572 | RowBox[{ 573 | RowBox[{"U", ".", 574 | RowBox[{"QuantumWalkK5", "[", 575 | RowBox[{"steps", "-", "1"}], "]"}]}], "//", "MatrixForm"}]}]}], 576 | ";"}], "\[IndentingNewLine]", 577 | RowBox[{ 578 | RowBox[{ 579 | RowBox[{"QuantumWalkK5Phase", "[", 580 | RowBox[{"0", ",", "Psi_"}], "]"}], "=", 581 | RowBox[{ 582 | TagBox[ 583 | RowBox[{"(", "\[NoBreak]", GridBox[{ 584 | {"1"}, 585 | {"0"}, 586 | {"0"}, 587 | {"0"}, 588 | {"0"}, 589 | {"0"}, 590 | {"0"}, 591 | {"0"}, 592 | {"0"}, 593 | {"0"} 594 | }, 595 | GridBoxAlignment -> { 596 | "Columns" -> {{Center}}, "ColumnsIndexed" -> {}, 597 | "Rows" -> {{Baseline}}, "RowsIndexed" -> {}}, 598 | GridBoxSpacings -> {"Columns" -> { 599 | Offset[0.27999999999999997`], { 600 | Offset[0.7]}, 601 | Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { 602 | Offset[0.2], { 603 | Offset[0.4]}, 604 | Offset[0.2]}, "RowsIndexed" -> {}}], "\[NoBreak]", ")"}], 605 | Function[BoxForm`e$, 606 | MatrixForm[BoxForm`e$]]], "//", "MatrixForm"}]}], ";"}], 607 | "\[IndentingNewLine]", 608 | RowBox[{ 609 | RowBox[{ 610 | RowBox[{"QuantumWalkK5Phase", "[", 611 | RowBox[{"steps_", ",", "Psi_"}], "]"}], ":=", 612 | RowBox[{ 613 | RowBox[{"QuantumWalkK5Phase", "[", "steps", "]"}], "=", 614 | RowBox[{ 615 | RowBox[{ 616 | RowBox[{"UPhase", "[", "Psi", "]"}], ".", 617 | RowBox[{"QuantumWalkK5Phase", "[", 618 | RowBox[{ 619 | RowBox[{"steps", "-", "1"}], ",", "Psi"}], "]"}]}], "//", 620 | "MatrixForm"}]}]}], ";"}], "\[IndentingNewLine]"}]}]], "Input", 621 | CellChangeTimes -> {{3.6690867707743993`*^9, 3.6690868112430534`*^9}, { 622 | 3.669086870588075*^9, 3.6690869893622427`*^9}, {3.669087024571819*^9, 623 | 3.6690872861065645`*^9}, {3.6690873588917885`*^9, 3.669087360294672*^9}, { 624 | 3.6690874572237663`*^9, 3.6690875662601657`*^9}, 3.66908761280852*^9, { 625 | 3.6690879686862674`*^9, 3.6690880074249277`*^9}, {3.669088101217921*^9, 626 | 3.669088325461237*^9}, {3.669088372852105*^9, 3.6690883832183523`*^9}, 627 | 3.669088439952918*^9, {3.6690903232998047`*^9, 3.669090337348643*^9}, { 628 | 3.669090647371254*^9, 3.6690907254245033`*^9}, {3.669090757321968*^9, 629 | 3.6690908128498917`*^9}, {3.6690908635229907`*^9, 630 | 3.6690909049929028`*^9}, {3.6690910358752174`*^9, 3.669091053235268*^9}, { 631 | 3.6690911836810856`*^9, 3.669091186232604*^9}, {3.669091230074918*^9, 632 | 3.6690912895373955`*^9}, {3.6690913636040154`*^9, 633 | 3.6690913721966267`*^9}, {3.6690915232129154`*^9, 3.669091600249637*^9}, { 634 | 3.6690918844111853`*^9, 3.669091892388918*^9}, {3.6690919879595594`*^9, 635 | 3.669092094729249*^9}, {3.669092861613515*^9, 3.669092928186165*^9}, { 636 | 3.6690932126519856`*^9, 3.6690932138551655`*^9}, {3.669093308117559*^9, 637 | 3.669093343365827*^9}, {3.669093400664897*^9, 3.6690934287682705`*^9}, { 638 | 3.669093489593053*^9, 3.669093521182536*^9}, 3.6690936298262887`*^9, { 639 | 3.669093743147219*^9, 3.669093756912764*^9}, {3.669094017258934*^9, 640 | 3.6690940477867365`*^9}, {3.669094129593167*^9, 3.6690942150325317`*^9}, { 641 | 3.6690943231927776`*^9, 3.669094383961775*^9}, {3.6690944155374827`*^9, 642 | 3.6690944614739485`*^9}, {3.6690945038518915`*^9, 643 | 3.6690945061217413`*^9}, {3.6690945372191963`*^9, 644 | 3.6690945491942263`*^9}, {3.6690945962530174`*^9, 3.669094787308114*^9}, 645 | 3.6690948934357414`*^9, {3.6690949417288847`*^9, 3.6690949461002207`*^9}, { 646 | 3.6690949866302967`*^9, 3.669095007383213*^9}, {3.669095895784773*^9, 647 | 3.669095898858717*^9}, {3.6690959637508683`*^9, 3.6690959701081066`*^9}, { 648 | 3.669096159997595*^9, 3.669096216510341*^9}, {3.669096273678705*^9, 649 | 3.6690962918294325`*^9}}], 650 | 651 | Cell[BoxData[ 652 | RowBox[{"QuantumWalkK5Phase", "[", 653 | RowBox[{"2", ",", 654 | RowBox[{"Pi", "/", "2"}]}], "]"}]], "Input", 655 | CellChangeTimes -> {{3.66909481424232*^9, 3.6690948292998123`*^9}, { 656 | 3.66909603811162*^9, 3.6690960383271446`*^9}, {3.669096352675271*^9, 657 | 3.669096352944249*^9}}] 658 | }, 659 | WindowSize -> {851, 514}, 660 | WindowMargins -> {{100, Automatic}, {Automatic, 29}}, 661 | FrontEndVersion -> "8.0 for Microsoft Windows (64-bit) (November 7, 2010)", 662 | StyleDefinitions -> "Default.nb" 663 | ] 664 | (* End of Notebook Content *) 665 | 666 | (* Internal cache information *) 667 | (*CellTagsOutline 668 | CellTagsIndex->{} 669 | *) 670 | (*CellTagsIndex 671 | CellTagsIndex->{} 672 | *) 673 | (*NotebookFileOutline 674 | Notebook[{ 675 | Cell[557, 20, 23973, 628, 2094, "Input"], 676 | Cell[24533, 650, 285, 6, 31, "Input"] 677 | } 678 | ] 679 | *) 680 | 681 | (* End of internal cache information *) 682 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 2D Quantum Walk Simulation 2 | This is a simple 2D classical random walk and quantum walk simulation project. 3 | There are two parts of simulation: 4 | 5 | - Classical Random walk 6 | 7 | See [Classical Random Walk](https://github.com/daihui/QuantumWalkSimulation/blob/master/classicalRWMat.py) 8 | 9 | - Quantum walk 10 | 11 | See [Quantum Walk](https://github.com/daihui/QuantumWalkSimulation/blob/master/quantumRWMat.py) 12 | 13 | Based on this simulation I have made two GIF which can do a intresting compare [Classical](https://github.com/daihui/QuantumWalkSimulation/blob/master/CRW.gif) VS [Quantum](https://github.com/daihui/QuantumWalkSimulation/blob/master/QW.gif) 14 | -------------------------------------------------------------------------------- /classicalRW.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | __author__ = 'levitan' 5 | import math 6 | from numpy import * 7 | import numpy as np 8 | import random 9 | from matplotlib import pyplot as plt 10 | 11 | 12 | def classicalRanNum(): 13 | coinX = int(random.choice(['1', '-1'])) 14 | coinY = int(random.choice(['1', '-1'])) 15 | return coinX, coinY 16 | 17 | 18 | # print classicalRanNum() 19 | 20 | def classicalWalkerPosition(): 21 | positionX = 0 22 | positionY = 0 23 | return positionX, positionY 24 | 25 | 26 | def classicalWalk(walkNum): 27 | walkerPositionX, walkerPositionY = classicalWalkerPosition() 28 | for i in range(0, walkNum): 29 | coinX, coinY = classicalRanNum() 30 | walkerPositionX += coinX 31 | walkerPositionY += coinY 32 | return walkerPositionX, walkerPositionY 33 | 34 | 35 | def classicalRWDistr(walkNum, matrixNum,satNum): 36 | # positionList = [] 37 | walkerCount = zeros([2 *matrixNum + 1, 2 * matrixNum + 1]) 38 | for i in range(0, satNum): 39 | walker = classicalWalk(walkNum) 40 | #print walker,walker[0],walker[1] 41 | # positionList.append(walker) 42 | walkerCount[walker[0]+matrixNum][walker[1]+matrixNum] += float(1.0 / satNum) 43 | return walkerCount 44 | 45 | def Plot2D(classcialWalker,steps): 46 | plt.figure(1) 47 | ax1=plt.subplot(111) 48 | plt.sca(ax1) 49 | plt.title('2D distribution of %s steps Classical Random Walk' %steps) 50 | plt.xlabel('X Position(started in center)') 51 | plt.ylabel('Y Position(started in center)') 52 | plt.imshow(classcialWalker) 53 | plt.savefig('Fig/CRW_' + str(steps) + '.png') 54 | plt.close() 55 | 56 | for i in range(1,11): 57 | classicalWalker=classicalRWDistr(i*10,100,100000) 58 | Plot2D(classicalWalker,i*10) -------------------------------------------------------------------------------- /classicalRWMat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #-*- coding: utf-8 -*- 3 | 4 | __author__ = 'levitan' 5 | 6 | """ 7 | This is a matrix release for 2D classic random walk simulation. 8 | 经典随机游走的基本过程:在一个初始位置,投一次coin(随机选择),根据coin的结果, 9 | 选择向某一个方向走一步,然后再投一次coin,周而复始。 10 | classicalRanMun():产生一个随机coin 11 | classcalWalkerPosition():walker的初始位置,设为(0,0) 12 | classicalWalk():根据参数walkNum随机游走walkNum步,返回最后位置walkerPosition 13 | classicalRWDistr():重复satNum次随机游走,得到walkerPosition的统计分布 14 | Plot2D():画图函数,画出2D的经典随机游走位置概率分布 15 | """ 16 | 17 | from numpy import * 18 | import random 19 | from matplotlib import pyplot as plt 20 | 21 | def classicalRanNum(): 22 | return mat([[random.choice([1,-1])],[random.choice([1,-1])]],int) 23 | 24 | def classicalWalkerPosition(): 25 | return mat([[0],[0]],int) 26 | 27 | def classicalWalk(walkNum): 28 | walkerPosition = classicalWalkerPosition() 29 | for i in range(0, walkNum): 30 | coin= classicalRanNum() 31 | walkerPosition+=coin 32 | return walkerPosition 33 | 34 | def classicalRWDistr(walkNum, matrixNum,satNum): 35 | walkerCount = zeros([2 *matrixNum + 1, 2 * matrixNum + 1]) 36 | for i in range(0, satNum): 37 | walker = classicalWalk(walkNum) 38 | walkerCount[int(walker[0])+matrixNum][int(walker[1])+matrixNum] += float(1.0 / satNum) 39 | return walkerCount 40 | 41 | def Plot2D(classcialWalker,steps): 42 | plt.figure(1) 43 | ax1=plt.subplot(111) 44 | plt.sca(ax1) 45 | plt.title('2D distribution of %s steps Classical Random Walk' %steps) 46 | plt.xlabel('X Position(started in center)') 47 | plt.ylabel('Y Position(started in center)') 48 | plt.imshow(classcialWalker) 49 | plt.savefig('Fig/CRW_' + str(steps) + '.png') 50 | plt.close() 51 | 52 | #for i in range(1,2): 53 | # classicalWalker=classicalRWDistr(i*10,100,100000) 54 | # Plot2D(classicalWalker,i*10) -------------------------------------------------------------------------------- /quantumRW.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | __author__ = 'levitan' 5 | 6 | from numpy import * 7 | from matplotlib import pyplot as plt 8 | import copy 9 | 10 | dimension = 1 11 | 12 | 13 | def initQuantumStateList(X0, X1, Y0, Y1, totalSteps): 14 | initquantumStateList = zeros([2 * totalSteps + 1, 2 * totalSteps + 1, 2, 2], dtype=complex) 15 | initquantumStateList[totalSteps][totalSteps][0][0] = complex(X0) 16 | initquantumStateList[totalSteps][totalSteps][0][1] = complex(X1) 17 | initquantumStateList[totalSteps][totalSteps][1][0] = complex(Y0) 18 | initquantumStateList[totalSteps][totalSteps][1][1] = complex(Y1) 19 | return initquantumStateList 20 | 21 | 22 | # print quantumState(1,1,0,0,1,1,0,0) 23 | 24 | def hadamardCoin(quantumStateList, totalSteps): 25 | global dimension 26 | for x in range(dimension): 27 | for y in range(dimension): 28 | tempState = zeros([2, 2], dtype=complex) 29 | tempState = copy.deepcopy( 30 | quantumStateList[totalSteps - (dimension - 1) / 2 + x][totalSteps - (dimension - 1) / 2 + y]) 31 | quantumStateList[totalSteps - (dimension - 1) / 2 + x][totalSteps - (dimension - 1) / 2 + y][0][ 32 | 0] = complex((tempState[0][0] + tempState[0][1]) / sqrt(2)) 33 | quantumStateList[totalSteps - (dimension - 1) / 2 + x][totalSteps - (dimension - 1) / 2 + y][0][ 34 | 1] = complex((tempState[0][0] - tempState[0][1]) / sqrt(2)) 35 | quantumStateList[totalSteps - (dimension - 1) / 2 + x][totalSteps - (dimension - 1) / 2 + y][1][ 36 | 0] = complex((tempState[1][0] + tempState[1][1]) / sqrt(2)) 37 | quantumStateList[totalSteps - (dimension - 1) / 2 + x][totalSteps - (dimension - 1) / 2 + y][1][ 38 | 1] = complex((tempState[1][0] - tempState[1][1]) / sqrt(2)) 39 | # print quantumStateList 40 | return quantumStateList 41 | 42 | 43 | # qs=quantumState(0,1,0,0,0,1,0,0) 44 | # print qs 45 | # print hadamardCoin(qs) 46 | 47 | def shiftOperator(quantumStateList, totalSteps): 48 | global dimension 49 | newQuanStatList = zeros([2 * totalSteps + 1, 2 * totalSteps + 1, 2, 2], dtype=complex) 50 | for x in range(dimension): 51 | for y in range(dimension): 52 | newQuanStat1 = zeros([2, 2], dtype=complex) 53 | newQuanStat1[0][0] = \ 54 | quantumStateList[totalSteps - (dimension - 1) / 2 + x][totalSteps - (dimension - 1) / 2 + y][0][0] 55 | newQuanStat1[0][1] = 0.0 56 | newQuanStat1[1][0] = \ 57 | quantumStateList[totalSteps - (dimension - 1) / 2 + x][totalSteps - (dimension - 1) / 2 + y][1][0] 58 | newQuanStat1[1][1] = 0.0 59 | newQuanStatList[totalSteps - (dimension - 1) / 2 + x - 1][ 60 | totalSteps - (dimension - 1) / 2 + y - 1] += (newQuanStat1 / sqrt(2)) 61 | 62 | newQuanStat2 = zeros([2, 2], dtype=complex) 63 | newQuanStat2[0][0] = \ 64 | quantumStateList[totalSteps - (dimension - 1) / 2 + x][totalSteps - (dimension - 1) / 2 + y][0][0] 65 | newQuanStat2[0][1] = 0.0 66 | newQuanStat2[1][0] = 0.0 67 | newQuanStat2[1][1] = \ 68 | quantumStateList[totalSteps - (dimension - 1) / 2 + x][totalSteps - (dimension - 1) / 2 + y][1][1] 69 | newQuanStatList[totalSteps - (dimension - 1) / 2 + x - 1][ 70 | totalSteps - (dimension - 1) / 2 + y + 1] += (newQuanStat2 / sqrt(2)) 71 | 72 | newQuanStat3 = zeros([2, 2], dtype=complex) 73 | newQuanStat3[0][0] = 0.0 74 | newQuanStat3[0][1] = \ 75 | quantumStateList[totalSteps - (dimension - 1) / 2 + x][totalSteps - (dimension - 1) / 2 + y][0][1] 76 | newQuanStat3[1][0] = 0.0 77 | newQuanStat3[1][1] = \ 78 | quantumStateList[totalSteps - (dimension - 1) / 2 + x][totalSteps - (dimension - 1) / 2 + y][1][1] 79 | newQuanStatList[totalSteps - (dimension - 1) / 2 + x + 1][ 80 | totalSteps - (dimension - 1) / 2 + y + 1] += (newQuanStat3 / sqrt(2)) 81 | 82 | newQuanStat4 = zeros([2, 2], dtype=complex) 83 | newQuanStat4[0][0] = 0.0 84 | newQuanStat4[0][1] = \ 85 | quantumStateList[totalSteps - (dimension - 1) / 2 + x][totalSteps - (dimension - 1) / 2 + y][0][1] 86 | newQuanStat4[1][0] = \ 87 | quantumStateList[totalSteps - (dimension - 1) / 2 + x][totalSteps - (dimension - 1) / 2 + y][1][0] 88 | newQuanStat4[1][1] = 0.0 89 | newQuanStatList[totalSteps - (dimension - 1) / 2 + x + 1][ 90 | totalSteps - (dimension - 1) / 2 + y - 1] += (newQuanStat4 / sqrt(2)) 91 | dimension += 2 92 | return newQuanStatList 93 | 94 | def quantumWalker(X0, X1, Y0, Y1, totalSteps,plotSteps): 95 | global dimension 96 | initStateList = initQuantumStateList(X0, X1, Y0, Y1, totalSteps) 97 | newQuanStatList = zeros([2 * totalSteps + 1, 2 * totalSteps + 1, 2, 2], dtype=complex) 98 | shiftQuanStatList = initStateList 99 | for i in range(1,totalSteps+1): 100 | newQuanStatList = hadamardCoin(shiftQuanStatList, totalSteps) 101 | shiftQuanStatList = shiftOperator(newQuanStatList, totalSteps) 102 | if (i!=0)&(i%plotSteps == 0) : 103 | print dimension 104 | print i 105 | plotQW=distriQW(shiftQuanStatList,dimension,totalSteps) 106 | Plot2D(plotQW,i) 107 | dimension = 1 108 | return shiftQuanStatList 109 | 110 | 111 | def distriQW(quantumWalkerList, dim,totalSteps): 112 | distribution = zeros([2 * totalSteps + 1, 2 * totalSteps + 1], dtype=float) 113 | sum = 0.0 114 | # print quantumWalkerList 115 | for x in range(dim): 116 | for y in range(dim): 117 | distribution[totalSteps-(dim-1)/2+x][totalSteps-(dim-1)/2 +y] += float( 118 | abs((quantumWalkerList[totalSteps-(dim-1)/2+x][totalSteps-(dim-1)/2 +y][0][0].real ** 2 + quantumWalkerList[totalSteps-(dim-1)/2+x][totalSteps-(dim-1)/2 +y][0][0].imag ** 2)) 119 | + abs((quantumWalkerList[totalSteps-(dim-1)/2+x][totalSteps-(dim-1)/2 +y][0][1].real ** 2 + quantumWalkerList[totalSteps-(dim-1)/2+x][totalSteps-(dim-1)/2 +y][0][1].imag ** 2)) 120 | + abs((quantumWalkerList[totalSteps-(dim-1)/2+x][totalSteps-(dim-1)/2 +y][1][0].real ** 2 + quantumWalkerList[totalSteps-(dim-1)/2+x][totalSteps-(dim-1)/2 +y][1][0].imag ** 2)) 121 | + abs((quantumWalkerList[totalSteps-(dim-1)/2+x][totalSteps-(dim-1)/2 +y][1][1].real ** 2 + quantumWalkerList[totalSteps-(dim-1)/2+x][totalSteps-(dim-1)/2 +y][1][1].imag ** 2))) 122 | sum += distribution[totalSteps-dim +x][totalSteps-dim +y] 123 | print('Sum= %s' % sum) 124 | return distribution / sum 125 | 126 | 127 | def writeQWtoArray(distribution, filename): 128 | distrFlie = open(filename, 'w+') 129 | for x in range(shape(distribution)[0]): 130 | for y in range(shape(distribution)[1]): 131 | distrFlie.write(str(distribution[x][y]) + '\t') 132 | distrFlie.write('\n') 133 | distrFlie.close() 134 | 135 | 136 | def writeQWtoList(distribution, filename): 137 | distrFlie = open('Data/' + filename, 'w+') 138 | for x in range(shape(distribution)[0]): 139 | for y in range(shape(distribution)[1]): 140 | distrFlie.write(str(x) + '\t' + str(y) + '\t' + str(distribution[x][y]) + '\n') 141 | distrFlie.close() 142 | 143 | 144 | def Plot2D(qw,steps): 145 | plt.figure(1) 146 | ax1 = plt.subplot(111) 147 | plt.sca(ax1) 148 | plt.title('2D distribution of %s steps Quantum Walk with hadamard coin' %steps) 149 | plt.xlabel('X Position (started in center)') 150 | plt.ylabel('Y Position (started in center)') 151 | plt.imshow(qw) 152 | plt.savefig('Fig/QW_' + str(steps) + '.png') 153 | plt.close() 154 | 155 | 156 | def PlotX(qw, Y): 157 | qwX = qw[:][Y] 158 | plt.plot(qwX) 159 | plt.title('a Slice of 2D distribution of Quantum Walk') 160 | plt.xlabel('Position(started in %s)' % Y) 161 | plt.ylabel('Probability') 162 | plt.show() 163 | -------------------------------------------------------------------------------- /quantumRWMat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | __author__ = 'levitan' 4 | 5 | """ 6 | This is matrix release for 2D quantum walk simulation. 7 | 量子游走的基本过程:有一个初始量子态,coin是一个幺正变换,一次游走即coin对量子态变换一次 8 | 然后根据量子态walk一步,得到一个位置概率分布,如此反复。 9 | """ 10 | 11 | from numpy import * 12 | from matplotlib import pyplot as plt 13 | 14 | 15 | # 初始化量子态,是一个对角矩阵 16 | def initQuanStat(X0, X1, Y0, Y1): 17 | initQuanStat = zeros([4, 4], complex) 18 | initQuanStat[0][0] = X0 19 | initQuanStat[1][1] = X1 20 | initQuanStat[2][2] = Y0 21 | initQuanStat[3][3] = Y1 22 | return initQuanStat 23 | 24 | 25 | # 定义2D hadamard coin,实际是2个1D Coin的直积态 26 | def hadamardCoin(): 27 | hadamardCoin = array(([1 / sqrt(2), 1 / sqrt(2), 0, 0], [1 / sqrt(2), -1 / sqrt(2), 0, 0] 28 | , [0, 0, 1 / sqrt(2), 1 / sqrt(2)], [0, 0, 1 / sqrt(2), -1 / sqrt(2)]), complex) 29 | return hadamardCoin 30 | 31 | 32 | # 根据走了的步数定义空位置矩阵 33 | def initPositionMap(steps): 34 | positionMap = zeros([2 * steps + 1, 2 * steps + 1, 4, 4], complex) 35 | return positionMap 36 | 37 | 38 | # 对量子态经行coin变换 39 | def coinOperator(positionMap, coin): 40 | dimension = shape(positionMap)[0] 41 | for i in range(dimension): 42 | for j in range(dimension): 43 | positionMap[i][j] = dot(positionMap[i][j], coin) 44 | return positionMap 45 | 46 | 47 | # 根据量子态进行位置变换,相当于一次walk 48 | def shiftOperator(coinMap, step): 49 | newPositionMap = initPositionMap(step) 50 | for i in range(2 * step - 1): 51 | for j in range(2 * step - 1): 52 | newPositionMap[i][j][0][0] += coinMap[i][j][0][0] 53 | newPositionMap[i][j][1][0] += coinMap[i][j][1][0] 54 | newPositionMap[i][j][2][2] += coinMap[i][j][2][2] 55 | newPositionMap[i][j][3][2] += coinMap[i][j][3][2] 56 | 57 | newPositionMap[i][j + 2][0][1] += coinMap[i][j][0][1] 58 | newPositionMap[i][j + 2][1][1] += coinMap[i][j][1][1] 59 | newPositionMap[i][j + 2][2][2] += coinMap[i][j][2][2] 60 | newPositionMap[i][j + 2][3][2] += coinMap[i][j][3][2] 61 | 62 | newPositionMap[i + 2][j][0][0] += coinMap[i][j][0][0] 63 | newPositionMap[i + 2][j][1][0] += coinMap[i][j][1][0] 64 | newPositionMap[i + 2][j][2][3] += coinMap[i][j][2][3] 65 | newPositionMap[i + 2][j][3][3] += coinMap[i][j][3][3] 66 | 67 | newPositionMap[i + 2][j + 2][0][1] += coinMap[i][j][0][1] 68 | newPositionMap[i + 2][j + 2][1][1] += coinMap[i][j][1][1] 69 | newPositionMap[i + 2][j + 2][2][3] += coinMap[i][j][2][3] 70 | newPositionMap[i + 2][j + 2][3][3] += coinMap[i][j][3][3] 71 | 72 | return newPositionMap 73 | 74 | 75 | # quantum walk 的整体封装,返回位置分布的量子态 76 | def quantumWalk(X0, X1, Y0, Y1, steps): 77 | initPositionMap = zeros([1, 1, 4, 4], complex) 78 | initPositionMap[0][0] = initQuanStat(X0, X1, Y0, Y1) 79 | coin = hadamardCoin() 80 | for step in range(1, steps + 1): 81 | positionMap = initPositionMap 82 | coinMap = coinOperator(positionMap, coin) 83 | initPositionMap = shiftOperator(coinMap, step) 84 | return initPositionMap 85 | 86 | 87 | # 计算QW的位置分布 88 | def QWDistribution(X0, X1, Y0, Y1, steps): 89 | positionMap = quantumWalk(X0, X1, Y0, Y1, steps) 90 | dimension = shape(positionMap)[0] 91 | distribution = zeros([dimension, dimension], dtype=float) 92 | sum = 0.0 93 | for i in range(dimension): 94 | for j in range(dimension): 95 | distribution[i][j] = float((positionMap[i][j][0][0].real ** 2 + positionMap[i][j][0][0].imag ** 2 + \ 96 | positionMap[i][j][1][0].real ** 2 + positionMap[i][j][1][0].imag ** 2 + \ 97 | positionMap[i][j][0][1].real ** 2 + positionMap[i][j][0][1].imag ** 2 + \ 98 | positionMap[i][j][1][1].real ** 2 + positionMap[i][j][1][1].imag ** 2 + \ 99 | positionMap[i][j][2][2].real ** 2 + positionMap[i][j][2][2].imag ** 2 + \ 100 | positionMap[i][j][3][2].real ** 2 + positionMap[i][j][3][2].imag ** 2 + \ 101 | positionMap[i][j][2][3].real ** 2 + positionMap[i][j][2][3].imag ** 2 + \ 102 | positionMap[i][j][3][3].real ** 2 + positionMap[i][j][3][3].imag ** 2)) 103 | sum += distribution[i][j] 104 | return distribution / sum 105 | 106 | 107 | def writeQWtoArray(distribution, filename): 108 | distrFlie = open(filename, 'w+') 109 | for x in range(shape(distribution)[0]): 110 | for y in range(shape(distribution)[1]): 111 | distrFlie.write(str(distribution[x][y]) + '\t') 112 | distrFlie.write('\n') 113 | distrFlie.close() 114 | 115 | 116 | def writeQWtoList(distribution, filename): 117 | distrFlie = open('Data/' + filename, 'w+') 118 | for x in range(shape(distribution)[0]): 119 | for y in range(shape(distribution)[1]): 120 | distrFlie.write(str(x) + '\t' + str(y) + '\t' + str(distribution[x][y]) + '\n') 121 | distrFlie.close() 122 | 123 | 124 | def Plot2D(qw, steps): 125 | plt.figure(1) 126 | ax1 = plt.subplot(111) 127 | plt.sca(ax1) 128 | plt.title('2D distribution of %s steps Quantum Walk with hadamard coin' % steps) 129 | plt.xlabel('X Position (started in center)') 130 | plt.ylabel('Y Position (started in center)') 131 | plt.imshow(qw) 132 | plt.savefig('Fig/QWM_' + str(steps) + '.png') 133 | plt.close() 134 | 135 | 136 | def PlotX(qw, Y): 137 | qwX = qw[:][Y] 138 | plt.plot(qwX) 139 | plt.title('a Slice of 2D distribution of Quantum Walk') 140 | plt.xlabel('Position(started in %s)' % Y) 141 | plt.ylabel('Probability') 142 | plt.show() 143 | -------------------------------------------------------------------------------- /quantumRWTest.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | __author__ = 'levitan' 4 | 5 | import quantumRW as QW 6 | from numpy import * 7 | from matplotlib import pyplot as plt 8 | import quantumRWMat as QWM 9 | 10 | totalSteps=100 11 | plotSteps=10 12 | # steps=50 13 | # qwalker= QW.distriQW(1/sqrt(2),1j/sqrt(2),1/sqrt(2),1j/sqrt(2),steps,1) 14 | 15 | #QW.Plot2D(qwalker) 16 | #QW.PlotX(qwalker,steps) 17 | #QW.writeQW(qwalker,'QW_'+str(steps)+'.txt') 18 | 19 | 20 | #qwalker=QW.quantumWalker(1/sqrt(2),1j/sqrt(2),1/sqrt(2),1j/sqrt(2),totalSteps,plotSteps) 21 | 22 | distribution=QWM.QWDistribution(1/sqrt(2),1j/sqrt(2),1/sqrt(2),1j/sqrt(2),100) 23 | QWM.Plot2D(distribution,100) --------------------------------------------------------------------------------