Example #1
0
def test_undefined_register_from_qubit_arg():
    qasm = """OPENQASM 2.0;
            qreg q[2];
            CX q[0], q2[1];
       """
    parser = QasmParser()

    with pytest.raises(QasmException, match=r"""Undefined.*register.*q2.*"""):
        parser.parse(qasm)
Example #2
0
def test_syntax_error():
    qasm = """OPENQASM 2.0;
         qreg q[2] bla;
         foobar q[0];
    """
    parser = QasmParser()

    with pytest.raises(QasmException, match=r"""Syntax error: 'bla'.*"""):
        parser.parse(qasm)
Example #3
0
def test_unexpected_end_of_file():
    qasm = """OPENQASM 2.0;
              include "qelib1.inc";
              creg
           """
    parser = QasmParser()

    with pytest.raises(QasmException, match="Unexpected end of file"):
        parser.parse(qasm)
Example #4
0
def test_qelib_gate_without_include_statement():
    qasm = """OPENQASM 2.0;
         qreg q[2];
         x q[0];
    """
    parser = QasmParser()

    with pytest.raises(QasmException, match=r"""Unknown gate "x".* line 3.*forget.*\?"""):
        parser.parse(qasm)
Example #5
0
def test_CX_gate_not_enough_args():
    qasm = """OPENQASM 2.0;
     qreg q[2];
     CX q[0];
"""
    parser = QasmParser()

    with pytest.raises(QasmException, match=r"CX.*takes.*got.*1.*line 3"):
        parser.parse(qasm)
Example #6
0
def test_unknown_function():
    qasm = """OPENQASM 2.0;
     qreg q[1];
     U(nonexistent(3), 2 * pi, pi / 3.0) q[0];
"""
    parser = QasmParser()

    with pytest.raises(QasmException, match=r".*not recognized.*'nonexistent'.*line 3"):
        parser.parse(qasm)
Example #7
0
def test_U_gate_too_much_params_error():
    qasm = """OPENQASM 2.0;
     qreg q[2];     
     U(pi, pi, pi, pi) q[1];"""

    parser = QasmParser()

    with pytest.raises(QasmException, match=r"U takes 3.*got.*4.*line 3"):
        parser.parse(qasm)
Example #8
0
def test_CX_gate_mismatched_registers():
    qasm = """OPENQASM 2.0;
     qreg q1[2];
     qreg q2[3];
     CX q1, q2;
"""
    parser = QasmParser()

    with pytest.raises(QasmException, match=r"Non matching.*length \[2 3\].*line 4"):
        parser.parse(qasm)
Example #9
0
def test_u_gate_zero_params_error():
    qasm = """OPENQASM 2.0;
     qreg q[2];     
     U() q[1];"""

    parser = QasmParser()

    with pytest.raises(QasmException,
                       match=r"U takes 3 parameter\(s\).*got.*0.*line 3"):
        parser.parse(qasm)
Example #10
0
def test_measurement_bounds():
    qasm = """OPENQASM 2.0;
     qreg q1[3];
     creg c1[3];                        
     measure q1[0] -> c1[4];  
"""
    parser = QasmParser()

    with pytest.raises(QasmException, match=r"Out of bounds bit.*4.*c1.*size 3.*line 4"):
        parser.parse(qasm)
Example #11
0
def test_CX_gate_arg_overlap():
    qasm = """OPENQASM 2.0;
     qreg q1[2];
     qreg q2[3];
     CX q1[1], q1[1];
"""
    parser = QasmParser()

    with pytest.raises(QasmException, match=r"Overlapping.*at line 4"):
        parser.parse(qasm)
Example #12
0
def test_CX_gate_bounds():
    qasm = """OPENQASM 2.0;
     qreg q1[2];
     qreg q2[3];
     CX q1[4], q2[0];
"""
    parser = QasmParser()

    with pytest.raises(QasmException, match=r"Out of bounds.*4.*q1.*2.*line 4"):
        parser.parse(qasm)
Example #13
0
def test_three_qubit_gates_with_too_much_parameters(qasm_gate: str):
    qasm = f"""OPENQASM 2.0;
     include "qelib1.inc";
     qreg q[3];
     {qasm_gate}(pi) q[0],q[1],q[2];
"""

    parser = QasmParser()

    with pytest.raises(QasmException, match=r""".*{}.*parameter.*line 4.*""".format(qasm_gate)):
        parser.parse(qasm)
Example #14
0
def test_rotation_gates_zero_params_error(qasm_gate: str):
    qasm = f"""OPENQASM 2.0;
     include "qelib1.inc";             
     qreg q[2];     
     {qasm_gate} q[1];     
"""

    parser = QasmParser()

    with pytest.raises(QasmException, match=r".*{}.* takes 1.*got.*0.*line 4".format(qasm_gate)):
        parser.parse(qasm)
Example #15
0
def test_format_header_with_quelibinc_circuit():
    qasm = """OPENQASM 2.0;
include "qelib1.inc";
"""
    parser = QasmParser()

    parsed_qasm = parser.parse(qasm)

    assert parsed_qasm.supportedFormat is True
    assert parsed_qasm.qelib1Include is True
    ct.assert_same_circuits(parsed_qasm.circuit, Circuit())
Example #16
0
def test_U_angles():
    qasm = """
    OPENQASM 2.0;
    qreg q[1];
    U(pi/2,0,pi) q[0];
    """

    c = QasmParser().parse(qasm).circuit
    cirq.testing.assert_allclose_up_to_global_phase(cirq.unitary(c),
                                                    cirq.unitary(cirq.H),
                                                    atol=1e-7)
