def _all_possible_datastore_states(
    keys: Iterable[tuple[cirq.MeasurementKey, int]],
    measurement_qubits: dict[cirq.MeasurementKey, list[tuple[cirq.Qid, ...]]],
) -> Iterable[cirq.ClassicalDataStoreReader]:
    """The cartesian product of all possible DataStore states for the given keys."""
    # First we get the list of all possible values. So if we have a key mapped to qubits of shape
    # (2, 2) and a key mapped to a qutrit, the possible measurement values are:
    # [((0, 0), (0,)),
    #  ((0, 0), (1,)),
    #  ((0, 0), (2,)),
    #  ((0, 1), (0,)),
    #  ((0, 1), (1,)),
    #  ((0, 1), (2,)),
    #  ((1, 0), (0,)),
    #  ((1, 0), (1,)),
    #  ((1, 0), (2,)),
    #  ((1, 1), (0,)),
    #  ((1, 1), (1,)),
    #  ((1, 1), (2,))]
    all_possible_measurements = itertools.product(
        *[
            tuple(itertools.product(*[range(q.dimension) for q in measurement_qubits[k][i]]))
            for k, i in keys
        ]
    )
    # Then we create the ClassicalDataDictionaryStore for each of the above. A `measurement_list`
    # is a single row of the above example, and can be zipped with `keys`.
    for measurement_list in all_possible_measurements:
        # Initialize a set of measurement records for this iteration. This will have the same shape
        # as `measurement_qubits` but zeros for all measurements.
        records = {
            key: [(0,) * len(qubits) for qubits in qubits_list]
            for key, qubits_list in measurement_qubits.items()
        }
        # Set the measurement values from the current row of the above, for each key/index we care
        # about.
        for (k, i), measurement in zip(keys, measurement_list):
            records[k][i] = measurement
        # Finally yield this sample to the consumer.
        yield value.ClassicalDataDictionaryStore(
            _records=records, _measured_qubits=measurement_qubits
        )
The cartesian product of all possible DataStore states for the given keys.
cirq-core/cirq/transformers/measurement_transformers.py