def test_purity(self): rho1 = [[1, 0], [0, 0]] rho2 = [[0.5, 0], [0, 0.5]] rho3 = 0.7 * np.array(rho1) + 0.3 * np.array(rho2) test_pass = (purity(rho1) == 1.0 and purity(rho2) == 0.5 and round(purity(rho3), 10) == 0.745) self.assertTrue(test_pass)
def test_purity(self): rho1 = [[1, 0], [0, 0]] rho2 = [[0.5, 0], [0, 0.5]] rho3 = 0.7 * np.array(rho1) + 0.3 * np.array(rho2) test_pass = purity(rho1) == 1.0 and \ purity(rho2) == 0.5 and \ round(purity(rho3), 10) == 0.745 self.assertTrue(test_pass)
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 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))
def test_purity_1d_input(self): input_state = [1, 0] res = purity(input_state) self.assertEqual(1, res)
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))