def fit_tomodata(tomo_data, method=None): if method == 'Linear inversion': print('Error, no method yet!') choi_fit = [] elif method == None: choi_fit = tomo.fit_tomography_data(tomo_data, options={'trace': 1}) else: choi_fit = tomo.fit_tomography_data(tomo_data, method, options={'trace': 1}) return choi_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 c = ClassicalRegister(self._num_q) self._circuit.add_register(c) tomo_qbits = list(range(self._num_q)) tomo_set = tomo.state_tomography_set(tomo_qbits) tomo_circuits = \ tomo.create_tomography_circuits(self._circuit, self._io_register, c, tomo_set) # Handling the results result = self._quantum_instance.execute(tomo_circuits) probs = [] for circ in tomo_circuits: counts = result.get_counts(circ) s, f = 0, 0 for k, v in counts.items(): if k[-1] == "1": s += v else: f += v probs.append(s/(f+s)) self._ret["probability_result"] = probs # Filtering the tomo data for valid results, i.e. c0==1 tomo_data = self._tomo_postselect(result, self._circuit.name, tomo_set, self._success_bit) # Fitting the tomography data rho_fit = tomo.fit_tomography_data(tomo_data) vec = rho_fit[:, 0]/np.sqrt(rho_fit[0, 0]) self._hhl_results(vec)
def _tomography_test_data(qp, name, qr, cr, tomoset, shots): tomo.create_tomography_circuits(qp, name, qr, cr, tomoset) result = qp.execute(tomo.tomography_circuit_names(tomoset, name), shots=shots, seed=42) data = tomo.tomography_data(result, name, tomoset) return tomo.fit_tomography_data(data)
def _state_tomography_quantum_program(self, target, state, n_qubits, shots=1): qp = qiskit.QuantumProgram() try: backend = 'local_qiskit_simulator' qp.get_backend_configuration(backend) except LookupError: backend = 'local_qasm_simulator' # Prepared target state and assess quality qp = self.target_prep(state, target, n_qubits, qp=qp) prep_result = qp.execute(['prep'], backend=backend, shots=1) prep_state = prep_result.get_data('prep')['quantum_state'] F_prep = state_fidelity(prep_state, target) print('Prepared state fidelity =', F_prep) # Run state tomography simulation and fit data to reconstruct circuit qp, tomo_set, tomo_circuits = self.add_tomo_circuits(qp) tomo_result = qp.execute(tomo_circuits, backend=backend, shots=shots) tomo_data = tomo.tomography_data(tomo_result, 'prep', tomo_set) rho_fit = tomo.fit_tomography_data(tomo_data) # calculate fidelity and purity of fitted state F_fit = state_fidelity(rho_fit, target) pur = purity(rho_fit) print('Fitted state fidelity =', F_fit) print('Fitted state purity =', str(pur))
def _tomography_test_data(circuit, qr, cr, tomoset, shots): tomo_circs = tomo.create_tomography_circuits(circuit, qr, cr, tomoset) result = execute(tomo_circs, Aer.get_backend('qasm_simulator_py'), shots=shots, seed=42).result() data = tomo.tomography_data(result, circuit.name, tomoset) return tomo.fit_tomography_data(data)
def _fit_tomodata_qiskit_(tomo_data, method=None): ''' Use the qiskit functions to fit the tomography data. There are two methods: 'magic' (standard): This fits the tomography data to a choi matrix that is completely positive and has trace 1. For more info of the fitting method, see the documentation of qiskit.tools.qcvv.tomography.fit_tomography_data There might be problems with the trace preservation-qualities of the matrix when using this method. 'leastsq' : This fits the data to a Choi matrix via simple linear inversion. There is no guarantee on CP (so in almost all cases the Choi matrix will not be CP) Therefore different methods have to be used to make the process CP Returned is a choi matrix corresponding to the data. ''' if method == None: choi_fit = tomo.fit_tomography_data(tomo_data, options={'trace': 1}) else: choi_fit = tomo.fit_tomography_data( tomo_data, method, options={'trace': 1}) return choi_fit
def state_tomography(state, n_qubits, shots): # cat target state: [1. 0. 0. ... 0. 0. 1.]/sqrt(2.) if state == 'cat': target = np.zeros(pow(2, n_qubits)) target[0] = 1 target[pow(2, n_qubits)-1] = 1.0 target /= np.sqrt(2.0) # random target state: first column of a random unitary elif state == 'random': target = random_unitary_matrix(pow(2, n_qubits))[0] else: raise QISKitError("Unknown state for tomography.") print("target: {}".format(target)) # Use the local qasm simulator backend = 'local_qasm_simulator' qp = QuantumProgram() # Prepared target state and assess quality qp = target_prep(qp, state, target) prep_result = qp.execute(['prep'], backend='local_statevector_simulator') prep_state = prep_result.get_data('prep')['statevector'] F_prep = state_fidelity(prep_state, target) print('Prepared state fidelity =', F_prep) # Run state tomography simulation and fit data to reconstruct circuit qp, tomo_set, tomo_circuits = add_tomo_circuits(qp) tomo_result = qp.execute(tomo_circuits, backend=backend, shots=shots) tomo_data = tomo.tomography_data(tomo_result, 'prep', tomo_set) rho_fit = tomo.fit_tomography_data(tomo_data) # calculate fidelity and purity of fitted state F_fit = state_fidelity(rho_fit, target) pur = purity(rho_fit) print('Fitted state fidelity =', F_fit) print('Fitted state purity =', str(pur)) return qp
def state_tomography(state, n_qubits, shots): # cat target state: [1. 0. 0. ... 0. 0. 1.]/sqrt(2.) if state == 'cat': target = np.zeros(pow(2, n_qubits)) target[0] = 1 target[pow(2, n_qubits)-1] = 1.0 target /= np.sqrt(2.0) # random target state: first column of a random unitary elif state == 'random': target = random_unitary_matrix(pow(2, n_qubits))[0] else: raise QISKitError("Unknown state for tomography.") print("target: {}".format(target)) # Use the local qasm simulator backend = 'local_qasm_simulator' qp = QuantumProgram() # Prepared target state and assess quality qp = target_prep(qp, state, target) prep_result = qp.execute(['prep'], backend='local_statevector_simulator') prep_state = prep_result.get_data('prep')['statevector'] F_prep = state_fidelity(prep_state, target) print('Prepared state fidelity =', F_prep) # Run state tomography simulation and fit data to reconstruct circuit qp, tomo_set, tomo_circuits = add_tomo_circuits(qp) tomo_result = qp.execute(tomo_circuits, backend=backend, shots=shots) tomo_data = tomo.tomography_data(tomo_result, 'prep', tomo_set) rho_fit = tomo.fit_tomography_data(tomo_data) # calculate fidelity and purity of fitted state F_fit = state_fidelity(rho_fit, target) pur = purity(rho_fit) print('Fitted state fidelity =', F_fit) print('Fitted state purity =', str(pur)) return qp
def _state_tomography(self, target, state, n_qubits, shots=1): # Use the local qasm simulator backend = qiskit.BasicAer.get_backend('statevector_simulator') # Prepared target state and assess quality prep_circ = self.target_prep(state, target, n_qubits) prep_result = qiskit.execute(prep_circ, backend=backend).result() prep_state = prep_result.get_statevector(prep_circ) F_prep = state_fidelity(prep_state, target) print('Prepared state fidelity =', F_prep) # Run state tomography simulation and fit data to reconstruct circuit tomo_set, tomo_circuits = self.add_tomo_circuits(prep_circ) tomo_result = qiskit.execute(tomo_circuits, backend=backend, shots=shots).result() tomo_data = tomo.tomography_data(tomo_result, prep_circ.name, tomo_set) rho_fit = tomo.fit_tomography_data(tomo_data) # calculate fidelity and purity of fitted state F_fit = state_fidelity(rho_fit, target) pur = purity(rho_fit) print('Fitted state fidelity =', F_fit) print('Fitted state purity =', str(pur))
U_had = np.array([[1, 1], [1, -1]])/np.sqrt(2) # compute Choi-matrix from unitary had_choi = outer(vectorize(U_had)) plot_state(had_choi) had_tomo_set = tomo.process_tomography_set([1],'Pauli','Pauli') had_tomo_circuits = tomo.create_tomography_circuits(Q_program, 'had',qr,cr,had_tomo_set) backend = 'local_qasm_simulator' shots = 1000 had_tomo_results = Q_program.execute(had_tomo_circuits, shots=shots, backend=backend) had_process_data = tomo.tomography_data(had_tomo_results,'had', had_tomo_set) had_choi_fit = tomo.fit_tomography_data(had_process_data,'leastsq',options={'trace':2}) plot_state(had_choi_fit,'paulivec') #unitary matrix for CNOT with qubit 1 as control and qubit 0 as target. U_cnot = np.array([[1,0,0,0],[0,1,0,0],[0,0,0,1],[0,0,1,0]]) # compute Choi-matrix from unitary cnot_choi = outer(vectorize(U_cnot)) plot_state(cnot_choi) cnot_tomo_set = tomo.process_tomography_set([1,0],'Pauli','Pauli') cnot_tomo_circuits = tomo.create_tomography_circuits(Q_program,'cnot',qr,cr,cnot_tomo_set) cnot_tomo_results = Q_program.execute(cnot_tomo_circuits, shots=shots, backend=backend,timeout=300) cnot_process_data = tomo.tomography_data(cnot_tomo_results,'cnot',cnot_tomo_set)
if sys.argv[1] == "run": backend = "ibmqx5" tomo_circuits = tomo.create_tomography_circuits(qp, 'tomo', qr, cr, tomo_set) qobj = qp.compile(tomo_circuits, backend=backend, shots=1024) qasms = split_qobj(qobj, M=1) run_splitted_qasms(qasms, qobj, backend) sys.exit(0) elif sys.argv[1] == "simulate": tomo_circuits = tomo.create_tomography_circuits(qp, 'tomo', qr, cr, tomo_set) qobj = qp.compile(tomo_circuits, backend="local_qiskit_simulator", shots=1024) res = qp.run(qobj) else: res = recover(sys.argv[1]) # pprint(res1._result) # pprint(res._result) # print(res.get_counts("tomo_meas_X(0)X(1)X(2)X(3)")) tomo_data = tomo.tomography_data(res, "tomo", tomo_set) rho_fit = tomo.fit_tomography_data(tomo_data) np.save("state_tomography/rho.npy", rho_fit) ev = eigvals(rho_fit) plt.bar(range(len(ev)), ev.real) plt.show()
################################################################################ # Create tomo set and tomo circuits; put them in the quantum program tomo_set = tomo.process_tomography_set([1, 0], 'Pauli', 'Pauli') tomo_circuits = tomo.create_tomography_circuits(Q_program, 'FTSWAP', q, c, tomo_set) # Execute the tomo circuits tomo_results = Q_program.execute(tomo_circuits, shots=shots, backend=backendsim, timeout=30) # Gather data from the results tomo_data = tomo.tomography_data(tomo_results, 'FTSWAP', tomo_set) swap_choi_fit = tomo.fit_tomography_data(tomo_data, 'leastsq', options={'trace': 4}) ############################################################################### # Define perfect results U_swap = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]) swap_choi = outer(vectorize(U_swap)) # Plot perfect and fitted results #print('Perfect Choi matrix for Swap operation:') #plot_state(swap_choi,'city') #print('Fitted Choi matrix from simulations using tomography:') #plot_state(swap_choi_fit,'city') # Analyse data
def _tomography_test_data(qp, name, qr, cr, tomoset, shots): tomo.create_tomography_circuits(qp, name, qr, cr, tomoset) result = qp.execute(tomo.tomography_circuit_names(tomoset, name), shots=shots, seed=42, timeout=180) data = tomo.tomography_data(result, name, tomoset) return tomo.fit_tomography_data(data)
print('Created State tomography circuits:') for name in bell_tomo_circuit_names: print(name) # Use the local simulator# Use t backend = 'local_qasm_simulator' # Take 5000 shots for each measurement basis shots = 5000 # Run the simulation bell_tomo_result = Q_program.execute(bell_tomo_circuit_names, backend=backend, shots=shots) print(bell_tomo_result) bell_tomo_data = tomo.tomography_data(bell_tomo_result, 'bell', bell_tomo_set) rho_fit = tomo.fit_tomography_data(bell_tomo_data) # calculate fidelity, concurrence and purity of fitted state# calcul F_fit = state_fidelity(rho_fit, bell_psi) con = concurrence(rho_fit) pur = purity(rho_fit) # plot plot_state(rho_fit, 'paulivec') plot_state(rho_fit, 'city') print('Fidelity =', F_fit) print('concurrence = ', str(con)) print('purity = ', str(pur))