{ "cells": [ { "cell_type": "markdown", "id": "eb89a144", "metadata": {}, "source": [ "# Compiling and Submitting Circuits onto Sqorpius via Cirq\n" ] }, { "cell_type": "markdown", "id": "capable-bradley", "metadata": {}, "source": [ "[](https://colab.research.google.com/github/Infleqtion/client-superstaq/blob/main/docs/source/optimizations/sqorpius/sqorpius_compile_css.ipynb) [](https://mybinder.org/v2/gh/Infleqtion/client-superstaq/HEAD?labpath=docs/source/optimizations/sqorpius/sqorpius_compile_css.ipynb)" ] }, { "cell_type": "markdown", "id": "cd44ef01", "metadata": {}, "source": [ "## Import Requirements" ] }, { "cell_type": "markdown", "id": "188579fc", "metadata": {}, "source": [ "This tutorial will showcase how to compile and submit a circuit onto Infleqtion's hardware, Sqorpius, using the ```cirq-superstaq``` client. " ] }, { "cell_type": "code", "execution_count": 1, "id": "aff4bd87", "metadata": {}, "outputs": [], "source": [ "# Required imports\n", "try:\n", " import cirq\n", " import cirq_superstaq as css\n", "except ImportError:\n", " print(\"Installing cirq-superstaq...\")\n", " %pip install --quiet 'cirq-superstaq[examples]'\n", " print(\"Installed cirq-superstaq.\")\n", " print(\"You may need to restart the kernel to import newly installed packages.\")\n", " import cirq\n", " import cirq_superstaq as css\n", "\n", "# Optional imports\n", "import os # Used if setting a token as an environment variable" ] }, { "cell_type": "markdown", "id": "ae0dfcb6", "metadata": {}, "source": [ "To interface Superstaq via Cirq, we must first instantiate a service provider in ```cirq-superstaq``` with ```Service()```. We then supply a Superstaq API key (which you can get from https://superstaq.infleqtion.com) by either providing the API key as an argument of Service, i.e., ```css.Service(api_key=\"token\")```, or by setting it as an environment variable. (see more details [here](https://superstaq.readthedocs.io/en/latest/get_started/basics/basics_css.html#Set-up-access-to-Superstaq%E2%80%99s-API))." ] }, { "cell_type": "code", "execution_count": 2, "id": "4464f34f", "metadata": {}, "outputs": [], "source": [ "service = css.Service()" ] }, { "cell_type": "markdown", "id": "415812a3", "metadata": {}, "source": [ "## Create a Circuit" ] }, { "cell_type": "markdown", "id": "f5d5ca21", "metadata": {}, "source": [ "First, we will create an example Cirq circuit that we can then compile and submit onto Sqorpius" ] }, { "cell_type": "code", "execution_count": 3, "id": "9521d540", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
0: ───H───@───M───\n",
" │\n",
"1: ───────X───────"
],
"text/plain": [
"0: ───H───@───M───\n",
" │\n",
"1: ───────X───────"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"qubits = cirq.LineQubit.range(4)\n",
"circuit1 = cirq.Circuit(cirq.H(qubits[0]), cirq.CNOT(qubits[0], qubits[1]), cirq.measure(qubits[0]))\n",
"circuit1"
]
},
{
"cell_type": "markdown",
"id": "b7812e22",
"metadata": {},
"source": [
"## Single Circuit Compilation"
]
},