Ejemplo n.º 1
0
def test_parse_formula_failures():
    with pytest.raises(TypeError, match='formula must be a string'):
        _ = parse_formula(2)

    with pytest.raises(TypeError, match='formula must be a string'):
        _ = parse_formula([])

    with pytest.raises(ValueError, match='Failed to parse the gate formula'):
        _ = parse_formula('5*__**DSA **)SADD')

    with pytest.raises(ValueError, match='Failed to parse the gate formula'):
        _ = parse_formula('5*x')
Ejemplo n.º 2
0
def test_parse_formula_failures():
    with pytest.raises(TypeError, match='formula must be a string'):
        _ = parse_formula(2)

    with pytest.raises(TypeError, match='formula must be a string'):
        _ = parse_formula([])

    with pytest.raises(ValueError, match='Unrecognized token'):
        _ = parse_formula('5*__**DSA **)SADD')

    with pytest.raises(ValueError, match='Unrecognized token'):
        _ = parse_formula('5*x')
Ejemplo n.º 3
0
def _formula_gate(
    identifier: str, default_formula: str,
    gate_func: Callable[[Union[sympy.Symbol, float]],
                        'cirq.Gate']) -> CellMaker:
    return CellMaker(identifier=identifier,
                     size=gate_func(0).num_qubits(),
                     maker=lambda args: gate_func(
                         parse_formula(default_formula if args.value is None
                                       else args.value)).on(*args.qubits))
Ejemplo n.º 4
0
def test_parse_real_formula():
    t = sympy.Symbol('t')
    assert parse_formula('1/2') == 0.5
    assert parse_formula('t*t + ln(t)') == t * t + sympy.ln(t)
    assert parse_formula('cos(pi*t)') == sympy.cos(sympy.pi * t)
    assert parse_formula('5t') == 5 * t
    np.testing.assert_allclose(parse_formula('cos(pi)'), -1, atol=1e-8)
    assert type(parse_formula('cos(pi)')) is float

    with pytest.raises(ValueError, match='real value'):
        _ = parse_formula('i')
Ejemplo n.º 5
0
def test_parse_complex_expression_failures():
    with pytest.raises(ValueError, match='Incomplete expression'):
        _ = parse_formula('(')
    with pytest.raises(ValueError, match=r"unmatched '\)'"):
        _ = parse_formula(')')
    with pytest.raises(ValueError, match='binary op in bad spot'):
        _ = parse_formula('5+(/)')
    with pytest.raises(ValueError, match='operated on nothing'):
        _ = parse_formula('(5+)')
    with pytest.raises(ValueError, match='operated on nothing'):
        _ = parse_formula('(5/)')
    with pytest.raises(ValueError, match='binary op in bad spot'):
        _ = parse_formula('5-/2')
    with pytest.raises(ValueError, match='binary op in bad spot'):
        _ = parse_formula('/2')

    # Quirk silently ignores partial operations that show up while typing.
    assert parse_formula('2/ ') == 2
    assert parse_formula('2* ') == 2
    assert parse_formula('2+ ') == 2
    assert parse_formula('2- ') == 2
    assert parse_formula('2^ ') == 2