Beispiel #1
0
        static void Main(string[] args)
        {
            // Reference answer for 4x4 sudoku we'll be using as an example.
            int[,] answer4 =
            {
                { 1, 2, 3, 4 },
                { 3, 4, 1, 2 },
                { 2, 3, 4, 1 },
                { 4, 1, 2, 3 }
            };

            // Solve 4x4 puzzle with 7 missing numbers (fast).
            int[,] puzzle4_3 =
            {
                { 0, 2, 0, 4 },
                { 3, 0, 0, 2 },
                { 0, 3, 0, 1 },
                { 4, 0, 2, 3 }
            };
            Console.WriteLine("Solving 4x4 Sudoku:");
            ShowGrid(puzzle4_3);
            SudokuQuantum sudokuQuantum = new SudokuQuantum();
            bool          resultFound   = sudokuQuantum.QuantumSolve(puzzle4_3).Result;

            VerifyAndShowResult(resultFound, puzzle4_3, answer4);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var puzzleToRun = args.Length > 0 ? args[0] : "all";

            var sim = new QuantumSimulator(throwOnReleasingQubitsNotInZeroState: true);

            int[,] answer4 =
            {
                { 1, 2, 3, 4 },
                { 3, 4, 1, 2 },
                { 2, 3, 4, 1 },
                { 4, 1, 2, 3 }
            };

            int[,] answer9 =
            {
                { 6, 7, 3, 8, 9, 4, 5, 1, 2 },
                { 9, 1, 2, 7, 3, 5, 4, 8, 6 },
                { 8, 4, 5, 6, 1, 2, 9, 7, 3 },
                { 7, 9, 8, 2, 6, 1, 3, 5, 4 },
                { 5, 2, 6, 4, 7, 3, 8, 9, 1 },
                { 1, 3, 4, 5, 8, 9, 2, 6, 7 },
                { 4, 6, 9, 1, 2, 8, 7, 3, 5 },
                { 2, 8, 7, 3, 5, 6, 1, 4, 9 },
                { 3, 5, 1, 9, 4, 7, 6, 2, 8 }
            };

            SudokuClassic sudokuClassic = new SudokuClassic();
            SudokuQuantum sudokuQuantum = new SudokuQuantum();

            if (puzzleToRun == "4x4-classic" || puzzleToRun == "all")
            {
                // Test solving a 4x4 Sudoku puzzle using classical computing.
                // Missing numbers are denoted by 0.
                int[,] puzzle4 =
                {
                    { 0, 2, 0, 4 },
                    { 3, 0, 0, 2 },
                    { 0, 0, 4, 1 },
                    { 4, 0, 2, 0 }
                };
                Console.WriteLine("Solving 4x4 using classical computing.");
                ShowGrid(puzzle4);
                bool resultFound = sudokuClassic.SolveSudokuClassic(puzzle4);
                VerifyAndShowResult(resultFound, puzzle4, answer4);
            }
            if (puzzleToRun == "4x4-1" || puzzleToRun == "all")
            {
                // Testing solving an easy 4x4 puzzle with only 1 missing number with Quantum.
                int[,] puzzle4_1 =
                {
                    { 0, 2, 3, 4 },
                    { 3, 4, 1, 2 },
                    { 2, 3, 4, 1 },
                    { 4, 1, 2, 3 }
                };
                Console.WriteLine("Quantum Solving 4x4 with 1 missing number.");
                ShowGrid(puzzle4_1);
                bool resultFound = sudokuQuantum.QuantumSolve(puzzle4_1, sim).Result;
                VerifyAndShowResult(resultFound, puzzle4_1, answer4);
            }
            if (puzzleToRun == "4x4-3" || puzzleToRun == "all")
            {
                // Test 4x4 puzzle with 3 missing numbers with Quantum.
                int[,] puzzle4_3 =
                {
                    { 0, 2, 3, 4 },
                    { 3, 0, 1, 2 },
                    { 2, 3, 4, 1 },
                    { 4, 0, 2, 3 }
                };
                Console.WriteLine("Quantum Solving 4x4 with 3 missing numbers.");
                ShowGrid(puzzle4_3);
                bool resultFound = sudokuQuantum.QuantumSolve(puzzle4_3, sim).Result;
                VerifyAndShowResult(resultFound, puzzle4_3, answer4);
            }
            if (puzzleToRun == "4x4-4" || puzzleToRun == "all")
            {
                // Test 4x4 puzzle with 4 missing numbers with Quantum.
                int[,] puzzle4_4 =
                {
                    { 0, 0, 3, 4 },
                    { 0, 0, 1, 2 },
                    { 2, 3, 4, 1 },
                    { 4, 1, 2, 3 }
                };
                Console.WriteLine("Quantum Solving 4x4 with 4 missing numbers.");
                ShowGrid(puzzle4_4);
                bool resultFound = sudokuQuantum.QuantumSolve(puzzle4_4, sim).Result;
                VerifyAndShowResult(resultFound, puzzle4_4, answer4);
            }
            if (puzzleToRun == "9x9-1" || puzzleToRun == "all")
            {
                // Test 9x9 puzzle with classical and quantum - 1 missing number.
                int[,] puzzle9_1 =
                {
                    { 0, 7, 3, 8, 9, 4, 5, 1, 2 },
                    { 9, 1, 2, 7, 3, 5, 4, 8, 6 },
                    { 8, 4, 5, 6, 1, 2, 9, 7, 3 },
                    { 7, 9, 8, 2, 6, 1, 3, 5, 4 },
                    { 5, 2, 6, 4, 7, 3, 8, 9, 1 },
                    { 1, 3, 4, 5, 8, 9, 2, 6, 7 },
                    { 4, 6, 9, 1, 2, 8, 7, 3, 5 },
                    { 2, 8, 7, 3, 5, 6, 1, 4, 9 },
                    { 3, 5, 1, 9, 4, 7, 6, 2, 8 }
                };
                int[,] puzzle9_1_copy = CopyIntArray(puzzle9_1);

                Console.WriteLine("Solving 9x9 with 1 missing number using classical computing.");
                ShowGrid(puzzle9_1);
                bool resultFound = sudokuClassic.SolveSudokuClassic(puzzle9_1);
                VerifyAndShowResult(resultFound, puzzle9_1, answer9);

                Console.WriteLine("Solving 9x9 with 1 missing number using Quantum Computing.");
                ShowGrid(puzzle9_1_copy);
                resultFound = sudokuQuantum.QuantumSolve(puzzle9_1_copy, sim).Result;
                VerifyAndShowResult(resultFound, puzzle9_1_copy, answer9);
            }
            if (puzzleToRun == "9x9-2" || puzzleToRun == "all")
            {
                // Test 9x9 puzzle with quantum - 2 missing numbers.
                int[,] puzzle9_2 =
                {
                    { 0, 7, 3, 8, 9, 4, 5, 1, 2 },
                    { 9, 0, 2, 7, 3, 5, 4, 8, 6 },
                    { 8, 4, 5, 6, 1, 2, 9, 7, 3 },
                    { 7, 9, 8, 2, 6, 1, 3, 5, 4 },
                    { 5, 2, 6, 4, 7, 3, 8, 9, 1 },
                    { 1, 3, 4, 5, 8, 9, 2, 6, 7 },
                    { 4, 6, 9, 1, 2, 8, 7, 3, 5 },
                    { 2, 8, 7, 3, 5, 6, 1, 4, 9 },
                    { 3, 5, 1, 9, 4, 7, 6, 2, 8 }
                };
                Console.WriteLine("Solving 9x9 with 2 missing numbers using Quantum Computing.");
                ShowGrid(puzzle9_2);
                bool resultFound = sudokuQuantum.QuantumSolve(puzzle9_2, sim).Result;
                VerifyAndShowResult(resultFound, puzzle9_2, answer9);
            }
            if (puzzleToRun == "9x9-64" || puzzleToRun == "all")
            {
                // Test hard 9x9 puzzle with classical and quantum.
                int[,] puzzle9 =
                {
                    { 0, 0, 0, 0, 0, 0, 0, 1, 2 },
                    { 0, 0, 0, 0, 3, 5, 0, 0, 0 },
                    { 0, 0, 0, 6, 0, 0, 0, 7, 0 },
                    { 7, 0, 0, 0, 0, 0, 3, 0, 0 },
                    { 0, 0, 0, 4, 0, 0, 8, 0, 0 },
                    { 1, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 1, 2, 0, 0, 0, 0 },
                    { 0, 8, 0, 0, 0, 0, 0, 4, 0 },
                    { 0, 5, 0, 0, 0, 0, 6, 0, 0 }
                };
                int[,] puzzle9_copy = CopyIntArray(puzzle9);

                Console.WriteLine("Solving 9x9 with 64 missing numbers using classical computing.");
                ShowGrid(puzzle9);
                bool resultFound = sudokuClassic.SolveSudokuClassic(puzzle9);
                VerifyAndShowResult(resultFound, puzzle9, answer9);

                Console.WriteLine("Solving 9x9 with 64 missing numbers using Quantum Computing. Cntrl-C to stop.");
                ShowGrid(puzzle9_copy);
                resultFound = sudokuQuantum.QuantumSolve(puzzle9_copy, sim).Result;
                VerifyAndShowResult(resultFound, puzzle9_copy, answer9);
            }
            Console.WriteLine("Finished.");
        }