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 in backend_result: converted_key = BitString.from_binary( BitStringLSB.from_int(integer=k, nbits=self.n_qubits).binary) if converted_key in result._state: result._state[converted_key] += 1 else: result._state[converted_key] = 1 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 backend_result: array from pyquil as list of lists of integers. :return: backend_result in Tequila format. """ def string_to_array(s): listing = [] for letter in s: if letter not in [',', ' ', '[', ']', '.']: listing.append(int(letter)) return listing result = QubitWaveFunction() bit_dict = {} for b in backend_result: try: bit_dict[str(b)] += 1 except: bit_dict[str(b)] = 1 for k, v in bit_dict.items(): arr = string_to_array(k) result._state[BitString.from_array(arr)] = v return result
def convert_measurements(self, backend_result: cirq.TrialResult) -> QubitWaveFunction: """ Take the results of a cirq measurement and translate them to teuqila QubitWaveFunction. Parameters ---------- backend_result: cirq.TrialResult: the result of sampled measurements. Returns ------- QubitWaveFunction: the result of sampling, as a tequila QubitWavefunction. """ assert (len(backend_result.measurements) == 1) for key, value in backend_result.measurements.items(): counter = QubitWaveFunction() for sample in value: binary = BitString.from_array(array=sample.astype(int)) if binary in counter._state: counter._state[binary] += 1 else: counter._state[binary] = 1 return counter
def strip_sympy_zeros(wfn: QubitWaveFunction): result = QubitWaveFunction() for k, v in wfn.items(): if v != 0: result[k] = v return result
def apply_on_standard_basis(cls, gate: QGate, basisfunction: BitString, qubits: dict, variables) -> QubitWaveFunction: basis_array = basisfunction.array if gate.is_controlled(): do_apply = True check = [basis_array[qubits[c]] == 1 for c in gate.control] for c in check: if not c: do_apply = False if not do_apply: return QubitWaveFunction.from_int(basisfunction) if len(gate.target) > 1: raise Exception( "Multi-targets not supported for symbolic simulators") result = QubitWaveFunction() for tt in gate.target: t = qubits[tt] qt = basis_array[t] a_array = copy.deepcopy(basis_array) a_array[t] = (a_array[t] + 1) % 2 current_state = QubitWaveFunction.from_int(basisfunction) altered_state = QubitWaveFunction.from_int( BitString.from_array(a_array)) fac1 = None fac2 = None if gate.name == "H": fac1 = (sympy.Integer(-1)**qt * sympy.sqrt(sympy.Rational(1 / 2))) fac2 = (sympy.sqrt(sympy.Rational(1 / 2))) elif gate.name.upper() == "CNOT" or gate.name.upper() == "X": fac2 = sympy.Integer(1) elif gate.name.upper() == "Y": fac2 = sympy.I * sympy.Integer(-1)**(qt) elif gate.name.upper() == "Z": fac1 = sympy.Integer(-1)**(qt) elif gate.name.upper() == "RX": angle = sympy.Rational(1 / 2) * gate.parameter(variables) fac1 = sympy.cos(angle) fac2 = -sympy.sin(angle) * sympy.I elif gate.name.upper() == "RY": angle = -sympy.Rational(1 / 2) * gate.parameter(variables) fac1 = sympy.cos(angle) fac2 = +sympy.sin(angle) * sympy.Integer(-1)**(qt + 1) elif gate.name.upper() == "RZ": angle = sympy.Rational(1 / 2) * gate.parameter(variables) fac1 = sympy.exp(-angle * sympy.I * sympy.Integer(-1)**(qt)) else: raise Exception("Gate is not known to simulators, " + str(gate)) if fac1 is None or fac1 == 0: result += fac2 * altered_state elif fac2 is None or fac2 == 0: result += fac1 * current_state elif fac1 is None and fac2 is None: raise Exception("???") else: result += fac1 * current_state + fac2 * altered_state return result