Example #1
0
def channel_fidelity(chi_error,B_choi, n):
    '''
    Returns the channel fidelity as <Om|Choi_error|
    '''
    choi_error = tomoself.chi_to_choi(chi_error, B_choi, n)
    maxent = tomoself.get_max_ent_2n(n)
    return complex(maxent.H @ choi_error @ maxent)
Example #2
0
def fit_tomodata_multiple(meas_data_all, tomo_set, B_chi, B_choi, n,stddv=True):
    '''
    Fit the measurement tomography data from multiple datasets in meas_data_all specified by tomo_set to a chi and a corresponding choi matrix.
    There are 3 different methods:
        -'own' (standard): This method used the _fit_tomodata_own_ function, which is specified in that docstring.
            This method uses the _fit_tomodata_own() function. It provides a CP and TP chi and choi matrix.
            Furthermore, it calculates the standard deviation on chi (and choi) if stddv==True (standard)
            For details, see the docstring of that function.
        
    The other two methods use the interal qiskit function qiskit.tools.qcvv.tomography.fit_tomography_data()
    Both these methods use the wrapper function _fit_tomodata_qiskit_(). For details, see that docstring.
        -'wizard' : This method gives a CP choi and chi matrix but has problems with trace preservation.
        -'leastsq': This method is a straightforward least squares fit that does not promise CP, but has less problems with TP.
        
    The process matrices are returned as:
        ((chi,chi_stddv),(choi,choi_stddv))
    '''
    B_chi = tomoself.get_pauli_basis(n, normalise=False)                                         # Get the basis in which chi is expressed
    B_prep = tomoself.get_pauli_basis(n, normalise=False)                       # Get the basis in which the experiments are prepared
    B_meas = tomoself.get_pauli_basis(n, normalise=False)                       # Get the basis in which the measurements are done
    
    lam, lampau, lamstddv = tomoself.get_lambda_from_meas_multiple(tomo_set,             # Get the vectors lambda and lambda_stddv from the tomography data
                                                          meas_data_all, n)
    A = tomoself.get_A_mat(B_prep, B_meas, B_chi)                               # Calculate the A matrix from the prep, meas and chi basis
    chivect = np.linalg.solve(A, lam)                                           # Invert to calculate chi in vectorform
    Ainv = np.linalg.inv(A)                                                     # Calculate the inverse of A for error calculations (A is full rank)
    Ainvsq = np.abs(Ainv)*np.abs(Ainv);                                         # Calculate the elementwise square of Ainv
    lamstddvsq = lamstddv*lamstddv;                                             # Calculate the elementwise square of l_stddv
    chistddvvect = np.sqrt(Ainvsq @ lamstddvsq)                                 # Calculate the standard deviation on chi using the method from the description
    chi = np.reshape(chivect, ((2*n)**2, (2*n)**2))                             # Reshape into a square matrix
    print('Minimum eigenvalue before: ', np.min(np.linalg.eigvals(chi)))
    chi_stddv = np.reshape(chistddvvect, ((2*n)**2, (2*n)**2))                   # Reshape into a square matrix
    print('largest eigenvalue before: ',np.max(np.linalg.eigvals(chi)))
    num = np.max(np.linalg.eigvals(chi)) + np.abs(np.min(np.linalg.eigvals(chi)))
    den = 1+16*np.abs(np.min(np.linalg.eigvals(chi)))
    print('test: ',num/den,'times 4:',4*num/den)
    #chi = make_CP(chi,n)
    print('Minimum eigenvalue after: ', np.min(np.linalg.eigvals(chi)))
    print('largest eigenvalue after: ',np.max(np.linalg.eigvals(chi)))
    choi = tomoself.chi_to_choi(chi,B_choi, n)
    choi_stddv = tomoself.chi_to_choi(chi_stddv, B_choi, n)
    return ((chi,chi_stddv),(choi,choi_stddv))
