-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ae9480
commit c35b860
Showing
4 changed files
with
158 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Copyright (C) 2024 qBraid | ||
# | ||
# This file is part of the qBraid-SDK | ||
# | ||
# The qBraid-SDK is free software released under the GNU General Public License v3 | ||
# or later. You can redistribute and/or modify it under the terms of the GPL v3. | ||
# See the LICENSE file in the project root or <https://www.gnu.org/licenses/gpl-3.0.html>. | ||
# | ||
# THERE IS NO WARRANTY for the qBraid-SDK, as per Section 15 of the GPL v3. | ||
|
||
""" | ||
Module containing OpenQASM semantic checker function | ||
""" | ||
from typing import Optional, Union | ||
|
||
import openqasm3 | ||
from pyqir import Context, qir_module | ||
|
||
from .elements import Qasm3Module, generate_module_id | ||
from .exceptions import Qasm3ConversionError | ||
from .visitor import BasicQasmVisitor | ||
|
||
|
||
def semantic_check( | ||
program: Union[openqasm3.ast.Program, str], | ||
name: Optional[str] = None, | ||
**kwargs, | ||
) -> tuple[bool, Optional[Exception]]: | ||
"""Validates a given OpenQASM 3 program for semantic correctness. | ||
Args: | ||
program (openqasm3.ast.Program or str): The OpenQASM 3 program to convert. | ||
name (str, optional): Identifier for created QIR module. Auto-generated if not provided. | ||
Keyword Args: | ||
initialize_runtime (bool): Whether to perform quantum runtime environment initialization, | ||
default `True`. | ||
record_output (bool): Whether to record output calls for registers, default `True` | ||
Returns: | ||
bool : True if the program is semantically correct, False otherwise. | ||
Raises: | ||
Exception: If the input is not a valid OpenQASM 3 program. | ||
""" | ||
if isinstance(program, str): | ||
program = openqasm3.parse(program) | ||
|
||
elif not isinstance(program, openqasm3.ast.Program): | ||
raise TypeError("Input quantum program must be of type openqasm3.ast.Program or str.") | ||
|
||
if name is None: | ||
name = generate_module_id() | ||
|
||
llvm_module = qir_module(Context(), name) | ||
module = Qasm3Module.from_program(program, llvm_module) | ||
|
||
try: | ||
visitor = BasicQasmVisitor(check_only=True, **kwargs) | ||
module.accept(visitor) | ||
except (Qasm3ConversionError, TypeError, ValueError) as err: | ||
return (False, err) | ||
|
||
return (True, None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright (C) 2023 qBraid | ||
# | ||
# This file is part of the qBraid-SDK | ||
# | ||
# The qBraid-SDK is free software released under the GNU General Public License v3 | ||
# or later. You can redistribute and/or modify it under the terms of the GPL v3. | ||
# See the LICENSE file in the project root or <https://www.gnu.org/licenses/gpl-3.0.html>. | ||
# | ||
# THERE IS NO WARRANTY for the qBraid-SDK, as per Section 15 of the GPL v3. | ||
|
||
""" | ||
Tests the checker module of qasm3 | ||
""" | ||
|
||
import pytest | ||
|
||
from qbraid_qir.qasm3.checker import semantic_check | ||
from qbraid_qir.qasm3.exceptions import Qasm3ConversionError | ||
|
||
|
||
def test_correct_check(): | ||
check, err = semantic_check("OPENQASM 3; include 'stdgates.inc'; qubit q;") | ||
assert check | ||
assert err is None | ||
|
||
|
||
def test_incorrect_check(): | ||
check, err = semantic_check( | ||
""" | ||
//semantically incorrect program | ||
OPENQASM 3; | ||
include 'stdgates.inc'; | ||
qubit q; | ||
rx(3.14) q[2]; | ||
""", | ||
name="test", | ||
) | ||
assert not check | ||
assert err is not None | ||
assert isinstance(err, Qasm3ConversionError) | ||
|
||
|
||
def test_incorrect_program_type(): | ||
with pytest.raises( | ||
TypeError, match="Input quantum program must be of type openqasm3.ast.Program or str." | ||
): | ||
check, err = semantic_check(1234) | ||
assert not check | ||
assert err is not None | ||
assert isinstance(err, TypeError) |