Example #1
0
    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)
Example #2
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)
Example #3
0
 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)
Example #4
0
    def setUp(self):
        # move working directories
        self.old = os.getcwd()
        os.chdir(os.path.abspath(os.path.dirname(__file__)))

        #Set GateSet objects to "strict" mode for testing
        pygsti.objects.GateSet._strict = True

        # density matrix == 3x3 block diagonal matrix: a 2x2 block followed by a 1x1 block
        self.stateSpaceDims = [2, 1]
        self.std = pygsti.Basis('std', 3)
        self.gm = pygsti.Basis('gm', 3)
        self.stdSmall = pygsti.Basis('std', [2, 1])
        self.gmSmall = pygsti.Basis('gm', [2, 1])

        #labels which give a tensor product interp. for the states within each density matrix block
        self.stateSpaceLabels = [('Qhappy', ), ('Lsad', )]

        #Build a test gate   -- old # X(pi,Qhappy)*LX(pi,0,2)
        self.testGate = pygsti.construction.build_gate(self.stateSpaceDims,
                                                       self.stateSpaceLabels,
                                                       "LX(pi,0,2)", "std")
        self.testGateGM_mx = bt.change_basis(self.testGate, self.stdSmall,
                                             self.gmSmall)
        self.expTestGate_mx = bt.flexible_change_basis(self.testGate,
                                                       self.stdSmall, self.std)
        self.expTestGateGM_mx = bt.change_basis(self.expTestGate_mx, self.std,
                                                self.gm)
Example #5
0
    def setUp(self):
        #Set Model objects to "strict" mode for testing
        ExplicitOpModel._strict = True

        # density matrix == 3x3 block diagonal matrix: a 2x2 block followed by a 1x1 block
        self.stateSpaceDims = [(4, ), (1, )]
        self.stateSpaceUDims = [(2, ), (1, )]
        self.std = Basis.cast('std', 9)
        self.gm = Basis.cast('gm', 9)
        self.stdSmall = Basis.cast('std', [4, 1])
        self.gmSmall = Basis.cast('gm', [4, 1])

        #labels which give a tensor product interp. for the states within each density matrix block
        self.stateSpaceLabels = [('Qhappy', ), ('Lsad', )]

        # Adjust for deprecation of _create_operation
        self.sslbls = statespace.ExplicitStateSpace(self.stateSpaceLabels,
                                                    self.stateSpaceUDims)

        #Build a test gate   -- old # X(pi,Qhappy)*LX(pi,0,2)
        self.testGate = create_operation("LX(pi,0,2)", self.sslbls,
                                         self.stdSmall)
        self.testGateGM_mx = bt.change_basis(self.testGate, self.stdSmall,
                                             self.gmSmall)
        self.expTestGate_mx = bt.flexible_change_basis(self.testGate,
                                                       self.stdSmall, self.std)
        self.expTestGateGM_mx = bt.change_basis(self.expTestGate_mx, self.std,
                                                self.gm)
Example #6
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)