class_docstr: Abstract base class for PennyLane qubit devices.
  
  The following abstract method **must** be defined:
  
  * :meth:`~.apply`: append circuit operations, compile the circuit (if applicable),
    and perform the quantum computation.
  
  Devices that generate their own samples (such as hardware) may optionally
  overwrite :meth:`~.probability`. This method otherwise automatically
  computes the probabilities from the generated samples, and **must**
  overwrite the following method:
  
  * :meth:`~.generate_samples`: Generate samples from the device from the
    exact or approximate probability distribution.
  
  Analytic devices **must** overwrite the following method:
  
  * :meth:`~.analytic_probability`: returns the probability or marginal probability from the
    device after circuit execution. :meth:`~.marginal_prob` may be used here.
  
  This device contains common utility methods for qubit-based devices. These
  do not need to be overwritten. Utility methods include:
  
  * :meth:`~.expval`, :meth:`~.var`, :meth:`~.sample`: return expectation values,
    variances, and samples of observables after the circuit has been rotated
    into the observable eigenbasis.
  
  Args:
      wires (int, Iterable[Number, str]]): Number of subsystems represented by the device,
          or iterable that contains unique labels for the subsystems as numbers (i.e., ``[-1, 0, 2]``)
          or strings (``['ancilla', 'q1', 'q2']``). Default 1 if not specified.
      shots (None, int, list[int]): Number of circuit evaluations/random samples used to estimate
          expectation values of observables. If ``None``, the device calculates probability, expectation values,
          and variances analytically. If an integer, it specifies the number of samples to estimate these quantities.
          If a list of integers is passed, the circuit evaluations are batched over the list of shots.
      r_dtype: Real floating point precision type.
      c_dtype: Complex floating point precision type.
docstr: Groups the samples into a dictionary showing number of occurences for
  each possible outcome.
  
  The format of the dictionary depends on mp.return_type, which is set when
  calling measurements.counts by setting the kwarg all_outcomes (bool). By default,
  the dictionary will only contain the observed outcomes. Optionally (all_outcomes=True)
  the dictionary will instead contain all possible outcomes, with a count of 0
  for those not observed. See example.
  
  
  Args:
      samples: An array of samples, with the shape being ``(shots,len(wires))`` if no observable
          is provided, with sample values being an array of 0s or 1s for each wire. Otherwise, it
          has shape ``(shots,)``, with sample values being scalar eigenvalues of the observable
      mp (~.measurements.CountsMP): the measurement process sampled
      num_wires (int): number of wires the sampled observable was performed on
  
  Returns:
      dict: dictionary with format ``{'outcome': num_occurences}``, including all
          outcomes for the sampled observable
  
  **Example**
  
      >>> num_wires = 2
      >>> dev = qml.device("default.qubit.legacy", wires=num_wires)
      >>> mp = qml.counts()
      >>> samples = pnp.array([[0, 0], [0, 0], [1, 0]])
      >>> dev._samples_to_counts(samples, mp, num_wires)
      {'00': 2, '10': 1}
      >>> mp = qml.counts(all_outcomes=True)
      >>> dev._samples_to_counts(samples, mp, num_wires)
      {'00': 2, '01': 0, '10': 1, '11': 0}
  
      The variable all_outcomes can be set when running measurements.counts, i.e.:
  
       .. code-block:: python3
  
          dev = qml.device("default.qubit", wires=2, shots=4)
  
          @qml.qnode(dev)
          def circuit(x):
              qml.RX(x, wires=0)
              return qml.counts(all_outcomes=True)
