Beispiel #1
0
def CalculateRoute(CostMatrix, solverSetting: SolverSetting, workspace):

    ##### Initialize solver specificationss -- In all cases we do parameter-free solving because we don't want to do manual tuning
    SolverName = solverSetting.SolverName  # Name of the solver
    HardwareSpec = solverSetting.Hardware  # The hardware, CPU or FPGA
    Timeout = solverSetting.Timeout  # The timeout for the solver, in seconds

    ##### Raise errors if invalid request - Some solvers require specific user inputs
    if (SolverName.lower() == 'quantum monte carlo'):
        raise HTTPException(
            status_code=400,
            detail=
            'Can only implement a Quantum Monte Carlo with paramtrization (manual tuning). It has been disabled for this demo. Check the QIO docs.'
        )

    if (SolverName.lower() == 'tabu search' or SolverName.lower()
            == 'quantum monte carlo' or SolverName.lower()
            == 'parallel tempering') and (HardwareSpec.lower() == 'fpga'):
        raise HTTPException(
            status_code=400,
            detail=
            'Can only implement Tabu search, Quantum Monte Carlo, Parallel Tempering on CPU. Check the QIO docs.'
        )

    ##### The type of hardware the solver will run on
    if HardwareSpec.lower() == 'cpu':
        HardwareInt = 1
    elif HardwareSpec.lower() == 'fpga':
        HardwareInt = 2
    else:
        raise HTTPException(
            status_code=400,
            detail='Hardware choice not valid - choose from CPU/FPGA')

    ##### The solver algorithm initialized
    if SolverName.lower() == 'simulated annealing':
        solver = SimulatedAnnealing(workspace,
                                    timeout=Timeout,
                                    platform=HardwareInt)
    elif SolverName.lower() == 'parallel tempering':
        solver = ParallelTempering(workspace, timeout=Timeout)
    elif SolverName.lower() == 'tabu search':
        solver = Tabu(workspace, timeout=Timeout)
    elif SolverName.lower() == 'quantum monte carlo':
        solver = QuantumMonteCarlo(workspace, timeout=Timeout)

    ##### Submit the optimization problem to the QIO workspace
    OptimizationProblem = OptProblem(
        CostMatrix)  # Initialize the optimization problem instance
    result = solver.submit(
        OptimizationProblem
    )  # We submit because we want to track the status of the optimization - means we do not immediately have a solution, will need to fetch it later

    ##### Return the optimization result
    return result
                   problem_type=ProblemType.ising,
                   terms=terms)


# This array contains a list of the weights of the containers
container_weights = [1, 5, 9, 21, 35, 5, 3, 5, 10, 11]

# Create a problem for the list of containers:
problem = create_problem_for_container_weights(container_weights)

# Submit problem to Azure Quantum using the ParallelTempering solver:
from azure.quantum.optimization import ParallelTempering
import time

# Instantiate a solver to solve the problem.
solver = ParallelTempering(workspace, timeout=100)

# Optimize the problem
print('Submitting problem...')
start = time.time()
result = solver.optimize(problem)
timeElapsed = time.time() - start
print(f'\nResult in {timeElapsed} seconds:\n{result}\n')


# Print out a summary of the results:
def print_result_summary(result):
    # Print a summary of the result
    ship_a_weight = 0
    ship_b_weight = 0
    for container in result['configuration']:
Beispiel #3
0
# This array contains a list of the weights of the containers:
containerWeights = [1, 5, 9, 21, 35, 5, 3, 5, 10, 11]

# Create the Terms for this list of containers:
terms = createProblemForContainerWeights(containerWeights)

# Create the Problem to submit to the solver:
problem = Problem(name="Ship Loading Problem",
                  problem_type=ProblemType.ising,
                  terms=terms)

from azure.quantum.optimization import ParallelTempering

# Instantiate a solver instance to solve the problem
solver = ParallelTempering(workspace, timeout=100)  # timeout in seconds

# Optimize the problem
result = solver.optimize(problem)


def printResultSummary(result):
    # Print a summary of the result
    shipAWeight = 0
    shipBWeight = 0
    for container in result['configuration']:
        containerAssignment = result['configuration'][container]
        containerWeight = containerWeights[int(container)]
        ship = ''
        if containerAssignment == 1:
            ship = 'A'