def test_par_is_symbolic(self, r):
        """Recognizing symbolic parameters."""
        p = FreeParameter("x")
        q = MeasuredParameter(RegRef(0))

        assert not par_is_symbolic(r)
        assert par_is_symbolic(pf.sin(r))
        assert par_is_symbolic(q)
        assert par_is_symbolic(p)
        assert par_is_symbolic(pf.sin(p))
        assert par_is_symbolic(p + r)
        assert par_is_symbolic(p - r)
        assert par_is_symbolic(p * r)
        assert par_is_symbolic(p / r)
        assert par_is_symbolic(p**r)
        assert par_is_symbolic(p - p)  # no simplification

        # object array with symbols
        a = np.array([[0.1, 3, 0], [0.3, 2, p], [1, 2, 4]])
        assert a.dtype == object
        assert par_is_symbolic(a)

        # object array, no symbols
        a = np.array([[0.1, 3, 0], [0.3, 2, 0], [1, 2, 4]], dtype=object)
        assert a.dtype == object
        assert not par_is_symbolic(a)

        # float array, no symbols
        a = np.array([[0.1, 3, 0], [0.3, 2, 0], [1, 2, 4]])
        assert a.dtype != object
        assert not par_is_symbolic(a)
        assert par_is_symbolic(pf.sin(a))
Esempio n. 2
0
def test_free_parameters(setup_eng, tol):
    """Programs with free parameters."""

    eng, prog = setup_eng(1)
    x = prog.params("x")  # free parameter
    with prog.context as q:
        ops.Dgate(x) | q
        ops.Sgate(-1.2 * x * pf.sin(x**2 - 0.1)) | q

    with pytest.raises(ParameterError, match="Unknown free parameter"):
        eng.run(prog, args={"foo": 1.0})
    with pytest.raises(ParameterError,
                       match="unbound parameter with no default value"):
        eng.run(prog)

    # successful run
    eng.run(prog, args={x: 0.0})
    assert np.all(eng.backend.is_vacuum(tol))
    eng.reset()

    # now set a default value for the free parameter
    x.default = 0.0
    eng.run(prog)
    assert np.all(eng.backend.is_vacuum(tol))
    eng.reset()

    # override the default
    x.default = 1.0
    eng.run(prog, args={x: 0.0})
    assert np.all(eng.backend.is_vacuum(tol))
Esempio n. 3
0
    def test_measured_par_str(self):
        """Test a MeasuredParameter with some transformations converts properly"""
        prog = Program(2)
        with prog.context as q:
            ops.Sgate(0.43) | q[0]
            ops.MeasureX | q[0]
            ops.Zgate(2 * pf.sin(q[0].par)) | q[1]

        bb = io.to_blackbird(prog)
        expected = {"op": "Zgate", "modes": [1], "args": ["2*sin(q0)"], "kwargs": {}}
        assert bb.operations[-1] == expected
    def test_measured_par_str(self):
        """Test a MeasuredParameter with some transformations converts properly"""
        prog = Program(2)
        with prog.context as q:
            ops.Sgate(0.43) | q[0]
            ops.MeasureX | q[0]
            ops.Zgate(2 * pf.sin(q[0].par)) | q[1]

        bb = io.to_blackbird(prog)
        assert bb.operations[-1]["op"] == "Zgate"
        assert bb.operations[-1]["modes"] == [1]

        assert isinstance(bb.operations[-1]["args"][0],
                          blackbird.RegRefTransform)
        assert bb.operations[-1]["args"][0].func_str == "2*sin(q0)"
        assert bb.operations[-1]["args"][0].regrefs == [0]

        assert bb.operations[-1]["kwargs"] == {}