
{
"cell_type": "markdown",
"id": "eed571b8",
"metadata": {},
"source": [
"We will now compile the above circuit onto Sqorpius's native gateset and visualize the differences by drawing the compiled circuit"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b9149284",
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"compiler_output = service.cq_compile(circuit1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "92f3d368",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"0: ────RGate(0.5π, -0.5π)───@───RGate(-0.25π, -0.5π)───────RGate(0.25π, -0.5π)───────M───\n",
" │ │ │ │\n",
"1: ────#2───────────────────@───#2─────────────────────Z───#2────────────────────Z───────\n",
" │ │ │\n",
"2: ────#3───────────────────────#3─────────────────────────#3────────────────────────────\n",
" │ │ │\n",
"3: ────#4───────────────────────#4─────────────────────────#4────────────────────────────\n",
" │ │ │\n",
"4: ────#5───────────────────────#5─────────────────────────#5────────────────────────────\n",
" │ │ │\n",
"5: ────#6───────────────────────#6─────────────────────────#6────────────────────────────\n",
" │ │ │\n",
"6: ────#7───────────────────────#7─────────────────────────#7────────────────────────────\n",
" │ │ │\n",
"7: ────#8───────────────────────#8─────────────────────────#8────────────────────────────\n",
" │ │ │\n",
"8: ────#9───────────────────────#9─────────────────────────#9────────────────────────────\n",
" │ │ │\n",
"9: ────#10──────────────────────#10────────────────────────#10───────────────────────────\n",
" │ │ │\n",
"10: ───#11──────────────────────#11────────────────────────#11───────────────────────────\n",
" │ │ │\n",
"11: ───#12──────────────────────#12────────────────────────#12───────────────────────────\n",
" │ │ │\n",
"12: ───#13──────────────────────#13────────────────────────#13───────────────────────────\n",
" │ │ │\n",
"13: ───#14──────────────────────#14────────────────────────#14───────────────────────────\n",
" │ │ │\n",
"14: ───#15──────────────────────#15────────────────────────#15───────────────────────────\n",
" │ │ │\n",
"15: ───#16──────────────────────#16────────────────────────#16───────────────────────────\n",
" │ │ │\n",
"16: ───#17──────────────────────#17────────────────────────#17───────────────────────────\n",
" │ │ │\n",
"17: ───#18──────────────────────#18────────────────────────#18───────────────────────────\n",
" │ │ │\n",
"18: ───#19──────────────────────#19────────────────────────#19───────────────────────────\n",
" │ │ │\n",
"19: ───#20──────────────────────#20────────────────────────#20───────────────────────────\n",
" │ │ │\n",
"20: ───#21──────────────────────#21────────────────────────#21───────────────────────────\n",
" │ │ │\n",
"21: ───#22──────────────────────#22────────────────────────#22───────────────────────────\n",
" │ │ │\n",
"22: ───#23──────────────────────#23────────────────────────#23───────────────────────────\n",
" │ │ │\n",
"23: ───#24──────────────────────#24────────────────────────#24───────────────────────────"
],
"text/plain": [
"0: ────RGate(0.5π, -0.5π)───@───RGate(-0.25π, -0.5π)───────RGate(0.25π, -0.5π)───────M───\n",
" │ │ │ │\n",
"1: ────#2───────────────────@───#2─────────────────────Z───#2────────────────────Z───────\n",
" │ │ │\n",
"2: ────#3───────────────────────#3─────────────────────────#3────────────────────────────\n",
" │ │ │\n",
"3: ────#4───────────────────────#4─────────────────────────#4────────────────────────────\n",
" │ │ │\n",
"4: ────#5───────────────────────#5─────────────────────────#5────────────────────────────\n",
" │ │ │\n",
"5: ────#6───────────────────────#6─────────────────────────#6────────────────────────────\n",
" │ │ │\n",
"6: ────#7───────────────────────#7─────────────────────────#7────────────────────────────\n",
" │ │ │\n",
"7: ────#8───────────────────────#8─────────────────────────#8────────────────────────────\n",
" │ │ │\n",
"8: ────#9───────────────────────#9─────────────────────────#9────────────────────────────\n",
" │ │ │\n",
"9: ────#10──────────────────────#10────────────────────────#10───────────────────────────\n",
" │ │ │\n",
"10: ───#11──────────────────────#11────────────────────────#11───────────────────────────\n",
" │ │ │\n",
"11: ───#12──────────────────────#12────────────────────────#12───────────────────────────\n",
" │ │ │\n",
"12: ───#13──────────────────────#13────────────────────────#13───────────────────────────\n",
" │ │ │\n",
"13: ───#14──────────────────────#14────────────────────────#14───────────────────────────\n",
" │ │ │\n",
"14: ───#15──────────────────────#15────────────────────────#15───────────────────────────\n",
" │ │ │\n",
"15: ───#16──────────────────────#16────────────────────────#16───────────────────────────\n",
" │ │ │\n",
"16: ───#17──────────────────────#17────────────────────────#17───────────────────────────\n",
" │ │ │\n",
"17: ───#18──────────────────────#18────────────────────────#18───────────────────────────\n",
" │ │ │\n",
"18: ───#19──────────────────────#19────────────────────────#19───────────────────────────\n",
" │ │ │\n",
"19: ───#20──────────────────────#20────────────────────────#20───────────────────────────\n",
" │ │ │\n",
"20: ───#21──────────────────────#21────────────────────────#21───────────────────────────\n",
" │ │ │\n",
"21: ───#22──────────────────────#22────────────────────────#22───────────────────────────\n",
" │ │ │\n",
"22: ───#23──────────────────────#23────────────────────────#23───────────────────────────\n",
" │ │ │\n",
"23: ───#24──────────────────────#24────────────────────────#24───────────────────────────"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compiler_output.circuit"
]
},