Beispiel #1
0
# Create empty graph
G = nx.Graph()

# Add edges to the graph (also adds nodes)
G.add_edges_from([(1, 2), (1, 3), (2, 4), (3, 4), (3, 5), (4, 5)])

# ------- Set up our QUBO -------

# Initialize our Q matrix
Q = np.zeros((len(G.nodes), len(G.nodes)))

# Update Q matrix for every edge in the graph
for i, j in G.edges:
    Q[i, i] += 1
    Q[j, j] += 1
    Q[i, j] += 2

qubo = dimod.BinaryQuadraticModel(Q)

# ------- Run our QUBO on SA -------
# Set up SA parameters
numruns = 1

# Run the QUBO on the solver from your config file
sampler = SimulatedAnnealingSampler()
response = sampler.sample_qubo(qubo, num_reads=numruns)
energies = iter(response.data())

print(response)
Beispiel #2
0
#     https://github.com/dwave-examples/simple-ocean-programs/blob/master/Basic_Programs/general_program_qubo.py


# Import the functions and packages that are used
from dwave.system import EmbeddingComposite, DWaveSampler, LeapHybridSampler
from neal import SimulatedAnnealingSampler

# Define the problem as a Python dictionary
Q = {('B','B'): 1, 
    ('K','K'): 1, 
    ('A','C'): 2, 
    ('A','K'): -2, 
    ('B','C'): -2}

###### D-WaveのQPU Samplerを用いる方法です。(Leap時間を消費します!)
# Define the sampler that will be used to run the problem
sampler = EmbeddingComposite(DWaveSampler())

# Run the problem on the sampler and print the results
sampleset = sampler.sample_qubo(Q, num_reads = 10)
print(sampleset)

###### Leap Hybrid samplerを用いる方法です。(Leap時間を消費します!)
sampler_hybrid = LeapHybridSampler()
sampleset_hybrid = sampler_hybrid.sample_qubo(Q,time_limit=3)
print(sampleset_hybrid)

###### Simulated Annealing samplerを用いる方法です。(Leap時間を消費しません)
sampler_neal = SimulatedAnnealingSampler()
sampleset_neal = sampler_neal.sample_qubo(Q, num_reads=10)
print(sampleset_neal.aggregate())
logging.basicConfig(
    stream=sys.stderr,
    format="%(asctime)s.%(msecs)03d [%(name)-15s %(levelname)-5s] %(message)s",
    datefmt='%Y-%m-%dT%H:%M:%S')

logging.getLogger('hepqpr').setLevel(loglevel)

# ==== build model

# load data
dw = DataWrapper.from_path(input_path)
with open(path_join(qubo_path, 'qubo.pickle'), 'rb') as f:
    Q = pickle.load(f)

# sample qubo
response = sampler.sample_qubo(Q)

# get the results
all_doublets = Qallse.process_sample(next(response.samples()))
final_tracks, final_doublets = TrackRecreaterD().process_results(all_doublets)

# compute stats
en0 = dw.compute_energy(Q)
en = response.record.energy[0]
occs = response.record.num_occurrences

p, r, ms = dw.compute_score(final_doublets)
trackml_score = dw.compute_trackml_score(final_tracks)

# print stats
print(f'SAMPLE -- energy: {en:.4f}, ideal: {en0:.4f} (diff: {en-en0:.6f})')