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));; }
//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, ); ; }