Skip to content

Commit 844a345

Browse files
committed
Explicitly convert NumPy ndarray of np.bool to Python bool
In NumPy 2 (and possibly earlier versions), lines 478-480 produced a deprecation warning: ``` DeprecationWarning: In future, it will be an error for 'np.bool' scalars to be interpreted as an index ``` This warning is somewhat misleading: it _is_ the case that Booleans are involved, but they are not being used as indices. The fields `rs`, `xs`, and `zs` of CliffordTableau as defined in file `cirq-core/cirq/qis/clifford_tableau.py` have type `Optional[np.ndarray]`, and the values in the ndarray have NumPy type `bool` in practice. The protocol buffer version of CliffordTableau defined in file `cirq-google/cirq_google/api/v2/program_pb2.pyi` defines those fields as `collections.abc.Iterable[builtins.bool]`. At first blush, you might think they're arrays of Booleans in both cases, but unfortunately, there's a wrinkle: Python defines its built-in `bool` type as being derived from `int` (see PEP 285), while NumPy explicitly does _not_ drive its `bool` from its integer class (see <https://numpy.org/doc/2.0/reference/arrays.scalars.html#numpy.bool>). The warning about converting `np.bool` to index values (i.e., integers) probably arises when the `np.bool` values in the ndarray are coerced into Python Booleans. At first, I thought the obvious solution would be to use `np.asarray` to convert the values to `builtins.bool`, but this did not work: ``` >>> import numpy as np >>> import builtins >>> arr = np.array([True, False], dtype=np.bool) >>> arr array([ True, False]) >>> type(arr[0]) <class 'numpy.bool'> >>> newarr = np.asarray(arr, dtype=builtins.bool) >>> newarr array([ True, False]) >>> type(newarr[0]) <class 'numpy.bool'> ``` They still end up being NumPy bools. Some other variations on this approach all failed to produce proper Python Booleans. In the end, what worked was to use `map()` to apply `builtins.bool` to every value in the incoming arrays. This may not be as efficient as possible; a possible optimization for the future is to look for a more efficient way to cast the types, or avoid having to do it at all.
1 parent d74e0dc commit 844a345

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

cirq-google/cirq_google/serialization/arg_func_langs.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import builtins
1415
import math
1516
import numbers
1617
from typing import cast, Dict, FrozenSet, Iterable, Iterator, List, Optional, Sequence, Union
@@ -474,9 +475,9 @@ def clifford_tableau_arg_to_proto(
474475
msg = v2.program_pb2.CliffordTableau() if out is None else out
475476
msg.num_qubits = value.n
476477
msg.initial_state = value.initial_state
477-
msg.xs.extend(value.xs.flatten())
478-
msg.rs.extend(value.rs.flatten())
479-
msg.zs.extend(value.zs.flatten())
478+
msg.xs.extend(map(builtins.bool, value.xs.flatten()))
479+
msg.rs.extend(map(builtins.bool, value.rs.flatten()))
480+
msg.zs.extend(map(builtins.bool, value.zs.flatten()))
480481
return msg
481482

482483

0 commit comments

Comments
 (0)