Skip to content

memq_dqc.preprocessing.qasm

qasm

OpenQASM 3 preprocessing.

The public surface here is the set of entry points for getting an OpenQASM 3 program in and out of memq_dqc, plus CircuitQubit, the qubit reference those programs are expressed in terms of.

The statement-cleaning helpers and the Cleaned* node types are implementation details of the circuit builders. They remain importable from their defining modules (memq_dqc.preprocessing.qasm.analysis, .ast_utils, .cleaning, .types) but are not part of the supported API and may change without notice.

CircuitQubit dataclass

CircuitQubit(register_name: str, index: int)

Represents a circuit-level qubit with register name and index.

dump_qasm_program

dump_qasm_program(program: Program) -> str

Serialize an OpenQASM 3 program to source text.

Parameters:

Name Type Description Default
program Program

Program to serialize.

required

Returns:

Type Description
str

OpenQASM 3 source text.

Source code in src/memq_dqc/preprocessing/qasm/io.py
def dump_qasm_program(program: ast.Program) -> str:
    """Serialize an OpenQASM 3 program to source text.

    Args:
        program: Program to serialize.

    Returns:
        OpenQASM 3 source text.
    """
    return openqasm3.dumps(program)

load_qasm_program

load_qasm_program(filename: str) -> Program

Load an OpenQASM 3 program from a file.

Parameters:

Name Type Description Default
filename str

Path to the OpenQASM 3 file.

required

Returns:

Type Description
Program

The parsed OpenQASM 3 program.

Source code in src/memq_dqc/preprocessing/qasm/io.py
def load_qasm_program(filename: str) -> ast.Program:
    """Load an OpenQASM 3 program from a file.

    Args:
        filename: Path to the OpenQASM 3 file.

    Returns:
        The parsed OpenQASM 3 program.
    """
    qasm_path = Path(filename)
    if not qasm_path.is_file():
        raise FileNotFoundError(f"File not found: {filename}")
    program = parse_qasm_source(qasm_path.read_text(encoding="utf-8"))
    num_qubit_registers = _count_qubit_declarations(program)
    if num_qubit_registers > 1:
        raise NotImplementedError(
            "Multiple qubit registers are not supported yet."
        )
    # TODO: is there a way to verify valid program here? (Recall pyqasm has this)
    return program

parse_qasm_file

parse_qasm_file(filename: str) -> Program

Parse an OpenQASM 3 file without additional structural constraints.

Parameters:

Name Type Description Default
filename str

Path to the OpenQASM 3 file.

required

Returns:

Type Description
Program

Parsed OpenQASM 3 program.

Raises:

Type Description
FileNotFoundError

If filename does not exist.

Source code in src/memq_dqc/preprocessing/qasm/io.py
def parse_qasm_file(filename: str) -> ast.Program:
    """Parse an OpenQASM 3 file without additional structural constraints.

    Args:
        filename: Path to the OpenQASM 3 file.

    Returns:
        Parsed OpenQASM 3 program.

    Raises:
        FileNotFoundError: If ``filename`` does not exist.
    """
    qasm_path = Path(filename)
    if not qasm_path.is_file():
        raise FileNotFoundError(f"File not found: {filename}")
    return parse_qasm_source(qasm_path.read_text(encoding="utf-8"))

parse_qasm_source

parse_qasm_source(qasm_source: str) -> Program

Parse OpenQASM 3 source text into an AST program.

Parameters:

Name Type Description Default
qasm_source str

OpenQASM 3 source text.

required

Returns:

Type Description
Program

Parsed OpenQASM 3 program.

Source code in src/memq_dqc/preprocessing/qasm/io.py
def parse_qasm_source(qasm_source: str) -> ast.Program:
    """Parse OpenQASM 3 source text into an AST program.

    Args:
        qasm_source: OpenQASM 3 source text.

    Returns:
        Parsed OpenQASM 3 program.
    """
    return openqasm3.parser.parse(qasm_source)