Repository: rigetticomputing/pyquil (python)

Issue:
Throwaway MEASURE instructions are not properly dealt with in classical register autodetection
Running a Quil simple program containing MEASURE instruction that does not store its result, such as

```
X 0
MEASURE 0
```
results into an error due to the fact that `get_classical_addresses_from_program` is not equipped to handle this case:

```
AttributeError: 'NoneType' object has no attribute 'address'
```

Pull request:
Support throwaway MEASURE instructions
Fixes #497 and implements tests to cover the issue.

Patch:
diff --git a/pyquil/quil.py b/pyquil/quil.py
index 7f0f8dd..1fcf09c 100644
--- a/pyquil/quil.py
+++ b/pyquil/quil.py
@@ -740,5 +740,7 @@ def get_classical_addresses_from_program(program):
     """
     # Required to use the `classical_reg.address` int attribute.
     # See https://github.com/rigetticomputing/pyquil/issues/388.
-    return sorted(set([instr.classical_reg.address for instr in program
-                       if isinstance(instr, Measurement)]))
+    return sorted(set([
+        instr.classical_reg.address for instr in program
+        if isinstance(instr, Measurement) and instr.classical_reg is not None
+    ]))


Test patch:
diff --git a/pyquil/tests/test_quil.py b/pyquil/tests/test_quil.py
index 1c3dca3..e4c6f05 100644
--- a/pyquil/tests/test_quil.py
+++ b/pyquil/tests/test_quil.py
@@ -782,6 +782,25 @@ def test_get_classical_addresses_from_program():
     assert get_classical_addresses_from_program(p) == [0, 1, 3]
 
 
+def test_get_classical_addresses_from_quil_program():
+    """
+    Tests that can get_classical_addresses_from_program can handle both MEASURE
+    quil instructions with and without explicit classical registers.
+    """
+
+    p = Program('\n'.join([
+        'X 0',
+        'MEASURE 0'
+    ]))
+    assert get_classical_addresses_from_program(p) == []
+
+    p = Program('\n'.join([
+        'X 0',
+        'MEASURE 0 [1]'
+    ]))
+    assert get_classical_addresses_from_program(p) == [1]
+
+
 def test_pragma_with_placeholders():
     q = QubitPlaceholder()
     q2 = QubitPlaceholder()