Skip to content

QIR Base profile Compliant QASM Conversions#228

Merged
TheGupta2012 merged 35 commits intoqBraid:mainfrom
feelerx:Base-Profile
Jun 12, 2025
Merged

QIR Base profile Compliant QASM Conversions#228
TheGupta2012 merged 35 commits intoqBraid:mainfrom
feelerx:Base-Profile

Conversation

@feelerx
Copy link
Copy Markdown
Contributor

@feelerx feelerx commented Jun 9, 2025

Summary of changes

This PR addresses critical compliance gaps between the current qasm3_to_qir implementation and the QIR Base Profile specification. The changes ensure proper measurement state tracking, correct function usage, and adherence to Base Profile restrictions.
Profile Structure
Each profile is defined as a JSON file with the following structure:

{
  "profile_name": "ProfileName",
  "version": "1.0.0",
  "description": "Profile description",
  "capabilities": {
    "feature_name": {
      "enabled": true,
      "description": "Feature description",
      "additional_options": "..."
    }
  },
  "required_functions": {
    "quantum_intrinsics": ["function_list"],
    "runtime_functions": ["function_list"]
  },
  "restrictions": {
    "restriction_category": {
      "setting": "value"
    }
  },
  "validation_rules": {
    "rule_category": {
      "rule_name": "rule_value"
    }
  },
  "compliance_checks": [
    {
      "rule_id": "PROFILE_001",
      "description": "Rule description",
      "severity": "error|warning|info",
      "check_type": "check_category"
    }
  ]
}

Compliance Issues Identified

  1. Missing Qubit Measurement State Tracking (CRITICAL)
    Issue: The Base Profile requires tracking which qubits have been measured to enforce the restriction that qubits cannot be used after measurement without reset.
    Current State: No measurement tracking implemented
    Current implementation lacks:
    self._measured_qubits: dict[int, bool] = {}
    Base Profile Requirement:
  • BASE_004: "Must not use qubits after measurement without reset"
  • qubit_usage.track_measurement_state: true
  • qubit_usage.allow_post_measurement_operations: false
  1. Inconsistent Function Usage Patterns (HIGH)
    Issue: Current implementation doesn't consistently enforce Base Profile function requirements.
    Base Profile Requirements:
  • BASE_001: Uses pyqir._native.mz for measurements
  • BASE_002: Uses pyqir._native.reset for qubit reset operations
  • BASE_003: Uses pyqir._native.if_result for conditional branching
    Current State: Functions are used correctly but without explicit profile validation
  1. Inadequate Post-Measurement Validation (MEDIUM)
    Issue: No validation to prevent quantum operations on measured qubits.
    Base Profile Restriction:
"qubit_reuse": {
  "enabled": false,
  "description": "Does not allow quantum operations on qubits after measurement"
}
  1. Missing Reset State Management (MEDIUM)
    Issue: Reset operations don't properly clear measurement state tracking.
    Expected Behavior: Reset should clear the measured state of qubits to allow future operations.
    Changes Made

  2. Added Measurement State Tracking

# In __init__
if self._profile.should_track_qubit_measurement():
    self._measured_qubits: dict[int, bool] = {}

# In _visit_measurement
if self._profile.should_track_qubit_measurement():
    qubit_id_result = pyqir.qubit_id(src_id)
    if qubit_id_result is not None:
        self._measured_qubits[qubit_id_result] = True
  1. Implemented Qubit Use After Measurement Validation
def _check_qubit_use_after_measurement(self, qubit_ids: List[pyqir.Constant]) -> None:
    """Check qubit use after measurement based on profile capabilities."""
    if not self._profile.allow_qubit_use_after_measurement():
        for qubit_id in qubit_ids:
            qubit_id_result = pyqir.qubit_id(qubit_id)
            if qubit_id_result is not None and self._measured_qubits.get(qubit_id_result, False):
                raise_qasm3_error(
                    f"Base Profile violation: Cannot use qubit {qubit_id_result} after measurement"
                )
  1. Added Reset State Management
# In _visit_reset
if self._profile.should_track_qubit_measurement():
    qubit_id_result = pyqir.qubit_id(qid)
    if qubit_id_result is not None:
        self._measured_qubits[qubit_id_result] = False
  1. Enhanced Profile-Aware Gate Operations
# In _visit_basic_gate_operation
def _visit_basic_gate_operation(self, operation: qasm3_ast.QuantumGate) -> None:
    # ... existing code ...
    op_qubits = self._get_op_bits(operation)
    
    # Profile-aware qubit usage check
    self._check_qubit_use_after_measurement(op_qubits)
    # ... rest of implementation
  1. Updated Profile Classes for Base Profile Compliance
class BaseProfile(Profile):
    def should_track_qubit_measurement(self) -> bool:
        return True  # Base profile requires measurement tracking
    
    def allow_qubit_use_after_measurement(self) -> bool:
        return False  # Base profile restriction

feelerx added 23 commits May 28, 2025 16:48
…r and Profile (only if imported) with qasm3_to_qir funtion. Validation logic also moved to Profile class
… abstracted visitor and module classes so both cirq and qasm can use core.py
@codecov
Copy link
Copy Markdown

codecov bot commented Jun 9, 2025

Codecov Report

Attention: Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
qbraid_qir/qasm3/visitor.py 80.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@TheGupta2012 TheGupta2012 self-requested a review June 9, 2025 13:59
@TheGupta2012
Copy link
Copy Markdown
Member

@feelerx could you please resolve merge conflicts in this PR? I feel this has a lot of overlapping changes from the Adaptive PR

@TheGupta2012
Copy link
Copy Markdown
Member

Also, better to rebase and apply changes on top of current main

@TheGupta2012
Copy link
Copy Markdown
Member

Thanks @feelerx, lgtm! I think most changes were already completed in #225, I'll let @ryanhill1 take one final look before approving.

Copy link
Copy Markdown
Member

@ryanhill1 ryanhill1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@TheGupta2012 TheGupta2012 merged commit 3b7f9fc into qBraid:main Jun 12, 2025
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] QIR Base Profile compliant QASM conversions Add support for generating QIR compliant with Base Profile

3 participants