def tomography(self, qc, backend, device_settings, meas_qubits): # State tomography function tomography_circuits = state_tomography_circuits(qc, meas_qubits) if backend == Aer.get_backend( 'qasm_simulator'): # Job execution on qasm_simulator if device_settings: job = execute(tomography_circuits, backend=backend, coupling_map=self.coupling_map, noise_model=self.noise_model, basis_gates=self.basis_gates, shots=self.shots, optimization_level=3) else: job = execute(tomography_circuits, backend=backend, shots=self.shots, optimization_level=3) job_id = None else: # Job execution on live quantum processor job = execute(tomography_circuits, backend=backend, shots=self.shots, optimization_level=3) job_monitor(job) # hold and monitor job until it is completed job_id = job.job_id() tomography_results = job.result() rho = StateTomographyFitter( tomography_results, tomography_circuits).fit() # Fit results to circuits return rho, job_id
def tomography(self, full): if self.state == 'GHZ_teleport': self.qc.barrier() self.qc.cx(0, 1) self.qc.h(0) self.qc.h(2) c0 = ClassicalRegister(1) c1 = ClassicalRegister(1) c2 = ClassicalRegister(1) self.qc.add_register(c0, c1, c2) self.qc.measure(0, c0) self.qc.measure(1, c1) self.qc.measure(2, c2) self.qc.z(3).c_if(c0, 1) self.qc.x(3).c_if(c1, 1) self.qc.z(3).c_if(c2, 1) tomography_circuits = state_tomography_circuits( self.qc, self.q[self.a]) else: tomography_circuits = state_tomography_circuits(self.qc, self.q) job = execute(tomography_circuits, backend=self.backend, coupling_map=self.coupling_map, noise_model=self.noise_model, basis_gates=self.basis_gates, shots=self.shots, optimization_level=3) tomography_results = job.result() rho = StateTomographyFitter(tomography_results, tomography_circuits).fit() if self.state == 'GHZ_teleport': self.histogram_data.append( tomography_results.get_counts(tomography_circuits[2])) else: self.histogram_data.append( tomography_results.get_counts(tomography_circuits[3**self.n - 1])) if not full: rho = partial_trace( rho, np.delete(np.arange(self.n), [self.a, self.b])) if self.state == 'GHZ_teleport': for i in range(3): self.qc.data.pop(-1) self.qc.remove_final_measurements() for i in range(3): self.qc.data.pop(-1) return rho
def state_tomography(qc, qubit_list): """ A function for the state tomography for the given circuit which the qubit_list are the qubits for the main system Parameters qc[QuantumCircuit]: a quantum circuit qubit_list[list]: a list of qubits in the system """ qc_tomo = state_tomography_circuits(qc, qubit_list) job = execute(qc_tomo, Aer.get_backend('qasm_simulator'), shots=10000) result = job.result() state_tomo = StateTomographyFitter(result, qc_tomo) state_tomo_fit = state_tomo.fit() return state_tomo_fit
def _state_tomography(self): """The state tomography. The HHL result gets extracted via state tomography. Available for qasm simulator and real hardware backends. """ # Preparing the state tomography circuits tomo_circuits = state_tomography_circuits(self._circuit, self._io_register) tomo_circuits_noanc = deepcopy(tomo_circuits) ca = ClassicalRegister(1) for circ in tomo_circuits: circ.add_register(ca) circ.measure(self._reciprocal._anc, ca[0]) # Extracting the probability of successful run results = self._quantum_instance.execute(tomo_circuits) probs = [] for circ in tomo_circuits: counts = results.get_counts(circ) s, f = 0, 0 for k, v in counts.items(): if k[0] == "1": s += v else: f += v probs.append(s/(f+s)) probs = self._resize_vector(probs) self._ret["probability_result"] = np.real(probs) # Filtering the tomo data for valid results with ancillary measured # to 1, i.e. c1==1 results_noanc = self._tomo_postselect(results) tomo_data = StateTomographyFitter(results_noanc, tomo_circuits_noanc) rho_fit = tomo_data.fit() vec = np.sqrt(np.diag(rho_fit)) self._hhl_results(vec)
def run(self, circuits, meas_qubits=None, measure=None, count_chunks=False): """ :param circuits: list of QuantumCircuit or QuantumCircuit :param meas_qubits: list of qubits that will be measured. :param measure: optional. By default after channel transformation we do tomography. Not implemented now :return: depend on measure parameter. By default, it's list of density matrix """ if measure is not None: raise NotImplementedError if isinstance(circuits, QuantumCircuit): circuits = [circuits] meas_qubits, s_matrix = sort_list_and_transformation_matrix( meas_qubits) number_measure_experiments = 3**len(meas_qubits) jobs = [] for qc in circuits: qr = qc.qregs[0] tomo_circuits = state_tomography_circuits( qc, [qr[qubit_number] for qubit_number in meas_qubits]) jobs.extend(tomo_circuits) res = None for i, chunk_jobs in enumerate(chunks(jobs, self.max_jobs_per_one)): if count_chunks: print(f'chunk number: {i + 1}') execute_kwargs = { 'experiments': chunk_jobs, 'backend': self.backend, 'shots': self.shots, 'max_credits': 15 } new_res = execute(**execute_kwargs).result() if res is None: res = new_res else: res += new_res matrices = [] for i in range(int(len(res.results) / number_measure_experiments)): res_matrix = copy(res) res_matrix.results = res.results[i * number_measure_experiments:(i + 1) * number_measure_experiments] rho = StateTomographyFitter( res_matrix, jobs[i * number_measure_experiments:(i + 1) * number_measure_experiments]).fit() rho = np.linalg.inv(s_matrix) @ rho @ s_matrix matrices.append(rho) return matrices
def createQC2(r): #parameter setting lis paraLis16 = [0.196, 0.379, 0.981, 0.589, 1.178, 16] paraLis8 = [1.963, 1.115, 1.963, 2.615, 1.178, 8] paraLis4 = [-0.785, 1.017, 3.927, 2.517, 2.356, 4] paraLis2 = [-9.014 * 10**(-9), -0.75, 1.571, 0.75, -1.571, 2] paraLis = [paraLis16, paraLis8, paraLis4, paraLis2] #Register and circuit s = QuantumRegister(1, name='s') j = QuantumRegister(4, name='j') q = QuantumRegister(2, name='q') #cr = ClassicalRegister(1,name='cr') #crtmp= ClassicalRegister(2,name='crtmp') qc = QuantumCircuit(s, j, q) #Gate preparation qft = QFT(4, inverse=False) iqft = QFT(4, inverse=True) gateLis = [] gateinvLis = [] for i in range(4): toPut = Agate(paraLis[i]).control() gateLis.append(toPut) qc.h(j) qc.h(q) for i in range(4): gate = gateLis[i] qc.append(gate, qargs=[j[i]] + q[:]) qc.append(iqft, qargs=j[:]) #qc.swap(j[1],j[3]) for i in range(4): angle = 2**(3 - i) * np.pi angle *= 2**(-r + 1) qc.cry(angle, j[i], s[0]) qc.append(qft, qargs=j[:]) for i in range(4): gate = gateLis[3 - i].inverse() qc.append(gate, qargs=[j[3 - i]] + q[:]) qc.barrier() qc.h(j) #qc.measure(s,cr) qc.barrier() print(qc) tomo_circuits = state_tomography_circuits(qc, q) tomo_circuits_noanc = deepcopy(tomo_circuits) to_put = ClassicalRegister(1) for i in tomo_circuits: i.add_register(to_put) i.measure(s, to_put[0]) print(i) backend = Aer.get_backend('qasm_simulator') results = execute(tomo_circuits, backend, shots=10**5).result() print(results.get_counts()) probs = [] for circ in tomo_circuits: counts = results.get_counts(circ) s, f = 0, 0 for k, v in counts.items(): if k[0] == "1": s += v else: f += v probs.append(s / (f + s)) #results_noanc = tomo_postselect(results) data = StateTomographyFitter(results, tomo_circuits) print(data) #omo_data = StateTomographyFitter(results_noanc,tomo_circuits_noanc) rho_fit = data.fit() print(rho_fit) '''
# exec state tomography experiments job = execute(tomo_circuits, backend = backend_qasm, shots = 8192) # In[10]: job.status() # In[51]: # get rho(density matrix) from tomography data tomo_data = StateTomographyFitter(job.result(), tomo_circuits) rho = tomo_data.fit() psi = get_psi(rho) new_params = optimize.fmin(diff(psi), params) # minimize 1 - |<Psi|RY(theta1,..,theta4)>|^2 print("new params",new_params) new_params # In[59]: new_Hamiltonian = eval_H(psi, h_matrix) print("new_Hamiltonian",new_Hamiltonian) # In[58]: