Source code for qbraid.passes.qasm.compat

# Copyright 2025 qBraid
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Module for providing transforamtions to ensure OpenQASM 3 compatibility
across various other quantum software frameworks.

"""
import math
import re
from functools import reduce

from openqasm3 import dumps, parse
from openqasm3.ast import (
    AssignmentOperator,
    BranchingStatement,
    ClassicalAssignment,
    ClassicalDeclaration,
    Identifier,
    IndexedIdentifier,
    Program,
    QuantumGate,
    QuantumMeasurement,
    QuantumMeasurementStatement,
    Statement,
    SwitchStatement,
)
from openqasm3.parser import QASM3ParsingError
from openqasm3.visitor import QASMVisitor

from qbraid._logging import logger

GATE_DEFINITIONS = {
    "iswap": """
gate iswap _gate_q_0, _gate_q_1 {
  s _gate_q_0;
  s _gate_q_1;
  h _gate_q_0;
  cx _gate_q_0, _gate_q_1;
  cx _gate_q_1, _gate_q_0;
  h _gate_q_1;
}""",
    "sxdg": """
gate sxdg _gate_q_0 {
  s _gate_q_0;
  h _gate_q_0;
  s _gate_q_0;
}""",
    "cv": """
gate cv _gate_q_0, _gate_q_1 {
  ctrl @ sx _gate_q_0, _gate_q_1;
}""",
}


def _insert_gate_def(qasm3_str: str, defn: str) -> str:
    """Add single gate definition to an OpenQASM3 string."""

    lines = qasm3_str.splitlines()

    # Note: In future move gate definition after include statement.
    insert_index = 0
    for i, line in enumerate(lines):
        if "include" in line or "OPENQASM" in line:
            insert_index = i + 1
            break

    lines.insert(insert_index, defn.strip())

    return "\n".join(lines)


[docs] def insert_gate_def(qasm3_str: str, gate_name: str | list[str], force_insert: bool = False) -> str: """Add gate definitions to an OpenQASM3 string. Args: qasm3_str (str): QASM 3.0 string. gate_name (str | list[str]): Name(s) of the gate(s) to insert. force_insert (bool): If True, the gate definition will be added even if the gate is never referenced. Defaults to False. Returns: str: QASM 3.0 string with gate definition. Raises: ValueError: If the gate definition is not found. """ # Normalize gate_name to a list gate_names = [gate_name] if isinstance(gate_name, str) else gate_name # Check for missing definitions missing = [name for name in gate_names if name not in GATE_DEFINITIONS] if missing: raise ValueError( f"Gate definitions not found for: {missing}. " f"Available gate definitions include: {set(GATE_DEFINITIONS.keys())}" ) # Determine which gates to actually add gates_to_add = [] if force_insert: gates_to_add = gate_names else: # Only add if gate name appears in QASM string for name in gate_names: if name in qasm3_str: gates_to_add.append(name) # If no gates referenced and force_insert is False, return original if not gates_to_add: return qasm3_str # Insert definitions for name in gates_to_add: defn = GATE_DEFINITIONS[name] qasm3_str = _insert_gate_def(qasm3_str, defn) return qasm3_str
def _normalize_case_insensitive_map(gate_mappings: dict[str, str]) -> dict[str, str]: """Normalize gate_mappings keys to lowercase and check for duplicates.""" lowercased_map = {} for key, value in gate_mappings.items(): lower_key = key.lower() if lower_key in lowercased_map: raise ValueError( f"Duplicate key detected after lowercasing: '{lower_key}'. Use case_sensitive=True." ) lowercased_map[lower_key] = value return lowercased_map def _replace_gate_in_statement( statement: Statement, gate_mappings: dict[str, str], case_sensitive: bool ) -> Statement: """Replace gate names in a single statement if applicable.""" if isinstance(statement, QuantumGate): gate_name = statement.name.name lookup_name = gate_name if case_sensitive else gate_name.lower() if lookup_name in gate_mappings: statement.name.name = gate_mappings[lookup_name] elif isinstance(statement, BranchingStatement): statement.if_block = [ _replace_gate_in_statement(s, gate_mappings, case_sensitive) for s in statement.if_block ] if statement.else_block: statement.else_block = [ _replace_gate_in_statement(s, gate_mappings, case_sensitive) for s in statement.else_block ] return statement def _replace_gate_names( program: Program, gate_mappings: dict[str, str], case_sensitive: bool ) -> Program: """Replace occurrences of specified gate names in a openqasm3.ast.Program.""" if not case_sensitive: gate_mappings = _normalize_case_insensitive_map(gate_mappings) statements = [ _replace_gate_in_statement(stmnt, gate_mappings, case_sensitive) for stmnt in program.statements ] return Program(statements=statements, version=program.version)
[docs] def replace_gate_names( qasm: str, gate_mappings: dict[str, str], case_sensitive: bool = False ) -> str: """Replace occurrences of specified gate names in a QASM program string. Args: qasm (str): The QASM program as a string. gate_mappings (dict[str, str]): A dictionary mapping old gate names (keys) to new gate names (values). case_sensitive (bool): Whether the gate name replacement should be case-sensitive. Defaults to False. Returns: str: The modified QASM program with the gate names replaced. Raises: ValueError: If duplicate keys are found in the gate_mappings after lowercasing. """ program = parse(qasm) program_out = _replace_gate_names(program, gate_mappings, case_sensitive) version_major = program_out.version.split(".")[0] qasm_out = dumps(program_out) if int(version_major) == 2: qasm_out = declarations_to_qasm2(qasm_out) return qasm_out
def normalize_if_blocks(qasm: str) -> str: """Convert QASM 3-style if blocks to QASM 2 single-line if syntax. Transforms patterns like:: if (c0[0] == true) { z q[2]; } into:: if(c0[0]==1) z q[2]; This is needed because pyqasm's ``dumps()`` emits QASM 3-style if blocks, but Cirq's QASM parser only understands QASM 2 single-line ``if()`` syntax. A single-bit condition must stay single-bit: ``if(c0==1)`` would also require every other bit of ``c0`` to be 0. Args: qasm: QASM program string potentially containing QASM 3-style if blocks. Returns: QASM string with if blocks normalized to QASM 2 single-line syntax. Raises: ValueError: If a single-bit condition compares against a value other than 0 or 1. """ def _replace_if_block(match: re.Match) -> str: reg = match.group(1) idx = match.group(2) val_str = match.group(3) statements = [s.strip() for s in match.group(4).split(";") if s.strip()] val = 1 if val_str == "true" else (0 if val_str == "false" else int(val_str)) if idx is not None and val not in (0, 1): # Cirq's parser keeps only the low bit of the value, so ``c[1]==2`` would # silently become ``c[1]==0``. raise ValueError(f"Cannot compare the single bit {reg}[{idx}] to {val_str}.") cond = reg if idx is None else f"{reg}[{idx}]" # QASM 2 conditions a single statement, so repeat the condition on each one. Exact # because Cirq accepts only gates there, and gates cannot change the condition bit. return "\n".join(f"if({cond}=={val}) {stmt};" for stmt in statements) pattern = re.compile( r"if\s*\(\s*(\w+)(?:\[(\d+)\])?\s*==\s*(\w+)\s*\)\s*\{\s*\n\s*(.+?)\n\s*\}", re.DOTALL, ) return pattern.sub(_replace_if_block, qasm) class _BranchFinder(QASMVisitor): """Records whether a program contains an ``if`` or ``switch`` statement.""" def __init__(self): self.found = False # pylint: disable-next=invalid-name,unused-argument def visit_BranchingStatement(self, node: BranchingStatement, context=None) -> None: """Record that the program contains an ``if`` statement.""" self.found = True # pylint: disable-next=invalid-name,unused-argument def visit_SwitchStatement(self, node: SwitchStatement, context=None) -> None: """Record that the program contains a ``switch`` statement.""" self.found = True
[docs] def contains_feedforward(program: Program) -> bool: """Check whether an unrolled OpenQASM program still contains an ``if`` or ``switch``. Pass ``QasmModule.unrolled_ast``, not the original program: unrolling resolves ``if`` and ``switch`` statements on compile-time constants. It keeps any ``if`` on a ``bit``, though, even one never measured; use :func:`branches_on_measurement` on the unrolled program to tell those apart. """ finder = _BranchFinder() finder.visit(program) return finder.found
class _MeasuredBranchFinder(QASMVisitor): """Records, in program order, measured variables and the ones branches read.""" def __init__(self): self.measured: set[str] = set() self.read: set[str] = set() # pylint: disable-next=invalid-name def visit_QuantumMeasurementStatement( self, node: QuantumMeasurementStatement, context=None # pylint: disable=unused-argument ) -> None: """Record the bit a measurement is assigned to.""" if node.target is not None: self.measured.add(_base_name(node.target)) # pylint: disable-next=invalid-name def visit_ClassicalDeclaration(self, node: ClassicalDeclaration, context=None) -> None: """Record a variable initialised from a measurement or a measured variable.""" init = node.init_expression if isinstance(init, QuantumMeasurement) or ( init is not None and _names(init) & self.measured ): self.measured.add(node.identifier.name) self.generic_visit(node, context) # pylint: disable-next=invalid-name def visit_ClassicalAssignment(self, node: ClassicalAssignment, context=None) -> None: """Track a variable assigned from a measured variable, or overwritten without one.""" if _names(node.rvalue) & self.measured: self.measured.add(_base_name(node.lvalue)) elif node.op == AssignmentOperator["="] and isinstance(node.lvalue, Identifier): self.measured.discard(node.lvalue.name) self.generic_visit(node, context) # pylint: disable-next=invalid-name def visit_BranchingStatement(self, node: BranchingStatement, context=None) -> None: """Record the measured names the condition reads.""" self.read |= _names(node.condition) & self.measured self.generic_visit(node, context) # pylint: disable-next=invalid-name def visit_SwitchStatement(self, node: SwitchStatement, context=None) -> None: """Record the measured names the switch target reads.""" self.read |= _names(node.target) & self.measured self.generic_visit(node, context) class _IdentifierCollector(QASMVisitor): """Collects the identifier names in an expression.""" def __init__(self): self.names: set[str] = set() # pylint: disable-next=invalid-name,unused-argument def visit_Identifier(self, node: Identifier, context=None) -> None: """Record the identifier's name.""" self.names.add(node.name) def _base_name(target: Identifier | IndexedIdentifier) -> str: """Return the variable an assignment target writes, without its index.""" return (target.name if isinstance(target, IndexedIdentifier) else target).name def _names(node) -> set[str]: """Return the identifier names an AST node reads.""" collector = _IdentifierCollector() collector.visit(node) return collector.names
[docs] def branches_on_measurement(program: Program) -> bool: """Check whether an ``if`` or ``switch`` reads a measurement result. For programs pyqasm can't unroll, where :func:`contains_feedforward` doesn't apply. Follows copies into other variables in program order, but not through subroutines. """ finder = _MeasuredBranchFinder() finder.visit(program) return bool(finder.read)
[docs] def add_stdgates_include(qasm_str: str) -> str: """Add 'include "stdgates.inc";' to the QASM string if it is missing.""" if 'include "stdgates.inc";' in qasm_str: return qasm_str lines = qasm_str.splitlines() for i, line in enumerate(lines): if "OPENQASM" in line: lines.insert(i + 1, 'include "stdgates.inc";') break return "\n".join(lines)
[docs] def remove_stdgates_include(qasm: str) -> str: """Remove 'include "stdgates.inc";' from the QASM string.""" return qasm.replace('include "stdgates.inc";', "")
def _evaluate_expression(match): """Helper function for simplifying arithmetic expressions within parentheses.""" expr = match.group(1) try: simplified_value = eval(expr) # pylint: disable=eval-used return f"({simplified_value})" except SyntaxError: return match.group(0) def simplify_arithmetic_expressions(qasm_str: str) -> str: """Simplifies arithmetic expressions within parentheses in a QASM string.""" pattern = r"\(([0-9+\-*/. ]+)\)" return re.sub(pattern, _evaluate_expression, qasm_str)
[docs] def convert_qasm_pi_to_decimal(qasm: str) -> str: """Convert all instances of 'pi' in the QASM string to their decimal value.""" pattern = r"(?<![a-zA-Z])(\d*\.?\d*\s*[*/+-]\s*)?pi(\s*[*/+-]\s*\d*\.?\d*)?(?![a-zA-Z])" gate_defs = set() try: program = parse(qasm) for statement in program.statements: if isinstance(statement, QuantumGate): name = statement.name.name if "pi" in name: gate_defs.add(name) except QASM3ParsingError as err: logger.debug("Failed to parse QASM program for pi conversion: %s", err) def replace_with_decimal(match: re.Match) -> str: expr: str = match.group() start = match.start() end = match.end() for gate_def in gate_defs: if gate_def in qasm[max(0, start - len(gate_def)) : end]: return expr # pragma: no cover expr_with_pi_as_decimal = expr.replace("pi", str(math.pi)) value = eval(expr_with_pi_as_decimal) # pylint: disable=eval-used return str(value) return re.sub(pattern, replace_with_decimal, qasm)
def has_redundant_parentheses(qasm_str: str) -> bool: """Checks if a QASM string contains gate parameters with redundant parentheses.""" pattern = r"\w+\(\(\s*[-+]?\d+(\.\d*)?\s*\)\)" if re.search(pattern, qasm_str): return True pattern_neg = r"\w+\(-\(\d*\.?\d+\)\)" if re.search(pattern_neg, qasm_str): return True return False def simplify_parentheses_in_qasm(qasm_str: str) -> str: """Simplifies unnecessary parentheses around numbers in QASM strings.""" lines = qasm_str.splitlines() simplified_lines = [] pattern = r"\(\s*([-+]?\s*\d+(\.\d*)?)\s*\)" def simplify(match): return match.group(1).replace(" ", "") for line in lines: if has_redundant_parentheses(line): line = re.sub(pattern, simplify, line) simplified_lines.append(line) return "\n".join(simplified_lines) def compose(*functions): """Compose multiple functions left to right.""" def compose_two(f, g): return lambda x: g(f(x)) return reduce(compose_two, functions, lambda x: x)
[docs] def normalize_qasm_gate_params(qasm: str) -> str: """Normalize the parameters of the gates in the QASM string using function composition.""" transform_qasm = compose( convert_qasm_pi_to_decimal, simplify_arithmetic_expressions, simplify_parentheses_in_qasm ) return transform_qasm(qasm)
def declarations_to_qasm2(qasm: str) -> str: """Converts QASM 3.0 qubit and bit declarations to QASM 2.0 qreg and creg declarations.""" for declaration_type, replacement_type in [("qubit", "qreg"), ("bit", "creg")]: pattern = rf"{declaration_type}\[(\d+)\]\s+(\w+);" replacement = rf"{replacement_type} \2[\1];" qasm = re.sub(pattern, replacement, qasm) return qasm def remove_empty_registers(qasm: str) -> str: """Remove zero-length register declarations (e.g. ``creg c[0];``) from a QASM string.""" return re.sub(r"^\s*(qreg|creg)\s+\w+\s*\[\s*0\s*\]\s*;\s*\n?", "", qasm, flags=re.MULTILINE)