コード例 #1
0
    def test_call_function(self, prog):
        """Test _call_function method for operation class"""
        rval = 4.32
        thetaval = -0.654
        phival = 0.543

        def dummy_func(r, theta, phi, q):
            Sgate(r) | q[0]
            BSgate(theta, phi) | (q[0], q[1])

        op = utils.operation(ns=1)
        op(dummy_func)(rval, thetaval, phival)

        with prog.context as q:
            res = op._call_function(q)

        # check register is returned
        assert res == q

        # check eng queue matches the operation
        assert len(prog) == 2

        # check first queue op is Sgate(rval)
        assert isinstance(prog.circuit[0].op, Sgate)
        assert prog.circuit[0].reg == [q[0]]
        assert prog.circuit[0].op.p[0].x == rval

        # check second queue op is BSgate(thetaval, phival)
        assert isinstance(prog.circuit[1].op, BSgate)
        assert prog.circuit[1].reg == list(q)
        assert prog.circuit[1].op.p[0].x == thetaval
        assert prog.circuit[1].op.p[1].x == phival
コード例 #2
0
    def test_applying_decorator(self):
        """Test the __call__ method of the operation class"""
        def f(x):
            return x**2

        op = utils.operation(ns=1)
        op(f)(0, 6, 3)

        assert op.func == f
        assert op.args == (0, 6, 3)
コード例 #3
0
    def test_incorrect_arg_num_call_function(self, rr):
        """Test exception raised if the wrapped function is called with wrong number of args"""
        def dummy_func(r, q):
            Sgate(r) | q[0]
            BSgate() | (q[0], q[1])

        op = utils.operation(ns=1)
        op(dummy_func)(1, 2)

        with pytest.raises(ValueError,
                           match="Mismatch in the number of arguments"):
            op._call_function(rr)
コード例 #4
0
    def test_no_arg_call_function(self, rr):
        """Test exception raised if the wrapped function doesn't accept a register"""
        def dummy_func():
            Sgate(r) | q[0]
            BSgate() | (q[0], q[1])

        op = utils.operation(ns=1)
        op(dummy_func)()

        with pytest.raises(ValueError,
                           match="must receive the qumode register"):
            op._call_function(rr)