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 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) -> 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 do_simulate(self, variables, initial_state=0, *args, **kwargs) -> QubitWaveFunction: """ Helper function for performing simulation. Parameters ---------- variables: variables to pass to the circuit for simulation. initial_state: indicate initial state on which the unitary self.circuit should act. args kwargs Returns ------- QubitWaveFunction: the result of simulation. """ if self.noise_model is None: qiskit_backend = self.retrieve_device('statevector_simulator') else: raise TequilaQiskitException( "wave function simulation with noise cannot be performed presently" ) optimization_level = None if "optimization_level" in kwargs: optimization_level = kwargs['optimization_level'] opts = None if initial_state != 0: array = numpy.zeros(shape=[2**self.n_qubits]) i = BitStringLSB.from_binary( BitString.from_int(integer=initial_state, nbits=self.n_qubits).binary) print(initial_state, " -> ", i) array[i.integer] = 1.0 opts = {"initial_statevector": array} print(opts) backend_result = qiskit.execute(experiments=self.circuit, optimization_level=optimization_level, backend=qiskit_backend, parameter_binds=[self.resolver], backend_options=opts).result() return QubitWaveFunction.from_array(arr=backend_result.get_statevector( self.circuit), numbering=self.numbering)
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
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 do_simulate(self, variables, initial_state=0, *args, **kwargs) -> QubitWaveFunction: if self.noise_model is None: qiskit_backend = self.get_backend(*args, **kwargs) if qiskit_backend != qiskit.Aer.get_backend( name="statevector_simulator"): raise TequilaQiskitException( "quiskit_backend for simulations without samples (full wavefunction simulations) need to be the statevector_simulator. Received: qiskit_backend={}" .format(qiskit_backend)) else: raise TequilaQiskitException( "wave function simulation with noise cannot be performed presently" ) optimization_level = None if "optimization_level" in kwargs: optimization_level = kwargs['optimization_level'] opts = None if initial_state != 0: array = numpy.zeros(shape=[2**self.n_qubits]) i = BitStringLSB.from_binary( BitString.from_int(integer=initial_state, nbits=self.n_qubits).binary) print(initial_state, " -> ", i) array[i.integer] = 1.0 opts = {"initial_statevector": array} print(opts) backend_result = qiskit.execute(experiments=self.circuit, optimization_level=optimization_level, backend=qiskit_backend, parameter_binds=[self.resolver], backend_options=opts).result() return QubitWaveFunction.from_array(arr=backend_result.get_statevector( self.circuit), numbering=self.numbering)