Beispiel #1
0
    def workflow_runner(bqm,
                        factory,
                        energy_threshold=None,
                        qpu=None,
                        **kwargs):
        workflow = factory(qpu=qpu, energy_threshold=energy_threshold)

        init_state = hybrid.State.from_sample(hybrid.min_sample(bqm), bqm)
        with hybrid.tictoc() as timer:
            samples = workflow.run(init_state).result().samples

        return samples, timer
Beispiel #2
0
    def sampler_runner(bqm,
                       factory,
                       energy_threshold=None,
                       qpu=None,
                       **kwargs):
        sampler = factory(init_sample=lambda: hybrid.min_sample(bqm),
                          energy_threshold=energy_threshold,
                          qpu=qpu)

        with hybrid.tictoc() as timer:
            samples = sampler(bqm)

        return samples, timer
problem = sys.argv[1]
with open(problem) as fp:
    bqm = dimod.BinaryQuadraticModel.from_coo(fp)

# construct a Dialectic Search workflow
generate_antithesis = (hybrid.IdentityDecomposer()
                       | hybrid.RandomSubproblemSampler()
                       | hybrid.SplatComposer()
                       | hybrid.TabuProblemSampler())

generate_synthesis = (hybrid.GreedyPathMerge() | hybrid.TabuProblemSampler())

tracker = hybrid.TrackMin()

local_update = hybrid.LoopWhileNoImprovement(
    hybrid.Parallel(hybrid.Identity(), generate_antithesis)
    | generate_synthesis | tracker,
    max_tries=10)

global_update = hybrid.Loop(generate_antithesis | local_update, max_iter=10)

# run the workflow
init_state = hybrid.State.from_sample(hybrid.min_sample(bqm), bqm)
final_state = global_update.run(init_state).result()

# show execution profile
hybrid.profiling.print_counters(global_update)

# show results
print("Solution: sample={.samples.first}".format(tracker.best))