Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions qbraid_qir/qasm3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@

"""
from .convert import qasm3_to_qir

# from .elements import QasmModule
from .elements import QasmModule
# from .visitor import BasicQisVisitor
10 changes: 5 additions & 5 deletions qbraid_qir/qasm3/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import openqasm3
from pyqir import Context, Module, qir_module

# from qbraid_qir.qasm3.elements import Qasm3Module
from qbraid_qir.qasm3.elements import Qasm3Module
# from qbraid_qir.qasm3.visitor import BasicQisVisitor
from qbraid_qir.exceptions import QirConversionError

Expand All @@ -38,19 +38,19 @@ def qasm3_to_qir(
record_output (bool): Whether to record output calls for registers, default `True`

Returns:
The QIR ``pyqir.Module`` representation of the input Cirq circuit.
The QIR ``pyqir.Module`` representation of the input OpenQASM 3 program.

Raises:
TypeError: If the input is not a valid Cirq circuit.
TypeError: If the input is not a valid OpenQASM 3 program.
QirConversionError: If the conversion fails.
"""
if not isinstance(program, openqasm3.ast.Program):
raise TypeError("Input quantum program must be of type openqasm3.ast.Program.")

# TODO: Implement this function

# llvm_module = qir_module(Context(), name)
# module = Qasm3Module.from_circuit(program, llvm_module)
llvm_module = qir_module(Context(), name)
module = Qasm3Module.from_circuit(program, llvm_module)

# visitor = BasicQisVisitor(**kwargs)
# module.accept(visitor)
Expand Down
94 changes: 94 additions & 0 deletions qbraid_qir/qasm3/elements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from typing import List, Optional
from pyqir import Context, Module

from openqasm3 import parser
import uuid



def generate_module_id(qasm_program):
"""
Generates a QIR module ID from a given openqasm3 program.
"""

#TODO: Consider a better approach of generating a unique identifier.
generated_id = uuid.uuid1()
return f'circuit-{generated_id}'


class Qasm3Module:
"""
A module representing an openqasm3 quantum program using QIR.

Args:
name (str): Name of the module.
module (Module): QIR Module instance.
num_qubits (int): Number of qubits in the circuit.
elements (List[Statement]): List of openqasm3 Statements.
"""

def __init__(
self,
name: str,
module: Module,
num_qubits :int,
elements
):
self._name = name
self._module = module
self._num_qubits = num_qubits
self._elements = elements


@property
def name(self) -> str:
"""Returns the name of the module."""
return self._name

@property
def module(self) -> Module:
"""Returns the QIR Module instance."""
return self._module

@property
def num_qubits(self) -> int:
"""Returns the number of qubits in the circuit."""
return self._num_qubits


@classmethod
def from_program(
cls,
program: openqasm3.ast.Program,
module: Optional[Module] = None
):
"""
Class method. Construct a Qasm3Module from a given openqasm3.ast.Program object
and an optional QIR Module.
"""
elements: List[Statement] = []

# parsing
parsed_program = parser.parse(program)
statements = parsed_program.statements
for statement in statements:
if isinstance(statement, QubitDeclaration):
number_of_qubits = statement.size.value
elements.append(statement)

if module is None:
module = Module(Context(), generate_module_id(parsed_program))

return cls(
name="main",
module=module,
num_qubits=number_of_qubits,
elements=elements,)


def accept(self, visitor):
#TODO: implement this method when QASMVisitor is implemented.

# for elem in self._elements:
# visitor.visit(elem)
pass