Skip to content

Commit

Permalink
Merge pull request #117 from arcondello/response_update_bug
Browse files Browse the repository at this point in the history
Response now makes a shallow copy of data_vectors on instantiation
  • Loading branch information
arcondello authored Feb 28, 2018
2 parents b0e1c9c + 8ad1f32 commit abce02f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions dimod/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(self, samples_matrix, data_vectors, vartype, info=None, variable_la
if 'energy' not in data_vectors:
raise ValueError("energy must be provided")
else:
data_vectors = data_vectors.copy() # shallow copy
data_vectors['energy'] = np.asarray(data_vectors['energy'])
for vector in data_vectors.values():
# todo - check that is a vector and that has the right length
Expand Down
62 changes: 60 additions & 2 deletions tests/test_response.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import division

import unittest

from collections import OrderedDict
Expand All @@ -14,8 +16,19 @@
_pandas = False


def sample_qubo(num_reads, num_variables=2000):
return np.matrix(np.random.randint(0, 2, size=(num_reads, num_variables), dtype='int8'))
def iter_spins(avg=.4):
"""infinite iterator that yields samples converging to the expected average"""

yield 1

total = 1
count = 1

while True:
val = 1 if total / count <= avg else -1
total += val
count += 1
yield val


class TestResponse(unittest.TestCase):
Expand All @@ -40,6 +53,18 @@ def test_instantiation(self):
npt.assert_equal(samples_matrix, response.samples_matrix)
npt.assert_allclose(energies, response.data_vectors['energy'])

def test_data_vector_copy(self):
samples_matrix = np.matrix([[0, 1, 0, 1],
[1, 0, 1, 0],
[0, 0, 0, 0],
[1, 1, 1, 1]])
energies = np.asarray([2, 2, 0, 4], dtype=float)

data_vectors = {'energy': energies}
response = dimod.Response(samples_matrix, data_vectors, dimod.BINARY)

self.assertIsNot(response.data_vectors, data_vectors)

def test_instantiation_without_energy(self):
samples_matrix = np.matrix([[0, 1, 0, 1],
[1, 0, 1, 0],
Expand Down Expand Up @@ -141,6 +166,39 @@ def test_update(self):
for vector in response0.data_vectors.values():
self.assertEqual(len(vector), 4)

def test_update_energy(self):

spingen = iter(iter_spins())

h = {v: v * .2 for v in range(8)}
J = {(u, u + 1): .3 * u * (u + 1) for u in range(7)}

samples = [{v: next(spingen) for v in range(8)}
for __ in range(10)]
energies = [dimod.ising_energy(sample, h, J) for sample in samples]

response = dimod.Response.from_dicts(samples, {'energy': energies}, vartype=dimod.SPIN)

# first make sure that all the energies are good
for sample, energy in response.data(['sample', 'energy']):
self.assertAlmostEqual(energy, dimod.ising_energy(sample, h, J))

# now do a bunch of updates
for __ in range(10):
samples = [{v: next(spingen) for v in range(8)}
for __ in range(10)]
energies = [dimod.ising_energy(sample, h, J) for sample in samples]

new_response = dimod.Response.from_dicts(samples, {'energy': energies}, vartype=dimod.SPIN)

response.update(new_response)

self.assertEqual(len(response), 110)

# and check again
for sample, energy in response.data(['sample', 'energy']):
self.assertAlmostEqual(energy, dimod.ising_energy(sample, h, J))

###############################################################################################
# Viewing a Response
###############################################################################################
Expand Down

0 comments on commit abce02f

Please sign in to comment.