{ "cells": [ { "cell_type": "markdown", "id": "12d9be42", "metadata": {}, "source": [ "# Guppy to H2" ] }, { "cell_type": "markdown", "id": "4abe570c", "metadata": {}, "source": [ "Guppy is a quantum-first programming language built for Quantinuum Helios. The experimental HUGR-QIR tool converts Guppy programs into QIR, enabling their execution on System Model H2. QIR is an industry-standard representation used to access different hardware platforms.\n", "\n", "HUGR (Hierarchical Unified Graph Representation) is a compact, graph-based representation of Guppy programs. HUGR and QIR are different representations of quantum programs. Whereas open-source, off-the-shelf tools such as NVIDIA CUDA-Q generate QIR, HUGR is generated from Guppy source. The HUGR-QIR package requires users to generate HUGR before translating it to QIR. HUGR-generated QIR is checked for backward compatibility with System Model H2 using the Quantinuum QIR checker {footcite:p}`quantinuum_qircheck`.\n", "\n", "For Guppy features supported by QIR, see the [platform specifications](index.ipynb#platform-specifications). Not all Guppy features are available in the full QIR adaptive profile specified in the linked table. Additionally, HUGR-QIR does not support conversion of the following Guppy features:\n", "\n", "* Guppy functions that accept or return arrays (see the [caveats](#use-of-compile-time-arrays))\n", "* Unbounded loops\n", "* Dynamic qubit allocation" ] }, { "cell_type": "markdown", "id": "5675fc8d", "metadata": {}, "source": [ "## Installation\n", "\n", "HUGR-QIR is available on PyPI and requires Python 3.12 or newer. See the [HUGR-QIR GitHub repository](https://github.com/quantinuum/hugr-qir.git) for more information.\n", "\n", "```{code} bash\n", "pip install hugr-qir\n", "```" ] }, { "cell_type": "markdown", "id": "8ebc5395", "metadata": {}, "source": [ "## User Workflow" ] }, { "cell_type": "code", "execution_count": 3, "id": "4938009e", "metadata": {}, "outputs": [], "source": [ "from typing import no_type_check\n", "\n", "from guppylang import guppy, qubit\n", "from guppylang.std.builtins import result\n", "from guppylang.std.quantum import h, measure\n", "\n", "@guppy\n", "@no_type_check\n", "def main() -> None:\n", " q0 = qubit()\n", " q1 = qubit()\n", "\n", " h(q0)\n", " h(q1)\n", "\n", " b0 = measure(q0).read()\n", " b1 = measure(q1).read()\n", " b2 = b0 ^ b1\n", "\n", " result(\"0\", b2)" ] }, { "cell_type": "markdown", "id": "2f0d1f53", "metadata": {}, "source": [ "By default, `hugr_to_qir` validates the generated QIR. Passing `output_format=OutputFormat.BITCODE` returns QIR bitcode." ] }, { "cell_type": "code", "execution_count": 4, "id": "be9c3688", "metadata": {}, "outputs": [], "source": [ "from hugr_qir.hugr_to_qir import hugr_to_qir\n", "from hugr_qir.output import OutputFormat\n", "\n", "guppy_qir_bitcode = hugr_to_qir(main.compile(), output_format=OutputFormat.BITCODE)" ] }, { "cell_type": "markdown", "id": "b6eb89ae", "metadata": {}, "source": [ "Specifying `OutputFormat.LLVM_IR` returns QIR as textual LLVM IR." ] }, { "cell_type": "code", "execution_count": 5, "id": "9f5bc36f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "; ModuleID = 'hugr-qir'\n", "source_filename = \"hugr-qir\"\n", "target datalayout = \"e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32\"\n", "target triple = \"aarch64-unknown-linux-gnu\"\n", "\n", "@0 = private unnamed_addr constant [9 x i8] c\"0___BOOL\\00\", align 1\n", "@gen_name = private unnamed_addr constant [8 x i8] c\"hugr-qir\", section \",qir_generator\"\n", "@gen_version = private unnamed_addr constant [5 x i8] c\"0.3.0\", section \",qir_generator\"\n", "\n", "define void @__hugr__.__main__.main.1() local_unnamed_addr #0 {\n", "alloca_block:\n", " tail call void @__quantum__rt__initialize(ptr null)\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__mz__body(ptr null, ptr null)\n", " %0 = tail call i1 @__quantum__rt__read_result(ptr null)\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 1 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 1 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 1 to ptr), ptr nonnull inttoptr (i64 1 to ptr))\n", " %1 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 1 to ptr))\n", " %2 = xor i1 %0, %1\n", " tail call void @__quantum__rt__bool_record_output(i1 %2, ptr nonnull @0)\n", " ret void\n", "}\n", "\n", "declare void @__quantum__qis__phasedx__body(double, double, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__qis__rz__body(double, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__qis__mz__body(ptr, ptr writeonly) local_unnamed_addr #1\n", "\n", "declare i1 @__quantum__rt__read_result(ptr readonly) local_unnamed_addr\n", "\n", "declare void @__quantum__rt__bool_record_output(i1, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__rt__initialize(ptr) local_unnamed_addr\n", "\n", "attributes #0 = { \"entry_point\" \"output_labeling_schema\" \"qir_profiles\"=\"adaptive_profile\" \"required_num_qubits\"=\"2\" \"required_num_results\"=\"2\" }\n", "attributes #1 = { \"irreversible\" }\n", "\n", "!llvm.module.flags = !{!0, !1, !2, !3}\n", "\n", "!0 = !{i32 1, !\"qir_major_version\", i32 1}\n", "!1 = !{i32 7, !\"qir_minor_version\", i32 0}\n", "!2 = !{i32 1, !\"dynamic_qubit_management\", i1 false}\n", "!3 = !{i32 1, !\"dynamic_result_management\", i1 false}\n", "\n" ] } ], "source": [ "guppy_qir_string = hugr_to_qir(main.compile(), output_format=OutputFormat.LLVM_IR)\n", "print(guppy_qir_string)" ] }, { "cell_type": "markdown", "id": "f303d655", "metadata": {}, "source": [ "Only QIR bitcode can be uploaded to Nexus for submission to Quantinuum Systems. For more information, see [QIR submission](../qir/qir_submission.ipynb)." ] }, { "cell_type": "markdown", "id": "9854ffdd", "metadata": {}, "source": [ "## Supported on H2\n", "\n", "Supported actions:\n", "\n", "* Native gates (see below)\n", "* Measurements\n", "* Result tagging\n", "* Conditional branching\n", "* For loops\n", "* Random number generation (RNG)\n", "* Shot number retrieval\n", "\n", "All hardware-native gate operations are supported on System Model H2. Users cannot call the *General $SU(4)$ Entangler* gate using Guppy. For a list of supported gate operations, see the [available QIS functions](index.ipynb#available-qis-functions)." ] }, { "cell_type": "code", "execution_count": 8, "id": "9a3e22dd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "; ModuleID = 'hugr-qir'\n", "source_filename = \"hugr-qir\"\n", "target datalayout = \"e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32\"\n", "target triple = \"aarch64-unknown-linux-gnu\"\n", "\n", "@0 = private unnamed_addr constant [9 x i8] c\"0___BOOL\\00\", align 1\n", "@1 = private unnamed_addr constant [9 x i8] c\"1___BOOL\\00\", align 1\n", "@gen_name = private unnamed_addr constant [8 x i8] c\"hugr-qir\", section \",qir_generator\"\n", "@gen_version = private unnamed_addr constant [5 x i8] c\"0.3.0\", section \",qir_generator\"\n", "\n", "define void @__hugr__.__main__.main.1() local_unnamed_addr #0 {\n", "alloca_block:\n", " tail call void @__quantum__rt__initialize(ptr null)\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__phasedx__body(double 0xBFF921FB54442D18, double 0x3FF921FB54442D18, ptr nonnull inttoptr (i64 1 to ptr))\n", " tail call void @__quantum__qis__rzz__body(double 0x3FF921FB54442D18, ptr null, ptr nonnull inttoptr (i64 1 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0x400921FB54442D18, ptr nonnull inttoptr (i64 1 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 1 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr null, ptr null)\n", " %0 = tail call i1 @__quantum__rt__read_result(ptr null)\n", " tail call void @__quantum__rt__bool_record_output(i1 %0, ptr nonnull @0)\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 1 to ptr), ptr nonnull inttoptr (i64 1 to ptr))\n", " %1 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 1 to ptr))\n", " tail call void @__quantum__rt__bool_record_output(i1 %1, ptr nonnull @1)\n", " ret void\n", "}\n", "\n", "declare void @__quantum__qis__phasedx__body(double, double, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__qis__rz__body(double, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__qis__rzz__body(double, ptr, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__qis__mz__body(ptr, ptr writeonly) local_unnamed_addr #1\n", "\n", "declare i1 @__quantum__rt__read_result(ptr readonly) local_unnamed_addr\n", "\n", "declare void @__quantum__rt__bool_record_output(i1, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__rt__initialize(ptr) local_unnamed_addr\n", "\n", "attributes #0 = { \"entry_point\" \"output_labeling_schema\" \"qir_profiles\"=\"adaptive_profile\" \"required_num_qubits\"=\"2\" \"required_num_results\"=\"2\" }\n", "attributes #1 = { \"irreversible\" }\n", "\n", "!llvm.module.flags = !{!0, !1, !2, !3}\n", "\n", "!0 = !{i32 1, !\"qir_major_version\", i32 1}\n", "!1 = !{i32 7, !\"qir_minor_version\", i32 0}\n", "!2 = !{i32 1, !\"dynamic_qubit_management\", i1 false}\n", "!3 = !{i32 1, !\"dynamic_result_management\", i1 false}\n", "\n" ] } ], "source": [ "from typing import no_type_check\n", "\n", "from guppylang import guppy\n", "from guppylang.std.builtins import result\n", "from guppylang.std.quantum import h, cx, qubit, measure\n", "\n", "from hugr_qir.hugr_to_qir import hugr_to_qir\n", "from hugr_qir.output import OutputFormat\n", "\n", "@guppy\n", "@no_type_check\n", "def main() -> None:\n", " q0 = qubit()\n", " q1 = qubit()\n", "\n", " h(q0)\n", " cx(q0, q1)\n", "\n", " result(\"0\", measure(q0).read())\n", " result(\"1\", measure(q1).read())\n", "\n", "hugr = main.compile()\n", "\n", "guppy_llvm_ir = hugr_to_qir(main.compile(), output_format=OutputFormat.LLVM_IR)\n", "print(guppy_llvm_ir)" ] }, { "cell_type": "markdown", "id": "33e5cd9f", "metadata": {}, "source": [ "For compliance with H2, iterations are compiled into a non-compact representation that uses forward branching." ] }, { "cell_type": "code", "execution_count": 9, "id": "da2ddea3", "metadata": {}, "outputs": [], "source": [ "from typing import no_type_check\n", "\n", "from guppylang import guppy, qubit\n", "from guppylang.std.builtins import result\n", "from guppylang.std.quantum import h, measure\n", "\n", "@guppy\n", "@no_type_check\n", "def main() -> None:\n", " q0 = qubit()\n", " q1 = qubit()\n", "\n", " for _ in range(10):\n", " q3 = qubit()\n", " h(q3)\n", " b = measure(q3)\n", " if b:\n", " h(q0)\n", "\n", " result(\"0\", measure(q0).read())\n", " result(\"1\", measure(q1).read())" ] }, { "cell_type": "code", "execution_count": 10, "id": "bea01150", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "; ModuleID = 'hugr-qir'\n", "source_filename = \"hugr-qir\"\n", "target datalayout = \"e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32\"\n", "target triple = \"aarch64-unknown-linux-gnu\"\n", "\n", "@0 = private unnamed_addr constant [9 x i8] c\"0___BOOL\\00\", align 1\n", "@1 = private unnamed_addr constant [9 x i8] c\"1___BOOL\\00\", align 1\n", "@gen_name = private unnamed_addr constant [8 x i8] c\"hugr-qir\", section \",qir_generator\"\n", "@gen_version = private unnamed_addr constant [5 x i8] c\"0.3.0\", section \",qir_generator\"\n", "\n", "define void @__hugr__.__main__.main.1() local_unnamed_addr #0 {\n", "alloca_block:\n", " tail call void @__quantum__rt__initialize(ptr null)\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 2 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 2 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 2 to ptr), ptr null)\n", " %0 = tail call i1 @__quantum__rt__read_result(ptr null)\n", " br i1 %0, label %bb8, label %cond_exit_47\n", "\n", "cond_exit_47: ; preds = %alloca_block, %bb8\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 3 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 3 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 3 to ptr), ptr nonnull inttoptr (i64 1 to ptr))\n", " %1 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 1 to ptr))\n", " br i1 %1, label %bb, label %cond_exit_47.1\n", "\n", "bb: ; preds = %cond_exit_47\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " br label %cond_exit_47.1\n", "\n", "cond_exit_47.1: ; preds = %bb, %cond_exit_47\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 4 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 4 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 4 to ptr), ptr nonnull inttoptr (i64 2 to ptr))\n", " %2 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 2 to ptr))\n", " br i1 %2, label %bb0, label %cond_exit_47.2\n", "\n", "bb0: ; preds = %cond_exit_47.1\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " br label %cond_exit_47.2\n", "\n", "cond_exit_47.2: ; preds = %bb0, %cond_exit_47.1\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 5 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 5 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 5 to ptr), ptr nonnull inttoptr (i64 3 to ptr))\n", " %3 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 3 to ptr))\n", " br i1 %3, label %bb1, label %cond_exit_47.3\n", "\n", "bb1: ; preds = %cond_exit_47.2\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " br label %cond_exit_47.3\n", "\n", "cond_exit_47.3: ; preds = %bb1, %cond_exit_47.2\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 6 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 6 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 6 to ptr), ptr nonnull inttoptr (i64 4 to ptr))\n", " %4 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 4 to ptr))\n", " br i1 %4, label %bb2, label %cond_exit_47.4\n", "\n", "bb2: ; preds = %cond_exit_47.3\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " br label %cond_exit_47.4\n", "\n", "cond_exit_47.4: ; preds = %bb2, %cond_exit_47.3\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 7 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 7 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 7 to ptr), ptr nonnull inttoptr (i64 5 to ptr))\n", " %5 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 5 to ptr))\n", " br i1 %5, label %bb3, label %cond_exit_47.5\n", "\n", "bb3: ; preds = %cond_exit_47.4\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " br label %cond_exit_47.5\n", "\n", "cond_exit_47.5: ; preds = %bb3, %cond_exit_47.4\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 8 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 8 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 8 to ptr), ptr nonnull inttoptr (i64 6 to ptr))\n", " %6 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 6 to ptr))\n", " br i1 %6, label %bb4, label %cond_exit_47.6\n", "\n", "bb4: ; preds = %cond_exit_47.5\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " br label %cond_exit_47.6\n", "\n", "cond_exit_47.6: ; preds = %bb4, %cond_exit_47.5\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 9 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 9 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 9 to ptr), ptr nonnull inttoptr (i64 7 to ptr))\n", " %7 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 7 to ptr))\n", " br i1 %7, label %bb5, label %cond_exit_47.7\n", "\n", "bb5: ; preds = %cond_exit_47.6\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " br label %cond_exit_47.7\n", "\n", "cond_exit_47.7: ; preds = %bb5, %cond_exit_47.6\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 10 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 10 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 10 to ptr), ptr nonnull inttoptr (i64 8 to ptr))\n", " %8 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 8 to ptr))\n", " br i1 %8, label %bb6, label %cond_exit_47.8\n", "\n", "bb6: ; preds = %cond_exit_47.7\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " br label %cond_exit_47.8\n", "\n", "cond_exit_47.8: ; preds = %bb6, %cond_exit_47.7\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 11 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 11 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 11 to ptr), ptr nonnull inttoptr (i64 9 to ptr))\n", " %9 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 9 to ptr))\n", " br i1 %9, label %bb7, label %cond_exit_47.9\n", "\n", "bb7: ; preds = %cond_exit_47.8\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " br label %cond_exit_47.9\n", "\n", "cond_exit_47.9: ; preds = %bb7, %cond_exit_47.8\n", " tail call void @__quantum__qis__mz__body(ptr null, ptr nonnull inttoptr (i64 10 to ptr))\n", " %10 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 10 to ptr))\n", " tail call void @__quantum__rt__bool_record_output(i1 %10, ptr nonnull @0)\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 1 to ptr), ptr nonnull inttoptr (i64 11 to ptr))\n", " %11 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 11 to ptr))\n", " tail call void @__quantum__rt__bool_record_output(i1 %11, ptr nonnull @1)\n", " ret void\n", "\n", "bb8: ; preds = %alloca_block\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " br label %cond_exit_47\n", "}\n", "\n", "declare void @__quantum__qis__phasedx__body(double, double, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__qis__rz__body(double, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__qis__mz__body(ptr, ptr writeonly) local_unnamed_addr #1\n", "\n", "declare i1 @__quantum__rt__read_result(ptr readonly) local_unnamed_addr\n", "\n", "declare void @__quantum__rt__bool_record_output(i1, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__rt__initialize(ptr) local_unnamed_addr\n", "\n", "attributes #0 = { \"entry_point\" \"output_labeling_schema\" \"qir_profiles\"=\"adaptive_profile\" \"required_num_qubits\"=\"12\" \"required_num_results\"=\"12\" }\n", "attributes #1 = { \"irreversible\" }\n", "\n", "!llvm.module.flags = !{!0, !1, !2, !3}\n", "\n", "!0 = !{i32 1, !\"qir_major_version\", i32 1}\n", "!1 = !{i32 7, !\"qir_minor_version\", i32 0}\n", "!2 = !{i32 1, !\"dynamic_qubit_management\", i1 false}\n", "!3 = !{i32 1, !\"dynamic_result_management\", i1 false}\n", "\n" ] } ], "source": [ "from hugr_qir.hugr_to_qir import hugr_to_qir\n", "from hugr_qir.output import OutputFormat\n", "\n", "guppy_llvm_ir = hugr_to_qir(main.compile(), output_format=OutputFormat.LLVM_IR)\n", "\n", "print(guppy_llvm_ir)" ] }, { "cell_type": "markdown", "id": "b5c4f7c8-0c96-4ed5-9300-02eca87072a2", "metadata": {}, "source": [ "### Random Number Generator (RNG)\n", "\n", "HUGR-QIR supports converting programs that use Guppy's integrated RNG to QIR, including the seeded `RNG` constructor and the `random_int` and `random_int_bounded(int)` methods. The following example demonstrates the use of the seeded constructor and `random_int_bounded`:\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "7d229fe8-1402-4b2b-a8c4-c1f5b23ed77e", "metadata": {}, "outputs": [], "source": [ "from guppylang import guppy\n", "from guppylang.std.builtins import result\n", "from guppylang.std.angles import angle\n", "from guppylang.std.quantum import measure, qubit\n", "from guppylang.std.qsystem import phased_x, rz\n", "from guppylang.std.qsystem.random import RNG\n", "\n", "@guppy\n", "def main() -> None:\n", " q = qubit()\n", " rng = RNG(8) # seed=8\n", " index = rng.random_int_bounded(5)\n", " if index == 1:\n", " phased_x(q, angle(1), angle(0))\n", " elif index == 2:\n", " phased_x(q, angle(0), angle(1))\n", " elif index == 3:\n", " rz(q, angle(1))\n", " elif index == 4:\n", " phased_x(q, angle(1), angle(0))\n", " phased_x(q, angle(0), angle(1))\n", " res = measure(q).read()\n", " result(\"result\", res)\n", " rng.discard()\n", "\n", "hugr = main.compile()" ] }, { "cell_type": "code", "execution_count": 13, "id": "34049b08-0eaa-41c5-b714-1b794eeeaffd", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "; ModuleID = 'hugr-qir'\n", "source_filename = \"hugr-qir\"\n", "target datalayout = \"e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32\"\n", "target triple = \"aarch64-unknown-linux-gnu\"\n", "\n", "@0 = private unnamed_addr constant [14 x i8] c\"result___BOOL\\00\", align 1\n", "@gen_name = private unnamed_addr constant [8 x i8] c\"hugr-qir\", section \",qir_generator\"\n", "@gen_version = private unnamed_addr constant [5 x i8] c\"0.3.0\", section \",qir_generator\"\n", "\n", "define void @__hugr__.__main__.main.1() local_unnamed_addr #0 {\n", "alloca_block:\n", " tail call void @__quantum__rt__initialize(ptr null)\n", " tail call void @___random_seed(i64 8)\n", " %rintb = tail call i32 @___random_int_bounded(i32 5)\n", " %Pivot222 = icmp slt i32 %rintb, 3\n", " br i1 %Pivot222, label %NodeBlock, label %NodeBlock219\n", "\n", "NodeBlock219: ; preds = %alloca_block\n", " br label %NodeBlock223\n", "\n", "NodeBlock223: ; preds = %NodeBlock219\n", " %Pivot = icmp slt i32 %rintb, 4\n", " br i1 %Pivot, label %bb3, label %LeafBlock\n", "\n", "LeafBlock: ; preds = %NodeBlock223\n", " %SwitchLeaf = icmp eq i32 %rintb, 4\n", " br i1 %SwitchLeaf, label %bb, label %bb0\n", "\n", "NodeBlock: ; preds = %alloca_block\n", " br label %NodeBlock226\n", "\n", "NodeBlock226: ; preds = %NodeBlock\n", " %Pivot227 = icmp slt i32 %rintb, 2\n", " br i1 %Pivot227, label %LeafBlock224, label %bb2\n", "\n", "LeafBlock224: ; preds = %NodeBlock226\n", " %SwitchLeaf225 = icmp eq i32 %rintb, 1\n", " br i1 %SwitchLeaf225, label %bb1, label %bb0\n", "\n", "bb: ; preds = %LeafBlock\n", " tail call void @__quantum__qis__phasedx__body(double 0x400921FB54442D18, double 0.000000e+00, ptr null)\n", " tail call void @__quantum__qis__phasedx__body(double 0.000000e+00, double 0x400921FB54442D18, ptr null)\n", " br label %bb0\n", "\n", "bb0: ; preds = %LeafBlock224, %LeafBlock, %bb2, %bb, %bb3, %bb1\n", " tail call void @__quantum__qis__mz__body(ptr null, ptr null)\n", " %0 = tail call i1 @__quantum__rt__read_result(ptr null)\n", " tail call void @__quantum__rt__bool_record_output(i1 %0, ptr nonnull @0)\n", " ret void\n", "\n", "bb1: ; preds = %LeafBlock224\n", " tail call void @__quantum__qis__phasedx__body(double 0x400921FB54442D18, double 0.000000e+00, ptr null)\n", " br label %bb0\n", "\n", "bb2: ; preds = %NodeBlock226\n", " tail call void @__quantum__qis__phasedx__body(double 0.000000e+00, double 0x400921FB54442D18, ptr null)\n", " br label %bb0\n", "\n", "bb3: ; preds = %NodeBlock223\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " br label %bb0\n", "}\n", "\n", "declare void @___random_seed(i64) local_unnamed_addr\n", "\n", "declare noundef i32 @___random_int_bounded(i32) local_unnamed_addr\n", "\n", "declare void @__quantum__qis__phasedx__body(double, double, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__qis__rz__body(double, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__qis__mz__body(ptr, ptr writeonly) local_unnamed_addr #1\n", "\n", "declare i1 @__quantum__rt__read_result(ptr readonly) local_unnamed_addr\n", "\n", "declare void @__quantum__rt__bool_record_output(i1, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__rt__initialize(ptr) local_unnamed_addr\n", "\n", "attributes #0 = { \"entry_point\" \"output_labeling_schema\" \"qir_profiles\"=\"adaptive_profile\" \"required_num_qubits\"=\"1\" \"required_num_results\"=\"1\" }\n", "attributes #1 = { \"irreversible\" }\n", "\n", "!llvm.module.flags = !{!0, !1, !2, !3}\n", "\n", "!0 = !{i32 1, !\"qir_major_version\", i32 1}\n", "!1 = !{i32 7, !\"qir_minor_version\", i32 0}\n", "!2 = !{i32 1, !\"dynamic_qubit_management\", i1 false}\n", "!3 = !{i32 1, !\"dynamic_result_management\", i1 false}\n", "\n" ] } ], "source": [ "guppy_qir = hugr_to_qir(main.compile(), output_format=OutputFormat.LLVM_IR, validate_qir=True)\n", "print(guppy_qir)\n" ] }, { "cell_type": "markdown", "id": "fb8ade56-566f-4103-8b19-941752128439", "metadata": {}, "source": [ "### Use of Compile-Time Arrays\n", "\n", "Guppy arrays cannot currently be converted when used in a non-`comptime` context. To use arrays in Guppy programs and convert them to QIR, allocate them within a `@guppy.comptime` context. In this context, arrays are equivalent to Python lists. The following guidelines should help ensure a smooth conversion to QIR:\n", "\n", "* Allocate arrays only within `@guppy.comptime` functions. Alternatively, use Python lists.\n", " * Even within `@guppy.comptime` functions, do not pass in or return arrays.\n", "* Use regular Python functions (called from a `@guppy.comptime` context) for reusable operations on qubit arrays/lists at compile time.\n", " * These functions can accept and return qubit lists.\n", "* To implement runtime logic, use `@guppy`-annotated functions, but do not pass or allocate arrays. Only pass or allocate tuples or basic types.\n", "* Do not use `measure_array`/`discard_array`; instead, use compile-time loops over arrays to perform measurements and report results.\n", "\n", "The following example demonstrates this approach:" ] }, { "cell_type": "code", "execution_count": 16, "id": "ba7bbc86-be1c-44fe-9f96-e2188356e9f4", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "from typing import no_type_check\n", "\n", "from guppylang import guppy\n", "from guppylang.defs import GuppyFunctionDefinition\n", "from guppylang.std.angles import angle\n", "from guppylang.std.builtins import array, owned\n", "from guppylang.std.platform import result\n", "from guppylang.std.qsystem.random import RNG\n", "from guppylang.std.quantum import crz, cx, h, measure, qubit\n", "\n", "\n", "# You can define pure Python functions that use Guppy functions and objects, then call them from `@guppy.comptime` functions.\n", "# Examples include cx_registers, measure_qubits_conditionally, and record_result_array.\n", "# These functions can accept and return arrays without breaking QIR generation.\n", "\n", "def cx_registers(qreg1: array[qubit], qreg2: array[qubit]) -> None:\n", " # This function can be called from a `@guppy.comptime` function.\n", " # It can accept and return qubit lists or qubit arrays.\n", " size = len(qreg1)\n", " for i in range(size):\n", " cx(qreg1[i], qreg2[i])\n", "\n", "def measure_qubits_conditionally(q: list[qubit]) -> list[bool]:\n", " size = len(q)\n", " creg_results: list[bool] = []\n", "\n", " for i in range(0, size - 1, 2):\n", "\n", " # Guppy functions (such as conditional_measure) can be called from\n", " # Python functions that are later included in a Guppy module.\n", " qres_i, qres_ip1 = conditional_measure(q[i], q[i + 1])\n", "\n", " creg_results.append(qres_i)\n", " creg_results.append(qres_ip1)\n", "\n", " return creg_results\n", "\n", "def record_result_array(prefix: str, creg_results: list[bool]) -> None:\n", " # Record all values as individual Boolean values.\n", " size = len(creg_results)\n", "\n", " for i in range(size):\n", " result(f\"{prefix}_{i}\", creg_results[i])\n", "\n", "def record_result_array_record_int(prefix: str, creg_results: list[bool]) -> None:\n", " # Combine all Boolean values into one integer for recording. Consider endianness.\n", " size = len(creg_results)\n", "\n", " integer_value = 0\n", "\n", " for b in creg_results:\n", " integer_value = (integer_value << 1) | int(b) # for big-endian\n", " # integer_value = (integer_value << 1) | int(b) # for little-endian\n", " result(prefix, integer_value)\n", "\n", "@guppy\n", "@no_type_check\n", "def conditional_measure(q0: qubit @ owned, q1: qubit @ owned) -> tuple[bool, bool]:\n", " # This function must be a Guppy function, not a Guppy comptime function, because it conditions\n", " # on runtime values (here: measurement results).\n", " # For QIR generation, this type of function cannot use arrays.\n", " q2 = qubit()\n", " h(q2)\n", " c = measure(q2).read()\n", " if c:\n", " h(q0)\n", " a = measure(q0).read()\n", " if a:\n", " h(q1)\n", " b = measure(q1).read()\n", " return a, b\n", "\n", "def main_generator(size: int, theta: float) -> GuppyFunctionDefinition[[None], None]:\n", " # This function returns the Guppy main function for a given size and angle.\n", "\n", " # Expect an even integer for the size.\n", " assert size % 2 == 0\n", "\n", " @guppy.comptime # For QIR generation, this function must be comptime because it uses arrays.\n", " @no_type_check\n", " def main() -> None:\n", " q = array(qubit() for _ in range(size))\n", " q2 = array(qubit() for _ in range(size))\n", " h(q[0])\n", " cx_registers(q, q2)\n", " r1 = measure_qubits_conditionally(q)\n", " r2 = measure_qubits_conditionally(q2)\n", " record_result_array(\"q1\", r1)\n", " record_result_array_record_int(\"q2\", r2)\n", "\n", " return main" ] }, { "cell_type": "code", "execution_count": 17, "id": "21bf24db-6482-4569-8f75-ca6d4b2ff745", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "; ModuleID = 'hugr-qir'\n", "source_filename = \"hugr-qir\"\n", "target datalayout = \"e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32\"\n", "target triple = \"aarch64-unknown-linux-gnu\"\n", "\n", "@0 = private unnamed_addr constant [12 x i8] c\"q1_0___BOOL\\00\", align 1\n", "@1 = private unnamed_addr constant [12 x i8] c\"q1_1___BOOL\\00\", align 1\n", "@2 = private unnamed_addr constant [12 x i8] c\"q1_2___BOOL\\00\", align 1\n", "@3 = private unnamed_addr constant [12 x i8] c\"q1_3___BOOL\\00\", align 1\n", "@4 = private unnamed_addr constant [12 x i8] c\"q1_4___BOOL\\00\", align 1\n", "@5 = private unnamed_addr constant [12 x i8] c\"q1_5___BOOL\\00\", align 1\n", "@6 = private unnamed_addr constant [9 x i8] c\"q2___INT\\00\", align 1\n", "@gen_name = private unnamed_addr constant [8 x i8] c\"hugr-qir\", section \",qir_generator\"\n", "@gen_version = private unnamed_addr constant [5 x i8] c\"0.3.0\", section \",qir_generator\"\n", "\n", "define void @__hugr__.main.1() local_unnamed_addr #0 {\n", "alloca_block:\n", " tail call void @__quantum__rt__initialize(ptr null)\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__phasedx__body(double 0xBFF921FB54442D18, double 0x3FF921FB54442D18, ptr nonnull inttoptr (i64 6 to ptr))\n", " tail call void @__quantum__qis__rzz__body(double 0x3FF921FB54442D18, ptr null, ptr nonnull inttoptr (i64 6 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0x400921FB54442D18, ptr nonnull inttoptr (i64 6 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 6 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0xBFF921FB54442D18, double 0x3FF921FB54442D18, ptr nonnull inttoptr (i64 7 to ptr))\n", " tail call void @__quantum__qis__rzz__body(double 0x3FF921FB54442D18, ptr nonnull inttoptr (i64 1 to ptr), ptr nonnull inttoptr (i64 7 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 1 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0x400921FB54442D18, ptr nonnull inttoptr (i64 7 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 7 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0xBFF921FB54442D18, double 0x3FF921FB54442D18, ptr nonnull inttoptr (i64 8 to ptr))\n", " tail call void @__quantum__qis__rzz__body(double 0x3FF921FB54442D18, ptr nonnull inttoptr (i64 2 to ptr), ptr nonnull inttoptr (i64 8 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 2 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0x400921FB54442D18, ptr nonnull inttoptr (i64 8 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 8 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0xBFF921FB54442D18, double 0x3FF921FB54442D18, ptr nonnull inttoptr (i64 9 to ptr))\n", " tail call void @__quantum__qis__rzz__body(double 0x3FF921FB54442D18, ptr nonnull inttoptr (i64 3 to ptr), ptr nonnull inttoptr (i64 9 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 3 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0x400921FB54442D18, ptr nonnull inttoptr (i64 9 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 9 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0xBFF921FB54442D18, double 0x3FF921FB54442D18, ptr nonnull inttoptr (i64 10 to ptr))\n", " tail call void @__quantum__qis__rzz__body(double 0x3FF921FB54442D18, ptr nonnull inttoptr (i64 4 to ptr), ptr nonnull inttoptr (i64 10 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 4 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0x400921FB54442D18, ptr nonnull inttoptr (i64 10 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 10 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 12 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 12 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 12 to ptr), ptr null)\n", " %0 = tail call i1 @__quantum__rt__read_result(ptr null)\n", " br i1 %0, label %bb1, label %bb\n", "\n", "bb: ; preds = %alloca_block, %bb1\n", " tail call void @__quantum__qis__mz__body(ptr null, ptr nonnull inttoptr (i64 1 to ptr))\n", " %1 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 1 to ptr))\n", " br i1 %1, label %bb0, label %cond_795_case_1\n", "\n", "bb0: ; preds = %bb\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 1 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 1 to ptr))\n", " br label %cond_795_case_1\n", "\n", "cond_795_case_1: ; preds = %bb, %bb0\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 1 to ptr), ptr nonnull inttoptr (i64 2 to ptr))\n", " %2 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 2 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 13 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 13 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 13 to ptr), ptr nonnull inttoptr (i64 3 to ptr))\n", " %3 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 3 to ptr))\n", " br i1 %3, label %bb4, label %bb2\n", "\n", "bb1: ; preds = %alloca_block\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr null)\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr null)\n", " br label %bb\n", "\n", "bb2: ; preds = %cond_795_case_1, %bb4\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 2 to ptr), ptr nonnull inttoptr (i64 4 to ptr))\n", " %4 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 4 to ptr))\n", " br i1 %4, label %bb3, label %cond_838_case_1\n", "\n", "bb3: ; preds = %bb2\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 3 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 3 to ptr))\n", " br label %cond_838_case_1\n", "\n", "cond_838_case_1: ; preds = %bb2, %bb3\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 3 to ptr), ptr nonnull inttoptr (i64 5 to ptr))\n", " %5 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 5 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0xBFF921FB54442D18, double 0x3FF921FB54442D18, ptr nonnull inttoptr (i64 11 to ptr))\n", " tail call void @__quantum__qis__rzz__body(double 0x3FF921FB54442D18, ptr nonnull inttoptr (i64 5 to ptr), ptr nonnull inttoptr (i64 11 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 5 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0x400921FB54442D18, ptr nonnull inttoptr (i64 11 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 11 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 14 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 14 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 14 to ptr), ptr nonnull inttoptr (i64 6 to ptr))\n", " %6 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 6 to ptr))\n", " br i1 %6, label %bb7, label %bb5\n", "\n", "bb4: ; preds = %cond_795_case_1\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 2 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 2 to ptr))\n", " br label %bb2\n", "\n", "bb5: ; preds = %cond_838_case_1, %bb7\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 4 to ptr), ptr nonnull inttoptr (i64 7 to ptr))\n", " %7 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 7 to ptr))\n", " br i1 %7, label %bb6, label %cond_881_case_1\n", "\n", "bb6: ; preds = %bb5\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 5 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 5 to ptr))\n", " br label %cond_881_case_1\n", "\n", "cond_881_case_1: ; preds = %bb5, %bb6\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 5 to ptr), ptr nonnull inttoptr (i64 8 to ptr))\n", " %8 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 8 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 15 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 15 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 15 to ptr), ptr nonnull inttoptr (i64 9 to ptr))\n", " %9 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 9 to ptr))\n", " br i1 %9, label %bb10, label %bb8\n", "\n", "bb7: ; preds = %cond_838_case_1\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 4 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 4 to ptr))\n", " br label %bb5\n", "\n", "bb8: ; preds = %cond_881_case_1, %bb10\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 6 to ptr), ptr nonnull inttoptr (i64 10 to ptr))\n", " %10 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 10 to ptr))\n", " br i1 %10, label %bb9, label %cond_924_case_1\n", "\n", "bb9: ; preds = %bb8\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 7 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 7 to ptr))\n", " br label %cond_924_case_1\n", "\n", "cond_924_case_1: ; preds = %bb8, %bb9\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 7 to ptr), ptr nonnull inttoptr (i64 11 to ptr))\n", " %11 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 11 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 16 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 16 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 16 to ptr), ptr nonnull inttoptr (i64 12 to ptr))\n", " %12 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 12 to ptr))\n", " br i1 %12, label %bb13, label %bb11\n", "\n", "bb10: ; preds = %cond_881_case_1\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 6 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 6 to ptr))\n", " br label %bb8\n", "\n", "bb11: ; preds = %cond_924_case_1, %bb13\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 8 to ptr), ptr nonnull inttoptr (i64 13 to ptr))\n", " %13 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 13 to ptr))\n", " br i1 %13, label %bb12, label %cond_967_case_1\n", "\n", "bb12: ; preds = %bb11\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 9 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 9 to ptr))\n", " br label %cond_967_case_1\n", "\n", "cond_967_case_1: ; preds = %bb11, %bb12\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 9 to ptr), ptr nonnull inttoptr (i64 14 to ptr))\n", " %14 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 14 to ptr))\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 17 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 17 to ptr))\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 17 to ptr), ptr nonnull inttoptr (i64 15 to ptr))\n", " %15 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 15 to ptr))\n", " br i1 %15, label %bb17, label %bb14\n", "\n", "bb13: ; preds = %cond_924_case_1\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 8 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 8 to ptr))\n", " br label %bb11\n", "\n", "bb14: ; preds = %cond_967_case_1, %bb17\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 10 to ptr), ptr nonnull inttoptr (i64 16 to ptr))\n", " %16 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 16 to ptr))\n", " br i1 %16, label %bb15, label %bb16\n", "\n", "bb15: ; preds = %bb14\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 11 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 11 to ptr))\n", " br label %bb16\n", "\n", "bb16: ; preds = %bb14, %bb15\n", " %.963 = phi i64 [ 0, %bb14 ], [ 2, %bb15 ]\n", " %.962 = select i1 %13, i64 8, i64 0\n", " %. = select i1 %10, i64 32, i64 0\n", " tail call void @__quantum__qis__mz__body(ptr nonnull inttoptr (i64 11 to ptr), ptr nonnull inttoptr (i64 17 to ptr))\n", " %17 = tail call i1 @__quantum__rt__read_result(ptr nonnull inttoptr (i64 17 to ptr))\n", " tail call void @__quantum__rt__bool_record_output(i1 %1, ptr nonnull @0)\n", " %\"0799.0\" = zext i1 %17 to i64\n", " %\"0814.0\" = select i1 %14, i64 4, i64 0\n", " %\"0829.0\" = select i1 %11, i64 16, i64 0\n", " %18 = or disjoint i64 %\"0829.0\", %.\n", " %19 = or disjoint i64 %18, %.962\n", " %20 = or disjoint i64 %19, %\"0814.0\"\n", " %21 = or disjoint i64 %20, %.963\n", " %22 = or disjoint i64 %21, %\"0799.0\"\n", " tail call void @__quantum__rt__bool_record_output(i1 %2, ptr nonnull @1)\n", " tail call void @__quantum__rt__bool_record_output(i1 %4, ptr nonnull @2)\n", " tail call void @__quantum__rt__bool_record_output(i1 %5, ptr nonnull @3)\n", " tail call void @__quantum__rt__bool_record_output(i1 %7, ptr nonnull @4)\n", " tail call void @__quantum__rt__bool_record_output(i1 %8, ptr nonnull @5)\n", " tail call void @__quantum__rt__int_record_output(i64 %22, ptr nonnull @6)\n", " ret void\n", "\n", "bb17: ; preds = %cond_967_case_1\n", " tail call void @__quantum__qis__phasedx__body(double 0x3FF921FB54442D18, double 0xBFF921FB54442D18, ptr nonnull inttoptr (i64 10 to ptr))\n", " tail call void @__quantum__qis__rz__body(double 0x400921FB54442D18, ptr nonnull inttoptr (i64 10 to ptr))\n", " br label %bb14\n", "}\n", "\n", "declare void @__quantum__qis__phasedx__body(double, double, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__qis__rz__body(double, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__qis__rzz__body(double, ptr, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__qis__mz__body(ptr, ptr writeonly) local_unnamed_addr #1\n", "\n", "declare i1 @__quantum__rt__read_result(ptr readonly) local_unnamed_addr\n", "\n", "declare void @__quantum__rt__bool_record_output(i1, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__rt__int_record_output(i64, ptr) local_unnamed_addr\n", "\n", "declare void @__quantum__rt__initialize(ptr) local_unnamed_addr\n", "\n", "attributes #0 = { \"entry_point\" \"output_labeling_schema\" \"qir_profiles\"=\"adaptive_profile\" \"required_num_qubits\"=\"18\" \"required_num_results\"=\"18\" }\n", "attributes #1 = { \"irreversible\" }\n", "\n", "!llvm.module.flags = !{!0, !1, !2, !3}\n", "\n", "!0 = !{i32 1, !\"qir_major_version\", i32 1}\n", "!1 = !{i32 7, !\"qir_minor_version\", i32 0}\n", "!2 = !{i32 1, !\"dynamic_qubit_management\", i1 false}\n", "!3 = !{i32 1, !\"dynamic_result_management\", i1 false}\n", "\n" ] } ], "source": [ "from hugr_qir.hugr_to_qir import hugr_to_qir\n", "from hugr_qir.output import OutputFormat\n", "\n", "\n", "guppy_qir = hugr_to_qir(main_generator(size=6, theta=0.3).compile(), output_format=OutputFormat.LLVM_IR, validate_qir=True)\n", "print(guppy_qir)\n" ] }, { "cell_type": "markdown", "id": "46c35ab4", "metadata": {}, "source": [ "## Unsupported Conversions" ] }, { "cell_type": "markdown", "id": "9cd11f6b", "metadata": {}, "source": [ "### Use Case 1: Unbounded Loops\n", "\n", "An example like the one above, but with a loop whose number of iterations depends on measurement results within the loop, generates invalid QIR." ] }, { "cell_type": "code", "execution_count": 19, "id": "91626f83", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Validation failed as expected:\n", "Compilation failed: Loop cannot be unrolled: its iteration count is not known at compile time or exceeds the limit of 800.\n" ] } ], "source": [ "from typing import no_type_check\n", "\n", "from guppylang import guppy, qubit\n", "from guppylang.std.builtins import result\n", "from guppylang.std.quantum import h, measure\n", "\n", "@guppy\n", "@no_type_check\n", "def main() -> None:\n", " q0 = qubit()\n", " q1 = qubit()\n", "\n", " i = 0\n", " while i < 10:\n", " q3 = qubit()\n", " h(q3)\n", " b = measure(q3).read()\n", " if b:\n", " h(q0)\n", " i += 1\n", "\n", " result(\"0\", measure(q0).read())\n", " result(\"1\", measure(q1).read())\n", "\n", "hugr = main.compile()\n", "\n", "try:\n", " guppy_qir = hugr_to_qir(main.compile(), output_format=OutputFormat.BITCODE, validate_qir=True)\n", " print(guppy_qir)\n", "except Exception as e:\n", " print('Validation failed as expected:')\n", " print(e)" ] }, { "cell_type": "markdown", "id": "e068faac", "metadata": {}, "source": [ "### Use Case 2: Non-Comptime Qubit Arrays\n", "\n", "Using arrays of qubits in non-comptime functions generates invalid QIR. See [the compile-time array caveats](#use-of-compile-time-arrays) for alternatives." ] }, { "cell_type": "code", "execution_count": 20, "id": "6c5b7537", "metadata": {}, "outputs": [], "source": [ "from guppylang import guppy\n", "from guppylang.std.builtins import array, result\n", "from guppylang.std.quantum import qubit, measure_array, h, cx\n", "\n", "\n", "@guppy\n", "def ghz_state() -> array[qubit, 4]:\n", " qubit_array = array(qubit() for _ in range(4))\n", " h(qubit_array[0])\n", " for i in range(1, 4):\n", " cx(qubit_array[0], qubit_array[i])\n", " return qubit_array\n", "\n", "@guppy\n", "def main() -> None:\n", " qubit_array = ghz_state()\n", " measure_results = measure_array(qubit_array)\n", " res = 0\n", " for i in range(4):\n", " if measure_results[i]:\n", " res += 2**int(i)\n", " result(\"result\", res)\n", "\n", "hugr = main.compile()" ] }, { "cell_type": "code", "execution_count": 21, "id": "01928989", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Validation failed as expected:\n", "Compilation failed: Loop cannot be unrolled: its iteration count is not known at compile time or exceeds the limit of 800.\n" ] } ], "source": [ "try:\n", " guppy_qir = hugr_to_qir(main.compile(), output_format=OutputFormat.BITCODE, validate_qir=True)\n", " print(guppy_qir)\n", "except Exception as e:\n", " print('Validation failed as expected:')\n", " print(e)" ] }, { "cell_type": "code", "execution_count": null, "id": "4c87a83d-190e-4857-a93a-32773a83e39c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }