예제 #1
0
    def test_free_par_str(self):
        """Test a FreeParameter with some transformations converts properly"""
        prog = Program(2)
        r, alpha = prog.params('r', 'alpha')
        with prog.context as q:
            ops.Sgate(r) | q[0]
            ops.Zgate(3 * pf.log(-alpha)) | q[1]

        bb = io.to_blackbird(prog)
        assert bb.operations[0] == {"op": "Sgate", "modes": [0], "args": ['{r}', 0.0], "kwargs": {}}
        assert bb.operations[1] == {"op": "Zgate", "modes": [1], "args": ['3*log(-{alpha})'], "kwargs": {}}
예제 #2
0
    def test_free_par_str(self):
        """Test a FreeParameter with some transformations converts properly"""
        sf_prog = Program(2)
        r, alpha = sf_prog.params("r", "alpha")
        with sf_prog.context as q:
            ops.Sgate(r) | q[0]
            ops.Zgate(3 * pf.log(-alpha)) | q[1]

        xir_prog = io.to_xir(sf_prog)

        expected = [("Sgate", ["r", 0.0], (0,)), ("Zgate", ["3*log(-alpha)"], (1,))]
        assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected
예제 #3
0
def test_tf_batch_in_gates_previously_supporting_complex(gate):
    """Test if gates that previously accepted complex arguments support the input of TF tensors in
    batch form"""
    tf = pytest.importorskip("tensorflow")

    batch_size = 2
    prog = Program(1)
    eng = Engine(backend="tf",
                 backend_options={
                     "cutoff_dim": 3,
                     "batch_size": batch_size
                 })

    theta = prog.params("theta")
    _theta = tf.Variable([0.1] * batch_size)

    with prog.context as q:
        gate(theta) | q[0]

    eng.run(prog, args={"theta": _theta})
예제 #4
0
def test_tf_batch_complex_raise(gate):
    """Test if an error is raised if complex TF tensors with a batch dimension are input for gates
    that previously accepted complex arguments"""
    tf = pytest.importorskip("tensorflow")

    batch_size = 2
    prog = Program(1)
    eng = Engine(backend="tf",
                 backend_options={
                     "cutoff_dim": 3,
                     "batch_size": batch_size
                 })

    theta = prog.params("theta")
    _theta = tf.Variable([0.1j] * batch_size)

    with prog.context as q:
        gate(theta) | q[0]

    with pytest.raises(ValueError, match="cannot be complex"):
        eng.run(prog, args={"theta": _theta})