cirq.slice_for_qubits_equal_to

cirq.slice_for_qubits_equal_to(target_qubit_axes: Sequence[int], little_endian_qureg_value: int, *, num_qubits: int = None) → Tuple[Union[slice, int, ellipsis], ...][source]

Returns an index corresponding to a desired subset of an np.ndarray.

It is assumed that the np.ndarray’s shape is of the form (2, 2, 2, …, 2).

Example

# A '4 qubit' tensor with values from 0 to 15.
r = np.array(range(16)).reshape((2,) * 4)

# We want to index into the subset where qubit #1 and qubit #3 are ON.
s = cirq.slice_for_qubits_equal_to([1, 3], 0b11)
print(s)
# (slice(None, None, None), 1, slice(None, None, None), 1, Ellipsis)

# Get that subset. It corresponds to numbers of the form 0b*1*1.
# where here '*' indicates any possible value.
print(r[s])
# [[ 5  7]
#  [13 15]]
Parameters:
  • target_qubit_axes – The qubits that are specified by the index bits. All other axes of the slice are unconstrained.
  • little_endian_qureg_value – An integer whose bits specify what value is desired for of the target qubits. The integer is little endian w.r.t. the target quit axes, meaning the low bit of the integer determines the desired value of the first targeted qubit, and so forth with the k’th targeted qubit’s value set to bool(qureg_value & (1 << k)).
  • num_qubits – If specified the slices will extend all the way up to this number of qubits, otherwise if it is None, the final element return will be Ellipsis. Optional and defaults to using Ellipsis.
Returns:

An index object that will slice out a mutable view of the desired subset of a tensor.