def test_use_template(self): """Test templates can be initialized""" bb = BlackbirdProgram(name="prog", version=0.0) x = sym.Symbol('x') hi = sym.Symbol('hi') bb._parameters = [str(x), str(hi)] bb._operations.append({ "op": "Dgate", "modes": [0], "args": [0.54 / x**2], "kwargs": { 'test': hi**4 } }) bb._operations.append({ "op": "Sgate", "modes": [0], "args": [0.543], "kwargs": {} }) bb._operations.append({"op": "Sgate", "modes": [0]}) assert bb.parameters == {'x', 'hi'} assert bb.is_template() bb2 = bb(x=5, hi=2) assert not bb2.parameters assert not bb2.is_template() assert bb2.operations[0]["args"] == [0.54 / 5**2] assert bb2.operations[0]["kwargs"] == {'test': 2**4} assert bb.operations[1] == bb2.operations[1]
def test_invalid_template_call(self): """Test templates raise exception if parameter values not passed""" bb = BlackbirdProgram(name="prog", version=0.0) x = sym.Symbol('x') hi = sym.Symbol('hi') bb._parameters = [x, hi] bb._operations.append({"op": "Dgate", "modes": [0], "args": [0.54/x**2], "kwargs": {'test': hi**4}}) with pytest.raises(ValueError, match="Invalid value for free parameter provided"): bb(hi=4) with pytest.raises(ValueError, match="Invalid value for free parameter provided"): bb(x=4)
def test_invalid_template_call_variables(self): """Test templates raise exception if parameter variable values not passed""" bb = BlackbirdProgram(name="prog", version=0.0) y = sym.Symbol('y') bye = sym.Symbol('bye') bb._parameters = [y, bye] bb._var.update({"y": y, "bye": np.array([[1, 2, bye]])}) with pytest.raises(ValueError, match="Invalid value for free parameter provided"): bb(bye=2) with pytest.raises(ValueError, match="Invalid value for free parameter provided"): bb(y=2)