docstr_tok: ["Groups","the","samples","into","a","dictionary","showing","number","of","occurences","for","each","possible","outcome",".","The","format","of","the","dictionary","depends","on","mp.return_type",",","which","is","set","when","calling","measurements.counts","by","setting","the","kwarg","all_outcomes","(","bool",")",".","By","default",",","the","dictionary","will","only","contain","the","observed","outcomes",".","Optionally","(","all_outcomes=True",")","the","dictionary","will","instead","contain","all","possible","outcomes",",","with","a","count","of","0","for","those","not","observed",".","See","example",".","Args",":","samples",":","An","array","of","samples",",","with","the","shape","being","``","(","shots",",","len","(","wires",")",")","``","if","no","observable","is","provided",",","with","sample","values","being","an","array","of","0s","or","1s","for","each","wire",".","Otherwise",",","it","has","shape","``","(","shots",",",")","``",",","with","sample","values","being","scalar","eigenvalues","of","the","observable","mp","(","~.measurements.CountsMP",")",":","the","measurement","process","sampled","num_wires","(","int",")",":","number","of","wires","the","sampled","observable","was","performed","on","Returns",":","dict",":","dictionary","with","format","``","{","'outcome","'",":","num_occurences","}","``",",","including","all","outcomes","for","the","sampled","observable","*","*","Example","*","*",">",">",">","num_wires","=","2",">",">",">","dev","=","qml.device","(","``","default.qubit.legacy","''",",","wires=num_wires",")",">",">",">","mp","=","qml.counts","(",")",">",">",">","samples","=","pnp.array","(","[","[","0",",","0","]",",","[","0",",","0","]",",","[","1",",","0","]","]",")",">",">",">","dev._samples_to_counts","(","samples",",","mp",",","num_wires",")","{","'00","'",":","2",",","'10","'",":","1","}",">",">",">","mp","=","qml.counts","(","all_outcomes=True",")",">",">",">","dev._samples_to_counts","(","samples",",","mp",",","num_wires",")","{","'00","'",":","2",",","'01","'",":","0",",","'10","'",":","1",",","'11","'",":","0","}","The","variable","all_outcomes","can","be","set","when","running","measurements.counts",",","i.e",".",":","..","code-block",":",":","python3","dev","=","qml.device","(","``","default.qubit","''",",","wires=2",",","shots=4",")","@","qml.qnode","(","dev",")","def","circuit","(","x",")",":","qml.RX","(","x",",","wires=0",")","return","qml.counts","(","all_outcomes=True",")"]
code: def _samples_to_counts(self, samples, mp: CountsMP, num_wires):
      """Groups the samples into a dictionary showing number of occurences for
      each possible outcome.
  
      The format of the dictionary depends on mp.return_type, which is set when
      calling measurements.counts by setting the kwarg all_outcomes (bool). By default,
      the dictionary will only contain the observed outcomes. Optionally (all_outcomes=True)
      the dictionary will instead contain all possible outcomes, with a count of 0
      for those not observed. See example.
  
  
      Args:
          samples: An array of samples, with the shape being ``(shots,len(wires))`` if no observable
              is provided, with sample values being an array of 0s or 1s for each wire. Otherwise, it
              has shape ``(shots,)``, with sample values being scalar eigenvalues of the observable
          mp (~.measurements.CountsMP): the measurement process sampled
          num_wires (int): number of wires the sampled observable was performed on
  
      Returns:
          dict: dictionary with format ``{'outcome': num_occurences}``, including all
              outcomes for the sampled observable
  
      **Example**
  
          >>> num_wires = 2
          >>> dev = qml.device("default.qubit.legacy", wires=num_wires)
          >>> mp = qml.counts()
          >>> samples = pnp.array([[0, 0], [0, 0], [1, 0]])
          >>> dev._samples_to_counts(samples, mp, num_wires)
          {'00': 2, '10': 1}
          >>> mp = qml.counts(all_outcomes=True)
          >>> dev._samples_to_counts(samples, mp, num_wires)
          {'00': 2, '01': 0, '10': 1, '11': 0}
  
          The variable all_outcomes can be set when running measurements.counts, i.e.:
  
           .. code-block:: python3
  
              dev = qml.device("default.qubit", wires=2, shots=4)
  
              @qml.qnode(dev)
              def circuit(x):
                  qml.RX(x, wires=0)
                  return qml.counts(all_outcomes=True)
  
      """
  
      outcomes = []
  
      # if an observable was provided, batched samples will have shape (batch_size, shots)
      batched_ndims = 2
      shape = samples.shape
  
      if mp.obs is None and not isinstance(mp.mv, MeasurementValue):
          # convert samples and outcomes (if using) from arrays to str for dict keys
          samples = np.array(
              [
                  sample
                  for sample in samples
                  if not np.any(np.isnan(sample))
              ]
          )
          samples = qml.math.cast_like(samples, qml.math.int8(0))
          samples = np.apply_along_axis(_sample_to_str, -1, samples)
          batched_ndims = 3  # no observable was provided, batched samples will have shape (batch_size, shots, len(wires))
          if mp.all_outcomes:
              outcomes = list(
                  map(
                      _sample_to_str,
                      self.generate_basis_states(num_wires),
                  )
              )
      elif mp.all_outcomes:
          outcomes = mp.eigvals()
  
      batched = len(shape) == batched_ndims
      if not batched:
          samples = samples[None]
  
      # generate empty outcome dict, populate values with state counts
      base_dict = {k: np.int64(0) for k in outcomes}
      outcome_dicts = [base_dict.copy() for _ in range(shape[0])]
      results = [
          np.unique(batch, return_counts=True) for batch in samples
      ]
      for result, outcome_dict in zip(results, outcome_dicts):
          states, counts = result
          for state, count in zip(states, counts):
              outcome_dict[state] = count
  
      return outcome_dicts if batched else outcome_dicts[0]
  
imports: import abc
  import itertools
  import warnings
  from collections import defaultdict
  from typing import Union, List
  import inspect
  import logging
  import numpy
  import pennylane
  from pennylane import Device, DeviceError
  from pennylane.workflow import set_shots
  from pennylane.math import multiply
  from pennylane.math import sum
  from pennylane.measurements import ClassicalShadowMP, CountsMP, ExpectationMP, MeasurementProcess, MeasurementTransform, MeasurementValue, MutualInfoMP, ProbabilityMP, SampleMeasurement, SampleMP, ShadowExpvalMP, StateMeasurement, StateMP, VarianceMP, VnEntropyMP, Shots
  from pennylane.resource import Resources
  from pennylane.operation import operation_derivative, Operation
  from pennylane.tape import QuantumTape
  from pennylane.wires import Wires
label_desc: Use image node_id 35 for calling the QubitDevice obj's underlying member method code with example usage: obj._samples_to_counts(samples, mp, num_wires) and returns: unknown