Repository: BQSKit/bqskit

Issue:
`circuit.dim` is 0 for large circuits
If you print `circuit.dim` for a large (e.g. 1024 qubit) circuit, you will get 0.  The 0 is coming from an integer overflow that numpy does not report.

I don't expect the correct answer (2^1024 is absurdly large), but some feedback (an error or warning) would be nice.  Since numpy silently ignores the issue, BQSKit will have to detect the problem.

Some ideas on where to detect the issue:
- In the implementation of `dim` in `UnitaryMatrix`

Some ideas on how to detect the issue:
- check that `np.prod(self.radixes) >= self.radixes[0]`
- find a way to make numpy raise an error or warning
- do the product more manually than using `np.prod`

Discussion:

Patch:
diff --git a/bqskit/qis/unitary/unitary.py b/bqskit/qis/unitary/unitary.py
index fb797ec..b9f9966 100644
--- a/bqskit/qis/unitary/unitary.py
+++ b/bqskit/qis/unitary/unitary.py
@@ -54,7 +54,14 @@ class Unitary(metaclass=UnitaryMeta):
         if hasattr(self, '_dim'):
             return self._dim
 
-        return int(np.prod(self.radixes))
+        # return int(np.prod(self.radixes))
+        # Above line removed due to failure to handle overflow and
+        # underflows for large dimensions.
+
+        acm = 1
+        for radix in self.radixes:
+            acm *= int(radix)
+        return acm
 
     @abc.abstractmethod
     def get_unitary(self, params: RealVector = []) -> UnitaryMatrix:


Test patch:
diff --git a/tests/qis/unitary/test_props.py b/tests/qis/unitary/test_props.py
new file mode 100644
index 0000000..2df433a
--- /dev/null
+++ b/tests/qis/unitary/test_props.py
@@ -0,0 +1,8 @@
+from __future__ import annotations
+
+from bqskit.ir.circuit import Circuit
+
+
+def test_circuit_dim_overflow() -> None:
+    c = Circuit(1024)
+    assert c.dim != 0