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));; }
static public void TestMethod() { var matrixArray = new Matrix[2]; Complex[,] array0 = { { 0.5, 0.5 }, { 0.5, 0.5 } }; matrixArray[0] = (Matrix)Matrix.Build.DenseOfArray(array0); Complex[,] array1 = { { 0.5, -0.5 }, { -0.5, 0.5 } }; matrixArray[1] = (Matrix)Matrix.Build.DenseOfArray(array1); var measureMatrixH = new MeasureMatrixH(matrixArray); Console.WriteLine("Measurement matrix 0"); Console.WriteLine(measureMatrixH.Value[0].ToComplexString()); Console.WriteLine("Measurement matrix 1"); Console.WriteLine(measureMatrixH.Value[1].ToComplexString()); int count_0 = 0, count_1 = 0; for (var i = 0; i < 100000; i++) { var pureDensityOperator = new DensityOperator(new QBit(false), new QBitBra(false)); switch (pureDensityOperator.MeasuHResultIndex(measureMatrixH)) { case 0: count_0++; break; case 1: count_1++; break; } } Console.WriteLine($"count_0 = {count_0}"); Console.WriteLine($"count_1 = {count_1}"); }
static Bob() { 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); 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); plusminusMeasure = new MeasureMatrixH(matrixArray1); }
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 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, ); ; }