QIR#

Quantum Intermediate Representation (QIR) module.

Contains the QIR compiler, loader and related files and functions.

Compiler Module#

Core QIR compiler.

Contains the core QIR compiler which uses the PyQIR API.

class BaseModule(name: str, num_qubits: int, num_bits: int, context: Context | None = None, **kwargs)[source]#

Bases: object

Module representing a QIR program conforming to the QIR basic profile.

Any keyword arguments are directly passed to the internal PyQIR Module. Below are listed the required keyword arguments with their defualt values.

Parameters:
  • name – Name of the module.

  • num_qubit – Number of qubits that the module contains.

  • num_bits – Number of bits/results that the module contains.

  • context – The context which the module is tied to. If None a new context is created.

Keyword Arguments:
  • qir_major_version – The QIR major version. Defaults to 1.

  • qir_minor_version – The QIR minor version. Defaults to 0.

  • dynamic_qubit_management – Whether to use dynamic qubit management. Defaults to False.

  • dynamic_result_management – Whether to use dynamic result management. Defaults to False.

add_external_function(name: str, parameter_types: Sequence[BaseModule.Types], return_type=None) None[source]#

Add an external function to the module.

Parameters:
  • name – The name of the external function. Preferably lowercase QIR style, e.g., "x" or "cnot".

  • parameter_types – Sequence of parameter types to the QIR function.

  • return_type – Return type of the QIR function. If None (default), return type is set to void.

add_instruction(block: str, instruction: Instruction) None[source]#

Add an instruction (operation) to the module.

Parameters:
  • block – The name of the block that the instruction should be added to.

  • instruction – The instruction that should be added.

property bitcode: bytes[source]#

Bitcode representation of the circuit.

Will be compiled if not previously compiled manually.

Returns:

A bitcode representation of the QIR circuit.

Return type:

bytes

property builder: Builder[source]#

Module builder.

compile(force: bool = False, strict: bool = True) None[source]#

Compile the module and verify it.

Parameters:
  • force – Whether to force recompilation if already compiled.

  • set_void_return – Whether to set a Void return in the final block.

  • strict – Whether the compiler should raise an error if verification fails.

property context: Context[source]#

Module context.

get_external_instruction(name: str, args: Sequence[Value]) Instruction[source]#

Gets an external function instruction.

Parameters:
  • name – The name of the function. Usually lowercase, QIR style, e.g., "x" or cnot.

  • args – The arguments for applying the function.

Returns:

Returns the stored external function or None if not found.

Return type:

Optional[pyqir.Function]

property name: str[source]#

Module source filename.

property qir: str[source]#

QIR string representation of the circuit.

Will be compiled if not previously compiled manually.

Returns:

QIR string representation.

Return type:

str

property qubits: Sequence[Constant][source]#

QIR module qubit references.

reset(rm_instructions: bool = True) None[source]#

Reset the module for recompilation.

Parameters:

rm_instructions – Whether to also reset all instructions.

property results: Sequence[Constant][source]#

QIR module results/bit references.

set_return(ret_cmd: pyqir.Type.Value | None = None, force: bool = False) None[source]#

Set the return instruction.

Will place the return instruction at the end of the final block. If forcing a re-set, the new return will again be placed at the end of the latest block, with the old one removed.

Parameters:
  • ret_cmd – The value to return. Returns void if None.

  • force – Whether to replace the former return command with a new one.

qir_module(circuit: Circuit, add_external: bool = False) BaseModule[source]#

Create a QIR module out of a circuit.

Parameters:
  • circuit – Circuit to convert to a QIR module.

  • add_external – Whether to add external function that are not part of the QIR Base Profile.

Returns:

QIR module representing the circuit.

Return type:

BaseModule

Instructions Module#

QIR instructions, types and Operation transformations.

Contains the Instruction class and the dwave-gate to/from QIR operation transformations.

class InstrType(value)[source]#

Bases: Enum

An enumeration.

BASE = 1[source]#

Valid QIR base profile instructions

DECOMPOSE = 3[source]#

Decomposes operation into valid QIR instructions (or external if allowed)

EXTERNAL = 2[source]#

Externally defined instructions. Falls back to decompositions if external functions are disallowed.

INVALID = 4[source]#

Invalid instructions that should raise an error if used

MEASUREMENT = 6[source]#

Measurement instructions

OUTPUT = 7[source]#

QIR output instructions

SKIP = 5[source]#

Instructions which are ignored when compiling to QIR (e.g., identity operation)

class Instruction(type_: InstrType, name: str, args: Sequence[int | float | Constant], external: Function | None = None)[source]#

Bases: object

Container class for QIR instructions.

Parameters:
  • type – Instruction type.

  • name – Name of the instruction to be applied.

  • args – QIR arguments to instruction.

  • external – Whether the operation is an external operation.

