def convert_measurements(self, backend_result, target_qubits=None) -> QubitWaveFunction: """ map backend results to QubitWaveFunction Parameters ---------- backend_result: the result returned directly qiskit simulation. Returns ------- QubitWaveFunction: measurements converted into wave function form. """ qiskit_counts = backend_result.result().get_counts() result = QubitWaveFunction() # todo there are faster ways for k, v in qiskit_counts.items(): converted_key = BitString.from_bitstring( other=BitStringLSB.from_binary(binary=k)) result._state[converted_key] = v if target_qubits is not None: mapped_target = [self.qubit_map[q].number for q in target_qubits] mapped_full = [ self.qubit_map[q].number for q in self.abstract_qubits ] keymap = KeyMapRegisterToSubregister(subregister=mapped_target, register=mapped_full) result = result.apply_keymap(keymap=keymap) return result
def convert_measurements(self, backend_result, target_qubits=None) -> QubitWaveFunction: """ Transform backend evaluation results into QubitWaveFunction Parameters ---------- backend_result: the return value of backend simulation. Returns ------- QubitWaveFunction results transformed to tequila native QubitWaveFunction """ result = QubitWaveFunction() # todo there are faster ways for k, v in backend_result.frequencies(binary=True).items(): converted_key = BitString.from_bitstring( other=BitStringLSB.from_binary(binary=k)) result._state[converted_key] = v if target_qubits is not None: mapped_target = [self.qubit_map[q].number for q in target_qubits] mapped_full = [ self.qubit_map[q].number for q in self.abstract_qubits ] keymap = KeyMapRegisterToSubregister(subregister=mapped_target, register=mapped_full) result = result.apply_keymap(keymap=keymap) return result
def test_endianness(): tests = [ "000111", "111000", "101010", "010101", "10010010001", "111100101000010" ] for string in tests: bits = len(string) i1 = BitString.from_int(int(string, 2)) i2 = BitString.from_binary(binary=string) assert (i1 == i2) i11 = BitStringLSB.from_int(int(string, 2)) i22 = BitStringLSB.from_binary(binary=string[::-1]) assert (i11 == i22) assert (i11.integer == i1.integer) assert (i22.integer == i2.integer) assert (i11.integer == i2.integer) assert (i1 == BitString.from_bitstring(i11)) assert (i1 == BitString.from_bitstring(i22)) assert (i2 == BitString.from_bitstring(i11))
def convert_measurements(self, backend_result) -> QubitWaveFunction: """0. :param qiskit_counts: qiskit counts as dictionary, states are binary in little endian (LSB) :return: Counts in OpenVQE format, states are big endian (MSB) """ qiskit_counts = backend_result.result().get_counts() result = QubitWaveFunction() # todo there are faster ways for k, v in qiskit_counts.items(): converted_key = BitString.from_bitstring( other=BitStringLSB.from_binary(binary=k)) result._state[converted_key] = v return result
def test_conversion(): for i in range(15): bita = BitString.from_int(integer=i) bita_lsb = BitStringLSB.from_int(integer=i) bita_converted = BitString.from_bitstring(other=bita_lsb) assert (bita == bita_converted) arrays = [[0, 0, 1], [1, 0, 0], [1, 0, 1], [1, 0, 1, 1, 1, 0], [1, 0, 0, 1, 1, 1, 0, 0, 1, 0]] for i, arr in enumerate(arrays): nbits = len(arr) bita = BitString.from_array(array=arr, nbits=nbits) bita_lsb = BitStringLSB.from_bitstring(other=bita) assert (bita_lsb.array == [x for x in reversed(arr)]) assert (bita.binary == bita_lsb.binary[::-1]) assert (bita.integer == bita_lsb.integer)
def convert_measurements(self, backend_result) -> QubitWaveFunction: """ map backend results to QubitWaveFunction Parameters ---------- backend_result: the result returned directly qiskit simulation. Returns ------- QubitWaveFunction: measurements converted into wave function form. """ qiskit_counts = backend_result.result().get_counts() result = QubitWaveFunction() # todo there are faster ways for k, v in qiskit_counts.items(): converted_key = BitString.from_bitstring( other=BitStringLSB.from_binary(binary=k)) result._state[converted_key] = v return result