def test_change_between_composites(self): a = Basis.cast('std', [4, 1]) b = Basis.cast('gm', [4, 1]) mxStd = np.identity(5) test = bt.change_basis(mxStd, a, b) self.assertEqual(test.shape, mxStd.shape) test2 = bt.change_basis(test, b, a) self.assertArraysAlmostEqual(test2, mxStd)
def test_flexible_change_basis(self): comp = Basis.cast([( 'gm', 4, ), ('gm', 1)]) std = Basis.cast('std', 9) mx = np.identity(5) test = bt.flexible_change_basis(mx, comp, std) self.assertEqual(test.shape[0], comp.elsize) test2 = bt.flexible_change_basis(test, std, comp) self.assertArraysAlmostEqual(test2, mx)
def test_auto_expand(self): comp = Basis.cast([( 'std', 4, ), ('std', 1)]) std = Basis.cast('std', 9) mxStd = np.identity(5) test = bt.resize_std_mx(mxStd, 'expand', comp, std) # TODO assert intermediate correctness test2 = bt.resize_std_mx(test, 'contract', std, comp) self.assertArraysAlmostEqual(test2, mxStd)
def test_sparse_lindblad_bases(self): sparsePP = Basis.cast("pp", 16, sparse=True) mxs = sparsePP.elements for lbl, mx in zip(sparsePP.labels, mxs): print("{}: {} matrix with {} nonzero entries (of {} total)".format( lbl, mx.shape, mx.nnz, mx.shape[0] * mx.shape[1])) print(mx.toarray()) print("{} basis elements".format(len(sparsePP))) self.assertEqual(len(sparsePP), 16) # TODO assert correctness M = np.ones((16, 16), 'd') v = np.ones(16, 'd') S = scipy.sparse.identity(16, 'd', 'csr') print("Test types after basis change by sparse basis:") Mout = bt.change_basis(M, sparsePP, 'std') vout = bt.change_basis(v, sparsePP, 'std') Sout = bt.change_basis(S, sparsePP, 'std') print("{} -> {}".format(type(M), type(Mout))) print("{} -> {}".format(type(v), type(vout))) print("{} -> {}".format(type(S), type(Sout))) self.assertIsInstance(Mout, np.ndarray) self.assertIsInstance(vout, np.ndarray) self.assertIsInstance(Sout, scipy.sparse.csr_matrix)
def test_general(self): std = Basis.cast('std', 4) std4 = Basis.cast('std', 16) std2x2 = Basis.cast([('std', 4), ('std', 4)]) gm = Basis.cast('gm', 4) from_basis, to_basis = bt.build_basis_pair(np.identity(4, 'd'), "std", "gm") from_basis, to_basis = bt.build_basis_pair(np.identity(4, 'd'), std, "gm") from_basis, to_basis = bt.build_basis_pair(np.identity(4, 'd'), "std", gm) mx = np.array([[1, 0, 0, 1], [0, 1, 2, 0], [0, 2, 1, 0], [1, 0, 0, 1]]) bt.change_basis(mx, 'std', 'gm') # shortname lookup bt.change_basis(mx, std, gm) # object bt.change_basis(mx, std, 'gm') # combination bt.flexible_change_basis(mx, std, gm) # same dimension I2x2 = np.identity(8, 'd') I4 = bt.flexible_change_basis(I2x2, std2x2, std4) self.assertArraysAlmostEqual( bt.flexible_change_basis(I4, std4, std2x2), I2x2) with self.assertRaises(AssertionError): bt.change_basis(mx, std, std4) # basis size mismatch mxInStdBasis = np.array( [[1, 0, 0, 2], [0, 0, 0, 0], [0, 0, 0, 0], [3, 0, 0, 4]], 'd') begin = Basis.cast('std', [1, 1]) end = Basis.cast('std', 4) mxInReducedBasis = bt.resize_std_mx(mxInStdBasis, 'contract', end, begin) original = bt.resize_std_mx(mxInReducedBasis, 'expand', begin, end)
def test_expand_contract(self): # matrix that operates on 2x2 density matrices, but only on the 0-th and 3-rd # elements which correspond to the diagonals of the 2x2 density matrix. mxInStdBasis = np.array( [[1, 0, 0, 2], [0, 0, 0, 0], [0, 0, 0, 0], [3, 0, 0, 4]], 'd') # Reduce to a matrix operating on a density matrix space with 2 1x1 blocks (hence [1,1]) begin = Basis.cast('std', [1, 1]) end = Basis.cast('std', 4) mxInReducedBasis = bt.resize_std_mx(mxInStdBasis, 'contract', end, begin) #mxInReducedBasis = bt.change_basis(mxInStdBasis, begin, end) notReallyContracted = bt.change_basis(mxInStdBasis, 'std', 'std') # 4 correctAnswer = np.array([[1.0, 2.0], [3.0, 4.0]]) self.assertArraysAlmostEqual(mxInReducedBasis, correctAnswer) self.assertArraysAlmostEqual(notReallyContracted, mxInStdBasis) expandedMx = bt.resize_std_mx(mxInReducedBasis, 'expand', begin, end) #expandedMx = bt.change_basis(mxInReducedBasis, end, begin) expandedMxAgain = bt.change_basis(expandedMx, 'std', 'std') # , 4) self.assertArraysAlmostEqual(expandedMx, mxInStdBasis) self.assertArraysAlmostEqual(expandedMxAgain, mxInStdBasis)
def test_lind_errgens(self): basis = Basis.cast('gm', 4) normalize = False other_mode = "all" ot.lindblad_error_generators(basis, basis, normalize, other_mode) ot.lindblad_error_generators(None, basis, normalize, other_mode) ot.lindblad_error_generators(basis, None, normalize, other_mode) ot.lindblad_error_generators(None, None, normalize, other_mode) normalize = True other_mode = "all" ot.lindblad_error_generators(basis, basis, normalize, other_mode) ot.lindblad_error_generators(None, basis, normalize, other_mode) ot.lindblad_error_generators(basis, None, normalize, other_mode) ot.lindblad_error_generators(None, None, normalize, other_mode) normalize = True other_mode = "diagonal" ot.lindblad_error_generators(basis, basis, normalize, other_mode) ot.lindblad_error_generators(None, basis, normalize, other_mode) ot.lindblad_error_generators(basis, None, normalize, other_mode) ot.lindblad_error_generators(None, None, normalize, other_mode) basis = Basis.cast('gm', 16) mxBasis = Basis.cast('gm', 16) errgen = np.identity(16, 'd') ot.lindblad_errgen_projections(errgen, basis, basis, mxBasis, normalize=True, return_generators=False, other_mode="all", sparse=False) ot.lindblad_errgen_projections(errgen, None, 'gm', mxBasis, normalize=True, return_generators=False, other_mode="all", sparse=False) ot.lindblad_errgen_projections(errgen, 'gm', None, mxBasis, normalize=True, return_generators=True, other_mode="diagonal", sparse=False) basisMxs = bt.basis_matrices('gm', 16, sparse=False) ot.lindblad_errgen_projections(errgen, basisMxs, basisMxs, mxBasis, normalize=True, return_generators=False, other_mode="all", sparse=False) ot.lindblad_errgen_projections(errgen, None, None, mxBasis, normalize=True, return_generators=False, other_mode="all", sparse=False)
def test_raises_on_basis_mismatch(self): with self.assertRaises(ValueError): mdl_target_gm = std2Q_XXYYII.target_model() mdl_target_gm.basis = Basis.cast("gm", 16) ot.project_model(self.model, mdl_target_gm, self.projectionTypes, 'logGti') # basis mismatch