Namespace qir

namespace qir

Enums

enum class Execution

Whether the JIT’d program runs to produce measurement samples or to leave the final quantum state in qir::Runtime for external extraction.

In StateExtraction mode the session strips QIR measurement and result-management calls from the IR before JIT-compiling, so the runtime’s quantum state remains intact after main returns. Intended for QIR Base Profile programs only.

Values:

enumerator Sampling
enumerator StateExtraction

Functions

std::size_t stripMeasurementRelatedCalls(llvm::Module &m)

Strips QIR measurement-related calls from m in place.

Erases calls to the QIR measurement intrinsics, to the result-recording intrinsic, and to the result reference-count update intrinsic (whose Result operands would otherwise reference the null pointers left by the stripped measurements). Intended for QIR Base Profile programs only: in Adaptive Profile programs, measurement results feed classical control flow, so removing them silently changes observable behavior.

The typical use is to prepare a Base Profile module for state-vector extraction: after this transform the JIT’d main can be run once and the resulting state remains in the qir::Runtime DD instead of being collapsed by measurement.

Parameters:

m – Module to rewrite in place.

Returns:

Number of instructions erased.

Variables

constexpr std::array<llvm::StringRef, 5> STRIP_TARGETS = {"__quantum__qis__mz__body", "__quantum__qis__m__body", "__quantum__qis__measure__body", "__quantum__rt__result_record_output", "__quantum__rt__result_update_reference_count",}

The set of call targets that stripMeasurementRelatedCalls erases.

template<typename T>
static constexpr bool IS_STD_ARRAY_V = false
template<typename T, typename ...Args>
static constexpr size_t SIZE_OF_PACK_OF_TYPE_V = SizeOfPackOfType<T, Args...>::VALUE
template<template<typename, typename...> class V, typename T, typename ...Args>
static constexpr size_t SKIP_UNTIL_TYPE_V = SkipUntilType<V, T, Args...>::VALUE
class JitSession
#include <Session.hpp>

In-process JIT executor for QIR programs.

The session does the following, in order:

  • Loads an LLVM module from either an IR file (text or bitcode) or an in-memory buffer,

  • JIT-compiles it via LLVM’s OrcJIT with lazy compilation.

  • wires up the QIR runtime symbols, and

  • runs the module’s main function. A session owns a single LLJIT instance and is not meant to be reused across modules; create a new JitSession for each program.

Public Types

using MainFn = int(int, char**)

Signature of the main function produced by QIR-compiled modules.

Public Functions

explicit JitSession(llvm::StringRef inputFile, Execution mode = Execution::Sampling)

Build a session by loading IR from a file on disk.

Parameters:
  • inputFile – Path to a textual IR or bitcode file.

  • mode – Execution mode; see Execution.

Throws:

std::runtime_error – if the file cannot be parsed or the JIT fails to initialize.

JitSession(llvm::StringRef irBytes, llvm::StringRef bufferName, Execution mode = Execution::Sampling)

Build a session by loading IR from a memory buffer.

Accepts either textual IR or bitcode. The buffer does not have to be null-terminated.

Parameters:
  • irBytes – Byte view of the IR.

  • bufferName – Identifier used in diagnostics.

  • mode – Execution mode; see Execution.

Throws:

std::runtime_error – if the IR cannot be parsed or the JIT fails to initialize.

~JitSession()

Tears down the LLJIT and any JIT’d resources owned by the session.

int run(llvm::ArrayRef<std::string> args = {}, llvm::StringRef progName = "") const

Executes the JIT’d main function.

Parameters:
  • args – Argument strings passed as argv (excluding argv[0]).

  • progName – Value used as argv[0].

Returns:

The integer returned by the JIT’d main.

class Runtime
#include <Runtime.hpp>

Note

This class is implemented following the design pattern Singleton in order to access an instance of this class from the C function without having a handle to it.

Public Functions

auto recordOutput(Result *result) -> void

Append the value referenced by result to the recorded outputs bit string in record order.

auto getRecordedOutputs() const -> const std::string&
Returns:

the outputs declared by the program as a bit string in record order.

auto takeState() -> QState

Move the quantum state out of the runtime. Then reset the runtime to a clean state ready for the next job. Intended for use after a JitSession constructed with Execution::StateExtraction has finished running.

Returns:

the moved QState from the runtime.

struct QState
#include <Runtime.hpp>

The quantum state held by the runtime:

  • a DD package,

  • the root edge into that package, and

  • the number of qubits the state spans.

Public Functions

inline auto reset() -> void

Reset to a fresh empty state. If dd is currently populated, the existing package’s decRef plus garbageCollect path is used so the package (and its internal caches) is kept warm. If dd was moved out (e.g. by Runtime::takeState), a new package is allocated.

template<typename T, typename ...Args>
struct SizeOfPackOfType
template<typename T>
struct SizeOfPackOfType<T>
template<typename T, typename ...Args>
struct SizeOfPackOfType<T, T, Args...>
template<typename T, typename U, typename ...Args>
struct SizeOfPackOfType<T, U, Args...>
template<template<typename, typename...> class V, typename T, typename ...Args>
struct SkipUntilType
template<template<typename, typename...> class V, typename T>
struct SkipUntilType<V, T>
template<template<typename, typename...> class V, typename T, typename ...Args>
struct SkipUntilType<V, T, T, Args...>
template<template<typename, typename...> class V, typename T, typename U, typename ...Args>
struct SkipUntilType<V, T, U, Args...>
class Utils

Public Static Functions

template<typename Func, typename S, typename R>
static inline constexpr void transform(Func &&func, S &&source, R &&result)

Helper function to apply a function to each element of the array and store the result in another equally sized array.

template<typename Func, typename S, typename T>
static inline constexpr void apply2(Func &&func, S &&arg1, T &&arg2)

Helper function to apply a function to each element of the array and store the result with the help of the store function in another equally sized array.