Example #3
0
def fit_tomodata(tomo_data, tomo_set, B_chi, B_choi, n, method='own',stddv=True):
    '''
    Fit the tomography data from tomo_data specified by tomo_set to a chi and a corresponding choi matrix.
    There are 3 different methods:
        -'own' (standard): This method used the _fit_tomodata_own_ function, which is specified in that docstring.
            This method uses the _fit_tomodata_own() function. It provides a CP and TP chi and choi matrix.
            Furthermore, it calculates the standard deviation on chi (and choi) if stddv==True (standard)
            For details, see the docstring of that function.
        
    The other two methods use the interal qiskit function qiskit.tools.qcvv.tomography.fit_tomography_data()
    Both these methods use the wrapper function _fit_tomodata_qiskit_(). For details, see that docstring.
        -'wizard' : This method gives a CP choi and chi matrix but has problems with trace preservation.
        -'leastsq': This method is a straightforward least squares fit that does not promise CP, but has less problems with TP.
        
    The process matrices are returned as:
        ((chi,chi_stddv),(choi,choi_stddv))
    '''
    if method == 'own':
        chi, chi_stddv = _fit_tomodata_own_(tomo_data, tomo_set, n, stddv)
        chi = make_CP(chi,n)
        choi = tomoself.chi_to_choi(chi,B_choi, n)
        choi_stddv = tomoself.chi_to_choi(chi_stddv, B_choi, n)
    elif method == 'wizard':
        choi = _fit_tomodata_qiskit_(tomo_data, method='wizard')
        chi = tomoself.choi_to_chi(choi, B_choi, n)
        chi_stddv = np.zeros_like(chi)
        choi_stddv = np.zeros_like(chi)
        print('Warning: no standard deviation calculated!')
    elif method == 'leastsq':
        choi = _fit_tomodata_qiskit_(tomo_data, method='leastsq')
        choi = make_CP(choi,n)
        chi = tomoself.choi_to_chi(choi, B_choi, n)
        chi_stddv = np.zeros_like(chi)
        choi_stddv = np.zeros_like(chi)
        print('Warning: no standard deviation calculated!')
    else:
        print('Wrong method supplied: %s is not a valid option.' %(method))
        return None
    return ((chi,chi_stddv),(choi,choi_stddv))
B_choi = tomoself.get_choi_basis(n, B_chi)

((chi,chistddv),(choi,choistddv)) = an.fit_tomodata_multiple(meas_data_all, tomo_set, B_chi, B_choi, n, stddv=True)
print('CP:', an.check_CP(chi))
print('TP:', an.check_TP(chi, B_chi, n))




#%% Filter meas errors out
chi_filtered = chi
choi_filtered = choi
if run_type == 'r' and n == 2:
    Bfilter = store.load_chi_meas_last()['B_filter']
    chi_filtered = an.filter_chi_meas(chi, Bfilter, n)
    choi_filtered = tomoself.chi_to_choi(chi_filtered, B_choi, n)
    del Bfilter

#%% Calculate error matrices
chi_perror = an.get_chi_error(chi_filtered, B_chi, Unitary)
chi_unfiltered_perror = an.get_chi_error(chi, B_chi, Unitary)
chi_perror_stddv = an.get_chi_error(chistddv, B_chi, Unitary)

#%% Calculate traces and fidelities
process_fidelity = an.process_fidelity(chi_perror, n)
process_fidelity_unfiltered = an.process_fidelity(chi_unfiltered_perror, n)
channel_fidelity = an.channel_fidelity(chi_perror, B_choi, n)


print('Process fidelity from error matrix:', np.around(np.abs(process_fidelity),3))
print('Process fidelity from error matrix unr:', np.abs(process_fidelity))
Example #5
0
import Functions.Data_storage as store
import Analysis.tomography_functions as tomoself
import Analysis.Analysefunc as an
import Functions.Plotting as pt
import time
import datetime
#%%
chidict_FTSWAP = store.load_chi_last('FTSWAP')
chidict_NFTSWAP = store.load_chi_last('NFTSWAP')

chiFT = chidict_FTSWAP['chi_filtered']
chiNFT = chidict_NFTSWAP['chi_filtered']

B_chi = tomoself.get_pauli_basis(2)
B_choi = tomoself.get_choi_basis(2, B_chi)
choi_FT = tomoself.chi_to_choi(chiFT, B_choi, 2)
choi_NFT = tomoself.chi_to_choi(chiNFT, B_choi, 2)

chi_tot_meas = np.kron(chiNFT, chiFT)

Fidft_meas = an.get_chi_error(chiFT, B_chi, sef.SWAP)[0, 0] / 4
Fidnft_meas = an.get_chi_error(chiNFT, B_chi, sef.SWAP)[0, 0] / 4
Fidtot_meas = an.get_chi_error(chi_tot_meas,
                               tomoself.get_pauli_basis(4, normalise=False),
                               np.kron(sef.SWAP, sef.SWAP))[0, 0] / 16

gridnr = 20

pslist = np.linspace(0, 0.05, num=gridnr)
pmlist = np.linspace(0, 0.10, num=gridnr)