Exemple #1
0
    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.
        """
        exp = self._get_experiment(experiment)
        try:
            header = exp.header.to_dict()
        except (AttributeError, QiskitError):  # header is not available
            header = None

        if 'counts' in self.data(experiment).keys():
            return postprocess.format_counts(
                self.data(experiment)['counts'], header)
        elif 'statevector' in self.data(experiment).keys():
            vec = postprocess.format_statevector(
                self.data(experiment)['statevector'])
            return state_to_counts(vec)
        else:
            raise QiskitError(
                'No counts for experiment "{0}"'.format(experiment))
Exemple #2
0
 def test_state_to_counts(self):
     """Test statevector to counts"""
     qc = QuantumCircuit(5)
     qc.h(2)
     qc.cx(2, 1)
     qc.cx(1, 0)
     qc.cx(2, 3)
     qc.cx(3, 4)
     sim = BasicAer.get_backend('statevector_simulator')
     res = execute(qc, sim).result()
     vec = res.get_statevector()
     counts = state_to_counts(vec)
     self.assertAlmostEqual(counts['00000'], 0.5)
     self.assertAlmostEqual(counts['11111'], 0.5)
Exemple #3
0
    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(state_to_counts(vec))
            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