Repository: quantumlib/OpenFermion
Pull request #712 (2021-02-26 03:56:26)

Added IsingOperator to count_qubits.
I've noticed that `count_qubits` in `operator_utils` doesn't handle `IsingOperator` and couldn't find a good reason why not, so I added it.

google-cla[bot]: 
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: **Please visit <https://cla.developers.google.com/> to sign.**

Once you've signed (or fixed any issues), please reply here with `@googlebot I signed it!` and we'll verify it.

----

#### What to do if you already signed the CLA

##### Individual signers

*   It's possible we don't have your GitHub username or you're using a different email address on your commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).

##### Corporate signers

*   Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the Google project maintainer to [go/cla#troubleshoot](http://go/cla#troubleshoot) ([Public version](https://opensource.google/docs/cla/#troubleshoot)).
*   The email used to register you as an authorized contributor must be the email used for the Git commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).
*   The email used to register you as an authorized contributor must also be [attached to your GitHub account](https://github.com/settings/emails).
		

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2Fquantumlib%2FOpenFermion%2Fpull%2F712) for more info**.

<!-- need_sender_cla -->
google-cla[bot]: 
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: **Please visit <https://cla.developers.google.com/> to sign.**

Once you've signed (or fixed any issues), please reply here with `@googlebot I signed it!` and we'll verify it.

----

#### What to do if you already signed the CLA

##### Individual signers

*   It's possible we don't have your GitHub username or you're using a different email address on your commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).

##### Corporate signers

*   Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the Google project maintainer to [go/cla#troubleshoot](http://go/cla#troubleshoot) ([Public version](https://opensource.google/docs/cla/#troubleshoot)).
*   The email used to register you as an authorized contributor must be the email used for the Git commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).
*   The email used to register you as an authorized contributor must also be [attached to your GitHub account](https://github.com/settings/emails).
		

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2Fquantumlib%2FOpenFermion%2Fpull%2F712) for more info**.

<!-- need_sender_cla -->
ncrubin: @mstechly Thank you.  Just a minor question and then we're good to go!
google-cla[bot]: We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors.  If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA ([login here](https://cla.developers.google.com/) to double check)?  If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment `@googlebot I fixed it.`. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2Fquantumlib%2FOpenFermion%2Fpull%2F712) for more info**.

<!-- need_author_cla -->

Patch:
diff --git a/src/openfermion/utils/operator_utils.py b/src/openfermion/utils/operator_utils.py
index 83d6601..8754394 100644
--- a/src/openfermion/utils/operator_utils.py
+++ b/src/openfermion/utils/operator_utils.py
@@ -21,7 +21,7 @@ from scipy.sparse import spmatrix
 from openfermion.config import DATA_DIRECTORY, EQ_TOLERANCE
 from openfermion.ops.operators import (BosonOperator, FermionOperator,
                                        MajoranaOperator, QuadOperator,
-                                       QubitOperator)
+                                       QubitOperator, IsingOperator)
 from openfermion.ops.representations import (PolynomialTensor,
                                              DiagonalCoulombHamiltonian,
                                              InteractionOperator,
@@ -184,6 +184,15 @@ def count_qubits(operator):
     elif isinstance(operator, PolynomialTensor):
         return operator.n_qubits
 
+    # Handle IsingOperator
+    elif isinstance(operator, IsingOperator):
+        num_qubits = 0
+        for term in operator.terms:
+            if term:
+                if term[-1][0] + 1 > num_qubits:
+                    num_qubits = term[-1][0] + 1
+        return num_qubits
+
     # Raise for other classes.
     else:
         raise TypeError('Operator of invalid type.')


Test patch:
diff --git a/src/openfermion/utils/operator_utils_test.py b/src/openfermion/utils/operator_utils_test.py
index 4cd86f5..c651261 100644
--- a/src/openfermion/utils/operator_utils_test.py
+++ b/src/openfermion/utils/operator_utils_test.py
@@ -27,7 +27,7 @@ from openfermion.config import DATA_DIRECTORY
 from openfermion.hamiltonians import fermi_hubbard
 from openfermion.ops.operators import (FermionOperator, MajoranaOperator,
                                        BosonOperator, QubitOperator,
-                                       QuadOperator)
+                                       QuadOperator, IsingOperator)
 from openfermion.ops.representations import InteractionOperator
 from openfermion.transforms.opconversions import (jordan_wigner, bravyi_kitaev)
 from openfermion.transforms.repconversions import get_interaction_operator
@@ -50,6 +50,7 @@ class OperatorUtilsTest(unittest.TestCase):
         self.qubit_operator = jordan_wigner(self.fermion_operator)
         self.interaction_operator = get_interaction_operator(
             self.fermion_operator)
+        self.ising_operator = IsingOperator("[Z0] + [Z1] + [Z2] + [Z3] + [Z4]")
 
     def test_n_qubits_majorana_operator(self):
         self.assertEqual(self.n_qubits, count_qubits(self.majorana_operator))
@@ -66,6 +67,9 @@ class OperatorUtilsTest(unittest.TestCase):
     def test_n_qubits_interaction_operator(self):
         self.assertEqual(self.n_qubits, count_qubits(self.interaction_operator))
 
+    def test_n_qubits_ising_operator(self):
+        self.assertEqual(self.n_qubits, count_qubits(self.ising_operator))
+
     def test_n_qubits_bad_type(self):
         with self.assertRaises(TypeError):
             count_qubits('twelve')