Ejemplo n.º 1
0
def test_examples(example_file):
    """Loop through all example files, verify that the ast_parser can parse the file.

    Examples located at: ``openqasm/examples``.
    """
    with open(example_file) as f:
        source = f.read()
        openqasm3.parse(source)
Ejemplo n.º 2
0
def test_examples(example_file):
    """Loop through all example files, verify that the ast_parser can parse the file.

    The `example_file` fixture is generated in `conftest.py`.  These tests are automatically skipped
    if the examples directly cannot be found.
    """
    with open(example_file) as f:
        source = f.read()
        openqasm3.parse(source)
Ejemplo n.º 3
0
 def test_old_measurement(self):
     old_input = "measure q -> c;"
     output = openqasm3.dumps(openqasm3.parse(old_input),
                              old_measurement=True).strip()
     assert output == old_input
     input_ = "c = measure q;"
     output = openqasm3.dumps(openqasm3.parse(input_),
                              old_measurement=True).strip()
     assert output == old_input
Ejemplo n.º 4
0
    def test_indent_nested(self, indent, outer_start, outer_end,
                           allow_classical):
        classicals = f"""
for uint i in [0:2] {{
{indent}true;
}}
while (i) {{
{indent}i -= 1;
}}
if (i) {{
{indent}x $0;
}} else {{
{indent}x $1;
}}
durationof({{
{indent}x $0;
}});
""".strip()
        quantums = f"""
box {{
{indent}x $0;
}}
""".strip()
        lines = quantums.splitlines()
        if allow_classical:
            lines.extend(classicals.splitlines())
        input_ = outer_start + "\n" + "\n".join(
            indent + line for line in lines) + "\n" + outer_end
        output = openqasm3.dumps(openqasm3.parse(input_),
                                 indent=indent).strip()
        assert output == input_
Ejemplo n.º 5
0
    def test_associativity_concatenation(self):
        """The associativity of concatenation is not fully defined by the grammar or specification,
        but the printer assumes left-associativity for now."""
        input_ = ast.Program(statements=[
            ast.AliasStatement(
                ast.Identifier("q"),
                ast.Concatenation(
                    lhs=ast.Concatenation(
                        lhs=ast.Identifier("a"),
                        rhs=ast.Identifier("b"),
                    ),
                    rhs=ast.Identifier("c"),
                ),
            ),
            ast.AliasStatement(
                ast.Identifier("q"),
                ast.Concatenation(
                    lhs=ast.Identifier("a"),
                    rhs=ast.Concatenation(
                        lhs=ast.Identifier("b"),
                        rhs=ast.Identifier("c"),
                    ),
                ),
            ),
        ], )
        expected = """
let q = a ++ b ++ c;
let q = a ++ (b ++ c);
""".strip()
        output = openqasm3.dumps(input_).strip()
        assert output == expected
        assert openqasm3.parse(output) == input_
Ejemplo n.º 6
0
    def test_measurement(self):
        input_ = """
measure q;
measure $0;
measure q[0];
measure q[1:3];
c = measure q;
c = measure $0;
c = measure q[0];
c = measure q[1:3];
def f() {
  return measure q;
}
def f() {
  return measure $0;
}
def f() {
  return measure q[0];
}
def f() {
  return measure q[1:3];
}
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_),
                                 indent="  ",
                                 old_measurement=False).strip()
        assert output == input_
Ejemplo n.º 7
0
    def test_associativity_binary(self, operator):
        """Test that the associativity of binary expressions is respected in the output."""
        input_ = ast.Program(statements=[
            ast.ExpressionStatement(
                ast.BinaryExpression(
                    lhs=ast.BinaryExpression(
                        lhs=ast.Identifier("a"),
                        op=operator,
                        rhs=ast.Identifier("b"),
                    ),
                    op=operator,
                    rhs=ast.Identifier("c"),
                ), ),
            ast.ExpressionStatement(
                ast.BinaryExpression(
                    lhs=ast.Identifier("a"),
                    op=operator,
                    rhs=ast.BinaryExpression(
                        lhs=ast.Identifier("b"),
                        op=operator,
                        rhs=ast.Identifier("c"),
                    ),
                ), ),
        ], )
        expected = f"""
