Repository: dwavesystems/dimod

Issue:
Add fix_variables (fix multiple variables at once)
**Application**
For example, given a multiplication circuit CSP, I'd like to do this:

```
In [142]: bqm.fix_variables({'a0': 1, 'a1': 0, 'a2': 1, 'b0': 1, 'b1': 1, 'b2': 0})
```

Instead of this:

```
In [142]: bqm.fix_variable('a0', 1)
In [143]: bqm.fix_variable('a1', 0)
In [144]: bqm.fix_variable('a2', 1)
In [145]: bqm.fix_variable('b0', 1)
In [146]: bqm.fix_variable('b1', 1)
In [147]: bqm.fix_variable('b2', 0)
```

**Proposed Solution**
Enable fixing multiple variables in a single command.

**Alternatives Considered**
Using fix_variables multiple times.

**Additional Context**
I think there are lots of situation where users will fix multiple variables and would like to manipulate the values using dicts.


Discussion:

Patch:
diff --git a/dimod/binary_quadratic_model.py b/dimod/binary_quadratic_model.py
index f6121123..28996cd9 100644
--- a/dimod/binary_quadratic_model.py
+++ b/dimod/binary_quadratic_model.py
@@ -981,6 +981,22 @@ class BinaryQuadraticModel(abc.Sized, abc.Container, abc.Iterable):
         self.add_offset(value * linear[v])
         self.remove_variable(v)
 
+    def fix_variables(self, fixed):
+        """Fix the value of the variables and remove it from a binary quadratic model.
+
+        Args:
+            fixed (dict):
+                A dictionary of variable assignments.
+
+        Examples:
+            >>> bqm = dimod.BinaryQuadraticModel({'a': -.5, 'b': 0., 'c': 5}, {('a', 'b'): -1}, 0.0, dimod.SPIN)
+            >>> bqm.fix_variables({'a': -1, 'b': +1})
+
+        """
+        for v, val in fixed.items():
+            self.fix_variable(v, val)
+
+
     def flip_variable(self, v):
         """Flip variable v in a binary quadratic model.
 
diff --git a/docs/reference/binary_quadratic_model.rst b/docs/reference/binary_quadratic_model.rst
index 882831e2..76150d8e 100644
--- a/docs/reference/binary_quadratic_model.rst
+++ b/docs/reference/binary_quadratic_model.rst
@@ -59,6 +59,7 @@ Transformations
 
    BinaryQuadraticModel.contract_variables
    BinaryQuadraticModel.fix_variable
+   BinaryQuadraticModel.fix_variables
    BinaryQuadraticModel.flip_variable
    BinaryQuadraticModel.relabel_variables
    BinaryQuadraticModel.scale


Test patch:
diff --git a/tests/test_binary_quadratic_model.py b/tests/test_binary_quadratic_model.py
index b43d8e27..7442c21a 100644
--- a/tests/test_binary_quadratic_model.py
+++ b/tests/test_binary_quadratic_model.py
@@ -757,6 +757,11 @@ class TestBinaryQuadraticModel(unittest.TestCase):
         self.assertConsistentBQM(bqm)
         self.assertEqual(bqm, dimod.BinaryQuadraticModel({0: -1, 1: 1}, {(0, 1): -1.}, .5, dimod.SPIN))
 
+    def test_fix_variables(self):
+        bqm = dimod.BinaryQuadraticModel({'a': .3}, {('a', 'b'): -1}, 1.2, dimod.SPIN)
+        bqm.fix_variables({'a': -1, 'b': +1})
+
+
     def test_fix_variable(self):
         # spin model, fix variable to +1
         bqm = dimod.BinaryQuadraticModel({'a': .3}, {('a', 'b'): -1}, 1.2, dimod.SPIN)