{ "cells": [ { "cell_type": "markdown", "id": "400bec26-4165-421e-bf3f-3ff88626fc25", "metadata": {}, "source": [ "# QIR Submission\n", "\n", "Quantinuum Nexus devices support the submission of Quantum Intermediate Representation (QIR)." ] }, { "cell_type": "code", "execution_count": null, "id": "d51801ac-f8b6-4d35-b8dd-88aebce96550", "metadata": {}, "outputs": [], "source": [ "import qnexus as qnx\n", "\n", "qnx.login()" ] }, { "cell_type": "markdown", "id": "1eada2b4-94f9-4618-8a24-62ac1fb0269a", "metadata": {}, "source": [ "Get/create a project and set it as the active project." ] }, { "cell_type": "code", "execution_count": null, "id": "7166a072-ab46-48c3-a19a-0387d61ce332", "metadata": {}, "outputs": [], "source": [ "import datetime\n", "\n", "project = qnx.projects.get_or_create(name=\"QIR-Demonstration\")\n", "qnx.context.set_active_project(project)\n", "\n", "qir_name = \"QIR Bell circuit\"\n", "jobname_suffix = datetime.datetime.now().strftime(\"%Y_%m_%d-%H-%M-%S\")" ] }, { "cell_type": "markdown", "id": "d45abfda-e0e7-4066-8681-83f524f95e1e", "metadata": {}, "source": [ "In this example, we have some LLVM QIR for a Bell circuit." ] }, { "cell_type": "code", "execution_count": null, "id": "5ca83eab-82c5-438e-8328-9b393b8c8b70", "metadata": {}, "outputs": [], "source": [ "bell_circuit_qir = \"\"\"\n", "; ModuleID = 'QIR Bell circuit'\n", "source_filename = \"QIR Bell circuit\"\n", "\n", "%Qubit = type opaque\n", "%Result = type opaque\n", "\n", "; Note the double slash here due to the enclosing triple quote\n", "@0 = internal constant [2 x i8] c\"c\\\\00\"\n", "\n", "define void @main() #0 {\n", "entry:\n", " %0 = call i1* @create_creg(i64 2)\n", " call void @__quantum__qis__h__body(%Qubit* null)\n", " call void @__quantum__qis__cnot__body(%Qubit* null, %Qubit* inttoptr (i64 1 to %Qubit*))\n", " call void @mz_to_creg_bit(%Qubit* null, i1* %0, i64 0)\n", " call void @mz_to_creg_bit(%Qubit* inttoptr (i64 1 to %Qubit*), i1* %0, i64 1)\n", " %1 = call i64 @get_int_from_creg(i1* %0)\n", " call void @__quantum__rt__int_record_output(i64 %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @0, i32 0, i32 0))\n", " ret void\n", "}\n", "\n", "declare i1 @__quantum__qis__read_result__body(%Result*)\n", "\n", "declare void @__quantum__rt__int_record_output(i64, i8*)\n", "\n", "declare i1 @get_creg_bit(i1*, i64)\n", "\n", "declare void @set_creg_bit(i1*, i64, i1)\n", "\n", "declare void @set_creg_to_int(i1*, i64)\n", "\n", "declare i1* @create_creg(i64)\n", "\n", "declare i64 @get_int_from_creg(i1*)\n", "\n", "declare void @mz_to_creg_bit(%Qubit*, i1*, i64)\n", "\n", "declare void @__quantum__qis__h__body(%Qubit*)\n", "\n", "declare void @__quantum__qis__cnot__body(%Qubit*, %Qubit*)\n", "\n", "attributes #0 = { \"entry_point\" \"output_labeling_schema\" \"qir_profiles\"=\"custom\" \"required_num_qubits\"=\"2\" \"required_num_results\"=\"2\" }\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", "\"\"\"" ] }, { "cell_type": "markdown", "id": "f7a9b591-f390-4dab-bf48-ea641b4cfd18", "metadata": {}, "source": [ "Compile the QIR to bitcode." ] }, { "cell_type": "code", "execution_count": null, "id": "3df05291-cc1d-4d84-80b7-9cd776054c5a", "metadata": {}, "outputs": [], "source": [ "import pyqir\n", "\n", "qir = pyqir.Module.from_ir(pyqir.Context(), bell_circuit_qir).bitcode" ] }, { "cell_type": "markdown", "id": "1b12fcff-d9ee-47e0-a2c7-8f2b4d08e52d", "metadata": {}, "source": [ "Upload the bitcode to Nexus." ] }, { "cell_type": "code", "execution_count": null, "id": "f508a96f-1755-4965-a2fb-9574046d89f8", "metadata": {}, "outputs": [], "source": [ "qir_program_ref = qnx.qir.upload(qir=qir, name=qir_name, project=project)" ] }, { "cell_type": "markdown", "id": "47025fdd-9b8f-47e6-bdb7-475e55d44811", "metadata": {}, "source": [ "Run the bitcode on the syntax checker." ] }, { "cell_type": "code", "execution_count": null, "id": "781b84d5-e3d7-41db-9a15-c5c896bb122c", "metadata": {}, "outputs": [], "source": [ "# Run on the H1-1 Syntax checker\n", "device_name = \"H1-1SC\"\n", "\n", "qnx.context.set_active_project(project)\n", "config = qnx.QuantinuumConfig(device_name=device_name)\n", "\n", "job_name = f\"execution-job-qir-{qir_name}-{device_name}-{jobname_suffix}\"\n", "ref_execute_job = qnx.start_execute_job(\n", " programs=[qir_program_ref],\n", " n_shots=[10],\n", " backend_config=config,\n", " name=job_name,\n", ")\n", "\n", "qnx.jobs.wait_for(ref_execute_job)" ] }, { "cell_type": "markdown", "id": "37e0b723-ca70-4663-a26d-311a60b2089d", "metadata": {}, "source": [ "Run the bitcode on the H1-1E emulator." ] }, { "cell_type": "code", "execution_count": null, "id": "87642da1-2458-4477-95d0-49a004b97217", "metadata": {}, "outputs": [], "source": [ "# Run on H1-1E\n", "device_name = \"H1-1E\"\n", "\n", "qnx.context.set_active_project(project)\n", "config = qnx.QuantinuumConfig(device_name=device_name)\n", "\n", "job_name = f\"execution-job-qir-{qir_name}-{device_name}-{jobname_suffix}\"\n", "ref_execute_job = qnx.start_execute_job(\n", " programs=[qir_program_ref],\n", " n_shots=[10],\n", " backend_config=config,\n", " name=job_name,\n", ")\n", "\n", "qnx.jobs.wait_for(ref_execute_job)" ] }, { "cell_type": "code", "execution_count": null, "id": "5ffec948-5c9a-4f3c-ba0b-7b4306682968", "metadata": {}, "outputs": [], "source": [ "qir_result = qnx.jobs.results(ref_execute_job)[0].download_result()\n", "qir_result.get_counts()" ] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 5 }