cirq.slice_for_qubits_equal_to

cirq.slice_for_qubits_equal_to(target_qubit_axes: Sequence[int], little_endian_qureg_value: int = 0, *, big_endian_qureg_value: int = 0, num_qubits: Optional[int] = None, qid_shape: Optional[Tuple[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 qubit 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)).

  • big_endian_qureg_value – Same as little_endian_qureg_value but big endian w.r.t. to target qubit axes, meaning the low bit of the integer dertemines the desired value of the last target qubit, and so forth. Specify exactly one of the *_qureg_value arguments.

  • 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.

  • qid_shape – The qid shape of the state vector being sliced. Specify this instead of num_qubits when using qids with dimension != 2. The qureg value is interpreted to store digits with corresponding bases packed into an int.

Returns

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