示例#1
0
        static void Main(string[] args)
        {
            cmdLineArguments = new CmdLineArguments(args);

            if (!cmdLineArguments.AreArgumentsValid)
            {
                return;
            }

            switch (cmdLineArguments.Operation)
            {
            case CmdLineArguments.OperationToExecute.TestAdderInPureState:
                Test4BitAdderPureState(PhysicalConsole.Singleton);
                break;

            case CmdLineArguments.OperationToExecute.TestAdderWithEntangledInput:
                TestEntangle4BitAdder(PhysicalConsole.Singleton, cmdLineArguments.Repeats);
                break;

            case CmdLineArguments.OperationToExecute.FindSummands:
                _4BitAdderGroverTest(PhysicalConsole.Singleton, cmdLineArguments);
                break;

            case CmdLineArguments.OperationToExecute.TestAdderOracle:
                Test4BitAdderOracle(PhysicalConsole.Singleton, cmdLineArguments.SummandA, cmdLineArguments.SummandB, cmdLineArguments.ExpectedResult);
                break;
            }
        }
示例#2
0
        public CmdLineArguments GetArguments(string cmdLine)
        {
            var tokenizer = new CmdTokenizer(cmdLine);
            tokenizer.ReadNext(); //Skip program path

            var result = new CmdLineArguments { TaskToDo = TaskToDo.ShowHelp };
            var token = tokenizer.ReadNext();
            while (!string.IsNullOrWhiteSpace(token))
            {
                if (token == "--slnrun-verbose" || token == "/slnrun-verbose")
                    result.Verbose = true;
                else
                {
                    result.SolutionToRun = token;
                    result.Parameter = tokenizer.ReadRemaining();
                    result.TaskToDo = TaskToDo.RunSolution;
                }
                token = tokenizer.ReadNext();
            }
            return result;
        }
示例#3
0
        static int _4BitAdderGroverTest(IConsole console, CmdLineArguments arguments)
        {
            console.WriteLine($"Finding the summands which give the specified sum ({cmdLineArguments.ExpectedResult}), using {cmdLineArguments.GroverIterations} Grover-iterations.");
            console.WriteLine(string.Empty);

            int successfulCount = 0;

            using (var sim = new QuantumSimulator(throwOnReleasingQubitsNotInZeroState: true))
            {
                for (int i = 0; i < cmdLineArguments.Repeats; ++i)
                {
                    // Each operation has a static method called Run which takes a simulator as
                    // an argument, along with all the arguments defined by the operation itself.
                    var result = Operation.Run(sim, cmdLineArguments.ExpectedResult, cmdLineArguments.GroverIterations).Result;

                    if (result.Item1 == Result.One)
                    {
                        successfulCount++;
                        string resultAsBinary = Convert.ToString(result.Item2, 2).PadLeft(14, '0').Insert(4, "'").Insert(6, "'").Insert(8, "'").Insert(13, "'");;
                        int    a         = ((int)result.Item2) & 0xf;
                        int    b         = (((int)result.Item2) >> 4) & 0xf;
                        var    prevColor = console.ForegroundColor;
                        console.ForegroundColor = ConsoleColor.Green;
                        console.Out.WriteLine($"{i + 1}: a={a} b={b}  ; {result.Item2} {resultAsBinary}");
                        console.ForegroundColor = prevColor;
                    }
                    else
                    {
                        var prevColor = console.ForegroundColor;
                        console.ForegroundColor = ConsoleColor.Red;
                        console.Out.WriteLine($"{i + 1}: not successful");
                        console.ForegroundColor = prevColor;
                    }
                }
            }

            console.Out.WriteLine(
                $"Grover-Iterations {cmdLineArguments.GroverIterations}: {successfulCount} of {arguments.Repeats} had the desired result.");
            return(successfulCount);
        }