1# This code is part of Qiskit. 2# 3# (C) Copyright IBM 2018, 2019. 4# 5# This code is licensed under the Apache License, Version 2.0. You may 6# obtain a copy of this license in the LICENSE.txt file in the root directory 7# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. 8# 9# Any modifications or derivative works of this code must retain this 10# copyright notice, and modified files need to carry a notice indicating 11# that they have been altered from the originals. 12 13""" 14Utils 15""" 16 17from math import log2 18 19 20def list2dict(counts_list, hex_counts=True): 21 """Convert a list of counts to a dict""" 22 if hex_counts: 23 return {hex(i): val for i, val in enumerate(counts_list) if val > 0} 24 # For bit-string counts we need to know number of qubits to 25 # pad bitstring 26 n_qubits = int(log2(counts_list)) 27 counts_dict = {} 28 for i, val in enumerate(counts_list): 29 if val > 0: 30 key = bin(i)[2:] 31 key = (n_qubits - len(key)) * '0' + key 32 counts_dict[key] = val 33 return counts_dict 34