pyqir
¶
- class pyqir.AttributeList¶
Bases:
object
The attribute list for a function.
- func¶
The attributes for the function itself.
- Type:
AttributeDict
- param(n)¶
The attributes for a parameter.
- Parameters:
n (int) – The parameter number, starting from zero.
- Returns:
The parameter attributes.
- Return type:
AttributeDict
- ret¶
The attributes for the return type.
- Type:
AttributeDict
- class pyqir.BasicBlock¶
Bases:
Value
A basic block.
If the before block is given, this basic block is inserted directly before it. If no before block is given, a parent function must be given, and this basic block is appended to the end of that function.
- Parameters:
- instructions¶
The instructions in this basic block.
- Type:
- terminator¶
The terminating instruction of this basic block if there is one.
- Type:
- class pyqir.BasicQisBuilder(builder: Builder)¶
Bases:
object
An instruction builder that generates instructions from the basic quantum instruction set.
- cx(control: Value, target: Value) None ¶
Inserts a controlled Pauli \(X\) gate.
- Parameters:
control – The control qubit.
target – The target qubit.
- cz(control: Value, target: Value) None ¶
Inserts a controlled Pauli \(Z\) gate.
- Parameters:
control – The control qubit.
target – The target qubit.
- if_result(cond: Value, one: ~typing.Callable[[], None] = <function BasicQisBuilder.<lambda>>, zero: ~typing.Callable[[], None] = <function BasicQisBuilder.<lambda>>) None ¶
Inserts a branch conditioned on a measurement result.
Instructions inserted when
one
is called will be inserted into the one branch. Instructions inserted whenzero
is called will be inserted into the zero branch. The one and zero callables should use this module’s builder to build instructions.- Parameters:
cond – The result condition to branch on.
one – A callable that inserts instructions for the branch where the result is one.
zero – A callable that inserts instructions for the branch where the result is zero.
- mz(qubit: Value, result: Value) None ¶
Inserts a Z-basis measurement operation.
- Parameters:
qubit – The qubit to measure.
result – A result where the measurement result will be written to.
- rx(theta: Value | float, qubit: Value) None ¶
Inserts a rotation gate about the \(x\) axis.
- Parameters:
theta – The angle to rotate by.
qubit – The qubit to rotate.
- ry(theta: Value | float, qubit: Value) None ¶
Inserts a rotation gate about the \(y\) axis.
- Parameters:
theta – The angle to rotate by.
qubit – The qubit to rotate.
- class pyqir.Builder¶
Bases:
object
An instruction builder.
- Parameters:
context (Context) – The LLVM context.
- add(lhs, rhs)¶
Inserts an addition instruction.
- and_(lhs, rhs)¶
Inserts a bitwise logical and instruction.
- br()¶
Inserts an unconditional branch instruction.
- Parameters:
dest (BasicBlock) – The destination block.
- Returns:
The branch instruction.
- Return type:
- call(callee, args)¶
Inserts a call instruction.
- condbr(then, else_)¶
Inserts an conditional branch instruction.
- Parameters:
if (BasicBlock) – The condition
then (BasicBlock) – The destination block if condition is 1
else (BasicBlock) – The destination block if condition is 0
- Returns:
The branch instruction.
- Return type:
- icmp(pred, lhs, rhs)¶
Inserts an integer comparison instruction.
- Parameters:
pred (IntPredicate) – The predicate to compare by.
lhs (Value) – The left-hand side.
rhs (Value) – The right-hand side.
- Returns:
The boolean result.
- Return type:
- if_(cond, true, false)¶
Inserts a branch conditioned on a boolean.
Instructions inserted when
true
is called will be inserted into the true branch. Instructions inserted whenfalse
is called will be inserted into the false branch. The true and false callables should use this module’s builder to build instructions.
- insert_at_end()¶
Tells this builder to insert subsequent instructions at the end of the block.
- Parameters:
block (BasicBlock) – The block to insert into.
- Return type:
None
- lshr(lhs, rhs)¶
Inserts a logical (zero fill) shift right instruction.
- mul(lhs, rhs)¶
Inserts a multiplication instruction.
- or_(lhs, rhs)¶
Inserts a bitwise logical or instruction.
- phi()¶
Inserts a phi node.
- Returns:
The phi node.
- Return type:
- ret()¶
Inserts a return instruction.
- Parameters:
value (Value) – The value to return. If None, returns void.
- Returns:
The return instruction.
- Return type:
- shl(lhs, rhs)¶
Inserts a shift left instruction.
- sub(lhs, rhs)¶
Inserts a subtraction instruction.
- trunc(val, ty)¶
The ‘trunc’ instruction truncates its operand to the given type.
- xor(lhs, rhs)¶
Inserts a bitwise logical exclusive or instruction.
- class pyqir.Call¶
Bases:
Instruction
A call instruction.
- class pyqir.FCmp¶
Bases:
Instruction
A floating-point comparison instruction.
- predicate¶
The comparison predicate.
- Type:
- class pyqir.FloatPredicate¶
Bases:
object
A floating-point comparison predicate.
- FALSE = FloatPredicate.FALSE¶
- OEQ = FloatPredicate.OEQ¶
- OGE = FloatPredicate.OGE¶
- OGT = FloatPredicate.OGT¶
- OLE = FloatPredicate.OLE¶
- OLT = FloatPredicate.OLT¶
- ONE = FloatPredicate.ONE¶
- ORD = FloatPredicate.ORD¶
- TRUE = FloatPredicate.TRUE¶
- UEQ = FloatPredicate.UEQ¶
- UGE = FloatPredicate.UGE¶
- UGT = FloatPredicate.UGT¶
- ULE = FloatPredicate.ULE¶
- ULT = FloatPredicate.ULT¶
- UNE = FloatPredicate.UNE¶
- UNO = FloatPredicate.UNO¶
- class pyqir.Function¶
Bases:
Constant
A function value.
- Parameters:
ty (FunctionType) – The function type.
linkage (Linkage) – The linkage kind.
name (str) – The function name.
module (Module) – The parent module.
- attributes¶
The attributes for this function.
- basic_blocks¶
The basic blocks in this function.
- Type:
- type¶
- class pyqir.ICmp¶
Bases:
Instruction
An integer comparison instruction.
- predicate¶
The comparison predicate.
- Type:
- class pyqir.Instruction¶
Bases:
Value
An instruction.
- erase()¶
Removes this instruction from its parent basic block, then deletes it from memory.
Warning
Using this instruction after erasing it is undefined behavior.
- Return type:
None
- successors¶
The basic blocks that are successors to this instruction. If this is not a terminator, the list is empty.
- Type:
- class pyqir.IntPredicate¶
Bases:
object
An integer comparison predicate.
- EQ = IntPredicate.EQ¶
- NE = IntPredicate.NE¶
- SGE = IntPredicate.SGE¶
- SGT = IntPredicate.SGT¶
- SLE = IntPredicate.SLE¶
- SLT = IntPredicate.SLT¶
- UGE = IntPredicate.UGE¶
- UGT = IntPredicate.UGT¶
- ULE = IntPredicate.ULE¶
- ULT = IntPredicate.ULT¶
- class pyqir.Linkage¶
Bases:
object
The linkage kind for a global value in a module.
- APPENDING = Linkage.APPENDING¶
- AVAILABLE_EXTERNALLY = Linkage.AVAILABLE_EXTERNALLY¶
- COMMON = Linkage.COMMON¶
- EXTERNAL = Linkage.EXTERNAL¶
- EXTERNAL_WEAK = Linkage.EXTERNAL_WEAK¶
- INTERNAL = Linkage.INTERNAL¶
- LINK_ONCE_ANY = Linkage.LINK_ONCE_ANY¶
- LINK_ONCE_ODR = Linkage.LINK_ONCE_ODR¶
- PRIVATE = Linkage.PRIVATE¶
- WEAK_ANY = Linkage.WEAK_ANY¶
- WEAK_ODR = Linkage.WEAK_ODR¶
- class pyqir.Module¶
Bases:
object
A module is a collection of global values.
- add_flag(id, flag)¶
Adds a flag to the llvm.module.flags metadata
See https://llvm.org/docs/LangRef.html#module-flags-metadata
- Parameters:
behavior (ModuleFlagBehavior) – flag specifying the behavior when two (or more) modules are merged together
id (str) – string that is a unique ID for the metadata.
- static from_bitcode(context, bitcode, name='')¶
Creates a module from LLVM bitcode.
- static from_ir(context, ir, name='')¶
Creates a module from LLVM IR.
- get_flag()¶
Gets the flag value from the llvm.module.flags metadata for a given id
See https://llvm.org/docs/LangRef.html#module-flags-metadata
- link(other)¶
Link the supplied module into the current module. Destroys the supplied module.
- Raises:
An error if linking failed.
- class pyqir.ModuleFlagBehavior¶
Bases:
object
Module flag behavior choices
- APPEND = ModuleFlagBehavior.APPEND¶
- APPEND_UNIQUE = ModuleFlagBehavior.APPEND_UNIQUE¶
- ERROR = ModuleFlagBehavior.ERROR¶
- MAX = ModuleFlagBehavior.MAX¶
- OVERRIDE = ModuleFlagBehavior.OVERRIDE¶
- REQUIRE = ModuleFlagBehavior.REQUIRE¶
- WARNING = ModuleFlagBehavior.WARNING¶
- class pyqir.Opcode¶
Bases:
object
An instruction opcode.
- ADD = Opcode.ADD¶
- ADDR_SPACE_CAST = Opcode.ADDR_SPACE_CAST¶
- ALLOCA = Opcode.ALLOCA¶
- AND = Opcode.AND¶
- ASHR = Opcode.ASHR¶
- ATOMIC_CMP_XCHG = Opcode.ATOMIC_CMP_XCHG¶
- ATOMIC_RMW = Opcode.ATOMIC_RMW¶
- BIT_CAST = Opcode.BIT_CAST¶
- BR = Opcode.BR¶
- CALL = Opcode.CALL¶
- CALL_BR = Opcode.CALL_BR¶
- CATCH_PAD = Opcode.CATCH_PAD¶
- CATCH_RET = Opcode.CATCH_RET¶
- CATCH_SWITCH = Opcode.CATCH_SWITCH¶
- CLEANUP_PAD = Opcode.CLEANUP_PAD¶
- CLEANUP_RET = Opcode.CLEANUP_RET¶
- EXTRACT_ELEMENT = Opcode.EXTRACT_ELEMENT¶
- EXTRACT_VALUE = Opcode.EXTRACT_VALUE¶
- FADD = Opcode.FADD¶
- FCMP = Opcode.FCMP¶
- FDIV = Opcode.FDIV¶
- FENCE = Opcode.FENCE¶
- FMUL = Opcode.FMUL¶
- FNEG = Opcode.FNEG¶
- FP_EXT = Opcode.FP_EXT¶
- FP_TO_SI = Opcode.FP_TO_SI¶
- FP_TO_UI = Opcode.FP_TO_UI¶
- FP_TRUNC = Opcode.FP_TRUNC¶
- FREEZE = Opcode.FREEZE¶
- FREM = Opcode.FREM¶
- FSUB = Opcode.FSUB¶
- GET_ELEMENT_PTR = Opcode.GET_ELEMENT_PTR¶
- ICMP = Opcode.ICMP¶
- INDIRECT_BR = Opcode.INDIRECT_BR¶
- INSERT_ELEMENT = Opcode.INSERT_ELEMENT¶
- INSERT_VALUE = Opcode.INSERT_VALUE¶
- INT_TO_PTR = Opcode.INT_TO_PTR¶
- INVOKE = Opcode.INVOKE¶
- LANDING_PAD = Opcode.LANDING_PAD¶
- LOAD = Opcode.LOAD¶
- LSHR = Opcode.LSHR¶
- MUL = Opcode.MUL¶
- OR = Opcode.OR¶
- PHI = Opcode.PHI¶
- PTR_TO_INT = Opcode.PTR_TO_INT¶
- RESUME = Opcode.RESUME¶
- RET = Opcode.RET¶
- SDIV = Opcode.SDIV¶
- SELECT = Opcode.SELECT¶
- SEXT = Opcode.SEXT¶
- SHL = Opcode.SHL¶
- SHUFFLE_VECTOR = Opcode.SHUFFLE_VECTOR¶
- SI_TO_FP = Opcode.SI_TO_FP¶
- SREM = Opcode.SREM¶
- STORE = Opcode.STORE¶
- SUB = Opcode.SUB¶
- SWITCH = Opcode.SWITCH¶
- TRUNC = Opcode.TRUNC¶
- UDIV = Opcode.UDIV¶
- UI_TO_FP = Opcode.UI_TO_FP¶
- UNREACHABLE = Opcode.UNREACHABLE¶
- UREM = Opcode.UREM¶
- USER_OP_1 = Opcode.USER_OP_1¶
- USER_OP_2 = Opcode.USER_OP_2¶
- VA_ARG = Opcode.VA_ARG¶
- XOR = Opcode.XOR¶
- ZEXT = Opcode.ZEXT¶
- class pyqir.Phi¶
Bases:
Instruction
A phi node instruction.
- add_incoming(block)¶
Adds an incoming value to the end of the phi list.
- incoming¶
The incoming values and their preceding basic blocks.
- Type:
List[Tuple[Value, BasicBlock]]
- class pyqir.PointerType¶
Bases:
Type
A pointer type.
- Parameters:
pointee (Type) – The type being pointed to.
- class pyqir.SimpleModule(name: str, num_qubits: int, num_results: int, context: Context | None = None, entry_point_name: str = 'main')¶
Bases:
object
A simple module represents a QIR program with the following assumptions:
All qubits and results are statically allocated.
There is exactly one function that is not externally linked, which is the entry point.
- add_byte_string(value: bytes) Constant ¶
Adds a global null-terminated byte string constant to the module.
- Parameters:
Value – The byte string value without a null terminator.
- Returns:
A pointer to the start of the null-terminated byte string.
- add_external_function(name: str, ty: FunctionType) Function ¶
Adds a declaration for an externally linked function to the module.
- Parameters:
name – The name of the function.
ty – The type of the function.
- Returns:
The function value.
- property entry_block: BasicBlock¶
The first basic block of the entry point (automatically generated).
- class pyqir.StructType¶
Bases:
Type
A structure type.
- name¶
The name of the structure or the empty string if the structure is anonymous.
- class pyqir.Switch¶
Bases:
Instruction
A switch instruction.
- cases¶
The switch cases.
- Type:
List[Tuple[Value, BasicBlock]]
- default¶
The default successor block if none of the cases match.
- Type:
- class pyqir.Value¶
Bases:
object
A value.
- name¶
The name of this value or the empty string if this value is anonymous.
- pyqir.add_string_attribute(function, key, value, index)¶
- pyqir.const(ty, value)¶
Creates a constant value.
- pyqir.dynamic_qubit_management(module)¶
Whether this module supports dynamic qubit management. None if unspecified.
- pyqir.dynamic_result_management(module)¶
Whether this module supports dynamic result management. None if unspecified.
- pyqir.entry_point(module: Module, name: str, required_num_qubits: int, required_num_results: int, qir_profiles: str = 'custom', output_labeling_schema: str | None = None) Function ¶
Creates an entry point.
- Parameters:
module (Module) – The parent module.
name (str) – The entry point name.
required_num_qubits (int) – The number of qubits required by the entry point.
required_num_results (int) – The number of results required by the entry point.
qir_profiles (Optional[str]) – Value identifying the profile the entry point has been compiled for. Use base_profile when QIR is compliant.
output_labeling_schema (Optional[str]) – An arbitrary string value that identifies the schema used by a compiler frontend that produced the IR to label the recorded output
- Returns:
An entry point.
- pyqir.extract_byte_string(value)¶
If the value is a pointer to a constant byte string, extracts it.
- pyqir.global_byte_string(module, value)¶
Creates a global null-terminated byte string constant in a module.
- pyqir.is_entry_point(function)¶
Whether the function is an entry point.
- pyqir.is_interop_friendly(function)¶
Whether the function is interop-friendly.
- pyqir.is_qubit_type(ty)¶
Whether the type is the QIR qubit type.
- pyqir.is_result_type(ty)¶
Whether the type is the QIR result type.
- pyqir.qir_major_version(module)¶
The QIR major version this module is built for. None if unspecified.
- pyqir.qir_minor_version(module)¶
The QIR minor version this module is built for. None if unspecified.
- pyqir.qir_module(context, name, qir_major_version, qir_minor_version, dynamic_qubit_management, dynamic_result_management)¶
Creates a module with required QIR module flag metadata
- Parameters:
context (Context) – The parent context.
name (str) – The module name.
qir_major_version (int) – The QIR major version this module is built for. Default 1.
qir_minor_version (int) – The QIR minor version this module is built for. Default 0.
dynamic_qubit_management (bool) – Whether this module supports dynamic qubit management. Default False.
dynamic_result_management (bool) – Whether this module supports dynamic result management. Default False.
- Return type:
- pyqir.qubit(context, id)¶
Creates a static qubit value.
- pyqir.qubit_id(value)¶
If the value is a static qubit ID, extracts it.
- pyqir.qubit_type(context)¶
The QIR qubit type.
- pyqir.required_num_qubits(function)¶
If the function declares a required number of qubits, extracts it.
- pyqir.required_num_results(function)¶
If the function declares a required number of results, extracts it.
- pyqir.result(context, id)¶
Creates a static result value.
- pyqir.result_id(value)¶
If the value is a static result ID, extracts it.