Skip to content

Commit

Permalink
0.3.1
Browse files Browse the repository at this point in the history
* Updated README to rst and removed erroneous file

* removed extra file

* updated README to .rst, added badges for coveralls and appveyor

* updated badges

* removed target from badges

* and now on one line...

* more badge stuff

* updates rst

* removed extra file

* updated README to .rst, added badges for coveralls and appveyor

* updated badges

* removed target from badges

* and now on one line...

* more badge stuff

* rst code snippets

* few more readme fixes

* more readme stuff

* added numpy tests and more reasonable returns for empty responses

* 0.3.0 -> 0.3.1 in __init__ for dev branch

* bug fix with relabel having unexpected keyword
  • Loading branch information
arcondello committed Oct 13, 2017
1 parent c14ef18 commit adc7ad0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
11 changes: 4 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@ dimod comes with a few samplers that are useful as reference implementations and
Basic Usage
-----------

```python
>>> import dimod
>>> sampler = dimod.SimulatedAnnealingSampler()
>>> Q = {(0, 0): 1, (1, 1): 1, (0, 1): -1}
>>> response = sampler.sample_qubo(Q)
>>> h = {0: 1, 1 : 1}
>>> J = {(0, 1): -1}
>>> spin_response = sampler.sample_ising(h, J)
```

The response object returned has many ways to access the information

```python
>>> list(response) # your results might vary
[{0: 0.0, 1: 0.0}, {0: 0.0, 1: 0.0}, {0: 0.0, 1: 0.0}, {0: 0.0, 1: 0.0}, {0: 0.0, 1: 0.0}, {0: 1.0, 1: 0.0}, {0: 1.0, 1: 0.0}, {0: 0.0, 1: 1.0}, {0: 0.0, 1: 1.0}, {0: 1.0, 1: 1.0}]
>>> list(response.samples())
Expand All @@ -45,7 +42,7 @@ The response object returned has many ways to access the information
[0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0]
>>> list(response.items()) # samples and energies
[({0: 0.0, 1: 0.0}, 0.0), ({0: 0.0, 1: 0.0}, 0.0), ({0: 0.0, 1: 0.0}, 0.0), ({0: 0.0, 1: 0.0}, 0.0), ({0: 0.0, 1: 0.0}, 0.0), ({0: 1.0, 1: 0.0}, 1.0), ({0: 1.0, 1: 0.0}, 1.0), ({0: 0.0, 1: 1.0}, 1.0), ({0: 0.0, 1: 1.0}, 1.0), ({0: 1.0, 1: 1.0}, 1.0)]
```


See documentation for more examples.

Expand All @@ -54,15 +51,15 @@ Install

Compatible with Python 2 and 3.

`pip install dimod`
>>> pip install dimod

To install with optional components

`pip install dimod[all]`
>>> pip install dimod[all]

To install from source:

`python setup.py install`
>>> python setup.py install

License
-------
Expand Down
2 changes: 1 addition & 1 deletion dimod/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import sys

__version__ = '0.3.0'
__version__ = '0.3.1'
__author__ = 'D-Wave Systems Inc.'
__authoremail__ = 'acondello@dwavesys.com'
__description__ = 'A shared API for binary quadratic model samplers.'
Expand Down
2 changes: 1 addition & 1 deletion dimod/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def _qubo_index_labels(f, *args, **kw):
response = f(*newargs, **kw)

# the returned response will need to be relabelled with inv_relabel
return response.relabel_samples(inv_relabel, copy=True)
return response.relabel_samples(inv_relabel)

return _qubo_index_labels

Expand Down
16 changes: 14 additions & 2 deletions dimod/responses/numpy_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,23 @@ def add_samples_from_array(self, samples, energies, sample_data=None, sorted_by_

def samples_array(self):
"""Returns the :obj:`numpy.ndarray` containing the samples."""
return self._samples
import numpy as np

samples = self._samples
if samples is None:
return np.empty((0, 0), dtype=int)
else:
return samples

def energies_array(self):
"""Returns the :obj:`numpy.ndarray` containing the energies."""
return self._energies
import numpy as np

energies = self._energies
if energies is None:
return np.empty((0,), dtype=float)
else:
return energies


class NumpySpinResponse(NumpyResponse):
Expand Down
38 changes: 38 additions & 0 deletions dimod/responses/tests/test_numpy_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,44 @@ def test_input_checking(self):
with self.assertRaises(ValueError):
response.add_samples_from_array(np.asarray([[0]]), np.asarray([0.0]), [{}, {}])

def test_sample_array_and_energy_array(self):

# first try loading them up by matrices
samples = np.asarray([[-1, -1, -1, -1],
[1, 1, 1, 1],
[-1, 1, -1, 1]])

h = {0: 1, 1: -1, 2: 1, 3: 1}
J = {(0, 2): -1, (2, 3): 1, (0, 2): .25}

energies = np.asarray([ising_energy(h, J, row) for row in samples])

response = self.response_factory()
response.add_samples_from_array(samples, energies)

r_samples = response.samples_array()
r_energies = response.energies_array()

# assert that samples are sorted by energy and that they agree
for idx, row in enumerate(r_samples):
self.assertEqual(ising_energy(h, J, row), r_energies[idx])

for idx in range(len(r_energies) - 1):
self.assertLessEqual(r_energies[idx], r_energies[idx + 1])
self.assertEqual(r_samples.ndim, 2)
self.assertEqual(r_energies.ndim, 1)

# test the empty case
response = self.response_factory()
r_samples = response.samples_array()
r_energies = response.energies_array()

self.assertIsInstance(r_energies, np.ndarray)
self.assertIsInstance(r_samples, np.ndarray)

self.assertEqual(r_samples.ndim, 2)
self.assertEqual(r_energies.ndim, 1)


@unittest.skipUnless(_numpy, "numpy not installed")
class TestSpinResponse(unittest.TestCase, ResponseGenericTests):
Expand Down

0 comments on commit adc7ad0

Please sign in to comment.