microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.22.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/python_interop/cirq_submission_to_azure.ipynb

183lines · modecode

1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "id": "fae381bf",
6 "metadata": {},
7 "source": [
8 "# Submitting Cirq Circuits to Azure Quantum with the QDK\n",
9 "\n",
10 "This notebook shows how to take a Cirq `Circuit`, export it to OpenQASM 3, compile that OpenQASM 3 source to QIR using the Quantum Development Kit (QDK) Python APIs, and submit it as a job to an Azure Quantum target."
11 ]
12 },
13 {
14 "cell_type": "markdown",
15 "id": "0d696a7c",
16 "metadata": {},
17 "source": [
18 "The workflow demonstrated here:\n",
19 "\n",
20 "1. Build (or load) a Cirq `Circuit`.\n",
21 "2. Convert it to OpenQASM 3 text via `circuit.to_qasm(version=\"3.0\")`.\n",
22 "3. Compile the OpenQASM 3 source to QIR with `qdk.openqasm.compile`.\n",
23 "4. Connect to (or create) an Azure Quantum workspace using `qdk.azure.Workspace`.\n",
24 "5. Pick a target (e.g., a simulator like `rigetti.sim.qvm`).\n",
25 "6. Submit the QIR payload and retrieve measurement results."
26 ]
27 },
28 {
29 "cell_type": "markdown",
30 "id": "22d78680",
31 "metadata": {},
32 "source": [
33 "## Prerequisites\n",
34 "Ensure the `qdk` package (with Azure + Cirq extras) and `cirq` are installed. If not, install dependencies below."
35 ]
36 },
37 {
38 "cell_type": "code",
39 "execution_count": null,
40 "id": "eb556730",
41 "metadata": {},
42 "outputs": [],
43 "source": [
44 "%pip install qdk[azure,cirq]"
45 ]
46 },
47 {
48 "cell_type": "markdown",
49 "id": "20b9ed32",
50 "metadata": {},
51 "source": [
52 "After installing, restart the kernel if necessary. Verify imports:"
53 ]
54 },
55 {
56 "cell_type": "code",
57 "execution_count": null,
58 "id": "7531d5a6",
59 "metadata": {},
60 "outputs": [],
61 "source": [
62 "import cirq, qdk, qdk.azure # should import without errors"
63 ]
64 },
65 {
66 "cell_type": "markdown",
67 "id": "e2b111db",
68 "metadata": {},
69 "source": [
70 "## Submitting a simple Cirq circuit\n",
71 "We'll build a small circuit creating a superposition on one qubit and flipping another, then measuring both. Afterwards we submit it to an Azure Quantum target."
72 ]
73 },
74 {
75 "cell_type": "code",
76 "execution_count": null,
77 "id": "db4e96e3",
78 "metadata": {},
79 "outputs": [],
80 "source": [
81 "# Build a simple circuit\n",
82 "q0, q1 = cirq.LineQubit.range(2)\n",
83 "simple_circuit = cirq.Circuit(\n",
84 " cirq.H(q0),\n",
85 " cirq.measure(q0, key='m0'),\n",
86 " cirq.X(q1),\n",
87 " cirq.measure(q1, key='m1'),\n",
88 ")\n",
89 "print(simple_circuit)"
90 ]
91 },
92 {
93 "cell_type": "markdown",
94 "id": "f582bc13",
95 "metadata": {},
96 "source": [
97 "## Configure Azure Quantum workspace connection\n",
98 "Replace the placeholder values below with your own subscription, resource group, workspace name, and location."
99 ]
100 },
101 {
102 "cell_type": "code",
103 "execution_count": null,
104 "id": "0344527e",
105 "metadata": {},
106 "outputs": [],
107 "source": [
108 "subscription_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'\n",
109 "resource_group = 'myresourcegroup'\n",
110 "workspace_name = 'myworkspace'\n",
111 "location = 'westus'"
112 ]
113 },
114 {
115 "cell_type": "code",
116 "execution_count": null,
117 "id": "9f336702",
118 "metadata": {},
119 "outputs": [],
120 "source": [
121 "from qdk.openqasm import compile\n",
122 "from qdk.azure import Workspace\n",
123 "from qdk import TargetProfile\n",
124 "\n",
125 "def submit_cirq_circuit_to_azure(circuit: cirq.Circuit, target_name: str, name: str, shots: int = 100):\n",
126 " # 1. Export to OpenQASM 3\n",
127 " qasm3_str = circuit.to_qasm(version='3.0')\n",
128 " # 2. Compile to QIR with a base target profile\n",
129 " qir = compile(qasm3_str, target_profile=TargetProfile.Base)\n",
130 " # 3. Connect workspace\n",
131 " workspace = Workspace(\n",
132 " subscription_id=subscription_id,\n",
133 " resource_group=resource_group,\n",
134 " name=workspace_name,\n",
135 " location=location,\n",
136 " )\n",
137 " # 4. Select target (string e.g. 'rigetti.sim.qvm' or other available target)\n",
138 " target = workspace.get_targets(target_name)\n",
139 " # 5. Submit QIR payload\n",
140 " job = target.submit(qir, name, shots=shots)\n",
141 " return job.get_results()"
142 ]
143 },
144 {
145 "cell_type": "markdown",
146 "id": "a0436202",
147 "metadata": {},
148 "source": [
149 "### Submit the simple circuit\n",
150 "(Make sure you changed the workspace credentials above.)"
151 ]
152 },
153 {
154 "cell_type": "code",
155 "execution_count": null,
156 "id": "417d7818",
157 "metadata": {},
158 "outputs": [],
159 "source": [
160 "# Uncomment after setting workspace credentials\n",
161 "# results = submit_cirq_circuit_to_azure(simple_circuit, 'rigetti.sim.qvm', 'cirq-simple-job')\n",
162 "# print(results)"
163 ]
164 },
165 {
166 "cell_type": "markdown",
167 "id": "1478e88a",
168 "metadata": {},
169 "source": [
170 "## Notes\n",
171 "- Ensure all measurement keys appear before any classical condition usage if you introduce classical controls.\n",
172 "- Multi-target or custom gates may need full decomposition before submission if they produce unsupported classical constructs."
173 ]
174 }
175 ],
176 "metadata": {
177 "language_info": {
178 "name": "python"
179 }
180 },
181 "nbformat": 4,
182 "nbformat_minor": 5
183}
184