Repository: rigetticomputing/pyquil (python)

Issue:
Creating a gate with all integers throws an error
```python
>>> DefGate("A", np.array([[1, 0], [0, 1]])).out()
```

throws the following error:
```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/steven/workspace/pyquil/pyquil/quilbase.py", line 208, in out
    fcols = [format_matrix_element(col) for col in row]
  File "/Users/steven/workspace/pyquil/pyquil/quilbase.py", line 208, in <listcomp>
    fcols = [format_matrix_element(col) for col in row]
  File "/Users/steven/workspace/pyquil/pyquil/quilbase.py", line 68, in format_matrix_element
    assert False, "Invalid matrix element: %r" % element
AssertionError: Invalid matrix element: 1
```

The type of the integer in a numpy array of all ints is unexpected:
```python
>>> type(np.array([[1, 0], [0, 1]])[0][0])
<class 'numpy.int64'>
```

Pull request description:
Update `format_matrix_element` and `format_parameter` to support num…
…py.int_`

Fixes #138.

Patch:
diff --git a/pyquil/quilbase.py b/pyquil/quilbase.py
index dd92242..2361cff 100644
--- a/pyquil/quilbase.py
+++ b/pyquil/quilbase.py
@@ -261,7 +261,7 @@ class DefGate(AbstractInstruction):
 
             :param element: {int, float, complex, str} The parameterized element to format.
             """
-            if isinstance(element, integer_types) or isinstance(element, (float, complex)):
+            if isinstance(element, integer_types) or isinstance(element, (float, complex, np.int_)):
                 return format_parameter(element)
             elif isinstance(element, string_types):
                 return element
@@ -514,7 +514,7 @@ def format_parameter(element):
 
     :param element: {int, float, long, complex, Slot} Formats a parameter for Quil output.
     """
-    if isinstance(element, integer_types):
+    if isinstance(element, integer_types) or isinstance(element, np.int_):
         return repr(element)
     elif isinstance(element, float):
         return check_for_pi(element)


Test patch:
diff --git a/pyquil/tests/test_quil.py b/pyquil/tests/test_quil.py
index ded1f81..1c0aae3 100644
--- a/pyquil/tests/test_quil.py
+++ b/pyquil/tests/test_quil.py
@@ -582,3 +582,10 @@ def test_pretty_print_pi():
     assert p.out() == 'RZ(0) 0\nRZ(pi) 1\nRZ(-pi) 2\nRZ(2*pi/3) 3\n' \
                       'RZ(0.3490658503988659) 4\n' \
                       'RZ(pi/8) 5\nCPHASE00(-45*pi) 0 1\n'
+
+
+# https://github.com/rigetticomputing/pyquil/issues/138
+def test_defgate_integer_input():
+    dg = DefGate("TEST", np.array([[1, 0],
+                                   [0, 1]]))
+    assert dg.out() == "DEFGATE TEST:\n    1, 0\n    0, 1\n"