示例#1
0
# Construct a problem
#bqm = dimod.BinaryQuadraticModel({}, {'ab': 1, 'bc': -1, 'ca': 1}, 0, dimod.SPIN)

with open('bqp1000_01.qubo') as problem:
    bqm = dimod.BinaryQuadraticModel.from_coo(problem)

subproblem_size = 100

print("BQM size: {}, subproblem size: {}".format(len(bqm), subproblem_size))
# Classical solvers
#subproblem = hybrid.EnergyImpactDecomposer(size=50, rolling_history=0.15)
#subproblem = hybrid.EnergyImpactDecomposer(size=1024, rolling_history=0.15, traversal="bfs")
# Parallel subproblem
subproblem = hybrid.Unwind(
    #hybrid.EnergyImpactDecomposer(size=subproblem_size, rolling_history=0.15, traversal="bfs")
    hybrid.EnergyImpactDecomposer(size=subproblem_size, rolling_history=0.1))

# QPU
#subsampler = hybrid.QPUSubproblemAutoEmbeddingSampler() | hybrid.SplatComposer()

subsampler = hybrid.Map(
    hybrid.QPUSubproblemAutoEmbeddingSampler()) | hybrid.Reduce(
        hybrid.Lambda(merge_substates)) | hybrid.SplatComposer()

# Define the workflow
# iteration = hybrid.RacingBranches(
#     hybrid.InterruptableTabuSampler(),
#     hybrid.EnergyImpactDecomposer(size=2)
#     | hybrid.QPUSubproblemAutoEmbeddingSampler()
#     | hybrid.SplatComposer()
H = 0

for i in range(len(arr)):
    if (i % 10 == 0):  #high impact variables
        H += 1000 * arr[i] * Spin(f'x_{i}')
        continue
    H += arr[i] * Spin(f'x_{i}')

H *= H
print("Compiling...")
model = H.compile()
bqm = model.to_bqm()
print("OK")

import hybrid
hybrid.logger.setLevel(hybrid.logging.DEBUG)

workflow = hybrid.Loop(hybrid.RacingBranches(
    hybrid.InterruptableSimulatedAnnealingProblemSampler(),
    hybrid.EnergyImpactDecomposer(size=10, rolling=True, rolling_history=.3)
    | hybrid.QPUSubproblemAutoEmbeddingSampler(num_reads=2)
    | hybrid.SplatComposer()) | hybrid.ArgMin(),
                       convergence=3)

# not our workflow
result = hybrid.KerberosSampler().sample(bqm)
hybrid.Unwind(workflow)
print("Solution: sample={}".format(result.first))
#hybrid.print_structure(workflow)
print("-----------------------")
#hybrid.print_counters(workflow)
示例#3
0
h = {v: 0.0 for v in graph.nodes}
J = {edge: random.choice([-1, 1]) for edge in graph.edges}
bqm = dimod.BQM(h, J, offset=0, vartype=dimod.SPIN)


# define a qbsolv-like workflow
def merge_substates(_, substates):
    a, b = substates
    return a.updated(
        subsamples=hybrid.hstack_samplesets(a.subsamples, b.subsamples))


# Redefine the workflow: a rolling decomposition window
#subproblem = hybrid.EnergyImpactDecomposer(size=50, rolling_history=0.15)
subproblem = hybrid.Unwind(
    hybrid.EnergyImpactDecomposer(size=100,
                                  rolling_history=0.15,
                                  traversal='bfs'))
# QPU
#subsampler = hybrid.QPUSubproblemAutoEmbeddingSampler() | hybrid.SplatComposer()
qpu_sampler = hybrid.Map(
    hybrid.QPUSubproblemAutoEmbeddingSampler()) | hybrid.Reduce(
        hybrid.Lambda(merge_substates)) | hybrid.SplatComposer()

rand_sampler = hybrid.Map(hybrid.RandomSubproblemSampler()) | hybrid.Reduce(
    hybrid.Lambda(merge_substates)) | hybrid.SplatComposer()

iteration = hybrid.RacingBranches(
    hybrid.InterruptableTabuSampler(),
    subproblem | qpu_sampler) | hybrid.ArgMin() | hybrid.TrackMin(output=True)

workflow = hybrid.LoopUntilNoImprovement(iteration, convergence=5)
示例#4
0
import hybrid

# load a problem
problem = sys.argv[1]
with open(problem) as fp:
    bqm = dimod.BinaryQuadraticModel.from_coo(fp)


# define a qbsolv-like workflow
def merge_substates(_, substates):
    a, b = substates
    return a.updated(
        subsamples=hybrid.hstack_samplesets(a.subsamples, b.subsamples))


subproblems = hybrid.Unwind(
    hybrid.EnergyImpactDecomposer(size=50, rolling_history=0.15))

qpu = hybrid.Map(hybrid.QPUSubproblemAutoEmbeddingSampler()) | hybrid.Reduce(
    hybrid.Lambda(merge_substates)) | hybrid.SplatComposer()

random = hybrid.Map(hybrid.RandomSubproblemSampler()) | hybrid.Reduce(
    hybrid.Lambda(merge_substates)) | hybrid.SplatComposer()

subsampler = hybrid.Parallel(qpu, random, endomorphic=False) | hybrid.ArgMin()

iteration = hybrid.Race(hybrid.InterruptableTabuSampler(),
                        subproblems | subsampler) | hybrid.ArgMin()

main = hybrid.Loop(iteration, max_iter=10, convergence=3)

# run the workflow
示例#5
0

# load a problem
problem = sys.argv[1]
with open(problem) as fp:
    bqm = dimod.BinaryQuadraticModel.from_coo(fp)

# define the solver
def merge_substates(component, substates):
    a, b = substates
    return a.updated(
        subsamples=hybrid.utils.meld_samplesets(a.subsamples, b.subsamples)
    )

subproblems = hybrid.Unwind(
    hybrid.EnergyImpactDecomposer(size=50, silent_rewind=False, rolling_history=0.15)
)

qpu = hybrid.Map(
    hybrid.QPUSubproblemAutoEmbeddingSampler()
) | hybrid.Reduce(
    hybrid.Lambda(merge_substates)
) | hybrid.SplatComposer()

random = hybrid.Map(
    hybrid.RandomSubproblemSampler()
) | hybrid.Reduce(
    hybrid.Lambda(merge_substates)
) | hybrid.SplatComposer()

subsampler = hybrid.Parallel(qpu, random, endomorphic=False) | hybrid.ArgMin()