a {operator.name} b {operator.name} c;
a {operator.name} (b {operator.name} c);
""".strip()
        output = openqasm3.dumps(input_).strip()
        assert output == expected
        assert openqasm3.parse(output) == input_
Ejemplo n.º 8
0
    def test_associativity_power(self):
        """Test that the right-associativity of the power expression is respected in the output."""
        input_ = ast.Program(statements=[
            ast.ExpressionStatement(
                ast.BinaryExpression(
                    lhs=ast.BinaryExpression(
                        lhs=ast.Identifier("a"),
                        op=ast.BinaryOperator["**"],
                        rhs=ast.Identifier("b"),
                    ),
                    op=ast.BinaryOperator["**"],
                    rhs=ast.Identifier("c"),
                ), ),
            ast.ExpressionStatement(
                ast.BinaryExpression(
                    lhs=ast.Identifier("a"),
                    op=ast.BinaryOperator["**"],
                    rhs=ast.BinaryExpression(
                        lhs=ast.Identifier("b"),
                        op=ast.BinaryOperator["**"],
                        rhs=ast.Identifier("c"),
                    ),
                ), ),
        ], )
        expected = f"""
(a ** b) ** c;
a ** b ** c;
""".strip()
        output = openqasm3.dumps(input_).strip()
        assert output == expected
        assert openqasm3.parse(output) == input_
Ejemplo n.º 9
0
    def test_indent(self, indent):
        input_ = f"""
def f(int[32] a) -> bool {{
{indent}return a == a;
}}
gate g(param) q {{
{indent}h q;
}}
for uint i in [0:2] {{
{indent}true;
}}
while (i) {{
{indent}i -= 1;
}}
if (i) {{
{indent}x $0;
}} else {{
{indent}x $1;
}}
box {{
{indent}x $0;
}}
durationof({{
{indent}x $0;
}});
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_),
                                 indent=indent).strip()
        assert output == input_
Ejemplo n.º 10
0
    def test_qubit_declarations(self):
        input_ = """
qubit q;
qubit[5] q;
qubit[SIZE] q;
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_

        old_input = """
qreg q;
qreg q[5];
qreg q[SIZE];
""".strip()
        old_output = openqasm3.dumps(openqasm3.parse(old_input)).strip()
        # Note we're testing that we normalise to the new form.
        assert input_ == old_output
Ejemplo n.º 11
0
    def test_pragma(self):
        input_ = """
pragma blah blah blah
pragma command
pragma !%^* word
""".lstrip()  # The ending newline is important for pragmas.
        output = openqasm3.dumps(openqasm3.parse(input_))
        assert output == input_
Ejemplo n.º 12
0
    def test_barrier(self):
        input_ = """
barrier q;
barrier $0;
barrier q[0];
barrier q[1:3];
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 13
0
    def test_reset(self):
        input_ = """
reset q;
reset $0;
reset q[0];
reset q[1:3];
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 14
0
    def test_alias(self):
        input_ = """
let q = a ++ b;
let q = a[1:2];
let q = a[{0, 2, 3}] ++ a[1:1] ++ a[{4, 5}];
let q = a;
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 15
0
    def test_function_call(self):
        input_ = """
f(1, 2, 3);
f();
f(a, b + c, a * b / c);
f(f(a));
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 16
0
    def test_duration_of(self):
        input_ = """
duration a = durationof({
  x $0;
  ctrl @ x $1, $2;
});
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_), indent="  ").strip()
        assert output == input_
Ejemplo n.º 17
0
    def test_array_initializer(self):
        input_ = """
array[int, 2] a = {1, 2};
array[float[64], 2, 2] a = {{1.0, 0.0}, {0.0, 1.0}};
array[angle[32], 2] a = {pi, pi / 8};
array[uint[16], 4, 4] a = {b, {1, 2, 3, 4}};
array[bool, 2, 2] a = b;
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 18
0
    def test_index_expression(self):
        input_ = """
