initialMatrix += spinMatrix[m][row] * tensor(matrixMap[m[0]], matrixMap[m[1]]) initialMatrix /= 4. # initialMatrix = renderMatrixPhysical(initialMatrix) import scipy.optimize # result = scipy.optimize.fmin(probability,parametrizeMatrix(initialMatrix),args = (spinMatrix,row),maxiter = 1000,maxfun = 100000) # resultMatrix = generateDensityMatrix(result) resultMatrix = initialMatrix for a in range(0, 4): for b in range(0, 4): datacube.set(**{str(a) + str(b): resultMatrix[a, b]}) datacube.commit() print trace(resultMatrix), eigh(resultMatrix)[0] fig = figure(1) fig.set_size_inches((4, 4)) clf() plotDensityMatricesContour([resultMatrix, initialMatrix], style="ellipse", annotate=False, labels=["00", "01", "10", "11"]) show() datacube.savetxt()
import scipy.optimize result = scipy.optimize.fmin(probability,parametrizeMatrix(initialMatrix),args = (spinMatrix,row),maxiter = 1000,maxfun = 100000) resultMatrix = generateDensityMatrix(result) toDatacube(resultMatrix,densityMatrices) densityMatrices.commit() print i fig=figure(1) fig.set_size_inches((4,4)) clf() plotDensityMatricesContour([resultMatrix,initialMatrix],style="rectangle",annotate = True,labels=["00","01","10","11"]) show() densityMatrices.savetxt() else: densityMatrices.loadtxt("grover - maximum likelihood density matrices-2") ##Simulate the Grover search algorithm swap = matrix([[1,0,0,0],[0,0,-1j,0],[0,-1j,0,0],[0,0,0,1]]) step1 = tensor(roty(math.pi/2),roty(math.pi/2)) step2 = swap step3 = lambda a,b:tensor(rotz(a*math.pi/2),rotz(b*math.pi/2))
y=1.5, dx=0.8, dy=0, zorder=10, width=2, fc=(0.7, 0.7, 0.7), lw=0) a.set_clip_on(False) ax.add_artist(a) figure(11, figsize=(4, 4)) clf() plotDensityMatricesContour( [densityMatrix, initialStates[int(i / 2) + offset]], style="ellipse", annotate=False, labels=["00", "01", "10", "11"], colors=["red", "black"], showOutline=[False, True]) savefig("input_matrices_%i.pdf" % ((i + offset * 2) / 2)) clf() plotDensityMatricesContour( [densityMatrix, outputStates[int(i / 2) + offset]], style="ellipse", annotate=False, labels=["00", "01", "10", "11"], colors=["red", "black"], showOutline=[False, True]) savefig("output_matrices_%i.pdf" % ((i + offset * 2) / 2)) figure(10)
print eigh(initialMatrix)[0] print "Parameters:",parametrizeMatrix(initialMatrix) reconstructedMatrix = generateDensityMatrix(parametrizeMatrix(initialMatrix)) if not allclose(initialMatrix,reconstructedMatrix): print "Error parametrizing initial matrix!" import scipy.optimize result = scipy.optimize.fmin(probability,parametrizeMatrix(initialMatrix),args = (spinMatrix,row),maxiter = 1000,maxfun = 100000) resultMatrix = generateDensityMatrix(result) for a in range(0,4): for b in range(0,4): datacube.set(**{str(a)+str(b):resultMatrix[a,b]}) datacube.commit() print trace(resultMatrix),eigh(resultMatrix)[0] fig=figure(1) fig.set_size_inches((4,4)) clf() plotDensityMatricesContour([resultMatrix,initialMatrix],style="ellipse",annotate = False,labels=["00","01","10","11"]) show() datacube.savetxt()
(0.03683472221169762 + 0.005920236771060207j), (-0.035290388800884955 - 0.013034753743398401j), (0.3696456445542211 + 0j)]]) matrices = [(phiPlusMatrix, phiPlusMatrixIdeal), (phiMinusMatrix, phiMinusMatrixIdeal), (psiPlusMatrix, psiPlusMatrixIdeal), (psiMinusMatrix, psiMinusMatrixIdeal)] fig = figure(1) fig.set_size_inches((8, 3)) clf() i = 1 for matrixPair in matrices: subplot(1, 4, i) if i == 1: annotate = True else: annotate = False plotDensityMatricesContour([matrixPair[0], matrixPair[1]], style="ellipse", annotate=annotate, labels=["00", "01", "10", "11"], colors=["red", "black"], showOutline=[False, True]) title("$F_{tr}$ = %.2g %%" % (traceFidelity(matrixPair[0], matrixPair[1]) * 100)) show() i += 1 savefig("bell matrices.pdf")
print "Error parametrizing initial matrix!" import scipy.optimize result = scipy.optimize.fmin(probability, parametrizeMatrix(initialMatrix), args=(spinMatrix, row), maxiter=1000, maxfun=100000) resultMatrix = generateDensityMatrix(result) for a in range(0, 4): for b in range(0, 4): densityMatrices.set(**{str(a) + str(b): resultMatrix[a, b]}) densityMatrices.commit() print trace(resultMatrix), eigh(resultMatrix)[0] fig = figure(1) fig.set_size_inches((4, 4)) clf() plotDensityMatricesContour([resultMatrix], showOutline=[False], annotate=False, labels=[]) savefig("matrices/matrix_%d.pdf" % (i + 1)) show() densityMatrices.savetxt()
print eigh(initialMatrix)[0] print "Parameters:",parametrizeMatrix(initialMatrix) reconstructedMatrix = generateDensityMatrix(parametrizeMatrix(initialMatrix)) if not allclose(initialMatrix,reconstructedMatrix): print "Error parametrizing initial matrix!" import scipy.optimize result = scipy.optimize.fmin(probability,parametrizeMatrix(initialMatrix),args = (spinMatrix,row),maxiter = 1000,maxfun = 100000) resultMatrix = generateDensityMatrix(result) for a in range(0,4): for b in range(0,4): densityMatrices.set(**{str(a)+str(b):resultMatrix[a,b]}) densityMatrices.commit() print trace(resultMatrix),eigh(resultMatrix)[0] fig=figure(1) fig.set_size_inches((4,4)) clf() plotDensityMatricesContour([resultMatrix],showOutline = [False],annotate = False,labels=["00","01","10","11"]) savefig("matrices/matrix_%d.pdf" % i) show() spinMatrix.savetxt()
args=(spinMatrix, row), maxiter=1000, maxfun=100000) resultMatrix = generateDensityMatrix(result) toDatacube(resultMatrix, densityMatrices) densityMatrices.commit() print i fig = figure(1) fig.set_size_inches((4, 4)) clf() plotDensityMatricesContour([resultMatrix, initialMatrix], style="rectangle", annotate=True, labels=["00", "01", "10", "11"]) show() densityMatrices.savetxt() else: densityMatrices.loadtxt("grover - maximum likelihood density matrices-2") ##Simulate the Grover search algorithm swap = matrix([[1, 0, 0, 0], [0, 0, -1j, 0], [0, -1j, 0, 0], [0, 0, 0, 1]]) step1 = tensor(roty(math.pi / 2), roty(math.pi / 2)) step2 = swap step3 = lambda a, b: tensor(rotz(a * math.pi / 2), rotz(b * math.pi / 2))
for j in range(0,d): densityMatrix[i,j] = cube[str(i)+str(j)][row] return densityMatrix detector1 = matrix([[0.9515,1-0.8843],[1-0.9515,0.8843]]) detector2 = matrix([[0.9563,1-0.8737],[1-0.9563,0.8737]]) detector = tensor(detector1,detector2) detectorReal = matrix(groverSingleRunData.parameters()["qubit1"]["detectorFunction"]) import matplotlib.gridspec as gridspec figure(12) clf() plotDensityMatricesContour([detector,detectorReal],style="rectangle",annotate = annotate,labels=["00","01","10","11"]) show() fig = figure(10) subplot(441) rc('font',size = 12) rc('axes', edgecolor=(0.5,0.5,0.5),facecolor = 'white') fig.set_size_inches((12,3)) gs = gridspec.GridSpec(1, 4) for state in range(0,4): densityMatrix = fromDatacube(densityMatrices,4+5*state) print densityMatrix measuredProbs = map(lambda i:float(groverSingleRunData.children()[state]["zzp"+i][0]),["00","01","10","11"]) probs = matrix(map(lambda i:float(densityMatrix[i,i]),range(0,4)))
densityMatrix = fromDatacube(densityMatrices,i+offset*2,order = order) labels = [] if int(i/4) == 0 and i % 4 == 0: labels = ["00","01","10","11"] annotate = False a = Arrow(x = 3.7,y = 1.5,dx = 0.8,dy = 0,zorder = 10,width = 2,fc = (0.7,0.7,0.7),lw = 0) a.set_clip_on(False) ax.add_artist(a) figure(11,figsize = (4,4)) clf() plotDensityMatricesContour([densityMatrix,initialStates[int(i/2)+offset]],style="ellipse",annotate = False,labels=["00","01","10","11"],colors = ["red","black"],showOutline = [False,True]) savefig("input_matrices_%i.pdf" % ((i+offset*2)/2)) clf() plotDensityMatricesContour([densityMatrix,outputStates[int(i/2)+offset]],style="ellipse",annotate = False,labels=["00","01","10","11"],colors = ["red","black"],showOutline = [False,True]) savefig("output_matrices_%i.pdf" % ((i+offset*2)/2)) figure(10) plotDensityMatricesContour([densityMatrix,initialStates[int(i/2)+offset]],style="ellipse",annotate = False,labels=labels,colors = ["red","black"],showOutline = [False,True]) fidelity = traceFidelity(densityMatrix,initialStates[int(i/2)+offset]) title("$F_{tr}$ = %.2g %%" % (fidelity*100)) xlabel("$%s$" % initialStatesLabels[int(i/2)+offset]) ax = subplot(gs[int(i/4),(i) % 4+1]) densityMatrix = fromDatacube(densityMatrices,i+1+offset*2)
from cmath import * for i in range(1,4): for j in range(0,i): for m in [phiMinusMatrix,phiPlusMatrix]: m[j,i] = m[i,j].conjugate() psiMinusMatrix = matrix([[(0.46897871228514415+0j), (0.0787538679534797+0j), (-0.01674925247855+0.00172926959478613j), (-0.4297743574491255-0.020974501340607927j)], [(0.0787538679534797+0j), (0.013224847002998141+0j), (0.03992324135304936+0j), (0.04211084712077284+0.016900339646178042j)], [(-0.01674925247855-0.00172926959478613j), (0.03992324135304936+0j), (0.12052050203473001+0j), (0.051750707321192996-0.0020741618904280444j)], [(-0.4297743574491255+0.020974501340607927j), (0.04211084712077284-0.016900339646178042j), (0.051750707321192996+0.0020741618904280444j), (0.39727593867712774+0j)]]) psiPlusMatrix = matrix([[(0.47356299048313577+0j), (0.04603334696364297+0j), (-0.04479988764319459-0.009609979426587522j), (0.40492025330865916+0.07908492432292454j)], [(0.04603334696364297+0j), (0.004474735305039848+0j), (0.026107022049126117+0j), (0.03683472221169762-0.005920236771060207j)], [(-0.04479988764319459+0.009609979426587522j), (0.026107022049126117+0j), (0.15231662965760334+0j), (-0.035290388800884955+0.013034753743398401j)], [(0.40492025330865916-0.07908492432292454j), (0.03683472221169762+0.005920236771060207j), (-0.035290388800884955-0.013034753743398401j), (0.3696456445542211+0j)]]) matrices = [(phiPlusMatrix,phiPlusMatrixIdeal),(phiMinusMatrix,phiMinusMatrixIdeal),(psiPlusMatrix,psiPlusMatrixIdeal),(psiMinusMatrix,psiMinusMatrixIdeal)] fig=figure(1) fig.set_size_inches((8,3)) clf() i = 1 for matrixPair in matrices: subplot(1,4,i) if i == 1: annotate = True else: annotate = False plotDensityMatricesContour([matrixPair[0],matrixPair[1]],style="ellipse",annotate = annotate,labels=["00","01","10","11"],colors = ["red","black"],showOutline = [False,True]) title("$F_{tr}$ = %.2g %%" % (traceFidelity(matrixPair[0],matrixPair[1])*100)) show() i+=1 savefig("bell matrices.pdf")