From 6e6cf02cd93a60df3bd455af445f6037dc87ee58 Mon Sep 17 00:00:00 2001 From: Spencer Churchill Date: Mon, 7 Aug 2023 23:05:26 -0700 Subject: [PATCH 1/2] add support for braket, pytket, qsharp, xacc --- braket/README.md | 3 ++ braket/main.ipynb | 104 ++++++++++++++++++++++++++++++++++++++++++++ pytket/README.md | 3 ++ pytket/main.ipynb | 81 ++++++++++++++++++++++++++++++++++ qsharp/Operation.qs | 10 +++++ qsharp/README.md | 3 ++ qsharp/main.ipynb | 66 ++++++++++++++++++++++++++++ requirements.txt | 4 ++ xacc/README.md | 3 ++ xacc/main.ipynb | 77 ++++++++++++++++++++++++++++++++ 10 files changed, 354 insertions(+) create mode 100644 braket/README.md create mode 100644 braket/main.ipynb create mode 100644 pytket/README.md create mode 100644 pytket/main.ipynb create mode 100644 qsharp/Operation.qs create mode 100644 qsharp/README.md create mode 100644 qsharp/main.ipynb create mode 100644 xacc/README.md create mode 100644 xacc/main.ipynb diff --git a/braket/README.md b/braket/README.md new file mode 100644 index 0000000..d8e57fb --- /dev/null +++ b/braket/README.md @@ -0,0 +1,3 @@ +# Braket + +[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ionq-samples/getting-started/blob/main/braket/main.ipynb) diff --git a/braket/main.ipynb b/braket/main.ipynb new file mode 100644 index 0000000..6568fe3 --- /dev/null +++ b/braket/main.ipynb @@ -0,0 +1,104 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "151d6c64", + "metadata": {}, + "outputs": [], + "source": [ + "%%capture\n", + "\n", + "!pip install amazon-braket-sdk" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "ff988bb3", + "metadata": {}, + "outputs": [], + "source": [ + "from braket.circuits import Circuit" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b7dd4b92", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T : |0|1|\n", + " \n", + "q0 : -H-C-\n", + " | \n", + "q1 : ---X-\n", + "\n", + "T : |0|1|\n" + ] + } + ], + "source": [ + "qc = Circuit()\n", + "qc.h(0)\n", + "qc.cnot(control=0, target=1)\n", + "\n", + "print(qc)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7c83f913", + "metadata": {}, + "outputs": [], + "source": [ + "device = AwsDevice(\"arn:aws:braket:::device/qpu/ionq/ionQdevice\")\n", + "\n", + "# Enter the name of S3 bucket you created earlier\n", + "my_bucket = \"amazon-braket-Your-Bucket-Name\" # the name of the bucket\n", + "my_prefix = \"Your-Folder-Name\" # the name of the folder in the bucket\n", + "s3_folder = (my_bucket, my_prefix)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4e02b63e", + "metadata": {}, + "outputs": [], + "source": [ + "result = device.run(qc, s3_folder, shots=1000).result()\n", + "\n", + "# Get measurement counts\n", + "counts = result.measurement_counts\n", + "print(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.11.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/pytket/README.md b/pytket/README.md new file mode 100644 index 0000000..881e5ea --- /dev/null +++ b/pytket/README.md @@ -0,0 +1,3 @@ +# Pytket + +[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ionq-samples/getting-started/blob/main/pytket/main.ipynb) diff --git a/pytket/main.ipynb b/pytket/main.ipynb new file mode 100644 index 0000000..b58d830 --- /dev/null +++ b/pytket/main.ipynb @@ -0,0 +1,81 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "151d6c64", + "metadata": {}, + "outputs": [], + "source": [ + "%%capture\n", + "\n", + "!pip install pytket" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3042105", + "metadata": {}, + "outputs": [], + "source": [ + "from pytket import Circuit\n", + "from pytket.extensions.ionq import IonQBackend\n", + "\n", + "import os\n", + "from getpass import getpass\n", + "\n", + "# Get your API key from https://cloud.ionq.com/settings/keys\n", + "api_key = os.getenv('IONQ_API_KEY') or getpass('Enter your IonQ API key: ')\n", + "backend = IonQBackend(api_key=api_key, device_name=\"qpu\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bd2c2829", + "metadata": {}, + "outputs": [], + "source": [ + "qc = Circuit(2, 2)\n", + "qc.H(0)\n", + "qc.CX(0, 1)\n", + "qc.measure_all()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6aa9658d", + "metadata": {}, + "outputs": [], + "source": [ + "backend.compile_circuit(qc)\n", + "handle = backend.process_circuit(qc, 100)\n", + "counts = backend.get_result(handle).get_counts()\n", + "print(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.11.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/qsharp/Operation.qs b/qsharp/Operation.qs new file mode 100644 index 0000000..af8a8bd --- /dev/null +++ b/qsharp/Operation.qs @@ -0,0 +1,10 @@ +namespace Bell { + open Microsoft.Quantum.Intrinsic; + open Microsoft.Quantum.Measurement; + operation MeasureEntanglement() : Result[] { + use qubits = Qubit[2]; + H(qubits[0]); + CNOT(qubits[0], qubits[1]); + return MultiM(qubits); + } +} \ No newline at end of file diff --git a/qsharp/README.md b/qsharp/README.md new file mode 100644 index 0000000..dc2e60e --- /dev/null +++ b/qsharp/README.md @@ -0,0 +1,3 @@ +# Q# + +[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ionq-samples/getting-started/blob/main/qsharp/main.ipynb) diff --git a/qsharp/main.ipynb b/qsharp/main.ipynb new file mode 100644 index 0000000..33e9fa0 --- /dev/null +++ b/qsharp/main.ipynb @@ -0,0 +1,66 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "151d6c64", + "metadata": {}, + "outputs": [], + "source": [ + "%%capture\n", + "\n", + "!pip install qsharp" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "f3042105", + "metadata": {}, + "outputs": [], + "source": [ + "import qsharp\n", + "import qsharp.azure\n", + "from Bell import MeasureEntanglement\n", + "\n", + "qsharp.azure.connect(\n", + " resourceId=\"the name of your quantum resource\",\n", + " location=\"East US\",\n", + ")\n", + "qsharp.azure.target(\"ionq.qpu\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "19020254", + "metadata": {}, + "outputs": [], + "source": [ + "result = qsharp.azure.execute(MeasureEntanglement, shots=1000, jobName=\"Bell\")\n", + "print(result)" + ] + } + ], + "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.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/requirements.txt b/requirements.txt index dadf6d4..5e2fe1b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,10 @@ pennylane pennylane-ionq projectq cuda-quantum +amazon-braket-sdk +qsharp +pytket +xacc matplotlib pylatexenc jupyter diff --git a/xacc/README.md b/xacc/README.md new file mode 100644 index 0000000..cedb3cf --- /dev/null +++ b/xacc/README.md @@ -0,0 +1,3 @@ +# XACC + +[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ionq-samples/getting-started/blob/main/xacc/main.ipynb) diff --git a/xacc/main.ipynb b/xacc/main.ipynb new file mode 100644 index 0000000..d917406 --- /dev/null +++ b/xacc/main.ipynb @@ -0,0 +1,77 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "151d6c64", + "metadata": {}, + "outputs": [], + "source": [ + "%%capture\n", + "\n", + "!pip install xacc" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3042105", + "metadata": {}, + "outputs": [], + "source": [ + "import xacc" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "66dba273", + "metadata": {}, + "outputs": [], + "source": [ + "ionq = xacc.getAccelerator(\"ionq:qpu\")\n", + "compiler = xacc.getCompiler(\"xasm\")\n", + "ir = compiler.compile(r\"\"\"__qpu__ void bell(qbit q) {\n", + " H(q[0]);\n", + " CX(q[0], q[1]);\n", + " Measure(q[0]);\n", + " Measure(q[1]);\n", + "}\"\"\", ionq)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7a58565e", + "metadata": {}, + "outputs": [], + "source": [ + "buffer = xacc.qalloc(2)\n", + "r = ionq.execute(buffer, ir.getComposite(\"bell\"))\n", + "results = buffer.getMeasurementCounts()\n", + "print(results)" + ] + } + ], + "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.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 3e532478715a2dfbe71c0408e967010016b868ba Mon Sep 17 00:00:00 2001 From: Spencer Churchill Date: Mon, 7 Aug 2023 23:17:25 -0700 Subject: [PATCH 2/2] fix pytket --- braket/main.ipynb | 51 ++++++++++------------------------------------- pytket/main.ipynb | 37 +++++++++++++++++++++++++--------- qsharp/main.ipynb | 4 ++-- requirements.txt | 1 + xacc/main.ipynb | 2 +- 5 files changed, 43 insertions(+), 52 deletions(-) diff --git a/braket/main.ipynb b/braket/main.ipynb index 6568fe3..63a4c4a 100644 --- a/braket/main.ipynb +++ b/braket/main.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "151d6c64", "metadata": {}, "outputs": [], @@ -14,57 +14,31 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "ff988bb3", "metadata": {}, "outputs": [], "source": [ - "from braket.circuits import Circuit" + "from braket.circuits import Circuit\n", + "from braket.aws import AwsDevice\n", + "\n", + "device = AwsDevice(\"arn:aws:braket:::device/qpu/ionq/ionQdevice\")" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "b7dd4b92", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "T : |0|1|\n", - " \n", - "q0 : -H-C-\n", - " | \n", - "q1 : ---X-\n", - "\n", - "T : |0|1|\n" - ] - } - ], + "outputs": [], "source": [ "qc = Circuit()\n", "qc.h(0)\n", - "qc.cnot(control=0, target=1)\n", + "qc.cnot(0, 1)\n", "\n", "print(qc)" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "7c83f913", - "metadata": {}, - "outputs": [], - "source": [ - "device = AwsDevice(\"arn:aws:braket:::device/qpu/ionq/ionQdevice\")\n", - "\n", - "# Enter the name of S3 bucket you created earlier\n", - "my_bucket = \"amazon-braket-Your-Bucket-Name\" # the name of the bucket\n", - "my_prefix = \"Your-Folder-Name\" # the name of the folder in the bucket\n", - "s3_folder = (my_bucket, my_prefix)" - ] - }, { "cell_type": "code", "execution_count": null, @@ -72,11 +46,8 @@ "metadata": {}, "outputs": [], "source": [ - "result = device.run(qc, s3_folder, shots=1000).result()\n", - "\n", - "# Get measurement counts\n", - "counts = result.measurement_counts\n", - "print(counts)" + "task = device.run(bell, shots=100)\n", + "print(task.result().measurement_counts)" ] } ], diff --git a/pytket/main.ipynb b/pytket/main.ipynb index b58d830..f819a77 100644 --- a/pytket/main.ipynb +++ b/pytket/main.ipynb @@ -2,19 +2,19 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "151d6c64", "metadata": {}, "outputs": [], "source": [ "%%capture\n", "\n", - "!pip install pytket" + "!pip install pytket pytket-ionq" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "f3042105", "metadata": {}, "outputs": [], @@ -27,15 +27,26 @@ "\n", "# Get your API key from https://cloud.ionq.com/settings/keys\n", "api_key = os.getenv('IONQ_API_KEY') or getpass('Enter your IonQ API key: ')\n", - "backend = IonQBackend(api_key=api_key, device_name=\"qpu\")" + "backend = IonQBackend(api_key=api_key, device_name=\"simulator\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "bd2c2829", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[H q[0]; CX q[0], q[1]; Measure q[0] --> c[0]; Measure q[1] --> c[1]; ]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "qc = Circuit(2, 2)\n", "qc.H(0)\n", @@ -45,12 +56,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "6aa9658d", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Counter({(0, 0): 55, (1, 1): 45})\n" + ] + } + ], "source": [ - "backend.compile_circuit(qc)\n", + "backend.get_compiled_circuit(qc)\n", "handle = backend.process_circuit(qc, 100)\n", "counts = backend.get_result(handle).get_counts()\n", "print(counts)" diff --git a/qsharp/main.ipynb b/qsharp/main.ipynb index 33e9fa0..97ba70b 100644 --- a/qsharp/main.ipynb +++ b/qsharp/main.ipynb @@ -27,7 +27,7 @@ " resourceId=\"the name of your quantum resource\",\n", " location=\"East US\",\n", ")\n", - "qsharp.azure.target(\"ionq.qpu\")" + "qsharp.azure.target(\"ionq.harmony\")" ] }, { @@ -37,7 +37,7 @@ "metadata": {}, "outputs": [], "source": [ - "result = qsharp.azure.execute(MeasureEntanglement, shots=1000, jobName=\"Bell\")\n", + "result = qsharp.azure.execute(MeasureEntanglement, shots=100, jobName=\"Bell\")\n", "print(result)" ] } diff --git a/requirements.txt b/requirements.txt index 5e2fe1b..eeacf6f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,6 +9,7 @@ cuda-quantum amazon-braket-sdk qsharp pytket +pytket-ionq xacc matplotlib pylatexenc diff --git a/xacc/main.ipynb b/xacc/main.ipynb index d917406..85eb38f 100644 --- a/xacc/main.ipynb +++ b/xacc/main.ipynb @@ -29,7 +29,7 @@ "metadata": {}, "outputs": [], "source": [ - "ionq = xacc.getAccelerator(\"ionq:qpu\")\n", + "ionq = xacc.getAccelerator(\"ionq:simulator\")\n", "compiler = xacc.getCompiler(\"xasm\")\n", "ir = compiler.compile(r\"\"\"__qpu__ void bell(qbit q) {\n", " H(q[0]);\n",