예제 #1
0
class StaticGateTestCase(BaseTestCase):
    def setUp(self):
        super(StaticGateTestCase, self).setUp()
        self.gate = StaticGate([[0, 0], [0, 0]])

    def test_bad(self):
        gate = self.gate.copy()
        with self.assertRaises(ValueError):
            gate.set_value([])
        with self.assertRaises(ValueError):
            gate.set_value([[1, 2], [1, 2]])

        gate.dim = 'adfadsflkj'
        with self.assertRaises(ValueError):
            gate.set_value([[1, 2], [1, 2]])
        with self.assertRaises(ValueError):
            gate.set_value([[[1, 2]], [], []])

        self.assertEqual(gate.num_params(), 0)
        self.assertArraysAlmostEqual(gate.to_vector(), np.array([], 'd'))
        with self.assertRaises(AssertionError):
            gate.from_vector([1])

    def test_deriv_wrt_params(self):
        self.assertArraysAlmostEqual(self.gate.deriv_wrt_params(),
                                     np.array([]))

    def test_transform(self):
        with self.assertRaises(ValueError):
            elT = pygsti.objects.FullGaugeGroupElement([[1, 0], [0, 1]])
            self.gate.transform(
                elT)  # can't transform a static gate - no params!

    def test_compose(self):
        self.gate.compose(self.gate)

    def test_str(self):
        str(self.gate)

    def test_reduce(self):
        self.gate.__reduce__()
예제 #2
0
class StaticGateTestCase(BaseTestCase):
    def setUp(self):
        super(StaticGateTestCase, self).setUp()
        self.gate = StaticGate([[0, 0], [0, 0]])

    def test_bad(self):
        gate = self.gate.copy()
        with self.assertRaises(ValueError):
            gate.set_matrix([])
        gate.set_matrix([[1, 2], [1, 2]])

        gate.dim = 'adfadsflkj'
        with self.assertRaises(TypeError):
            gate.set_matrix([[1, 2], [1, 2]])
        with self.assertRaises(ValueError):
            gate.set_matrix([[[1, 2]], [], []])

        self.assertEqual(gate.num_params(), 0)
        self.assertArraysAlmostEqual(gate.to_vector(), np.array([], 'd'))
        with self.assertRaises(AssertionError):
            gate.from_vector([1])

    def test_deriv_wrt_params(self):
        self.assertArraysAlmostEqual(self.gate.deriv_wrt_params(),
                                     np.array([]))

    def test_transform(self):
        self.gate.transform([[1, 0], [0, 1]], [[0, 1], [1, 0]])

    def test_compose(self):
        self.gate.compose(self.gate)

    def test_str(self):
        str(self.gate)

    def test_reduce(self):
        self.gate.__reduce__()
예제 #3
0
class StaticGateTestCase(BaseTestCase):

    def setUp(self):
        super(StaticGateTestCase, self).setUp()
        self.gate = StaticGate([[0,0], [0,0]])

    def test_bad(self):
        gate = self.gate.copy()
        with self.assertRaises(ValueError):
            gate.set_matrix([])
        gate.set_matrix([[1, 2],[1, 2]])

        gate.dim = 'adfadsflkj'
        with self.assertRaises(TypeError):
            gate.set_matrix([[1, 2],[1, 2]])
        with self.assertRaises(ValueError):
            gate.set_matrix([[[1, 2]],[], []])

        self.assertEqual(gate.num_params(), 0)
        self.assertArraysAlmostEqual(gate.to_vector(), np.array([], 'd'))
        with self.assertRaises(AssertionError):
            gate.from_vector([1])

    def test_deriv_wrt_params(self):
        self.assertArraysAlmostEqual(self.gate.deriv_wrt_params(), np.array([]))

    def test_transform(self):
        self.gate.transform([[1,0],[0,1]], [[0,1],[1,0]])

    def test_compose(self):
        self.gate.compose(self.gate)

    def test_str(self):
        str(self.gate)

    def test_reduce(self):
        self.gate.__reduce__()
