Repository: quantumlib/Cirq

Issue:
Factoring 21 using Shor algorithm allocates 1TB unitary
**Description of the issue**

An attempt to factor 21 using cirq's [Shor algorithm example](https://github.com/quantumlib/Cirq/blob/master/examples/shor.py) attempts to allocate 1TB unitary.

**How to reproduce the issue**

As shown by the description of the [original PR](https://github.com/quantumlib/Cirq/pull/2436), it used to work a while back, but now fails like this:

```
$ time python examples/shor.py 21 --order_finder=quantum
Traceback (most recent call last):
  File "examples/shor.py", line 380, in <module>
    main(n=args.n, order_finder=ORDER_FINDERS[args.order_finder])
  File "examples/shor.py", line 362, in main
    d = find_factor(n, order_finder)
  File "examples/shor.py", line 340, in find_factor
    r = order_finder(x, n)
  File "examples/shor.py", line 279, in quantum_order_finder
    result = cirq.sample(circuit)
  File "/usr/local/google/home/viathor/src/Cirq/cirq/sim/mux.py", line 71, in sample
    if _is_clifford_circuit(program):
  File "/usr/local/google/home/viathor/src/Cirq/cirq/sim/mux.py", line 45, in _is_clifford_circuit
    for op in program.all_operations())
  File "/usr/local/google/home/viathor/src/Cirq/cirq/sim/mux.py", line 45, in <genexpr>
    for op in program.all_operations())
  File "/usr/local/google/home/viathor/src/Cirq/cirq/sim/clifford/clifford_simulator.py", line 69, in is_supported_operation
    u = unitary(op)
  File "/usr/local/google/home/viathor/src/Cirq/cirq/protocols/unitary_protocol.py", line 130, in unitary
    result = strat(val)
  File "/usr/local/google/home/viathor/src/Cirq/cirq/protocols/unitary_protocol.py", line 175, in _strat_unitary_from_apply_unitary
    state = qis.eye_tensor(val_qid_shape, dtype=np.complex128)
  File "/usr/local/google/home/viathor/src/Cirq/cirq/qis/states.py", line 585, in eye_tensor
    identity = np.eye(np.prod(half_shape, dtype=int), dtype=dtype)
  File "/usr/local/google/home/viathor/.virtualenvs/cirq/lib/python3.7/site-packages/numpy/lib/twodim_base.py", line 201, in eye
    m = zeros((N, M), dtype=dtype, order=order)
MemoryError: Unable to allocate array with shape (262144, 262144) and data type complex128

real    0m9.040s
user    0m9.353s
sys     0m1.298s
```

**Cirq version**
You can get the cirq version by printing `cirq.__version__`. From the command line:

```
$ python -c 'import cirq; print(cirq.__version__)'
0.9.0.dev
```




Discussion:

Resolving patch:
diff --git a/cirq/sim/clifford/clifford_simulator.py b/cirq/sim/clifford/clifford_simulator.py
--- a/cirq/sim/clifford/clifford_simulator.py
+++ b/cirq/sim/clifford/clifford_simulator.py
@@ -66,9 +66,9 @@ def is_supported_operation(op: 'cirq.Operation') -> bool:
         if isinstance(op.gate, cirq.MeasurementGate): return True
         if isinstance(op, GlobalPhaseOperation): return True
         if not protocols.has_unitary(op): return False
-        u = unitary(op)
-        if u.shape == (2, 2):
-            return not SingleQubitCliffordGate.from_unitary(u) is None
+        if len(op.qubits) == 1:
+            u = unitary(op)
+            return SingleQubitCliffordGate.from_unitary(u) is not None
         else:
             return op.gate in [cirq.CNOT, cirq.CZ]
 


Test patch: