class_docstr: Measurement process that samples from the supplied observable and returns the number of
  counts for each sample.
  
  Please refer to :func:`counts` for detailed documentation.
  
  Args:
      obs (Union[.Operator, .MeasurementValue]): The observable that is to be measured
          as part of the measurement process. Not all measurement processes require observables
          (for example ``Probability``); this argument is optional.
      wires (.Wires): The wires the measurement process applies to.
          This can only be specified if an observable was not provided.
      eigvals (array): A flat array representing the eigenvalues of the measurement.
          This can only be specified if an observable was not provided.
      id (str): custom label given to a measurement instance, can be useful for some applications
          where the instance has to be identified
      all_outcomes(bool): determines whether the returned dict will contain only the observed
          outcomes (default), or whether it will display all possible outcomes for the system
class_docstr_tok: ["Measurement","process","that","samples","from","the","supplied","observable","and","returns","the","number","of","counts","for","each","sample",".","Please","refer","to",":","func",":","`","counts","`","for","detailed","documentation",".","Args",":","obs","(","Union","[",".Operator",",",".MeasurementValue","]",")",":","The","observable","that","is","to","be","measured","as","part","of","the","measurement","process",".","Not","all","measurement","processes","require","observables","(","for","example","``","Probability","``",")",";","this","argument","is","optional",".","wires","(",".Wires",")",":","The","wires","the","measurement","process","applies","to",".","This","can","only","be","specified","if","an","observable","was","not","provided",".","eigvals","(","array",")",":","A","flat","array","representing","the","eigenvalues","of","the","measurement",".","This","can","only","be","specified","if","an","observable","was","not","provided",".","id","(","str",")",":","custom","label","given","to","a","measurement","instance",",","can","be","useful","for","some","applications","where","the","instance","has","to","be","identified","all_outcomes","(","bool",")",":","determines","whether","the","returned","dict","will","contain","only","the","observed","outcomes","(","default",")",",","or","whether","it","will","display","all","possible","outcomes","for","the","system"]
docstr: Groups the samples into a dictionary showing number of occurrences for
  each possible outcome.
  
  The format of the dictionary depends on the all_outcomes attribute. 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 an 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
  
  Returns:
      dict: dictionary with format ``{'outcome': num_occurrences}``, including all
          outcomes for the sampled observable
  
  **Example**
  
      >>> samples
      tensor([[0, 0],
              [0, 0],
              [1, 0]], requires_grad=True)
  
      By default, this will return:
      >>> self._samples_to_counts(samples)
      {'00': 2, '10': 1}
  
      However, if ``all_outcomes=True``, this will return:
      >>> self._samples_to_counts(samples)
      {'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","occurrences","for","each","possible","outcome",".","The","format","of","the","dictionary","depends","on","the","all_outcomes","attribute",".","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","an","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","Returns",":","dict",":","dictionary","with","format","``","{","'outcome","'",":","num_occurrences","}","``",",","including","all","outcomes","for","the","sampled","observable","*","*","Example","*","*",">",">",">","samples","tensor","(","[","[","0",",","0","]",",","[","0",",","0","]",",","[","1",",","0","]","]",",","requires_grad=True",")","By","default",",","this","will","return",":",">",">",">","self._samples_to_counts","(","samples",")","{","'00","'",":","2",",","'10","'",":","1","}","However",",","if","``","all_outcomes=True","``",",","this","will","return",":",">",">",">","self._samples_to_counts","(","samples",")","{","'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):
      """Groups the samples into a dictionary showing number of occurrences for
      each possible outcome.
  
      The format of the dictionary depends on the all_outcomes attribute. 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 an 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
  
      Returns:
          dict: dictionary with format ``{'outcome': num_occurrences}``, including all
              outcomes for the sampled observable
  
      **Example**
  
          >>> samples
          tensor([[0, 0],
                  [0, 0],
                  [1, 0]], requires_grad=True)
  
          By default, this will return:
          >>> self._samples_to_counts(samples)
          {'00': 2, '10': 1}
  
          However, if ``all_outcomes=True``, this will return:
          >>> self._samples_to_counts(samples)
          {'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 = qml.math.shape(samples)
  
      if self.obs is None and not isinstance(self.mv, MeasurementValue):
          # convert samples and outcomes (if using) from arrays to str for dict keys
          samples = qml.math.array(
              [
                  sample
                  for sample in samples
                  if not qml.math.any(qml.math.isnan(sample))
              ]
          )
          samples = qml.math.cast_like(samples, qml.math.int8(0))
          samples = qml.math.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 self.all_outcomes:
              num_wires = (
                  len(self.wires) if len(self.wires) > 0 else shape[-1]
              )
              outcomes = list(
                  map(
                      _sample_to_str,
                      qml.QubitDevice.generate_basis_states(num_wires),
                  )
              )
      elif self.all_outcomes:
          # This also covers statistics for mid-circuit measurements manipulated using
          # arithmetic operators
          outcomes = self.eigvals()
  
      batched = len(shape) == batched_ndims
      if not batched:
          samples = samples[None]
  
      # generate empty outcome dict, populate values with state counts
      base_dict = {k: qml.math.int64(0) for k in outcomes}
      outcome_dicts = [base_dict.copy() for _ in range(shape[0])]
      results = [
          qml.math.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(qml.math.unwrap(states), _counts):
              outcome_dict[state] = count
  
      return outcome_dicts if batched else outcome_dicts[0]
  
file_path: pennylane/pennylane/measurements/counts.py
imports: import warnings
  from typing import Sequence, Tuple, Optional
  import pennylane
  from pennylane.operation import Operator
  from pennylane.wires import Wires
  from .measurements import AllCounts, Counts, SampleMeasurement
  from .mid_measure import MeasurementValue
label_desc: Use image node_id 7 for calling the CountsMP obj's underlying member method code with example usage: obj._samples_to_counts(samples) and returns: unknown