예제 #4
0
 def setUp(self):
     super(StaticGateTestCase, self).setUp()
     self.gate = StaticGate([[0, 0], [0, 0]])
    def test_composed_embedded_param(self):
        #Test out above cell
        gs1Q = std1Q_XYI.gs_target.copy()

        print("START")

        nQubits = 3  # say
        Id_1Q = np.identity(4**1, 'd')
        idleErr0 = LindbladParameterizedGate.from_gate_matrix(
            Id_1Q)  # 1-qubit error generator
        idleErr1 = LindbladParameterizedGate.from_gate_matrix(
            Id_1Q)  # allow different "idle"
        idleErr2 = LindbladParameterizedGate.from_gate_matrix(
            Id_1Q)  # 1Q errors on each qubit
        # so far no gpindices have been set...

        ss3Q = [('Q0', 'Q1', 'Q2')]  #3Q state space
        basis3Q = pygsti.objects.Basis('pp', 2**nQubits)  #3Q basis
        Giii = ComposedGate([
            EmbeddedGate(ss3Q, ('Q0', ), idleErr0),
            EmbeddedGate(ss3Q, ('Q1', ), idleErr1),
            EmbeddedGate(ss3Q, ('Q2', ), idleErr2)
        ])

        targetGx = StaticGate(gs1Q.gates['Gx'])
        Gxii_xErr = LindbladParameterizedGate.from_gate_matrix(Id_1Q)
        Gxii_xGate = ComposedGate([targetGx, idleErr0, Gxii_xErr])
        Gxii = ComposedGate([
            EmbeddedGate(ss3Q, ('Q0', ), Gxii_xGate),
            EmbeddedGate(ss3Q, ('Q1', ), idleErr1),
            EmbeddedGate(ss3Q, ('Q2', ), idleErr2)
        ])

        def printInfo():
            def pp(x):
                return id(x) if (x is not None) else x  #print's parent nicely

            print("INDEX INFO")
            print("idleErr0 (%d):" % id(idleErr0), idleErr0.gpindices,
                  pp(idleErr0.parent), idleErr0.num_params())
            print("idleErr1 (%d):" % id(idleErr1), idleErr1.gpindices,
                  pp(idleErr1.parent), idleErr1.num_params())
            print("idleErr2 (%d):" % id(idleErr2), idleErr2.gpindices,
                  pp(idleErr2.parent), idleErr2.num_params())
            print("Gxii_xErr (%d):" % id(Gxii_xErr), Gxii_xErr.gpindices,
                  pp(Gxii_xErr.parent), Gxii_xErr.num_params())
            print("Gxii_xGate (%d):" % id(Gxii_xGate), Gxii_xGate.gpindices,
                  pp(Gxii_xGate.parent), Gxii_xGate.num_params())
            print("Giii (%d):" % id(Giii), Giii.gpindices, pp(Giii.parent),
                  Giii.num_params())
            print("Gxii (%d):" % id(Gxii), Gxii.gpindices, pp(Gxii.parent),
                  Gxii.num_params())
            print()

        # rubber meets road: how to assign gpindices??
        # need gateset.from_vector() to work, and also to_vector(), but maybe less important
        print("PREGAME")
        printInfo()
        print("BEGIN")
        gs_constructed = pygsti.obj.GateSet()
        print("Gateset id = ", id(gs_constructed))
        print("INSERT1: Giii indices = ", Giii.gpindices, " parent = ",
              Giii.parent)
        gs_constructed.gates[
            'Giii'] = Giii  # will set gpindices of Giii, which will set those of
        # of it's contained EmbeddedGates which will set gpindices of idleErr0, idleErr1, & idleErr2
        # (because they're None to begin with)
        print("POST")
        Giii = gs_constructed.gates['Giii']  #so printInfo works
        printInfo()
        print("INSERT2: Gxii indices = ", Gxii.gpindices, " parent = ",
              Gxii.parent)
        gs_constructed.gates[
            'Gxii'] = Gxii  # similar, but will only set indices of Gxii_xGate (gpindices
        # of idleErr1 and idleErr2 are already set), which will only set indices of Gxii_xErr
        # (since idleErr0.gpindices is already set )
        Giii = gs_constructed.gates['Giii']  #so printInfo works
        Gxii = gs_constructed.gates['Gxii']  #so printInfo works
        printInfo()

        print("TOTAL Params = ", gs_constructed.num_params())

        v = gs_constructed.to_vector()
        print("len(v) =", len(v))
        gs_constructed2 = gs_constructed.copy()
        print("Copy's total params = ", gs_constructed2.num_params())
        gs_constructed2.from_vector(v)
        print("Diff = %g (should be 0)" %
              gs_constructed.frobeniusdist(gs_constructed2))
        self.assertAlmostEqual(gs_constructed.frobeniusdist(gs_constructed2),
                               0)
예제 #6
0
 def setUp(self):
     super(StaticGateTestCase, self).setUp()
     self.gate = StaticGate([[0,0], [0,0]])