Example #17
0
def circuit_from_qasm(qasm: str) -> circuits.Circuit:
    """Parses an OpenQASM string to `cirq.Circuit`.

    Args:
        qasm: The OpenQASM string

    Returns:
        The parsed circuit
    """

    return QasmParser().parse(qasm).circuit
Example #18
0
def test_unknown_basic_gate():
    qasm = """OPENQASM 2.0;
         qreg q[2];
         foobar q[0];
    """
    parser = QasmParser()

    with pytest.raises(
            QasmException,
            match=r"""Unknown gate "foobar".* line 3.*forgot.*\?"""):
        parser.parse(qasm)
Example #19
0
def test_standard_single_qubit_gates_wrong_number_of_args(qasm_gate):
    qasm = f"""
     OPENQASM 2.0;
     include "qelib1.inc";             
     qreg q[2];     
     {qasm_gate} q[0], q[1];     
"""

    parser = QasmParser()

    with pytest.raises(QasmException, match=r".* takes 1.*got.*2.*line 5"):
        parser.parse(qasm)
Example #20
0
def test_rotation_gates_wrong_number_of_args(qasm_gate: str):
    qasm = f"""
     OPENQASM 2.0;
     include "qelib1.inc";             
     qreg q[2];     
     {qasm_gate}(pi) q[0], q[1];     
"""

    parser = QasmParser()

    with pytest.raises(QasmException, match=r".*{}.* takes 1.*got.*2.*line 5".format(qasm_gate)):
        parser.parse(qasm)
Example #21
0
def test_measure_mismatched_register_size():
    qasm = """OPENQASM 2.0;
         include "qelib1.inc";       
         qreg q1[2];
         creg c1[3];                        
         measure q1 -> c1;       
    """

    parser = QasmParser()

    with pytest.raises(QasmException, match=r""".*mismatched .* 2 -> 3.*line 5"""):
        parser.parse(qasm)
Example #22
0
def test_measure_undefined_classical_bit():
    qasm = """OPENQASM 2.0;
         include "qelib1.inc";       
         qreg q1[3];    
         creg c1[3];                        
         measure q1[1] -> c2[1];       
    """

    parser = QasmParser()

    with pytest.raises(QasmException, match=r"""Undefined classical register.*c2.*line 5"""):
        parser.parse(qasm)
Example #23
0
def test_measurement_bounds():
    qasm = """OPENQASM 2.0;
     qreg q1[3];
     creg c1[3];                        
     measure q1[0] -> c1[4];  
"""
    parser = QasmParser()

    with pytest.raises(QasmException,
                       match=r"Out of bounds bit index 4"
                       r" on classical register c1 of size 3 at line 4"):
        parser.parse(qasm)
Example #24
0
def test_cx_gate_bounds():
    qasm = """OPENQASM 2.0;
     qreg q1[2];
     qreg q2[3];
     CX q1[4], q2[0];
"""
    parser = QasmParser()

    with pytest.raises(QasmException,
                       match=r"Out of bounds qubit index 4"
                       r" on register q1 of size 2 at line 4"):
        parser.parse(qasm)
Example #25
0
def test_CX_gate_not_enough_args():
    qasm = """OPENQASM 2.0;
     qreg q[2];
     CX q[0];
"""
    parser = QasmParser()

    with pytest.raises(QasmException,
                       match=(r"CX only takes 2 arg\(s\) "
                              r"\(qubits and/or registers\)"
                              r", got: 1, at line 3")):
        parser.parse(qasm)
Example #26
0
def test_measure_to_quantum_register():
    qasm = """OPENQASM 2.0;
         include "qelib1.inc";       
         qreg q1[3];
         qreg q2[3];
         creg c1[3];                        
         measure q2 -> q1;       
    """

    parser = QasmParser()

    with pytest.raises(QasmException, match=r"""Undefined classical register.*q1.*line 6"""):
        parser.parse(qasm)
Example #27
0
def test_measure_from_classical_register():
    qasm = """OPENQASM 2.0;
         include "qelib1.inc";       
         qreg q1[2];
         creg c1[3];                        
         creg c2[3];                        
         measure c1 -> c2;       
    """

    parser = QasmParser()

    with pytest.raises(QasmException, match=r"""Undefined quantum register.*c1.*line 6"""):
        parser.parse(qasm)
Example #28
0
def test_three_qubit_gates_not_enough_args(qasm_gate: str):
    qasm = f"""OPENQASM 2.0;
     include "qelib1.inc";
     qreg q[2];
     {qasm_gate} q[0];
"""

    parser = QasmParser()

    with pytest.raises(
        QasmException, match=r""".*{}.* takes 3 arg\(s\).*got.*1.*line 4""".format(qasm_gate)
    ):
        parser.parse(qasm)
Example #29
0
def test_rotation_gates_too_many_params_error(qasm_gate: str):
    qasm = """OPENQASM 2.0;
     include "qelib1.inc";             
     qreg q[2];     
     {}(pi, 2*pi) q[1];     
""".format(qasm_gate)

    parser = QasmParser()

    with pytest.raises(
            QasmException,
            match=r".*{}.* takes 1.*got.*2.*line 4".format(qasm_gate)):
        parser.parse(qasm)
Example #30
0
def test_multiple_qreg_declaration():
    qasm = """OPENQASM 2.0;
     include "qelib1.inc";
     qreg a_quantum_register [ 1337 ];
     qreg q[42];
"""
    parser = QasmParser()

    parsed_qasm = parser.parse(qasm)

    assert parsed_qasm.supportedFormat
    assert parsed_qasm.qelib1Include
    ct.assert_same_circuits(parsed_qasm.circuit, Circuit())
    assert parsed_qasm.qregs == {'a_quantum_register': 1337, 'q': 42}