def run1(): t = 3.0 M = 100 delta = t / M h = 3.0 n_qubits = 6 coefs = ([(1.0, [("Z", i), ("Z", (i + 1) % n_qubits)]) for i in range(n_qubits)] + [(h, [("X", i)]) for i in range(n_qubits)]) hamiltonian = Hamiltonian(n_qubits, coefs) circuit = hamiltonian.time_evolution_circuit(delta) # target observable z_obs = qulacs.Observable(n_qubits) for i in range(n_qubits): z_obs.add_operator(qulacs.PauliOperator("Z " + str(i), 1.0 / n_qubits)) # target observable z_obs = qulacs.Observable(n_qubits) for i in range(n_qubits): z_obs.add_operator(qulacs.PauliOperator("Z " + str(i), 1.0 / n_qubits)) # initial state state = qulacs.QuantumState(n_qubits) state.set_zero_state() # time evolution t = [i * delta for i in range(M + 1)] y = list() y.append(z_obs.get_expectation_value(state)) for i in range(M): circuit.update_quantum_state(state) y.append(z_obs.get_expectation_value(state)) plt.plot(t, y) plt.show()
def initialize_hamiltonian(self, hamiltonians): result = [] for H in hamiltonians: if self.use_mapping: # initialize only the active parts of the Hamiltonian and pre-evaluate the passive ones # passive parts are the components of each individual pauli string which act on qubits where the circuit does not act on # if the circuit does not act on those qubits the passive parts are always evaluating to 1 (if the pauli operator is Z) or 0 (otherwise) # since those qubits are always in state |0> non_zero_strings = [] unit_strings = [] for ps in H.paulistrings: string = "" for k, v in ps.items(): if k in self.U.qubit_map: string += v.upper() + " " + str( self.U.qubit_map[k]) + " " elif v.upper() != "Z": string = "ZERO" break string = string.strip() if string != "ZERO": non_zero_strings.append((ps.coeff, string)) elif string == "": unit_strings.append((ps.coeff, string)) # accumulate unit strings if len(unit_strings) > 0: coeffs = [x[0] for x in unit_strings] result.append(sum(coeffs)) if len(non_zero_strings) > 0: qulacs_H = qulacs.Observable(self.n_qubits) for coeff, string in non_zero_strings: qulacs_H.add_operator(coeff, string) result.append(qulacs_H) else: if self.U.n_qubits < H.n_qubits: raise TequilaQulacsException( "Hamiltonian has more qubits as the Unitary. Mapped expectationvalues are switched off" ) qulacs_H = qulacs.Observable(self.n_qubits) for ps in H.paulistrings: string = "" for k, v in ps.items(): string += v.upper() + " " + str(k) qulacs_H.add_operator(ps.coeff, string) result.append(qulacs_H) return result
def initialize_hamiltonian(self, hamiltonians): """ Convert reduced hamiltonians to native Qulacs types for efficient expectation value evaluation. Parameters ---------- hamiltonians: an interable set of hamiltonian objects. Returns ------- list: initialized hamiltonian objects. """ # map the reduced operators to the potentially smaller qubit system qubit_map = {} for i, q in enumerate(self.U.abstract_circuit.qubits): qubit_map[q] = i result = [] for H in hamiltonians: qulacs_H = qulacs.Observable(self.n_qubits) for ps in H.paulistrings: string = "" for k, v in ps.items(): string += v.upper() + " " + str(qubit_map[k]) qulacs_H.add_operator(ps.coeff, string) result.append(qulacs_H) return result
def initialize_hamiltonian(self, hamiltonians): """ Convert hamiltonian to native Qulacs types for efficient expectation value evaluation. Parameters ---------- hamiltonians: an interable set of hamiltonian objects. Returns ------- list: initialized hamiltonian objects. """ result = [] for H in hamiltonians: qulacs_H = qulacs.Observable(self.n_qubits) for ps in H.paulistrings: string = "" for k, v in ps.items(): string += v.upper() + " " + str(k) qulacs_H.add_operator(ps.coeff, string) result.append(qulacs_H) return result
def run2(): # Hamiltonian n_qubits = 6 h = 3.0 coefs = ([(1.0, [("Z", i), ("Z", (i + 1) % n_qubits)]) for i in range(n_qubits)] + [(h, [("X", i)]) for i in range(n_qubits)]) hamiltonian = TIDHamiltonian(n_qubits, coefs) # target observable z_obs = qulacs.Observable(n_qubits) for i in range(n_qubits): z_obs.add_operator(qulacs.PauliOperator("Z " + str(i), 1.0 / n_qubits)) # dynamics t = 3.0 nt = 100 dy = QuantumDynamics(hamiltonian, t, nt, z_obs) (t, y) = dy.run() # plot plt.plot(t, y) plt.show()