def test_few_qubit_fns(self): state_vec = np.array([1,0],'complex') dmVec = pygsti.state_to_pauli_density_vec(state_vec) self.assertArraysAlmostEqual(dmVec, np.array([[0.70710678],[0],[0],[0.70710678]], 'complex')) theta = np.pi ex = 1j * theta*pygsti.sigmax/2 U = scipy.linalg.expm(ex) # U is 2x2 unitary matrix operating on single qubit in [0,1] basis (X(pi) rotation) op = pygsti.unitary_to_pauligate(U) op_ans = np.array([[ 1., 0., 0., 0.], [ 0., 1., 0., 0.], [ 0., 0., -1., 0.], [ 0., 0., 0., -1.]], 'd') self.assertArraysAlmostEqual(op, op_ans) U_2Q = np.identity(4, 'complex'); U_2Q[2:,2:] = U # U_2Q is 4x4 unitary matrix operating on isolated two-qubit space (CX(pi) rotation) op_2Q = pygsti.unitary_to_pauligate(U_2Q) stdMx = np.array( [[1,0],[0,0]], 'complex' ) #density matrix pauliVec = pygsti.stdmx_to_ppvec(stdMx) self.assertArraysAlmostEqual(pauliVec, np.array([[0.70710678],[0],[0],[0.70710678]], 'complex')) stdMx2 = pygsti.ppvec_to_stdmx(pauliVec) self.assertArraysAlmostEqual( stdMx, stdMx2 )
def test_few_qubit_fns(self): state_vec = np.array([1,0],'complex') dmVec = pygsti.state_to_pauli_density_vec(state_vec) self.assertArraysAlmostEqual(dmVec, np.array([[0.70710678],[0],[0],[0.70710678]], 'complex')) theta = np.pi ex = 1j * theta*pygsti.sigmax/2 U = scipy.linalg.expm(ex) # U is 2x2 unitary matrix operating on single qubit in [0,1] basis (X(pi) rotation) op = pygsti.unitary_to_pauligate_1q(U) op_ans = np.array([[ 1., 0., 0., 0.], [ 0., 1., 0., 0.], [ 0., 0., -1., 0.], [ 0., 0., 0., -1.]], 'd') self.assertArraysAlmostEqual(op, op_ans) U_2Q = np.identity(4, 'complex'); U_2Q[2:,2:] = U # U_2Q is 4x4 unitary matrix operating on isolated two-qubit space (CX(pi) rotation) op_2Q = pygsti.unitary_to_pauligate_2q(U_2Q) stdMx = np.array( [[1,0],[0,0]], 'complex' ) #density matrix pauliVec = pygsti.stdmx_to_ppvec(stdMx) self.assertArraysAlmostEqual(pauliVec, np.array([[0.70710678],[0],[0],[0.70710678]], 'complex')) stdMx2 = pygsti.ppvec_to_stdmx(pauliVec) self.assertArraysAlmostEqual( stdMx, stdMx2 )
def test_transforms(self): mxStd = np.array([[1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]], 'complex') vecStd = np.array([1,0,0,0], 'complex') change = bt.change_basis mxGM = change(mxStd, 'std', 'gm') mxStd2 = change(mxGM, 'gm', 'std') self.assertArraysAlmostEqual( mxStd, mxStd2) vecGM = change(vecStd, 'std', 'gm') vecStd2 = change(vecGM, 'gm', 'std') self.assertArraysAlmostEqual( vecStd, vecStd2 ) mxPP = change(mxStd, 'std', 'pp') mxStd2 = change(mxPP, 'pp', 'std') self.assertArraysAlmostEqual( mxStd, mxStd2 ) vecPP = change(vecStd, 'std', 'pp') vecStd2 = change(vecPP, 'pp', 'std') self.assertArraysAlmostEqual( vecStd, vecStd2 ) mxPP2 = change(mxGM, 'gm', 'pp') self.assertArraysAlmostEqual( mxPP, mxPP2 ) vecPP2 = change(vecGM, 'gm', 'pp') self.assertArraysAlmostEqual( vecPP, vecPP2 ) mxGM2 = change(mxPP, 'pp', 'gm') self.assertArraysAlmostEqual( mxGM, mxGM2 ) vecGM2 = change(vecPP, 'pp', 'gm') self.assertArraysAlmostEqual( vecGM, vecGM2 ) non_herm_mxStd = np.array([[1,0,2,3j], [0,1,0,2], [0,0,1,0], [0,0,0,1]], 'complex') non_herm_vecStd = np.array([1,0,2,3j], 'complex') # ~ non-herm 2x2 density mx rank3tensor = np.ones((4,4,4),'d') with self.assertRaises(ValueError): change(non_herm_mxStd, 'std', 'gm') #will result in gm mx with *imag* part with self.assertRaises(ValueError): change(non_herm_vecStd, 'std', 'gm') #will result in gm vec with *imag* part with self.assertRaises(ValueError): change(non_herm_mxStd, 'std', 'pp') #will result in pp mx with *imag* part with self.assertRaises(ValueError): change(non_herm_vecStd, 'std', 'pp') #will result in pp vec with *imag* part with self.assertRaises(ValueError): change(rank3tensor, 'std', 'gm') #only convert rank 1 & 2 objects with self.assertRaises(ValueError): change(rank3tensor, 'gm', 'std') #only convert rank 1 & 2 objects with self.assertRaises(ValueError): change(rank3tensor, 'std', 'pp') #only convert rank 1 & 2 objects with self.assertRaises(ValueError): change(rank3tensor, 'pp', 'std') #only convert rank 1 & 2 objects with self.assertRaises(ValueError): change(rank3tensor, 'gm', 'pp') #only convert rank 1 & 2 objects with self.assertRaises(ValueError): change(rank3tensor, 'pp', 'gm') #only convert rank 1 & 2 objects densityMx = np.array( [[1,0],[0,-1]], 'complex' ) gmVec = pygsti.stdmx_to_gmvec(densityMx) ppVec = pygsti.stdmx_to_ppvec(densityMx) stdVec = pygsti.stdmx_to_stdvec(densityMx) self.assertArraysAlmostEqual( gmVec, np.array( [[0],[0],[0],[np.sqrt(2)]], 'd') ) self.assertArraysAlmostEqual( ppVec, np.array( [[0],[0],[0],[np.sqrt(2)]], 'd') ) self.assertArraysAlmostEqual( stdVec, np.array( [[1],[0],[0],[-1]], 'complex') ) mxFromGM = pygsti.gmvec_to_stdmx(gmVec) mxFromPP = pygsti.ppvec_to_stdmx(ppVec) mxFromStd = pygsti.stdvec_to_stdmx(stdVec) self.assertArraysAlmostEqual( mxFromGM, densityMx) self.assertArraysAlmostEqual( mxFromPP, densityMx) self.assertArraysAlmostEqual( mxFromStd, densityMx)
def test_transforms(self): mxStd = np.array([[1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]], 'complex') vecStd = np.array([1,0,0,0], 'complex') mxGM = pygsti.std_to_gm(mxStd) mxStd2 = pygsti.gm_to_std(mxGM) self.assertArraysAlmostEqual( mxStd, mxStd2 ) vecGM = pygsti.std_to_gm(vecStd) vecStd2 = pygsti.gm_to_std(vecGM) self.assertArraysAlmostEqual( vecStd, vecStd2 ) mxPP = pygsti.std_to_pp(mxStd) mxStd2 = pygsti.pp_to_std(mxPP) self.assertArraysAlmostEqual( mxStd, mxStd2 ) vecPP = pygsti.std_to_pp(vecStd) vecStd2 = pygsti.pp_to_std(vecPP) self.assertArraysAlmostEqual( vecStd, vecStd2 ) mxPP2 = pygsti.gm_to_pp(mxGM) self.assertArraysAlmostEqual( mxPP, mxPP2 ) vecPP2 = pygsti.gm_to_pp(vecGM) self.assertArraysAlmostEqual( vecPP, vecPP2 ) mxGM2 = pygsti.pp_to_gm(mxPP) self.assertArraysAlmostEqual( mxGM, mxGM2 ) vecGM2 = pygsti.pp_to_gm(vecPP) self.assertArraysAlmostEqual( vecGM, vecGM2 ) non_herm_mxStd = np.array([[1,0,2,3j], [0,1,0,2], [0,0,1,0], [0,0,0,1]], 'complex') non_herm_vecStd = np.array([1,0,2,3j], 'complex') # ~ non-herm 2x2 density mx rank3tensor = np.ones((4,4,4),'d') with self.assertRaises(ValueError): pygsti.std_to_gm(non_herm_mxStd) #will result in gm mx with *imag* part with self.assertRaises(ValueError): pygsti.std_to_gm(non_herm_vecStd) #will result in gm vec with *imag* part with self.assertRaises(ValueError): pygsti.std_to_pp(non_herm_mxStd) #will result in pp mx with *imag* part with self.assertRaises(ValueError): pygsti.std_to_pp(non_herm_vecStd) #will result in pp vec with *imag* part with self.assertRaises(ValueError): pygsti.std_to_gm(rank3tensor) #only convert rank 1 & 2 objects with self.assertRaises(ValueError): pygsti.gm_to_std(rank3tensor) #only convert rank 1 & 2 objects with self.assertRaises(ValueError): pygsti.std_to_pp(rank3tensor) #only convert rank 1 & 2 objects with self.assertRaises(ValueError): pygsti.pp_to_std(rank3tensor) #only convert rank 1 & 2 objects with self.assertRaises(ValueError): pygsti.gm_to_pp(rank3tensor) #only convert rank 1 & 2 objects with self.assertRaises(ValueError): pygsti.pp_to_gm(rank3tensor) #only convert rank 1 & 2 objects densityMx = np.array( [[1,0],[0,-1]], 'complex' ) gmVec = pygsti.stdmx_to_gmvec(densityMx) ppVec = pygsti.stdmx_to_ppvec(densityMx) stdVec = pygsti.stdmx_to_stdvec(densityMx) self.assertArraysAlmostEqual( gmVec, np.array( [[0],[0],[0],[np.sqrt(2)]], 'd') ) self.assertArraysAlmostEqual( ppVec, np.array( [[0],[0],[0],[np.sqrt(2)]], 'd') ) self.assertArraysAlmostEqual( stdVec, np.array( [[1],[0],[0],[-1]], 'complex') ) mxFromGM = pygsti.gmvec_to_stdmx(gmVec) mxFromPP = pygsti.ppvec_to_stdmx(ppVec) mxFromStd = pygsti.stdvec_to_stdmx(stdVec) self.assertArraysAlmostEqual( mxFromGM, densityMx) self.assertArraysAlmostEqual( mxFromPP, densityMx) self.assertArraysAlmostEqual( mxFromStd, densityMx)