예제 #1
0
 def test_auto_expand(self):
     comp = Basis(matrices=[('std', 2,), ('std', 1)])
     std  = Basis('std', 3)
     mxStd = np.identity(5)
     test   = bt.resize_std_mx(mxStd, 'expand', comp, std)
     test2  = bt.resize_std_mx(test, 'contract', std, comp)
     self.assertArraysAlmostEqual(test2, mxStd)
예제 #2
0
    def test_composite_basis(self):
        comp = Basis([('std', 2,), ('std', 1)])

        a = Basis([('std', 2), ('std', 2)])
        b = Basis('std', [2,2])
        self.assertEqual(len(a), len(b))
        self.assertArraysAlmostEqual(np.array(a._matrices), np.array(b._matrices))
예제 #3
0
    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('std', [1,1])
        end   = Basis('std', 2)
        
        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 )
예제 #4
0
 def test_change_between_composites(self):
     a = Basis('std', [2, 1])
     b = Basis('gm',  [2, 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)
예제 #5
0
 def test_flexible_change_basis(self):
     comp  = Basis(matrices=[('gm', 2,), ('gm', 1)])
     std   = Basis('std', 3)
     mx    = np.identity(5)
     test  = bt.flexible_change_basis(mx, comp, std)
     self.assertEqual(test.shape[0], sum(comp.dim.blockDims) ** 2)
     test2 = bt.flexible_change_basis(test, std, comp)
     self.assertArraysAlmostEqual(test2, mx)
예제 #6
0
    def test_sparse_basis(self):
        sparsePP = Basis("pp",2,sparse=True)
        sparsePP2 = Basis("pp",2,sparse=True)
        sparseBlockPP = Basis("pp",[2,2],sparse=True)
        sparsePP_2Q = Basis("pp",4,sparse=True)
        sparseGM_2Q = Basis("gm",2,sparse=True) #different sparsity structure than PP 2Q
        denseGM = Basis("gm",2,sparse=False)
        
        mxs = sparsePP.get_composite_matrices()
        block_mxs = sparseBlockPP.get_composite_matrices()

        expeq = sparsePP.expanded_equivalent()
        block_expeq = sparseBlockPP.expanded_equivalent()

        raw_mxs = bt.basis_matrices("pp",2,sparse=True)
        
        #test equality of bases with other bases and matrices
        self.assertEqual(sparsePP, sparsePP2)
        self.assertEqual(sparsePP, raw_mxs)
        self.assertNotEqual(sparsePP, sparsePP_2Q)
        self.assertNotEqual(sparsePP_2Q, sparseGM_2Q)

        #sparse transform matrix
        trans = sparsePP.transform_matrix(sparsePP2)
        self.assertArraysAlmostEqual(trans, np.identity(4,'d'))
        trans2 = sparsePP.transform_matrix(denseGM)

        #test equality for large bases, which is too expensive so it always returns false
        large_sparsePP = Basis("pp",16,sparse=True)
        large_sparsePP2 = Basis("pp",16,sparse=True)
        self.assertNotEqual(large_sparsePP, large_sparsePP2)
예제 #7
0
    def test_basis_object(self):
        #test a few aspects of a Basis object that other tests miss...
        b = Basis("pp",2)
        beq = b.expanded_equivalent()
        longnm = bt.basis_longname(b)
        lbls = bt.basis_element_labels(b)

        raw_mxs = bt.basis_matrices("pp",2)
        with self.assertRaises(NotImplementedError):
            bt.basis_matrices("foobar",2) #invalid basis name

        print("Dim = ", repr(b.dim) ) # calls Dim.__repr__
예제 #8
0
    def test_jamiolkowski_ops(self):
        gm = Basis('gm', 2)
        pp = Basis('pp', 2)
        std = Basis('std', 2)
        mxGM = np.array(
            [[1, 0, 0, 0], [0, 0, 1, 0], [0, -1, 0, 0], [0, 0, 0, 1]],
            'complex')

        mxStd = bt.change_basis(mxGM, gm, std)
        mxPP = bt.change_basis(mxGM, gm, pp)

        choiStd = pygsti.jamiolkowski_iso(mxStd, std, std)
        choiStd2 = pygsti.jamiolkowski_iso(mxGM, gm, std)
        choiStd3 = pygsti.jamiolkowski_iso(mxPP, pp, std)
        fastChoiStd = pygsti.fast_jamiolkowski_iso_std(mxStd, std)
        fastChoiStd2 = pygsti.fast_jamiolkowski_iso_std(mxGM, gm)
        fastChoiStd3 = pygsti.fast_jamiolkowski_iso_std(mxPP, pp)

        choiGM = pygsti.jamiolkowski_iso(mxStd, std, gm)
        choiGM2 = pygsti.jamiolkowski_iso(mxGM, gm, gm)
        choiGM3 = pygsti.jamiolkowski_iso(mxPP, pp, gm)

        choiPP = pygsti.jamiolkowski_iso(mxStd, std, pp)
        choiPP2 = pygsti.jamiolkowski_iso(mxGM, gm, pp)
        choiPP3 = pygsti.jamiolkowski_iso(mxPP, pp, pp)

        self.assertArraysAlmostEqual(choiStd, choiStd2)
        self.assertArraysAlmostEqual(choiStd, choiStd3)
        self.assertArraysAlmostEqual(choiStd, fastChoiStd)
        self.assertArraysAlmostEqual(choiStd, fastChoiStd2)
        self.assertArraysAlmostEqual(choiStd, fastChoiStd3)
        self.assertArraysAlmostEqual(choiGM, choiGM2)
        self.assertArraysAlmostEqual(choiGM, choiGM3)
        self.assertArraysAlmostEqual(choiPP, choiPP2)
        self.assertArraysAlmostEqual(choiPP, choiPP3)

        gateStd = pygsti.jamiolkowski_iso_inv(choiStd, std, std)
        gateStd2 = pygsti.jamiolkowski_iso_inv(choiGM, gm, std)
        gateStd3 = pygsti.jamiolkowski_iso_inv(choiPP, pp, std)

        gateGM = pygsti.jamiolkowski_iso_inv(choiStd, std, gm)
        gateGM2 = pygsti.jamiolkowski_iso_inv(choiGM, gm, gm)
        gateGM3 = pygsti.jamiolkowski_iso_inv(choiPP, pp, gm)

        gatePP = pygsti.jamiolkowski_iso_inv(choiStd, std, pp)
        gatePP2 = pygsti.jamiolkowski_iso_inv(choiGM, gm, pp)
        gatePP3 = pygsti.jamiolkowski_iso_inv(choiPP, pp, pp)

        fastGateStd = pygsti.fast_jamiolkowski_iso_std_inv(choiStd, std)
        fastGateGM = pygsti.fast_jamiolkowski_iso_std_inv(choiStd, gm)
        fastGatePP = pygsti.fast_jamiolkowski_iso_std_inv(choiStd, pp)

        self.assertArraysAlmostEqual(gateStd, mxStd)
        self.assertArraysAlmostEqual(gateStd2, mxStd)
        self.assertArraysAlmostEqual(gateStd3, mxStd)
        self.assertArraysAlmostEqual(fastGateStd, mxStd)

        self.assertArraysAlmostEqual(gateGM, mxGM)
        self.assertArraysAlmostEqual(gateGM2, mxGM)
        self.assertArraysAlmostEqual(gateGM3, mxGM)
        self.assertArraysAlmostEqual(fastGateGM, mxGM)

        self.assertArraysAlmostEqual(gatePP, mxPP)
        self.assertArraysAlmostEqual(gatePP2, mxPP)
        self.assertArraysAlmostEqual(gatePP3, mxPP)
        self.assertArraysAlmostEqual(fastGatePP, mxPP)
        '''
        with self.assertRaises(NotImplementedError):
            pygsti.jamiolkowski_iso(mxStd, "foobar", gm) #invalid gate basis
        with self.assertRaises(NotImplementedError):
            pygsti.jamiolkowski_iso(mxStd, std, "foobar") #invalid choi basis
        with self.assertRaises(NotImplementedError):
            pygsti.jamiolkowski_iso_inv(choiStd, "foobar", gm) #invalid choi basis
        with self.assertRaises(NotImplementedError):
            pygsti.jamiolkowski_iso_inv(choiStd, std, "foobar") #invalid gate basis
        '''

        sumOfNeg = pygsti.sum_of_negative_choi_evals(std1Q.gs_target)
        sumOfNegWt = pygsti.sum_of_negative_choi_evals(std1Q.gs_target, {
            'Gx': 1.0,
            'Gy': 0.5
        })
        sumsOfNeg = pygsti.sums_of_negative_choi_evals(std1Q.gs_target)
        magsOfNeg = pygsti.mags_of_negative_choi_evals(std1Q.gs_target)
        self.assertAlmostEqual(sumOfNeg, 0.0)
        self.assertArraysAlmostEqual(sumsOfNeg,
                                     np.zeros(3,
                                              'd'))  # 3 gates in std.gs_target
        self.assertArraysAlmostEqual(magsOfNeg, np.zeros(
            12, 'd'))  # 3 gates * 4 evals each = 12
예제 #9
0
 def test_gm_basis(self):
     cmb = Basis('gm', sum(self.stateSpaceDims))
     self.checkBasis(cmb)
예제 #10
0
 def test_std_basis(self):
     cmb = Basis('std', sum(self.stateSpaceDims))
     self.checkBasis(cmb)
예제 #11
0
    def test_general(self):
        Basis('pp', 2)
        Basis('std', [2, 1])
        Basis([('std', 2), ('gm', 2)])

        std  = Basis('std', 2)
        std4  = Basis('std', 4)
        std2x2 = Basis([('std', 2), ('std', 2)])
        gm   = Basis('gm', 2)
        ungm = Basis('gm_unnormalized', 2)
        empty = Basis([]) #special "empty" basis
        self.assertEqual(empty.name, "*Empty*")

        from_basis,to_basis = pygsti.tools.build_basis_pair(np.identity(4,'d'),"std","gm")
        from_basis,to_basis = pygsti.tools.build_basis_pair(np.identity(4,'d'),std,"gm")
        from_basis,to_basis = pygsti.tools.build_basis_pair(np.identity(4,'d'),"std",gm)

        gm_mxs = gm.get_composite_matrices()
        unnorm = Basis(matrices=[ gm_mxs[0], 2*gm_mxs[1] ])

        std[0]
        std.get_sub_basis_matrices(0)

        print(gm.get_composite_matrices())
        self.assertTrue(gm.is_normalized())
        self.assertFalse(ungm.is_normalized())
        self.assertFalse(unnorm.is_normalized())

        transMx = bt.transform_matrix(std, gm)

        composite = Basis([std, gm])

        comp = Basis(matrices=[std, gm], name='comp', longname='CustomComposite')

        comp.labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
        comp = Basis(matrices=[std, gm], name='comp', longname='CustomComposite', labels=[
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'
            ])

        std2x2Matrices = np.array([
            [[1, 0],
             [0, 0]],
            
            [[0, 1],
             [0, 0]],
            
            [[0, 0],
             [1, 0]],
            
            [[0, 0],
             [0, 1]]
        ],'complex')

        empty = Basis(matrices=[])
        alt_standard = Basis(matrices=std2x2Matrices)
        print("MXS = \n",alt_standard._matrices)
        alt_standard = Basis(matrices=std2x2Matrices,
                             name='std',
                             longname='Standard'
                            )
        self.assertEqual(alt_standard, std2x2Matrices)

        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(ValueError):
            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('std', [1,1])
        end   = Basis('std', 2)
        mxInReducedBasis = bt.resize_std_mx(mxInStdBasis, 'contract', end, begin)
        original         = bt.resize_std_mx(mxInReducedBasis, 'expand', begin, end)
예제 #12
0
 def test_qt(self):
     qt = Basis('qt', 3)
     qt = Basis('qt', [3])