
{
"cell_type": "markdown",
"id": "a865f7b3",
"metadata": {},
"source": [
"If you would like to compile (or submit) on a different number of qubits, this can be done via the ```grid_shape``` option. This simply sets the shape of the rectangular qubit grid. However, specifying a grid that is incompatible with Sqorpius's current capabilities will result in an error when submitting. Anything equal to or smaller than than (6,4) is compatible with Sqorpius's current capabilities. "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "2f37e6bd",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"(0, 0): ───RGate(0.5π, -0.5π)───Z───\n",
" │\n",
"(1, 0): ───#2───────────────────Z───\n",
" │\n",
"(2, 0): ───#3───────────────────Z───\n",
" │\n",
"(3, 0): ───#4───────────────────Z───"
],
"text/plain": [
"(0, 0): ───RGate(0.5π, -0.5π)───Z───\n",
" │\n",
"(1, 0): ───#2───────────────────Z───\n",
" │\n",
"(2, 0): ───#3───────────────────Z───\n",
" │\n",
"(3, 0): ───#4───────────────────Z───"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"example_circuit = cirq.Circuit(cirq.H.on_each(*qubits))\n",
"new_compiler_output = service.cq_compile(example_circuit, grid_shape=(4, 1))\n",
"new_compiler_output.circuit"
]
},
{
"cell_type": "markdown",
"id": "04ad6645",
"metadata": {},
"source": [
"## Single Circuit Submission"
]
},
{
"cell_type": "markdown",
"id": "ac35a589",
"metadata": {},
"source": [
"The code below will submitt he circuit to the noiseless Sqorpius simulator. If you would like to run on Sqorpius, change the ```target``` argument in ```job_css``` from ```cq_sqorpius_simulator``` to ```cq_sqorpius_qpu```. It is recommended to first submit to the simulator for testing to ensure your code runs before submitting to Sqorpius. "
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "dcadf503",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Counts from cirq-superstaq submission: {'1': 53, '0': 47}\n"
]
}
],
"source": [
"job_css = service.create_job(circuit1, repetitions=100, target=\"cq_sqorpius_simulator\")\n",
"result_css = job_css.counts(0)\n",
"print(f\"Counts from cirq-superstaq submission: {result_css}\")"
]
},
{
"cell_type": "markdown",
"id": "d8780e3e",
"metadata": {},
"source": [
"If you would like to submit these circuits to a noisy simulator, to see the effects of noise on your simulated measurements without submitting to an actual device, you can also add in the argument ```method = noise-sim``` in ```job_css```. "
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "21ce8024",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Counts from noisy cirq-superstaq submission: {'0': 51, '1': 49}\n"
]
}
],
"source": [
"job_css = service.create_job(\n",
" circuit1, repetitions=100, target=\"cq_sqorpius_simulator\", method=\"noise-sim\"\n",
")\n",
"result_css = job_css.counts(0)\n",
"print(f\"Counts from noisy cirq-superstaq submission: {result_css}\")"
]
},
{
"cell_type": "markdown",
"id": "c6a4b1d3",
"metadata": {},
"source": [
"## Multiple circuit compilation"
]
},