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 extract_data(results, circuit_name, tomo_set): ''' Extract the data from the results of a tomography experiment. Tomo_data is a dictionary containing keys 'data','meas_basis' and 'prep_basis' ''' tomo_data = tomo.tomography_data(results, circuit_name, tomo_set) return tomo_data
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 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))
cnot.cx(qr[1], qr[0]) 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)
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()
'FTSWAPnoncomp')) ################################################################################ # 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')
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)
def extract_data(results, circuit_name, tomo_set): tomo_data = tomo.tomography_data(results, circuit_name, tomo_set) return tomo_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))