for j in range(0,N): for i in range(0,N): for k in range(0,N): if k>i: if SolvedSudoku[k,j] == SolvedSudoku[i,j]: #print(f'{j},{i},{k}') print(f'Duplicate int in column {j+1}') InvalidSolution = True print(f"Is this solution valid? {not InvalidSolution}") return InvalidSolution ############################################################################################ ##### Generate cost function OptimizationProblem = SudokuProblem(Sudoku9B) # Choose the solver and parameters --- uncomment if you wish to use a different one solver = SimulatedAnnealing(workspace, timeout = 120) #solver = ParallelTempering(workspace, timeout = 120) #solver = Tabu(workspace, timeout = 120) #solver = QuantumMonteCarlo(workspace, sweeps = 2, trotter_number = 10, restarts = 72, seed = 22, beta_start = 0.1, transverse_field_start = 10, transverse_field_stop = 0.1) # QMC is not available parameter-free yet SolverSolution = solver.optimize(OptimizationProblem) SolvedSudoku = ReadResults(SolverSolution['configuration'], Sudoku9B)
terms = c1 + c2 + c3 + c4 from azure.quantum.optimization import Problem, ProblemType from azure.quantum.optimization import SimulatedAnnealing # Change this line to match the Azure Quantum Optimization solver type you wish to use # Problem type is PUBO in this instance. You could also have chosen to represent the problem in Ising form. problem = Problem(name="Job shop sample", problem_type=ProblemType.pubo, terms=terms) # Provide details of your workspace, created at the beginning of this tutorial # Provide the name of the solver you wish to use for this problem (as imported above) solver = SimulatedAnnealing(workspace, timeout=100) # Timeout in seconds # Run job synchronously result = solver.optimize(problem) config = result['configuration'] # Run job asynchronously # Alternatively, a job can be run asynchronously, as shown below: """ ## Submit problem to solver job = solver.submit(problem) print(job.id) ## Get job status job.refresh() print(job.details.status) ## Get results result = job.get_results()
raise RuntimeError('This solution is not valid -- Traveled to a non-starting node more than once') PastNodes.append(Path[k][1]) print(f"Number of different nodes passed = {NumNodes}. This is valid!") ############################################################################################ ##### Check if the end node is same as the start node if Path[1][1] != Path[-1][1]: raise RuntimeError(f'This solution is not valid -- Start node {Path[1][1]} is not equal to end node {Path[-1][1]}') print('Start and end node are the same. This is valid!') print('Valid route!') ############################################################################################ # Generate cost function OptimizationProblem = OptProblem(CostMatrix) # Choose the solver and parameters --- uncomment if you wish to use a different one solver = SimulatedAnnealing(workspace, timeout = 120) #solver = ParallelTempering(workspace, timeout = 120) #solver = Tabu(workspace, timeout = 120) #solver = QuantumMonteCarlo(workspace, sweeps = 2, trotter_number = 10, restarts = 72, seed = 22, beta_start = 0.1, transverse_field_start = 10, transverse_field_stop = 0.1) # QMC is not available parameter-free yet route = solver.optimize(OptimizationProblem) # Synchronously submit the optimization problem to the service -- wait until done. print(route) # Call the function to interpret/convert/analyze the optimization results into a more meaningful/understandable format PathDict = ReadResults(route['configuration'], NodeName, CostMatrix, NumNodes) print(PathDict)
raise RuntimeError( 'This solution is not correct -- Traveled to a non-starting node more than once' ) PastNodes.append(Path[k][1]) print(f"Number of different nodes passed = {NumNodes}. This is correct!") ############################################################################################ ##### Check if the end nodes is same as the start node if Path[1][1] != Path[-1][1]: raise RuntimeError( f'This solution is not correct -- Start node {Path[1][1]} is not equal to end node {Path[-1][1]}' ) print('Start and end node are the same. This is correct!') print('Valid route!') ##### Call the function to interpret/convert/analyze the optimization results into a more meaningful/understandable format OptimizationProblem = OptProblem(CostMatrix) #select QIO solver solver = SimulatedAnnealing(workspace, timeout=120) #solver = ParallelTempering(workspace, timeout = 120) #solver = Tabu(workspace, timeout = 120) #solver = QuantumMonteCarlo(workspace, timeout = 120) route = solver.optimize( OptimizationProblem) # Solve the optimization problem -- wait until done. PathDict = ReadResults(route['configuration'], NodeName, CostMatrix, NumNodes) print(PathDict)