def mitigated_results(circuit, backend, results): # Import the required methods from qiskit.providers.aer.noise import NoiseModel from qiskit.ignis.mitigation.measurement import (complete_meas_cal, CompleteMeasFitter) # Get noise model for backend noise_model = NoiseModel.from_backend(backend) # Create the measurement fitter qr = QuantumRegister(circuit.num_qubits) meas_calibs, state_labels = complete_meas_cal(qr=qr, circlabel='mcal') job = execute(meas_calibs, backend=Aer.get_backend('qasm_simulator'), shots=8192, noise_model=noise_model) cal_results = job.result() meas_fitter = CompleteMeasFitter(cal_results, state_labels, circlabel='mcal') #print(meas_fitter.cal_matrix) # Plot the calibration matrix print("Calibration matrix") meas_fitter.plot_calibration() # Get the filter object meas_filter = meas_fitter.filter # Results with mitigation mitigated_results = meas_filter.apply(results) mitigated_counts = mitigated_results.get_counts(0) print("Mitigated", backend, "results:\n", mitigated_counts) return (mitigated_counts)
def _run_analysis(self, experiment_data, parameter_guess=None, plot=True, ax=None): state_labels = [] for datum in experiment_data.data(): state_label = datum['metadata']['state_label'] if state_label in state_labels: break state_labels.append(state_label) meas_fitter = CompleteMeasFitter(None, state_labels, circlabel='mcal') nstates = len(state_labels) for job_id in experiment_data.job_ids: full_result = experiment_data.backend.retrieve_job(job_id).result() # full_result might contain repeated experiments for iset in range(len(full_result.results) // nstates): try: date = full_result.date except: date = None try: status = full_result.status except: status = None try: header = full_result.header except: header = None result = Result(full_result.backend_name, full_result.backend_version, \ full_result.qobj_id, full_result.job_id, \ full_result.success, full_result.results[iset * nstates:(iset + 1) * nstates], \ date=date, status=status, header=header, **full_result._metadata) meas_fitter.add_data(result) results = [ AnalysisResultData('error_matrix', meas_fitter.cal_matrix, extra=state_labels) ] plots = [] if plot: figure, ax = plt.subplots(1, 1) meas_fitter.plot_calibration(ax=ax) plots.append(figure) return results, plots
from qiskit.tools.visualization import * plt.figure() plot_histogram(raw_counts), plt.title('raw') plt.figure() plot_histogram(mitigated_counts[-1]) plt.title('mitigated') plt.figure() plt.bar(range(2),[r_counts,m_counts]), plt.xticks('01','10','00','11') plt.legend('raw','mitigated') plt.title('Q1 counts') # Plot the calibration matrix print('Q1 Calibration Matrix') meas_fitter.plot_calibration() plt.show() flag = 1 # What is the measurement fidelity of Q0? print("Average Measurement Fidelity of Q1: %f" % meas_fitter.readout_fidelity( label_list = [['00','01'],['10','11']])) if label1[l][i] == 0 and label2[l][i] == 1: drive_idx = 0 target_idx = 1 flip_drive = True backend_sim = qiskit.providers.aer.PulseSimulator() # qubit_lo_freq01 = two_qubit_model.hamiltonian.get_qubit_lo_from_drift() # experiments10 = cr_drive_experiments(drive_idx, target_idx, flip_drive) # compute frequencies from the Hamiltonian rabifit_result = RabiFitter(result0, amps0, qubits, fit_p0 = [0.5,0.5,0.6,1.5])
def twospin_df(J, Bx, dB, Bz_max, dt_steps, dt_steps_bool, gamma, skip, provider, backend, pq1, pq2): count_list = [] calibrated_count_list = [] tarray, Bzarray, dt_or_steps, _ = adiabaticramp(J, Bx, dB, Bz_max, dt_steps, dt_steps_bool, gamma) if dt_steps_bool == 'dt': dt = dt_steps else: dt = dt_or_steps thetaarray, _ = theta(Bzarray, dt) # Calibration Matrix meas_calibs, state_labels = complete_meas_cal(qubit_list=[0, 1], qr=QuantumRegister(2), circlabel='2spin') cal_results = execute(meas_calibs, backend=provider.get_backend(backend), shots=1000).result() meas_calibs, state_labels = complete_meas_cal(qubit_list=[0, 1], qr=QuantumRegister(2), circlabel='2spin') meas_fitter = CompleteMeasFitter(cal_results, state_labels, circlabel='2spin') meas_fitter.plot_calibration() qmap = Layout() i = 0 while (i < len(thetaarray)): print('Bz = %f' % (Bzarray[i])) qr = QuantumRegister(2, 'qr') cr = ClassicalRegister(2, 'cr') circ = QuantumCircuit(qr, cr) qmap.from_dict(input_dict={qr[0]: pq1, qr[1]: pq2}) circ.initialize([0, 0, 0, 1], [0, 1]) twospin_instruction(circ, J, Bx, thetaarray[:i + 1], dt) circ.measure([0, 1], [0, 1]) result = execute(circ, provider.get_backend(backend), shots=5000).result() counts = result.get_counts() counts['Time'] = tarray[i] counts['Bz'] = Bzarray[i] count_list.append(counts) with open("count_list.txt", "w") as output: output.write(str(count_list)) mitigated_counts = meas_fitter.filter.apply(result).get_counts() mitigated_counts['Time'] = tarray[i] mitigated_counts['Bz'] = Bzarray[i] calibrated_count_list.append(mitigated_counts) with open("calibrated_count_list.txt", "w") as output: output.write(str(calibrated_count_list)) i = i + 1 + skip # Creating dataframe df = pd.DataFrame(count_list) time_col = df.pop('Time') df.insert(0, 'Time', time_col) df['Exact'] = exactm(J, Bx, Bzarray, dt) df['Interaction'] = interactionm(J, Bx, thetaarray, dt) calibrated_df = pd.DataFrame(calibrated_count_list) time_col = df.pop('Time') df.insert(0, 'Time', time_col) if '00' not in df: df['00'] = 0 if '01' not in df: df['01'] = 0 if '10' not in df: df['10'] = 0 if '11' not in df: df['11'] = 0 df = df.fillna(0) if '00' not in calibrated_df: calibrated_df['00'] = 0 if '01' not in calibrated_df: calibrated_df['01'] = 0 if '10' not in calibrated_df: calibrated_df['10'] = 0 if '11' not in calibrated_df: calibrated_df['11'] = 0 calibrated_df = calibrated_df.fillna(0) # Calculating mz total = df['00'] + df['01'] + df['10'] + df['11'] df['mz'] = -(df['00'] / total - df['11'] / total) calibrated_df['mz'] = -(df['00'] / total - df['11'] / total) # Creating Files if dt_steps_bool == 'dt': df.to_csv( 'J={:.3f} Bx={:.3f} dB={:.3f} BzMax={:.3f} dt={:.3f} gamma={:.3f}.csv' .format(J, Bx, dB, Bz_max, dt_steps, gamma)) calibrated_df.to_csv( 'Calibrated J={:.3f} Bx={:.3f} dB={:.3f} BzMax={:.3f} dt={:.3f} gamma={:.3f}.csv' .format(J, Bx, dB, Bz_max, dt_steps, gamma)) else: df.to_csv( 'J={:.3f} Bx={:.3f} dB={:.3f} BzMax={:.3f} BzSteps={:.3f} gamma={:.3f}.csv' .format(J, Bx, dB, Bz_max, dt_steps, gamma)) calibrated_df.to_csv( 'Calibrated J={:.3f} Bx={:.3f} dB={:.3f} BzMax={:.3f} BzSteps={:.3f} gamma={:.3f}.csv' .format(J, Bx, dB, Bz_max, dt_steps, gamma)) return df, dt_or_steps, thetaarray