static public void oracle(ref Ket oracleMatrixStorage, int ansIndex)// ansIndex from 0 to n-1 { #if DEBUG Console.WriteLine("Call oracle up"); #endif oracleMatrixStorage.Value[ansIndex, 0] = -1 * oracleMatrixStorage.Value[ansIndex, 0]; }
static public void TestMethod() { Console.WriteLine("\nThe default oracle position is '3'."); Console.WriteLine("Grover Search algorithm begins:"); Ket q1 = new Ket(2, 0); Ket q2 = new Ket(2, 0); Ket q = new Ket(2, 0); q.UnitaryTrans(XGate.Value); q1.UnitaryTrans(HGate.Value); q2.UnitaryTrans(HGate.Value); q.UnitaryTrans(HGate.Value); //Prepare tensor product Ket tempTensorProduct = new Ket((Matrix)q1.Value.KroneckerProduct(q2.Value)); int r = 1; while (r <= 1) { oracle(ref tempTensorProduct, ref q); tensorH(ref tempTensorProduct); ph(ref tempTensorProduct); tensorH(ref tempTensorProduct); r++; } MeasureMatrixH mMH = new MeasureMatrixH(measureMatrixComput()); Console.WriteLine("The result number is {0}.\n", tempTensorProduct.MeasuHResultIndex(mMH));; }
public void DistrubiveWithScalars() { Ket k1 = new Ket(new Complex[] { new Complex(2, 2), 2 }, "k1"); Ket k2 = new Ket(new Complex[] { new Complex(2, 2), 2 }, "k2"); Ket expected = (3 + 2) * k1; Assert.AreEqual(expected, 3 * k1 + 2 * k1); }
static public void oracle(ref Ket oracleMatrixStorage, List<int> ansIndexColList)// ansIndex from 0 to n-1 { #if DEBUG Console.WriteLine("Call oracle up"); #endif foreach (var e in ansIndexColList) { oracleMatrixStorage.Value[e, 0] = -1 * oracleMatrixStorage.Value[e, 0]; } }
static public void tensorH(ref Ket tempTensorProduct, int spaceLength) { //H only needs to tensor n-1 times Matrix HTemp = (Matrix)Matrix.Build.Dense(1, 1, 1); for (int j = 0; j < spaceLength; j++) { HTemp = (Matrix)HTemp.KroneckerProduct(HGate.Value); //tempTensorProduct.Value = (Matrix)((Matrix)HGate.Value.KroneckerProduct(HGate.Value) * tempTensorProduct.Value); } tempTensorProduct.Value = (Matrix)(HTemp * tempTensorProduct.Value); }
static public void ph(ref Ket tempTensorProduct) { Complex[,] phArray = { { 1, 0, 0, 0 }, { 0, -1, 0, 0 }, { 0, 0, -1, 0 }, { 0, 0, 0, -1 } }; Matrix phMatrix = (Matrix)Matrix.Build.DenseOfArray(phArray); tempTensorProduct.Value = (Matrix)(phMatrix * tempTensorProduct.Value); }
static public void ph(ref Ket tempTensorProduct, int binSpaceLength) { Matrix phMatrix = (Matrix)Matrix.Build.DenseDiagonal(binSpaceLength, binSpaceLength, new Complex(-1, 0)); phMatrix[0, 0] = Complex.One; //Complex[,] phArray = //{ // {1, 0,0,0}, // {0, -1,0,0}, // { 0,0,-1,0}, // { 0,0,0,-1} //}; tempTensorProduct.Value = (Matrix)(phMatrix * tempTensorProduct.Value); }
static public void oracle(ref Ket oracleMatrixStorage, ref Ket q) { //|11> is the answer Ket qbit1 = new Ket(2, 1); Ket qbit2 = new Ket(2, 1); Ket oracleFlipBit = new Ket((Matrix)qbit1.Value.KroneckerProduct(qbit2.Value)); oracleFlipBit.Value = (Matrix)(new Complex(-1, 0) * oracleFlipBit.Value); //对 oracleFlipBit.value 里面的每个值检测,如果非0,则把oracleMatrixStorage对应的位置的值,更换为oracleFlipBit 的值 for (int j = 0; j < oracleFlipBit.Value.RowCount; j++) { if (Math.Abs(oracleFlipBit.Value[j, 0].Real) >= 0.0001) { oracleMatrixStorage.Value[j, 0] = -1 * oracleMatrixStorage.Value[j, 0];//assignment } } }
static public int TestMethod(int totalLength, int sampleLength) { //初始化部分,zeroOneMeasure是个MeasureMatrixH类型,里面有两个元素{1 0;0 0}, {0 0;0 1} //plusminusMeasure是个MeasureMatrixH类型,里面有两个元素{1/2 1/2;1/2 1/2}, {1/2 -1/2;-1/2 1/2} var matrixArray0 = new Matrix[2]; Complex[,] array0 = { { 1, 0 }, { 0, 0 } }; matrixArray0[0] = (Matrix)Matrix.Build.DenseOfArray(array0); Complex[,] array1 = { { 0, 0 }, { 0, 1 } }; matrixArray0[1] = (Matrix)Matrix.Build.DenseOfArray(array1); var zeroOneMeasure = new MeasureMatrixH(matrixArray0); var matrixArray1 = new Matrix[2]; Complex[,] array2 = { { 0.5, 0.5 }, { 0.5, 0.5 } }; matrixArray1[0] = (Matrix)Matrix.Build.DenseOfArray(array2); Complex[,] array3 = { { 0.5, -0.5 }, { -0.5, 0.5 } }; matrixArray1[1] = (Matrix)Matrix.Build.DenseOfArray(array3); var plusminusMeasure = new MeasureMatrixH(matrixArray1); //-------------Alice---------------------- //Debug Console.Write("Input Array Length:"); //Debug var arrayLengthStr = Console.ReadLine(); int arrayLength = totalLength; //-------------------------check length---------------- int checkLengthPercent = sampleLength; var RandomGen = new CryptoRandomSource(); //生成一个rawKeyArray[10],是bool数组类型,每一个item,从0,1这两个中选择 //Produce raw key array 0,1 var rawKeyArray = new byte[arrayLength]; //生成一个basisRawArray[10],是bool数组类型,每一个item,从0,1这两个中选择 //0表示0,1基,1表示{+,-}基 var basisRawArray = new byte[arrayLength]; //生成一个ketEncArray[10]是ket数组类型.每一位由rawKeyArray和basisRawArray共同决定。 //if basisRawArray里面的值是0,rawKeyArray里面的值是0,则 ketEncArray里面的值是ket(0) //if basisRawArray里面的值是0,rawKeyArray里面的值是1,则 ketEncArray里面的值是ket(1) // if basisRawArray里面的值是1,rawKeyArray里面的值是0,则 ketEncArray里面的值是ket(1/Sqrt(2),1/Sqrt(2)) // if basisRawArray里面的值是1,rawKeyArray里面的值是1,则 ketEncArray里面的值是ket(1/Sqrt(2),-1/Sqrt(2)) var ketEncArray = new Ket[arrayLength]; var complexArray = new Complex[2]; for (var i = 0; i < arrayLength; i++) { rawKeyArray[i] = (byte)RandomGen.Next(2); basisRawArray[i] = (byte)RandomGen.Next(2); if (basisRawArray[i] == 0 && rawKeyArray[i] == 0) { complexArray[0] = new Complex(1, 0); complexArray[1] = new Complex(0, 0); ketEncArray[i] = new Ket(complexArray); } else if (basisRawArray[i] == 0 && rawKeyArray[i] == 1) { complexArray[0] = new Complex(0, 0); complexArray[1] = new Complex(1, 0); ketEncArray[i] = new Ket(complexArray); } else if (basisRawArray[i] == 1 && rawKeyArray[i] == 0) { complexArray[0] = new Complex(1 / Sqrt(2), 0); complexArray[1] = new Complex(1 / Sqrt(2), 0); ketEncArray[i] = new Ket(complexArray); } else if (basisRawArray[i] == 1 && rawKeyArray[i] == 1) { complexArray[0] = new Complex(1 / Sqrt(2), 0); complexArray[1] = new Complex(-1 / Sqrt(2), 0); ketEncArray[i] = new Ket(complexArray); } } // -----------alice end--------------------- ////--------------Quantum Channel begins, ignoring EVE ---------- var densityopEncArray = new DensityOperator[arrayLength]; for (var i = 0; i < arrayLength; i++) { densityopEncArray[i] = new DensityOperator(ketEncArray[i], new Bra(ketEncArray[i])); } QuantumChannelProcess(densityopEncArray); //---------------Quantum Channel End-------Eve end--------------------- //---------------------Bob begin-------------------- //Bob 生成一个measureRawArray,是bool数组类型随机{0,1}------------ var measureRawArray = new byte[arrayLength]; //生成结果数组 resultMeasureArray,是bool数组类型,其中的值由 measureRawArray和 densityopEncArray共同决定 // foreach item in densityopEncArray //if measureRawArray[index]=0则用 zeroOneMeasure 作为调用的参数传递到 densityopEncArray的本位,ket[index].MeasuHResultIndex(zeroOneMeasure ) //if measureRawArray[index]=1则用 plusminusMeasure 作为调用的参数传递到 densityopEncArray的本位,ket[index].MeasuHResultIndex(plusminusMeasure ) var resultMeasureArray = new byte[arrayLength]; for (var i = 0; i < arrayLength; i++) { measureRawArray[i] = (byte)RandomGen.Next(2); if (measureRawArray[i] == 0) { if (densityopEncArray[i].MeasuHResultIndex(zeroOneMeasure) != 0) { resultMeasureArray[i] = 1; } } else { if (densityopEncArray[i].MeasuHResultIndex(plusminusMeasure) != 0) { resultMeasureArray[i] = 1; } } } // ---------------------Bob end-------------------- //--------------公共信道广播不做-------- //--------------公共信道结束---------- // --------------ALice begin--------------- //生成应答数组,correctBroadArray,bool数组类型。 //foreach item in measureRawArray //if measureRawArray[index]== basisRawArray[index], correctBroadArray[index]==1. else correctBroadArray[index]==0 var correctBroadArray = new byte[arrayLength]; for (var i = 0; i < arrayLength; i++) { if (measureRawArray[i] == basisRawArray[i]) { correctBroadArray[i] = 1; } } // 生成finalAliceKey数组是bool数组类型, // foreach item in correctBroadArray // if correctBroadArray[index]==1 , push(rawKeyArray[index]),即把 correctBroadArray[index]==1的那些位置的 rawKeyArray[index]取出来放一起finalAliceKey。 var finalAliceKeyList = new List <byte>(); for (var i = 0; i < arrayLength; i++) { if (correctBroadArray[i] != 0) { finalAliceKeyList.Add(rawKeyArray[i]); } } var finalAliceKey = finalAliceKeyList.ToArray(); //------------Alice end-------------------- //------------公共信道开始----------------------- //-----------公共信道结束------------------------ //--------------Bob开始------------------------- //生成finalBobKey数组是bool数组类型 // foreach item in correctBroadArray // if correctBroadArray[index]==1 , push(resultMeasureArray[index]),即把 correctBroadArray[index]==1的那些位置的 resultMeasureArray[index]取出来放一起存到finalBobKey。 var finalBobKeyList = new List <byte>(); for (var i = 0; i < arrayLength; i++) { if (correctBroadArray[i] != 0) { finalBobKeyList.Add(resultMeasureArray[i]); } } var finalBobKey = finalBobKeyList.ToArray(); //----------------Bob结束---------------------- //Check start //Check the alice and bob's length is equal var check = true; if (finalAliceKey.Length != finalBobKey.Length) { check = false; } if (check) { // Console.WriteLine("Length: Success"); } else { //Console.WriteLine("Length: Failed"); } //Sampling start var samplingLength = (checkLengthPercent * finalAliceKey.Length) / 100; //Console.WriteLine($"Sampling {samplingLength} bits"); var samplingAliceKey = new List <byte>(finalAliceKey); var samplingBobKey = new List <byte>(finalBobKey); var samplingAliceKeySample = new List <byte>(); var samplingBobKeySample = new List <byte>(); for (var i = 0; i < samplingLength; i++) { var position = RandomGen.Next(samplingAliceKey.Count); //shuffle here, sample from agreement key and then delete it samplingAliceKeySample.Add(samplingAliceKey[position]); samplingAliceKey.RemoveAt(position); samplingBobKeySample.Add(samplingBobKey[position]); samplingBobKey.RemoveAt(position); } for (var i = 0; i < samplingLength; i++) { if (samplingAliceKeySample[i] != samplingBobKeySample[i]) { check = false; } } if (check) { //Console.WriteLine("Sampling process: Success"); return(1); //Add success times here } else { //Console.WriteLine("Sampling process: Failed"); return(0); } }
static public void tensorH(ref Ket tempTensorProduct) { tempTensorProduct.Value = (Matrix)((Matrix)HGate.Value.KroneckerProduct(HGate.Value) * tempTensorProduct.Value); }
static public void TestMethod() { Console.WriteLine("The length of an adjacent matrix:"); int n = int.Parse(Console.ReadLine()); Console.WriteLine("The adjacent matrix:"); double[,] M = new double[n, n]; for (int i = 0; i < n;) { string X = Console.ReadLine(); string[] Y = X.Split(' '); for (int j = 0; j < n;) { M[i, j] = double.Parse(Y[j]); j++; } i++; } double[] rowsum = new double[n]; for (int i = 0; i < n; i++) { rowsum[i] = 0; for (int j = 0; j < n; j++) { rowsum[i] = rowsum[i] + M[i, j]; } if (rowsum[i] == 0) { M[i, i] = 1; rowsum[i] = 1; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { M[i, j] = 0.85 * M[i, j] / rowsum[i] + 0.15 / n; } } Ket[] Base = new Ket[n]; Ket[,] Basetensor = new Ket[n, n]; Matrix[] Basematrix = new Matrix[n]; for (int j = 0; j < n; j++) { Matrix value = (Matrix)Matrix.Build.Dense(n, 1, Complex.Zero); value[j, 0] = Complex.One; Base[j] = new Ket(value); Basematrix[j] = value; } Matrix[,] Basematrixtensor = new Matrix[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Matrix value = (Matrix)Basematrix[i].KroneckerProduct(Basematrix[j]); Basetensor[i, j] = new Ket(value); Basematrixtensor[i, j] = value; } } Ket[] psi = new Ket[n]; for (int i = 0; i < n; i++) { Matrix value = (Matrix)Matrix.Build.Dense(n * n, 1, Complex.Zero); Ket temvalue = new Ket(value); for (int j = 0; j < n; j++) { temvalue.Value = (Matrix)(temvalue.Value + Math.Sqrt(M[i, j]) * Basetensor[i, j].Value); } psi[i] = new Ket(temvalue.Value); } Matrix value1 = (Matrix)Matrix.Build.Dense(n * n, 1, Complex.Zero); Ket psi0 = new Ket(value1); for (int i = 0; i < n; i++) { psi0.Value = (Matrix)(psi[i].Value + psi0.Value); } psi0.Value = (Matrix)(psi0.Value.Divide(Math.Sqrt(n))); Matrix U = (Matrix)Matrix.Build.Dense(n * n, n * n, Complex.Zero); for (int i = 0; i < n; i++) { var tempsi = (Matrix)psi[i].Value.ConjugateTranspose(); U = (Matrix)(psi[i].Value * tempsi + U); } Matrix S = (Matrix)Matrix.Build.Dense(n * n, n * n, Complex.Zero); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { var tembasetensor = Basetensor[i, j].Value.ConjugateTranspose(); S = (Matrix)(Basetensor[j, i].Value * tembasetensor + S); } } //Console.WriteLine(U); Matrix I = (Matrix)Matrix.Build.DenseIdentity(n * n, n * n); //Console.WriteLine(I); //Console.WriteLine(S); U = (Matrix)(S * (2 * U - I)); //Console.WriteLine(U); U = (Matrix)U.Power(2); Console.WriteLine("The step of quantum evolution"); int m = int.Parse(Console.ReadLine()); var Measurematrix = new Matrix[n]; Matrix sI = (Matrix)Matrix.Build.DenseIdentity(n, n); for (int i = 0; i < n; i++) { Measurematrix[i] = (Matrix)(sI.KroneckerProduct(Basematrix[i])); } Matrix [] R = new Matrix[n]; double[] Rank = new double[n]; for (int i = 0; i < n; i++) { Matrix temR = (Matrix)Matrix.Build.Dense(1, n, Complex.Zero); Matrix tem1 = (Matrix)Matrix.Build.Dense(1, n * n, Complex.Zero); Matrix tem2 = (Matrix)Matrix.Build.Dense(n * n, n * n, Complex.Zero); Matrix tem3 = (Matrix)Matrix.Build.Dense(n, 1, Complex.Zero); tem1 = (Matrix)psi0.Value.ConjugateTranspose(); tem2 = (Matrix)U.ConjugateTranspose(); temR = (Matrix)(tem1 * tem2.Power(m) * Measurematrix[i]); tem3 = (Matrix)temR.ConjugateTranspose(); R[i] = (Matrix)(temR * tem3); Rank[i] = R[i].Real().At(0, 0); } Console.WriteLine("The PageRank (from high to low) is (the first column is pages and the second is the corresponding importantance)"); Rank2[] Rank1 = new Rank2[n]; for (int i = 0; i < n; i++) { Rank1[i] = new Rank2 { Page = i, PageRank = Rank[i] }; } IEnumerable <Rank2> query = Rank1.OrderByDescending(Rank2 => Rank2.PageRank); foreach (Rank2 rank in query) { Console.WriteLine("{0} - {1}", rank.Page, rank.PageRank); } Console.ReadKey(); }
static public void TestMethod() { //Initilization,zeroOneMeasureis a MeasureMatrixH class. {1 0;0 0}, {0 0;0 1} //plusminusMeasure is MeasureMatrixH class,{1/2 1/2;1/2 1/2}, {1/2 -1/2;-1/2 1/2} var matrixArray0 = new Matrix[2]; Complex[,] array0 = { { 1, 0 }, { 0, 0 } }; matrixArray0[0] = (Matrix)Matrix.Build.DenseOfArray(array0); Complex[,] array1 = { { 0, 0 }, { 0, 1 } }; matrixArray0[1] = (Matrix)Matrix.Build.DenseOfArray(array1); var zeroOneMeasure = new MeasureMatrixH(matrixArray0); var matrixArray1 = new Matrix[2]; Complex[,] array2 = { { 0.5, 0.5 }, { 0.5, 0.5 } }; matrixArray1[0] = (Matrix)Matrix.Build.DenseOfArray(array2); Complex[,] array3 = { { 0.5, -0.5 }, { -0.5, 0.5 } }; matrixArray1[1] = (Matrix)Matrix.Build.DenseOfArray(array3); var plusminusMeasure = new MeasureMatrixH(matrixArray1); //-------------Alice---------------------- Console.Write("Input Array Length:"); var arrayLengthStr = Console.ReadLine(); int arrayLength; if (!int.TryParse(arrayLengthStr, out arrayLength)) { Console.WriteLine("Must input integer, use default(10)"); arrayLength = 10; } var RandomGen = new CryptoRandomSource(); //Produce raw key array 0,1 var rawKeyArray = new byte[arrayLength]; //0 is {|0>,|1>},1 is {|+>,|->} basis. var basisRawArray = new byte[arrayLength]; //Every KetEncArray is decided by rawKeyArray and basisRawArray //if basisRawArray is 0,rawKeyArray is 0,then ketEncArray is ket(0) //if basisRawArray is 0,rawKeyArray is 1,then ketEncArray is ket(1) // if basisRawArray is 1,rawKeyArray is 0,then ketEncArray is ket(1/Sqrt(2),1/Sqrt(2)) // if basisRawArray is 1,rawKeyArray is 1,then ketEncArray ket(1/Sqrt(2),-1/Sqrt(2)) var ketEncArray = new Ket[arrayLength]; var complexArray = new Complex[2]; for (var i = 0; i < arrayLength; i++) { rawKeyArray[i] = (byte)RandomGen.Next(2); basisRawArray[i] = (byte)RandomGen.Next(2); if (basisRawArray[i] == 0 && rawKeyArray[i] == 0) { complexArray[0] = new Complex(1, 0); complexArray[1] = new Complex(0, 0); ketEncArray[i] = new Ket(complexArray); } else if (basisRawArray[i] == 0 && rawKeyArray[i] == 1) { complexArray[0] = new Complex(0, 0); complexArray[1] = new Complex(1, 0); ketEncArray[i] = new Ket(complexArray); } else if (basisRawArray[i] == 1 && rawKeyArray[i] == 0) { complexArray[0] = new Complex(1 / Sqrt(2), 0); complexArray[1] = new Complex(1 / Sqrt(2), 0); ketEncArray[i] = new Ket(complexArray); } else if (basisRawArray[i] == 1 && rawKeyArray[i] == 1) { complexArray[0] = new Complex(1 / Sqrt(2), 0); complexArray[1] = new Complex(-1 / Sqrt(2), 0); ketEncArray[i] = new Ket(complexArray); } } // -----------alice end--------------------- ////--------------Quantum Channel begins, ignoring EVE ---------- var densityopEncArray = new DensityOperator[arrayLength]; for (var i = 0; i < arrayLength; i++) { densityopEncArray[i] = new DensityOperator(ketEncArray[i], new Bra(ketEncArray[i])); } QuantumChannelProcess(densityopEncArray); //---------------Quantum Channel End-------Eve end--------------------- //---------------------Bob begin-------------------- //Bob produces measureRawArray,randomly {0,1}------------ var measureRawArray = new byte[arrayLength]; // resultMeasureArray is bool array. The value is decided by measureRawArray and ketEncArray. // foreach item in ketEncArray //if measureRawArray[index]=0 then zeroOneMeasure would be transferred to ketEncArray ,ket[index].MeasuHResultIndex(zeroOneMeasure ) //if measureRawArray[index]=1 then plusminusMeasure would be transferred to ketEncArray,ket[index].MeasuHResultIndex(plusminusMeasure ) var resultMeasureArray = new byte[arrayLength]; for (var i = 0; i < arrayLength; i++) { measureRawArray[i] = (byte)RandomGen.Next(2); if (measureRawArray[i] == 0) { if (densityopEncArray[i].MeasuHResultIndex(zeroOneMeasure) != 0) { resultMeasureArray[i] = 1; } } else { if (densityopEncArray[i].MeasuHResultIndex(plusminusMeasure) != 0) { resultMeasureArray[i] = 1; } } } // ---------------------Bob end-------------------- // --------------ALice begin--------------- //Produce answer array,correctBroadArray,bool array class. //foreach item in measureRawArray //if measureRawArray[index]== basisRawArray[index], correctBroadArray[index]==1. else correctBroadArray[index]==0 var correctBroadArray = new byte[arrayLength]; for (var i = 0; i < arrayLength; i++) { if (measureRawArray[i] == basisRawArray[i]) { correctBroadArray[i] = 1; } } // foreach item in correctBroadArray // if correctBroadArray[index]==1 , push(rawKeyArray[index]),i.e. correctBroadArray[index]==1 position rawKeyArray[index]is taken out and storage into finalAliceKey. var finalAliceKeyList = new List <byte>(); for (var i = 0; i < arrayLength; i++) { if (correctBroadArray[i] != 0) { finalAliceKeyList.Add(rawKeyArray[i]); } } var finalAliceKey = finalAliceKeyList.ToArray(); //------------Alice end-------------------- //--------------Bob begins------------------------- //Produce finalBobKey array is bool array // foreach item in correctBroadArray // if correctBroadArray[index]==1 , push(resultMeasureArray[index]),i.e. correctBroadArray[index]==1 position, resultMeasureArray[index] is taken out and storage into finalBobKey. var finalBobKeyList = new List <byte>(); for (var i = 0; i < arrayLength; i++) { if (correctBroadArray[i] != 0) { finalBobKeyList.Add(resultMeasureArray[i]); } } var finalBobKey = finalBobKeyList.ToArray(); //----------------Bob END---------------------- //Check Begin //check finalAliceKey is equal to finalBobKey bool check = true; if (finalAliceKey.Length != finalBobKey.Length) { check = false; } for (var i = 0; i < finalAliceKey.Length; i++) { if (finalAliceKey[i] != finalBobKey[i]) { check = false; } } //if (check) //{ // Console.WriteLine("Success"); //} //else //{ // Console.WriteLine("Failed"); //} ////check end Console.WriteLine($"zeroOneMeasure:\n{zeroOneMeasure.Value[0].ToComplexString()}\n{zeroOneMeasure.Value[1].ToComplexString()}"); Console.WriteLine($"plusMinusMeasure:\n{plusminusMeasure.Value[0].ToComplexString()}\n{plusminusMeasure.Value[1].ToComplexString()}"); Console.Write("rawKeyArray\t"); foreach (var b in rawKeyArray) { Console.Write(b); } Console.WriteLine(); Console.Write("basisRawArray\t"); foreach (var b in basisRawArray) { Console.Write(b); } Console.WriteLine(); Console.WriteLine(); Console.Write("measureRawArray\t\t"); foreach (var b in measureRawArray) { Console.Write(b); } Console.WriteLine(); Console.Write("resultMeasureArray\t"); foreach (var b in resultMeasureArray) { Console.Write(b); } Console.WriteLine(); Console.Write("correctBroadArray\t"); foreach (var b in correctBroadArray) { Console.Write(b); } Console.WriteLine(); Console.Write("finalAliceKey\t\t"); foreach (var b in finalAliceKey) { Console.Write(b); } Console.WriteLine(); Console.Write("finalBobKey\t\t"); foreach (var b in finalBobKey) { Console.Write(b); } if (check) { Console.WriteLine("\nThe protocol: Success"); } else { Console.WriteLine("\nThe protocol: Failed"); } //check end Console.WriteLine(); }
//User can call the Testmethod running for once directly,using ({0},{1},{2}). //However when {2} is false, {0} and {1} will be ignored. static public void TestMethod(int realSpaceLength, int realAnsIndex, bool autoOpen) { //Figure out the space 2^n int spaceLength = 2; int ansIndex = 1; if (autoOpen) { spaceLength = realSpaceLength; ansIndex = realAnsIndex; } else { } #if DEBUG Console.WriteLine("How large the space(2^n) do you want to search?"); var spaceLengthStr = Console.ReadLine(); if (!int.TryParse(spaceLengthStr, out spaceLength)) { Console.WriteLine("Must input integer, use default(4)"); spaceLength = 4; } #endif int binSpaceLength = Convert.ToInt32(Math.Pow(2, spaceLength)); #if DEBUG Console.WriteLine("You want to search {0} numbers", binSpaceLength); //Figure out the oracle answer //In this stage, only one correct answer Console.WriteLine("Intialling the oracle......,where are the correct answers(From 0 to 2^n-1)?"); List<int> ansIndexColList=new List<int>(); while (true) { var ansIndexStr = Console.ReadLine(); if (ansIndexStr.Length==0) { break; } if (!int.TryParse(ansIndexStr, out ansIndex)) { Console.WriteLine("Must input integer to initial the oracle, use default (1)"); ansIndex = 1; } #endif if (ansIndex >= binSpaceLength || spaceLength > 10) { Console.WriteLine("You target is more than search space or space is more than 2^10"); return; //while (true) //{ // Thread.Sleep(Timeout.Infinite); //} } ansIndexColList.Add(ansIndex); } Ket[] orginKetSets = new Ket[spaceLength]; for (int j = 0; j < spaceLength; j++) { Matrix value = (Matrix)Matrix.Build.Dense(2, 1, Complex.Zero); value[0, 0] = Complex.One; orginKetSets[j] = new Ket(value); } for (int j = 0; j < spaceLength; j++) { orginKetSets[j].UnitaryTrans(HGate.Value); } int limit = (int)(Math.PI / 4.0 * Math.Sqrt(binSpaceLength)); int roundTime = ansIndexColList.Count; for (int j = 0; j < roundTime; j++) { //Prepare tensor product Matrix tempMatrix = (Matrix)Matrix.Build.Dense(1, 1, Complex.One); for (int k = 0; k < spaceLength; k++) { tempMatrix = (Matrix)tempMatrix.KroneckerProduct(orginKetSets[k].Value); } Ket tempTensorProduct = new Ket(tempMatrix); int r = 1; while (r <= limit) { oracle(ref tempTensorProduct, ansIndexColList); tensorH(ref tempTensorProduct, spaceLength); ph(ref tempTensorProduct, binSpaceLength); tensorH(ref tempTensorProduct, spaceLength); r++; } MeasureMatrixH mMH = new MeasureMatrixH(measureMatrixComput(binSpaceLength)); int roundNum = tempTensorProduct.MeasuHResultIndex(mMH); ansIndexColList.Remove(roundNum); Console.WriteLine(roundNum); } #if DEBUG //Console.WriteLine("The loop time is {0}", r - 1); // Console.WriteLine("TempTensorProduct is {0}", tempTensorProduct.Value); #endif // Console.WriteLine("Target is {0}, the final result number is {1}", ansIndex, ); ; }
static public void bindOracle(ref Ket tempTensorProduct, int bindNum) { tempTensorProduct.Value[bindNum, 0] = -1*tempTensorProduct.Value[bindNum, 0]; }
static public void TestMethod() { var useDefault = false; Console.WriteLine("Please enter the length of an adjacency matrix (Enter key to use Default config):\n"); if (!int.TryParse(Console.ReadLine(), out int n)) { Console.WriteLine("Currently, we are using the default configuration.\n"); useDefault = true; n = 7; } Console.WriteLine("The length of the adjacency matrix is {0}.\n", n); var M = new double[n, n]; if (useDefault == false) { Console.WriteLine("Please enter the adjacency matrix:"); for (var i = 0; i < n;) { var X = Console.ReadLine(); var Y = X.Split(' '); for (var j = 0; j < n;) { if (!double.TryParse(Y[j], out M[i, j])) { useDefault = true; break; } j++; } if (useDefault) { break; } i++; } } else { M = new double[7, 7] { { 0, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0 }, { 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 1, 0, 0, 0, 0 } }; } var outputMatrix = MathNet.Numerics.LinearAlgebra.Double.Matrix.Build.DenseOfArray(M); Console.WriteLine("The adjacency matrix is:\n"); Console.WriteLine(outputMatrix); var rowsum = new double[n]; for (var i = 0; i < n; i++) { rowsum[i] = 0; for (var j = 0; j < n; j++) { rowsum[i] = rowsum[i] + M[i, j]; } if (rowsum[i] == 0) { M[i, i] = 1; rowsum[i] = 1; } } for (var i = 0; i < n; i++) { for (var j = 0; j < n; j++) { M[i, j] = 0.85 * M[i, j] / rowsum[i] + 0.15 / n; } } var Base = new Ket[n]; var Basetensor = new Ket[n, n]; var Basematrix = new Matrix[n]; for (var j = 0; j < n; j++) { var value = (Matrix)Matrix.Build.Dense(n, 1, Complex.Zero); value[j, 0] = Complex.One; Base[j] = new Ket(value); Basematrix[j] = value; } var Basematrixtensor = new Matrix[n, n]; for (var i = 0; i < n; i++) { for (var j = 0; j < n; j++) { var value = (Matrix)Basematrix[i].KroneckerProduct(Basematrix[j]); Basetensor[i, j] = new Ket(value); Basematrixtensor[i, j] = value; } } var psi = new Ket[n]; for (var i = 0; i < n; i++) { var value = (Matrix)Matrix.Build.Dense(n * n, 1, Complex.Zero); var temvalue = new Ket(value); for (var j = 0; j < n; j++) { temvalue.Value = (Matrix)(temvalue.Value + Math.Sqrt(M[i, j]) * Basetensor[i, j].Value); } psi[i] = new Ket(temvalue.Value); } var value1 = (Matrix)Matrix.Build.Dense(n * n, 1, Complex.Zero); var psi0 = new Ket(value1); for (var i = 0; i < n; i++) { psi0.Value = (Matrix)(psi[i].Value + psi0.Value); } psi0.Value = (Matrix)(psi0.Value.Divide(Math.Sqrt(n))); var U = (Matrix)Matrix.Build.Dense(n * n, n * n, Complex.Zero); for (var i = 0; i < n; i++) { var tempsi = (Matrix)psi[i].Value.ConjugateTranspose(); U = (Matrix)(psi[i].Value * tempsi + U); } var S = (Matrix)Matrix.Build.Dense(n * n, n * n, Complex.Zero); for (var i = 0; i < n; i++) { for (var j = 0; j < n; j++) { var tembasetensor = Basetensor[i, j].Value.ConjugateTranspose(); S = (Matrix)(Basetensor[j, i].Value * tembasetensor + S); } } //Console.WriteLine(U.ToComplexString()); var I = (Matrix)Matrix.Build.DenseIdentity(n * n, n * n); //Console.WriteLine(I.ToComplexString()); //Console.WriteLine(S.ToComplexString()); U = (Matrix)(S * (2 * U - I)); //Console.WriteLine(U.ToComplexString()); U = (Matrix)U.Power(2); int m = 0; if (useDefault == false) { Console.WriteLine("Please enter the step of quantum evolution"); m = int.Parse(Console.ReadLine()); } else { m = 5; } Console.WriteLine("The Step of quantum evolution is {0}.\n ", m); var Measurematrix = new Matrix[n]; var sI = (Matrix)Matrix.Build.DenseIdentity(n, n); for (var i = 0; i < n; i++) { Measurematrix[i] = (Matrix)(sI.KroneckerProduct(Basematrix[i])); } var R = new Matrix[n]; var Rank = new double[n]; for (var i = 0; i < n; i++) { var temR = (Matrix)Matrix.Build.Dense(1, n, Complex.Zero); var tem1 = (Matrix)Matrix.Build.Dense(1, n * n, Complex.Zero); var tem2 = (Matrix)Matrix.Build.Dense(n * n, n * n, Complex.Zero); var tem3 = (Matrix)Matrix.Build.Dense(n, 1, Complex.Zero); tem1 = (Matrix)psi0.Value.ConjugateTranspose(); tem2 = (Matrix)U.ConjugateTranspose(); temR = (Matrix)(tem1 * tem2.Power(m) * Measurematrix[i]); tem3 = (Matrix)temR.ConjugateTranspose(); R[i] = (Matrix)(temR * tem3); Rank[i] = R[i].Real().At(0, 0); } Console.WriteLine("The {0}-step quantum PageRank (from high to low) is ", m); Console.WriteLine("(the first column is page and the second is the corresponding importantance)"); var Rank1 = new Rank2[n]; for (var i = 0; i < n; i++) { Rank1[i] = new Rank2 { Page = i, PageRank = Rank[i] }; } var query = Rank1.OrderByDescending(Rank2 => Rank2.PageRank); foreach (var rank in query) { Console.WriteLine("{0} - {1}", rank.Page + 1, rank.PageRank); } for (var i = 0; i < n; i++) { Rank[i] = 0; for (var j = 0; j < 300; j++) { var temR = (Matrix)Matrix.Build.Dense(1, n, Complex.Zero); var tem1 = (Matrix)Matrix.Build.Dense(1, n * n, Complex.Zero); var tem2 = (Matrix)Matrix.Build.Dense(n * n, n * n, Complex.Zero); var tem3 = (Matrix)Matrix.Build.Dense(n, 1, Complex.Zero); tem1 = (Matrix)psi0.Value.ConjugateTranspose(); tem2 = (Matrix)U.ConjugateTranspose(); temR = (Matrix)(tem1 * tem2.Power(j) * Measurematrix[i]); tem3 = (Matrix)temR.ConjugateTranspose(); R[i] = (Matrix)(temR * tem3); Rank[i] = R[i].Real().At(0, 0) + Rank[i]; } Rank[i] = Rank[i] / 300; } Console.WriteLine("\nThe averaged quantum PageRank (from high to low) is "); var Rank3 = new Rank2[n]; for (var i = 0; i < n; i++) { Rank3[i] = new Rank2 { Page = i, PageRank = Rank[i] }; } var query1 = Rank3.OrderByDescending(Rank2 => Rank2.PageRank); foreach (var rank in query1) { Console.WriteLine("{0} - {1}", rank.Page + 1, rank.PageRank); } //Console.ReadKey(); }