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 add_tomo_circuits(self, circ): if not self.use_quantum_program: # Construct state tomography set for measurement of qubits in the # register qr = next(iter(circ.qregs)) cr = next(iter(circ.cregs)) tomo_set = tomo.state_tomography_set(list(range(qr.size))) # Add the state tomography measurement circuits tomo_circuits = tomo.create_tomography_circuits( circ, qr, cr, tomo_set) return tomo_set, tomo_circuits if self.use_quantum_program: # Construct state tomography set for measurement of qubits in the # register qr_name = list(circ.get_quantum_register_names())[0] cr_name = list(circ.get_classical_register_names())[0] qr = circ.get_quantum_register(qr_name) cr = circ.get_classical_register(cr_name) tomo_set = tomo.state_tomography_set(list(range(qr.size))) # Add the state tomography measurement circuits to the Quantum # Program tomo_circuits = tomo.create_tomography_circuits( circ, qr, cr, tomo_set) return circ, tomo_set, tomo_circuits raise Exception
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(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 add_tomo_circuits(circ): # Construct state tomography set for measurement of qubits in the register qr = next(iter(circ.get_qregs().values())) cr = next(iter(circ.get_cregs().values())) tomo_set = tomo.state_tomography_set(list(range(qr.size))) # Add the state tomography measurement circuits tomo_circuits = tomo.create_tomography_circuits(circ, qr, cr, tomo_set) return tomo_set, tomo_circuits
def add_tomo_circuits(qp): # Construct state tomography set for measurement of qubits in the register qr_name = list(qp.get_quantum_register_names())[0] cr_name = list(qp.get_classical_register_names())[0] qr = qp.get_quantum_register(qr_name) cr = qp.get_classical_register(cr_name) tomo_set = tomo.state_tomography_set(list(range(qr.size))) # Add the state tomography measurement circuits to the Quantum Program tomo_circuits = tomo.create_tomography_circuits(qp, 'prep', qr, cr, tomo_set) return qp, tomo_set, tomo_circuits
def create_tomo_circuits(Quantum_program, circuit_name, quantum_register, classical_register, qubit_list, meas_basis='Pauli', prep_basis='Pauli'): # Create tomo set and tomo circuits; put them in the quantum program tomo_set = tomo.process_tomography_set(qubit_list, meas_basis, prep_basis) tomo_circuits = tomo.create_tomography_circuits(Quantum_program, circuit_name, quantum_register, classical_register, tomo_set) return [Quantum_program, tomo_set, tomo_circuits]
def create_tomo_circuits(Quantum_program, circuit_name, quantum_register, classical_register, qubit_list, meas_basis='Pauli', prep_basis='Pauli'): ''' Create tomo set and tomo circuits for the circuit circuit_name in quantum_program. The circuits are put in the quantum program, and their names are returned as a list tomo_circuits The measurement basis and the preperation basis for the tomography circuits can be specified Standard is a Pauli basis for both meas and prep. The set of tomography experiments is also returned as tomo_set. This function is basically a wrapper for two qiskit: functions 'process_tomography_set' and 'create_tomograpy_circuits' in qiskit.tools.qcvv.tomography For more information on the containments of tomo_set and tomo_circuits see the documentation of these two functions ''' tomo_set = tomo.process_tomography_set(qubit_list, meas_basis, prep_basis) tomo_circuits = tomo.create_tomography_circuits(Quantum_program, circuit_name, quantum_register, classical_register, tomo_set) return [Quantum_program, tomo_set, tomo_circuits]
# hadamard on qubit-1 only had = Q_program.create_circuit("had", [qr], [cr]) had.h(qr[1]) # CNOT gate with qubit 1 control, qubit 0 target (target for ibmqx4) cnot = Q_program.create_circuit("cnot", [qr], [cr]) 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))
c += 1 if not b: sys.exit(1) return Result({"result": xs, "status": "DONE"}, qobj) # tomo_set = tomo.state_tomography_set(list(range(4))) # tomo_circuits = tomo.create_tomography_circuits(qp, 'tomo', qr, cr, tomo_set) # # res1 = qp.execute(tomo_circuits, backend="local_qiskit_simulator", shots=1024) tomo_set = tomo.state_tomography_set(list(range(int(n / 2)))) 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])
#FTSWAP_comp = Q_program.compile(['FTSWAP'],coupling_map=coupling_map ,initial_layout=initial_layout_1) FTSWAP_comp = Q_program.compile(['FTSWAP'], backend=backendreal) print(Q_program.get_compiled_qasm(FTSWAP_comp, 'FTSWAP')) print(Q_program.get_compiled_configuration(FTSWAP_comp, 'FTSWAP')) #Compile non compiled version of circuit to ibmqx4 to test #FTSWAPnoncomp_comp = Q_program.compile(['FTSWAPnoncomp'],coupling_map=coupling_map ,initial_layout=initial_layout_2) FTSWAPnoncomp_comp = Q_program.compile(['FTSWAPnoncomp'], backend=backendreal) print(Q_program.get_compiled_qasm(FTSWAPnoncomp_comp, 'FTSWAPnoncomp')) print(Q_program.get_compiled_configuration(FTSWAPnoncomp_comp, '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
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)
bell_psi = job.result().get_statevector(bell) bell_rho = outer( bell_psi) # construct the density matrix from the state vector #plot the state plot_state(bell_rho, 'paulivec') # Construct state tomography set for measurement of qubits [0, 1] in the Pauli basis bell_tomo_set = tomo.state_tomography_set([0, 1]) # Create a quantum program containing the state preparation circuit Q_program = QuantumProgram() Q_program.add_circuit('bell', bell) # Add the state tomography measurement circuits to the Quantum Program bell_tomo_circuit_names = tomo.create_tomography_circuits( Q_program, 'bell', qr, cr, bell_tomo_set) 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)
# Compile and run the Quantum circuit on a simulator backend job_sim = execute(qc, "local_qasm_simulator", shots=10) sim_result = job_sim.result() # Create a quantum program for the process tomography simulation processprogram = QuantumProgram() processprogram.add_circuit('FTSWAP', qc) # Create registers for the process tomography qr = QuantumRegister(2) cr = ClassicalRegister(2) # Create the tomography set tomo_set = tomo.process_tomography_set([1, 0], 'Pauli', 'Pauli') tomo_circuits = tomo.create_tomography_circuits(processprogram, 'FTSWAP', qr, cr, tomo_set) # Use the local simulator# Use t backend = 'local_qasm_simulator' # Take 5000 shots for each measurement basis shots = 500 # Run the simulation tomo_result = processprogram.execute(tomo_circuits, backend=backend, shots=shots) tomo_data = tomo.tomography_data(tomo_result, 'FTSWAP', tomo_set) #rho_fit = tomo.fit_tomography_data(tomo_data)