예제 #1
0
        /* Measures the time taken to execute Grover's with the specified number of input qubits and number of iterations, 100 times each
         * Prints the numer of qubits, average time taken for an exeuction, and the standard deviation of execution times.
         */
        private static void TimeGrovers(int numInputQubits, int numIters)
        {
            var sim          = new QuantumSimulator();
            int numSuccesses = 0;
            int numRuns      = 100;
            var execTimes    = new List <double>(numRuns);

            for (int i = 0; i < numRuns; i++)
            {
                var watch = System.Diagnostics.Stopwatch.StartNew();
                var found = RunGrovers.Run(sim, numInputQubits, numIters).Result; // Number of total qubits is 4 because of ancilla qubit
                if (found.TrueForAll(r => r == Result.Zero))
                {
                    numSuccesses++;
                }
                watch.Stop();
                double elapsedMs = watch.ElapsedMilliseconds;
                // Discard first outlier
                if (!(elapsedMs > 100 && i == 0))
                {
                    execTimes.Add(elapsedMs);
                }
            }
            var avg   = execTimes.Average();
            var stdev = System.Math.Sqrt(execTimes.Average(v => System.Math.Pow(v - avg, 2)));

            System.Console.WriteLine($"{numInputQubits}, {avg}, {stdev}");
        }
예제 #2
0
        /* Runs Grover's with the specified number of input qubits and number of iterations, 1000 times each
         * Prints the numer of successes.
         */
        private static void ExecuteGrovers(int numInputQubits, int numIters)
        {
            var sim          = new QuantumSimulator();
            int numSuccesses = 0;
            int numTotal     = 1000;

            for (int i = 0; i < numTotal; i++)
            {
                var found = RunGrovers.Run(sim, numInputQubits, numIters).Result; // Number of total qubits is 4 because of ancilla qubit
                if (found.TrueForAll(r => r == Result.Zero))
                {
                    numSuccesses++;
                }
            }
            System.Console.WriteLine($"{numSuccesses}");
        }