From 2b2db15c0c3cbe516870cfbfbff86c8cf657c327 Mon Sep 17 00:00:00 2001 From: Craig Gidney Date: Mon, 16 Jul 2018 16:03:09 -0700 Subject: [PATCH] Work around index-limiting bug in np.eisnum - Just turn on optimize when 26 or more indices --- cirq/circuits/circuit.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cirq/circuits/circuit.py b/cirq/circuits/circuit.py index 3ad17d4d757..9372013b6c2 100644 --- a/cirq/circuits/circuit.py +++ b/cirq/circuits/circuit.py @@ -1155,11 +1155,18 @@ def _apply_unitary_operation(state: np.ndarray, work_indices = tuple(range(k)) data_indices = tuple(range(k, k + d)) used_data_indices = tuple(data_indices[q] for q in target_axes) + input_indices = work_indices + used_data_indices output_indices = list(data_indices) for w, t in zip(work_indices, target_axes): output_indices[t] = w - return np.einsum(matrix, work_indices + used_data_indices, + all_indices = set(input_indices + data_indices + tuple(output_indices)) + + return np.einsum(matrix, input_indices, state, data_indices, output_indices, - out=out) + out=out, + # Note: this is a workaround for a bug in numpy: + # https://github.com/numpy/numpy/issues/10926 + # Turning optimize on actually makes things slower. + optimize=len(all_indices) >= 26)