
{
"cell_type": "markdown",
"id": "82da705a",
"metadata": {},
"source": [
"All the functionalities we have seen so far can also be used on a multiple-circuit input as well. To illustrate this, let us create a different example two-qubit circuit, and compile both circuits we have created at the same time. "
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "32b95bf4",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"0: ───H───M───" ], "text/plain": [ "0: ───H───M───" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circuit2 = cirq.Circuit(cirq.H(qubits[0]), cirq.measure(qubits[0]))\n", "circuit2" ] }, { "cell_type": "code", "execution_count": 10, "id": "40fd33d8", "metadata": {}, "outputs": [], "source": [ "compiler_output = service.cq_compile([circuit1, circuit2])" ] }, { "cell_type": "code", "execution_count": 11, "id": "d1961e28", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
0: ────RGate(0.5π, -0.5π)───@───RGate(-0.25π, -0.5π)───────RGate(0.25π, -0.5π)───────M───\n",
" │ │ │ │\n",
"1: ────#2───────────────────@───#2─────────────────────Z───#2────────────────────Z───────\n",
" │ │ │\n",
"2: ────#3───────────────────────#3─────────────────────────#3────────────────────────────\n",
" │ │ │\n",
"3: ────#4───────────────────────#4─────────────────────────#4────────────────────────────\n",
" │ │ │\n",
"4: ────#5───────────────────────#5─────────────────────────#5────────────────────────────\n",
" │ │ │\n",
"5: ────#6───────────────────────#6─────────────────────────#6────────────────────────────\n",
" │ │ │\n",
"6: ────#7───────────────────────#7─────────────────────────#7────────────────────────────\n",
" │ │ │\n",
"7: ────#8───────────────────────#8─────────────────────────#8────────────────────────────\n",
" │ │ │\n",
"8: ────#9───────────────────────#9─────────────────────────#9────────────────────────────\n",
" │ │ │\n",
"9: ────#10──────────────────────#10────────────────────────#10───────────────────────────\n",
" │ │ │\n",
"10: ───#11──────────────────────#11────────────────────────#11───────────────────────────\n",
" │ │ │\n",
"11: ───#12──────────────────────#12────────────────────────#12───────────────────────────\n",
" │ │ │\n",
"12: ───#13──────────────────────#13────────────────────────#13───────────────────────────\n",
" │ │ │\n",
"13: ───#14──────────────────────#14────────────────────────#14───────────────────────────\n",
" │ │ │\n",
"14: ───#15──────────────────────#15────────────────────────#15───────────────────────────\n",
" │ │ │\n",
"15: ───#16──────────────────────#16────────────────────────#16───────────────────────────\n",
" │ │ │\n",
"16: ───#17──────────────────────#17────────────────────────#17───────────────────────────\n",
" │ │ │\n",
"17: ───#18──────────────────────#18────────────────────────#18───────────────────────────\n",
" │ │ │\n",
"18: ───#19──────────────────────#19────────────────────────#19───────────────────────────\n",
" │ │ │\n",
"19: ───#20──────────────────────#20────────────────────────#20───────────────────────────\n",
" │ │ │\n",
"20: ───#21──────────────────────#21────────────────────────#21───────────────────────────\n",
" │ │ │\n",
"21: ───#22──────────────────────#22────────────────────────#22───────────────────────────\n",
" │ │ │\n",
"22: ───#23──────────────────────#23────────────────────────#23───────────────────────────\n",
" │ │ │\n",
"23: ───#24──────────────────────#24────────────────────────#24───────────────────────────"
],
"text/plain": [
"0: ────RGate(0.5π, -0.5π)───@───RGate(-0.25π, -0.5π)───────RGate(0.25π, -0.5π)───────M───\n",
" │ │ │ │\n",
"1: ────#2───────────────────@───#2─────────────────────Z───#2────────────────────Z───────\n",
" │ │ │\n",
"2: ────#3───────────────────────#3─────────────────────────#3────────────────────────────\n",
" │ │ │\n",
"3: ────#4───────────────────────#4─────────────────────────#4────────────────────────────\n",
" │ │ │\n",
"4: ────#5───────────────────────#5─────────────────────────#5────────────────────────────\n",
" │ │ │\n",
"5: ────#6───────────────────────#6─────────────────────────#6────────────────────────────\n",
" │ │ │\n",
"6: ────#7───────────────────────#7─────────────────────────#7────────────────────────────\n",
" │ │ │\n",
"7: ────#8───────────────────────#8─────────────────────────#8────────────────────────────\n",
" │ │ │\n",
"8: ────#9───────────────────────#9─────────────────────────#9────────────────────────────\n",
" │ │ │\n",
"9: ────#10──────────────────────#10────────────────────────#10───────────────────────────\n",
" │ │ │\n",
"10: ───#11──────────────────────#11────────────────────────#11───────────────────────────\n",
" │ │ │\n",
"11: ───#12──────────────────────#12────────────────────────#12───────────────────────────\n",
" │ │ │\n",
"12: ───#13──────────────────────#13────────────────────────#13───────────────────────────\n",
" │ │ │\n",
"13: ───#14──────────────────────#14────────────────────────#14───────────────────────────\n",
" │ │ │\n",
"14: ───#15──────────────────────#15────────────────────────#15───────────────────────────\n",
" │ │ │\n",
"15: ───#16──────────────────────#16────────────────────────#16───────────────────────────\n",
" │ │ │\n",
"16: ───#17──────────────────────#17────────────────────────#17───────────────────────────\n",
" │ │ │\n",
"17: ───#18──────────────────────#18────────────────────────#18───────────────────────────\n",
" │ │ │\n",
"18: ───#19──────────────────────#19────────────────────────#19───────────────────────────\n",
" │ │ │\n",
"19: ───#20──────────────────────#20────────────────────────#20───────────────────────────\n",
" │ │ │\n",
"20: ───#21──────────────────────#21────────────────────────#21───────────────────────────\n",
" │ │ │\n",
"21: ───#22──────────────────────#22────────────────────────#22───────────────────────────\n",
" │ │ │\n",
"22: ───#23──────────────────────#23────────────────────────#23───────────────────────────\n",
" │ │ │\n",
"23: ───#24──────────────────────#24────────────────────────#24───────────────────────────"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compiler_output.circuits[0]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "4499aedd",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"0: ────RGate(0.5π, -0.5π)───M───\n",
" │\n",
"1: ────#2───────────────────────\n",
" │\n",
"2: ────#3───────────────────────\n",
" │\n",
"3: ────#4───────────────────────\n",
" │\n",
"4: ────#5───────────────────────\n",
" │\n",
"5: ────#6───────────────────────\n",
" │\n",
"6: ────#7───────────────────────\n",
" │\n",
"7: ────#8───────────────────────\n",
" │\n",
"8: ────#9───────────────────────\n",
" │\n",
"9: ────#10──────────────────────\n",
" │\n",
"10: ───#11──────────────────────\n",
" │\n",
"11: ───#12──────────────────────\n",
" │\n",
"12: ───#13──────────────────────\n",
" │\n",
"13: ───#14──────────────────────\n",
" │\n",
"14: ───#15──────────────────────\n",
" │\n",
"15: ───#16──────────────────────\n",
" │\n",
"16: ───#17──────────────────────\n",
" │\n",
"17: ───#18──────────────────────\n",
" │\n",
"18: ───#19──────────────────────\n",
" │\n",
"19: ───#20──────────────────────\n",
" │\n",
"20: ───#21──────────────────────\n",
" │\n",
"21: ───#22──────────────────────\n",
" │\n",
"22: ───#23──────────────────────\n",
" │\n",
"23: ───#24──────────────────────"
],
"text/plain": [
"0: ────RGate(0.5π, -0.5π)───M───\n",
" │\n",
"1: ────#2───────────────────────\n",
" │\n",
"2: ────#3───────────────────────\n",
" │\n",
"3: ────#4───────────────────────\n",
" │\n",
"4: ────#5───────────────────────\n",
" │\n",
"5: ────#6───────────────────────\n",
" │\n",
"6: ────#7───────────────────────\n",
" │\n",
"7: ────#8───────────────────────\n",
" │\n",
"8: ────#9───────────────────────\n",
" │\n",
"9: ────#10──────────────────────\n",
" │\n",
"10: ───#11──────────────────────\n",
" │\n",
"11: ───#12──────────────────────\n",
" │\n",
"12: ───#13──────────────────────\n",
" │\n",
"13: ───#14──────────────────────\n",
" │\n",
"14: ───#15──────────────────────\n",
" │\n",
"15: ───#16──────────────────────\n",
" │\n",
"16: ───#17──────────────────────\n",
" │\n",
"17: ───#18──────────────────────\n",
" │\n",
"18: ───#19──────────────────────\n",
" │\n",
"19: ───#20──────────────────────\n",
" │\n",
"20: ───#21──────────────────────\n",
" │\n",
"21: ───#22──────────────────────\n",
" │\n",
"22: ───#23──────────────────────\n",
" │\n",
"23: ───#24──────────────────────"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compiler_output.circuits[1]"
]
}
],
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}