qir-alliance / pyqir

PyQIR is a set of APIs for generating, parsing, and evaluating Quantum Intermediate Representation (QIR).
https://qir-alliance.github.io/pyqir
MIT License
54 stars 24 forks source link

Add functionality to work with (qubit) arrays #264

Closed ghost closed 6 months ago

ghost commented 8 months ago

I've encountered limitations in PyQIR when working with ArrayTypes and StructTypes, which are essential data structures for representing more complex quantum circuits. Specifically:

ArrayType Declaration: It appears impossible to directly declare an ArrayType in PyQIR. Attempting to instantiate the existing ArrayType class results in a "no constructor defined" error. Additionally, defining a new type that inherits from ArrayType is also not feasible. StructType Limitations: Similar issues arise with the StructType.

There does not seem to be a way to group qubits into iterable data structures, which are often necessary for circuit design and algorithms. Our specific use case involves functions that take arrays of qubits as input parameters. This functionality is severely limited without proper array type support.Interacting with QIR-supported runtime functions (e.g. __quantum__rt__qubit_allocate_array) that allocate and release qubit arrays becomes does not seem possible without the ability to declare array types.

Requests:

idavis commented 7 months ago

Hi @fiona-guerin, PyQIR is primarily designed for hardware targeted QIR. It can parse arbitrary QIR produced with LLVM14 and older, but generation is limited as it focuses on profile compliant code generation rather than unrestricted LLVM IR/BC.

As the profiles advance, we will add more features. Current hardware can't allocate dynamic memory and there currently isn't anything planned for user defined types.

The array type you see is for primitive string arrays of global string constants used for output tagging. This is the only part of the API that can create Arrays and it is rust only. They ArrayType in the Python API is only allowed for parsing the structures from existing modules as there is no other profile compliant way to generate arrays than global constant strings (at which point the i8* array is an implementation detail and we provide helper APIs for reading those arrays as strings).