Skip to content
This repository has been archived by the owner on Jan 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #115 from bellert/alphabet
Browse files Browse the repository at this point in the history
Add bqm_index_labels decorator
  • Loading branch information
bellert authored Mar 29, 2018
2 parents 94c1166 + cbc6cea commit 6fe74e7
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
9 changes: 6 additions & 3 deletions python/dwave_qbsolv/dimod_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ def __init__(self):
'verbosity': [], 'timeout': [], 'solver_limit': [], 'solver': [],
'target': [], 'find_max': [], 'sample_kwargs': []}

def sample_qubo(self, Q, num_repeats=50, seed=None, algorithm=None,
verbosity=-1, timeout=2592000, solver_limit=None, solver=None,
target=None, find_max=False, **sample_kwargs):
@dimod.decorators.bqm_index_labels
def sample(self, bqm, num_repeats=50, seed=None, algorithm=None,
verbosity=-1, timeout=2592000, solver_limit=None, solver=None,
target=None, find_max=False, **sample_kwargs):
"""Sample low-energy states defined by a QUBO using qbsolv.
Note:
Expand Down Expand Up @@ -84,11 +85,13 @@ def sample_qubo(self, Q, num_repeats=50, seed=None, algorithm=None,
raise ValueError("num_repeats must be a positive integer")

# pose the QUBO to qbsolv
Q, offset = bqm.to_qubo()
samples, energies, counts = run_qbsolv(Q=Q, num_repeats=num_repeats, seed=seed, algorithm=algorithm,
verbosity=verbosity, timeout=timeout, solver_limit=solver_limit,
solver=solver, target=target, find_max=find_max, sample_kwargs=sample_kwargs)

# load the response
response = dimod.Response.from_dicts(samples, {'energy': energies, 'num_occurrences': counts}, dimod.BINARY)
response.change_vartype(bqm.vartype, {'energy': [offset] * len(energies)})

return response
Empty file.
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from __future__ import absolute_import
from tests.test_can_call import *
from tests.test_dwave_qbsolv import *
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ def test_energy_calculation(self):
response = qbs.QBSolv().sample_ising(h, J)

for sample, energy in response.data(['sample', 'energy']):
self.assertLessEqual(abs(dimod.ising_energy(sample, h, J) - energy), 10**-5)
self.assertAlmostEqual(dimod.ising_energy(sample, h, J), energy)

0 comments on commit 6fe74e7

Please sign in to comment.