Actively maintained. Last release 4 weeks ago, commits in 10 of the last 26 weeks. A safe choice.
pip install pyqirPyQIR
The pyqir package provides the ability to generate
QIR as well as an easy way to
parse and analyze QIR.
QIR generation is intended to be used by code automating translation processes enabling the conversion in some format to QIR via Python; i.e., this is a low-level API intended to be used as a bridge to existing Python frameworks enabling the generation of QIR rather than directly consumed by an end-user. It is not intended to be used as a framework for algorithm and application development.
Installation
The package is released on PyPI and can be installed via pip:
pip install pyqir
Examples
PyQIR examples can be found in this repository's examples folder.
Generation
The following code creates QIR for a Bell pair before measuring each qubit and returning the result. The unoptimized QIR is displayed in the terminal when executed:
from pyqir import BasicQisBuilder, SimpleModule
module = SimpleModule("Bell", num_qubits=2, num_results=2)
qis = BasicQisBuilder(module.builder)
qis.h(module.qubits[0])
qis.cx(module.qubits[0], module.qubits[1])
qis.mz(module.qubits[0], module.results[0])
qis.mz(module.qubits[1], module.results[1])
print(module.ir())
The QIR output will look like:
; ModuleID = 'Bell'
source_filename = "Bell"
define void @main() #0 {
entry:
call void @__quantum__qis__h__body(ptr null)
call void @__quantum__qis__cnot__body(ptr null, ptr inttoptr (i64 1 to ptr))
call void @__quantum__qis__mz__body(ptr null, ptr null)
call void @__quantum__qis__mz__body(ptr inttoptr (i64 1 to ptr), ptr inttoptr (i64 1 to ptr))
ret void
}
declare void @__quantum__qis__h__body(ptr)
declare void @__quantum__qis__cnot__body(ptr, ptr)
declare void @__quantum__qis__mz__body(ptr, ptr writeonly) #1
attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="custom" "required_num_qubits"="2" "required_num_results"="2" }
attributes #1 = { "irreversible" }
!llvm.module.flags = !{!0, !1, !2, !3}
!0 = !{i32 1, !"qir_major_version", i32 2}
!1 = !{i32 7, !"qir_minor_version", i32 0}
!2 = !{i32 1, !"dynamic_qubit_management", i1 false}
!3 = !{i32 1, !"dynamic_result_management", i1 false}
LLVM Opaque Pointers
Starting in PyQIR 0.12, we support LLVM opaque pointers via dependency on LLVM version 18 or higher. These newer version of PyQIR can parse IR or bitcode (.ll or .bc) with either style of pointers, but will always produce opaque pointers in any QIR output. Given that, PyQIR 0.12 also produces QIR with major version 2 by default. If you have need to produce QIR output with major version 1 that uses typed pointers, use PyQIR 0.11 or earlier.