def get_counts(self, experiment=None): """Get the histogram data of an experiment. Args: experiment (str or QuantumCircuit or Schedule or int or None): the index of the experiment, as specified by ``get_data()``. Returns: dict[str:int]: a dictionary with the counts for each qubit, with the keys containing a string in binary format and separated according to the registers in circuit (e.g. ``0100 1110``). The string is little-endian (cr[0] on the right hand side). Raises: QiskitError: if there are no counts for the experiment. """ try: exp = self._get_experiment(experiment) try: header = exp.header.to_dict() except (AttributeError, QiskitError): # header is not available header = None return postprocess.format_counts( self.data(experiment)['counts'], header) except KeyError: raise QiskitError( 'No counts for experiment "{0}"'.format(experiment))
def get_counts(self, experiment=None): """Get the histogram data of an experiment. Args: experiment (str or QuantumCircuit or Schedule or int or None): the index of the experiment, as specified by ``get_data()``. Returns: dict[str:int] or list[dict[str:int]]: a dictionary or a list of dictionaries. A dictionary has the counts for each qubit with the keys containing a string in binary format and separated according to the registers in circuit (e.g. ``0100 1110``). The string is little-endian (cr[0] on the right hand side). Raises: QiskitError: if there are no counts for the experiment. """ if experiment is None: exp_keys = range(len(self.results)) else: exp_keys = [experiment] dict_list = [] for key in exp_keys: exp = self._get_experiment(key) try: header = exp.header.to_dict() except (AttributeError, QiskitError): # header is not available header = None if 'counts' in self.data(key).keys(): dict_list.append( postprocess.format_counts( self.data(key)['counts'], header)) elif 'statevector' in self.data(key).keys(): vec = postprocess.format_statevector( self.data(key)['statevector']) dict_list.append( Statevector(vec).probabilities_dict(decimals=15)) else: raise QiskitError('No counts for experiment "{0}"'.format(key)) # Return first item of dict_list if size is 1 if len(dict_list) == 1: return dict_list[0] else: return dict_list
def get_probabilities( self, experiment: Any = None ) -> Union[Dict[str, float], List[Dict[str, float]]]: """Get the probability data of an experiment. The probability data is added as a separate result by Quantum Inspire backend. Based on Qiskit get_count method from Result. :param experiment (str or QuantumCircuit or Schedule or int or None): the index of the experiment, as specified by ``get_data()``. :return: One or more dictionaries which holds the states and probabilities for each result. :raises QiskitBackendError: raised if there are no probabilities in a result for the experiment(s). """ if experiment is None: exp_keys = range(len(self.results)) else: exp_keys = [experiment] # type: ignore dict_list: List[Dict[str, float]] = [] for key in exp_keys: exp = self._get_experiment(key) try: header = exp.header.to_dict() except (AttributeError, QiskitError): # header is not available header = None if "probabilities" in self.data(key).keys(): probabilities = self.data(key)["probabilities"] dict_list.append( postprocess.format_counts(probabilities, header)) else: raise QiskitBackendError( 'No probabilities for experiment "{0}"'.format(key)) # Return first item of dict_list if size is 1 if len(dict_list) == 1: return dict_list[0] return dict_list
def __init__(self, data, time_taken=None, creg_sizes=None, memory_slots=None): """Build a counts object Args: data (dict): The dictionary input for the counts. Where the keys represent a measured classical value and the value is an integer the number of shots with that result. The keys can be one of several formats: * A hexadecimal string of the form ``"0x4a"`` * A bit string prefixed with ``0b`` for example ``'0b1011'`` * A bit string formatted across register and memory slots. For example, ``'00 10'``. * A dit string, for example ``'02'``. Note for objects created with dit strings the ``creg_sizes``and ``memory_slots`` kwargs don't work and :meth:`hex_outcomes` and :meth:`int_outcomes` also do not work. time_taken (float): The duration of the experiment that generated the counts creg_sizes (list): a nested list where the inner element is a list of tuples containing both the classical register name and classical register size. For example, ``[('c_reg', 2), ('my_creg', 4)]``. memory_slots (int): The number of total ``memory_slots`` in the experiment. Raises: TypeError: If the input key type is not an int or string QiskitError: If a dit string key is input with creg_sizes and/or memory_slots """ bin_data = None data = dict(data) if not data: self.int_raw = {} self.hex_raw = {} bin_data = {} else: first_key = next(iter(data.keys())) if isinstance(first_key, int): self.int_raw = data self.hex_raw = { hex(key): value for key, value in self.int_raw.items() } elif isinstance(first_key, str): if first_key.startswith('0x'): self.hex_raw = data self.int_raw = { int(key, 0): value for key, value in self.hex_raw.items() } elif first_key.startswith('0b'): self.int_raw = { int(key, 0): value for key, value in data.items() } self.hex_raw = { hex(key): value for key, value in self.int_raw.items() } else: if not creg_sizes and not memory_slots: self.hex_raw = None self.int_raw = None bin_data = data else: hex_dict = {} int_dict = {} for bitstring, value in data.items(): if not self.bitstring_regex.search(bitstring): raise exceptions.QiskitError( 'Counts objects with dit strings do not ' 'currently support dit string formatting parameters ' 'creg_sizes or memory_slots') int_key = self._remove_space_underscore(bitstring) int_dict[int_key] = value hex_dict[hex(int_key)] = value self.hex_raw = hex_dict self.int_raw = int_dict else: raise TypeError( "Invalid input key type %s, must be either an int " "key or string key with hexademical value or bit string") header = {} self.creg_sizes = creg_sizes if self.creg_sizes: header['creg_sizes'] = self.creg_sizes self.memory_slots = memory_slots if self.memory_slots: header['memory_slots'] = self.memory_slots if not bin_data: bin_data = postprocess.format_counts(self.hex_raw, header=header) super().__init__(bin_data) self.time_taken = time_taken