qir-alliance/pyqir

QirModule not parsing function attributes

RolandMacDoland opened this issue · 2 comments

If I consider the following QIR program example with function attributes defined:

; ModuleID = 'tag_examples.bc'
source_filename = "RTCircuit"

%Qubit = type opaque
%Result = type opaque

@0 = internal constant [5 x i8] c"0_t0\00"
@1 = internal constant [5 x i8] c"0_t1\00"

declare void @__quantum__qis__y__body(%Qubit*) local_unnamed_addr

declare void @__quantum__qis__z__body(%Qubit*) local_unnamed_addr

declare void @__quantum__qis__x__body(%Qubit*) local_unnamed_addr

declare void @__quantum__qis__h__body(%Qubit*) local_unnamed_addr

declare void @__quantum__qis__reset__body(%Qubit*) local_unnamed_addr

declare void @__quantum__qis__mz__body(%Qubit*, %Result*) local_unnamed_addr

declare void @__quantum__rt__result_record_output(%Result*, i8*) local_unnamed_addr

declare void @__quantum__rt__tuple_start_record_output() local_unnamed_addr

declare void @__quantum__rt__tuple_end_record_output() local_unnamed_addr

define void @Quantinuum__EntangledState() #0 {
entry:
  call void @__quantum__qis__h__body(%Qubit* null)
  call void @__quantum__qis__mz__body(%Qubit* null, %Result* null)
  call void @__quantum__qis__reset__body(%Qubit* null)
  call void @__quantum__qis__mz__body(%Qubit* nonnull inttoptr (i64 1 to %Qubit*), %Result* nonnull inttoptr (i64 1 to %Result*))
  call void @__quantum__qis__reset__body(%Qubit* nonnull inttoptr (i64 1 to %Qubit*))
  call void @__quantum__rt__result_record_output(%Result* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @0, i32 0, i32 0))
  call void @__quantum__rt__result_record_output(%Result* nonnull inttoptr (i64 1 to %Result*), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @1, i32 0, i32 0))
  ret void
}

attributes #0 = { "EntryPoint" "requiredQubits=2" "requiredResults=2" }

and try to retrieve them, the output is None:

Python 3.10.0 (v3.10.0:b494f5935c, Oct  4 2021, 14:59:20) [Clang 12.0.5 (clang-1205.0.22.11)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyqir.parser import QirModule
>>> mod  = QirModule("tag_examples.bc")
>>> dir(mod)
['__annotations__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_entrypoint_funcs', '_functions', '_interop_funcs', 'entrypoint_funcs', 'functions', 'get_func_by_name', 'get_funcs_by_attr', 'get_global_bytes_value', 'interop_funcs', 'module']
>>> ep0 = mod.entrypoint_funcs[0]
>>> dir(ep0)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_blocks', '_parameters', '_return_type', 'blocks', 'func', 'get_attribute_value', 'get_block_by_name', 'get_instruction_by_output_name', 'name', 'parameters', 'required_qubits', 'required_results', 'return_type']
>>> ep0.required_qubits
>>> ep0.required_results
attributes #0 = { "EntryPoint" "requiredQubits=2" "requiredResults=2" }

This attribute group represents:

Key Value
EntryPoint none
requiredQubits=2 none
requiredResults=2 none

But you actually want:

Key Value
EntryPoint none
requiredQubits 2
requiredResults 2

This would correspond to the LLVM:

attributes #0 = { "EntryPoint" "requiredQubits"="2" "requiredResults"="2" }

Ah yes of course, syntactic error from my side. Sorry for being stupid. Thanks.