property args: Sequence[Any][source]#

Optional arguments to instruction.

execute(builder: Builder) None[source]#

Executes operation using the provided builder.

Parameters:

builder – PyQIR module builder.

property name: str[source]#

Instruction to apply.

property type: InstrType[source]#

The instruction type.

class Operations[source]#

Bases: object

Dataclass containing operation transformations between QIR and dwave-gate.

class Op(type, name)[source]#

Bases: tuple

name: str[source]#

Alias for field number 1

type: InstrType[source]#

Alias for field number 0

from_qir = {'ccnot': 'CCX', 'ch': 'CHadamard', 'crot': 'CRotation', 'crx': 'CRX', 'cry': 'CRY', 'crz': 'CRZ', 'cswap': 'CSWAP', 'cx': 'CX', 'cy': 'CY', 'cz': 'CZ', 'h': 'Hadamard', 'id': 'Identity', 'mz': 'Measurement', 'rot': 'Rotation', 'rx': 'RX', 'ry': 'RY', 'rz': 'RZ', 's': 'S', 'swap': 'SWAP', 't': 'T', 'x': 'X', 'y': 'Y', 'z': 'Z'}[source]#
from_qis_operation = {'__quantum__qis__ccnot__body': 'CCX', '__quantum__qis__ch__body': 'CHadamard', '__quantum__qis__cnot__body': 'CX', '__quantum__qis__crot__body': 'CRotation', '__quantum__qis__crx__body': 'CRX', '__quantum__qis__cry__body': 'CRY', '__quantum__qis__crz__body': 'CRZ', '__quantum__qis__cswap__body': 'CSWAP', '__quantum__qis__cx__body': 'CX', '__quantum__qis__cy__body': 'CY', '__quantum__qis__cz__body': 'CZ', '__quantum__qis__h__body': 'Hadamard', '__quantum__qis__id__body': 'Identity', '__quantum__qis__mz__body': 'Measurement', '__quantum__qis__rot__body': 'Rotation', '__quantum__qis__rx__body': 'RX', '__quantum__qis__ry__body': 'RY', '__quantum__qis__rz__body': 'RZ', '__quantum__qis__s__body': 'S', '__quantum__qis__swap__body': 'SWAP', '__quantum__qis__t__body': 'T', '__quantum__qis__x__body': 'X', '__quantum__qis__y__body': 'Y', '__quantum__qis__z__body': 'Z'}[source]#
to_qir = {'CCX': (InstrType.EXTERNAL, 'ccnot'), 'CHadamard': (InstrType.EXTERNAL, 'ch'), 'CRX': (InstrType.EXTERNAL, 'crx'), 'CRY': (InstrType.EXTERNAL, 'cry'), 'CRZ': (InstrType.EXTERNAL, 'crz'), 'CRotation': (InstrType.EXTERNAL, 'crot'), 'CSWAP': (InstrType.EXTERNAL, 'cswap'), 'CX': (InstrType.BASE, 'cx'), 'CY': (InstrType.EXTERNAL, 'cy'), 'CZ': (InstrType.BASE, 'cz'), 'Hadamard': (InstrType.BASE, 'h'), 'Identity': (InstrType.SKIP, 'id'), 'Measurement': (InstrType.MEASUREMENT, 'mz'), 'RX': (InstrType.BASE, 'rx'), 'RY': (InstrType.BASE, 'ry'), 'RZ': (InstrType.BASE, 'rz'), 'Rotation': (InstrType.EXTERNAL, 'rot'), 'S': (InstrType.BASE, 's'), 'SWAP': (InstrType.EXTERNAL, 'swap'), 'T': (InstrType.BASE, 't'), 'X': (InstrType.BASE, 'x'), 'Y': (InstrType.BASE, 'y'), 'Z': (InstrType.BASE, 'z')}[source]#

Loader Module#

QIR circuit loader.

Contains the QIR-to-dwave-gate circuit object loaders.

load_qir_bitcode(qir_bitcode: bytes, circuit: Circuit | None = None, context: Context | None = None) Circuit[source]#

Loads QIR bitcode into a Circuit object.

Parameters:
  • qir_bitcode – QIR bitcode representation of the circuit.

  • circuit – Optional circuit into which to load QIR bitcode. If None a new circuit is created (default).

  • context – Context to use to construct the module.

Returns:

Circuit representation of the QIR.

Return type:

Circuit

load_qir_string(qir_str: str, circuit: Circuit | None = None, context: Context | None = None) Circuit[source]#

Loads a QIR string into a Circuit object.

Parameters:
  • qir_str – QIR string representation of the circuit.

  • circuit – Optional circuit into which to load QIR bitcode. If None a new circuit is created (default).

  • context – Context to use to construct the module.

Returns:

Circuit representation of the QIR.

Return type:

Circuit