a[0];
a[{1, 2, 3}];
a[0][0];
a[1:2][0];
a[0][1:2];
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 19
0
    def test_extern_declaration(self):
        input_ = """
extern f();
extern f() -> bool;
extern f(bool);
extern f(int[32], uint[32]);
extern f(mutable array[complex[float[64]], N_ELEMENTS]) -> int[2 * INT_SIZE];
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 20
0
    def test_unary_expression(self):
        input_ = """
!a;
-a;
~(a + a);
-a ** 2;
!true;
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 21
0
    def test_gate_call(self):
        input_ = """
h q;
h q[0];
gphase(pi);
U(1, 2, 3) q;
U(1, 2, 3) q[0];
my_gate a, b[0:2], c;
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 22
0
    def test_io_declarations(self):
        input_ = """
input int a;
input float[64] a;
input complex[float[FLOAT_WIDTH]] a;
output bit b;
output bit[SIZE] b;
output bool b;
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 23
0
    def test_const_declaration(self):
        input_ = """
const bool x = true;
const int x = 2;
const int[32] x = -5;
const uint x = 0;
const uint[16] x = 0;
const angle x = pi;
const angle[SIZE] x = pi / 8;
const float x = 2.0;
const float[SIZE * 2] x = 4.0;
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 24
0
    def test_gate_modifiers(self):
        input_ = """
ctrl @ U(1, 2, 3) a, b;
ctrl(1) @ x a, b[0];
negctrl @ U(1, 2, 3) a[0:2], b;
negctrl(2) @ h a, b, c;
pow(2) @ h a;
ctrl @ gphase(pi / 2) a, b;
inv @ h a;
inv @ ctrl @ x a, b;
ctrl(1) @ inv @ x a, b;
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 25
0
    def test_else(self):
        input_ = """
if (true) {
} else {
  x $0;
}
if (true) {
} else {
  x $0;
  a = b + 2;
}
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_), indent="  ").strip()
        assert output == input_
Ejemplo n.º 26
0
    def test_delay(self):
        input_ = """
delay[50.0ns] q;
delay[50.0ns] $0;
delay[50.0ns] q[0];
delay[50.0ns] q[1:3];
delay[2 * SIZE] q;
delay[2 * SIZE] $0;
delay[2 * SIZE] q[0];
delay[2 * SIZE] q[1:3];
delay[100.0ns];
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 27
0
    def test_box(self):
        input_ = """
box {
  x $0;
}
box[100.0ns] {
  x $0;
}
box[a + b] {
  x $0;
}
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_), indent="  ").strip()
        assert output == input_
Ejemplo n.º 28
0
 def test_example_files(self, parsed_example, indent, chain_else_if,
                        old_measurement):
     """Test that the cycle 'parse - print - parse' does not affect the generated AST of the
     example files.  Printing should just be an exercise in formatting, so should not affect how
     subsequent runs parse the file.  This also functions as something of a general integration
     test, testing much of the basic surface of the language."""
     roundtrip_ast = openqasm3.parse(
         openqasm3.dumps(
             parsed_example.ast,
             indent=indent,
             chain_else_if=chain_else_if,
             old_measurement=old_measurement,
         ))
     assert _remove_spans(roundtrip_ast) == _remove_spans(
         parsed_example.ast)
Ejemplo n.º 29
0
    def test_cast(self):
        input_ = """
int(a);
int[32](2.0);
int[SIZE](bitstring);
uint[16 + 16](a);
bit[SIZE](pi);
bool(i);
complex[float[64]](2.0);
complex[float](2.5);
float[32](1);
float(2.0);
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_)).strip()
        assert output == input_
Ejemplo n.º 30
0
    def test_while_loop(self):
        input_ = """
while (i) {
  x $0;
  i -= 1;
}
while (i == 0) {
  x $0;
  i -= 1;
}
while (!true) {
}
""".strip()
        output = openqasm3.dumps(openqasm3.parse(input_), indent="  ").strip()
        assert output == input_