Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
plutoniumm committed Feb 28, 2024
1 parent a114fde commit 0510021
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 1 deletion.
139 changes: 139 additions & 0 deletions examples/cudaq.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "ff197fa2-58fa-4d56-abbd-ad1f8bc5cee8",
"metadata": {},
"outputs": [],
"source": [
"# We need Qiskit, CudaQ and Abraxas\n",
"from qiskit.circuit.library import EfficientSU2\n",
"from cudaq import make_kernel, sample\n",
"from abrax import toPrime, toCudaq"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b5793bbd-07ce-44fc-b536-b7a924efaa41",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"word-wrap: normal;white-space: pre;background: #fff0;line-height: 1.1;font-family: &quot;Courier New&quot;,Courier,monospace\"> ┌──────────┐┌──────────┐ ┌──────────┐ ┌──────────┐\n",
"q_0: ┤ Ry(θ[0]) ├┤ Rz(θ[3]) ├──────────■───────┤ Ry(θ[6]) ├─┤ Rz(θ[9]) ├\n",
" ├──────────┤├──────────┤ ┌─┴─┐ ├──────────┤┌┴──────────┤\n",
"q_1: ┤ Ry(θ[1]) ├┤ Rz(θ[4]) ├──■─────┤ X ├─────┤ Ry(θ[7]) ├┤ Rz(θ[10]) ├\n",
" ├──────────┤├──────────┤┌─┴─┐┌──┴───┴───┐┌┴──────────┤└───────────┘\n",
"q_2: ┤ Ry(θ[2]) ├┤ Rz(θ[5]) ├┤ X ├┤ Ry(θ[8]) ├┤ Rz(θ[11]) ├─────────────\n",
" └──────────┘└──────────┘└───┘└──────────┘└───────────┘ </pre>"
],
"text/plain": [
" ┌──────────┐┌──────────┐ ┌──────────┐ ┌──────────┐\n",
"q_0: ┤ Ry(θ[0]) ├┤ Rz(θ[3]) ├──────────■───────┤ Ry(θ[6]) ├─┤ Rz(θ[9]) ├\n",
" ├──────────┤├──────────┤ ┌─┴─┐ ├──────────┤┌┴──────────┤\n",
"q_1: ┤ Ry(θ[1]) ├┤ Rz(θ[4]) ├──■─────┤ X ├─────┤ Ry(θ[7]) ├┤ Rz(θ[10]) ├\n",
" ├──────────┤├──────────┤┌─┴─┐┌──┴───┴───┐┌┴──────────┤└───────────┘\n",
"q_2: ┤ Ry(θ[2]) ├┤ Rz(θ[5]) ├┤ X ├┤ Ry(θ[8]) ├┤ Rz(θ[11]) ├─────────────\n",
" └──────────┘└──────────┘└───┘└──────────┘└───────────┘ "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Let us initialise Qiskit SU2\n",
"qc = EfficientSU2(3, reps=1).decompose()\n",
"qc.draw()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c302fbd6-3776-4914-96fc-26f9309032f3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-0 ry(θ[0]) rz(θ[3]) cx(1) ry(θ[6]) \n",
"-1 ry(θ[1]) rz(θ[4]) cx(2) ry(θ[7]) \n",
"-2 ry(θ[2]) rz(θ[5]) ry(θ[8]) rz(θ[11])\n"
]
}
],
"source": [
"# We use Abraxas String as a pseudo-IR\n",
"string = toPrime(qc)\n",
"print(string)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "aacf5589-2c29-4512-ab9e-e2c0b92ec0da",
"metadata": {},
"outputs": [],
"source": [
"# Can be run normally by Cuda\n",
"kernel, thetas = make_kernel(list)\n",
"qubits = kernel.qalloc(3)\n",
"\n",
"cudaO = {\n",
" 'kernel': kernel,\n",
" 'qubits': qubits,\n",
" 'quake': thetas,\n",
" 'params': 0,\n",
"}\n",
"kernel = toCudaq(cudaO, string)\n",
"vals = [0.1 * i for i in range(cudaO['params'])]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a0ae838e-d439-4c05-bcbe-a5a127f8ccfc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{ 011:27 101:4 001:56 110:19 100:89 111:8 010:100 000:697 }\n",
"\n"
]
}
],
"source": [
"result = sample(kernel, vals)\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.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
140 changes: 140 additions & 0 deletions examples/pennylane.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "ff197fa2-58fa-4d56-abbd-ad1f8bc5cee8",
"metadata": {},
"outputs": [],
"source": [
"# We need Qiskit, Pennylane and Abraxas\n",
"from qiskit.circuit.library import EfficientSU2\n",
"from abrax import toPrime, toPennylane\n",
"import pennylane as qml"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b5793bbd-07ce-44fc-b536-b7a924efaa41",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"word-wrap: normal;white-space: pre;background: #fff0;line-height: 1.1;font-family: &quot;Courier New&quot;,Courier,monospace\"> ┌──────────┐┌──────────┐ ┌──────────┐ ┌──────────┐\n",
"q_0: ┤ Ry(θ[0]) ├┤ Rz(θ[3]) ├──────────■───────┤ Ry(θ[6]) ├─┤ Rz(θ[9]) ├\n",
" ├──────────┤├──────────┤ ┌─┴─┐ ├──────────┤┌┴──────────┤\n",
"q_1: ┤ Ry(θ[1]) ├┤ Rz(θ[4]) ├──■─────┤ X ├─────┤ Ry(θ[7]) ├┤ Rz(θ[10]) ├\n",
" ├──────────┤├──────────┤┌─┴─┐┌──┴───┴───┐┌┴──────────┤└───────────┘\n",
"q_2: ┤ Ry(θ[2]) ├┤ Rz(θ[5]) ├┤ X ├┤ Ry(θ[8]) ├┤ Rz(θ[11]) ├─────────────\n",
" └──────────┘└──────────┘└───┘└──────────┘└───────────┘ </pre>"
],
"text/plain": [
" ┌──────────┐┌──────────┐ ┌──────────┐ ┌──────────┐\n",
"q_0: ┤ Ry(θ[0]) ├┤ Rz(θ[3]) ├──────────■───────┤ Ry(θ[6]) ├─┤ Rz(θ[9]) ├\n",
" ├──────────┤├──────────┤ ┌─┴─┐ ├──────────┤┌┴──────────┤\n",
"q_1: ┤ Ry(θ[1]) ├┤ Rz(θ[4]) ├──■─────┤ X ├─────┤ Ry(θ[7]) ├┤ Rz(θ[10]) ├\n",
" ├──────────┤├──────────┤┌─┴─┐┌──┴───┴───┐┌┴──────────┤└───────────┘\n",
"q_2: ┤ Ry(θ[2]) ├┤ Rz(θ[5]) ├┤ X ├┤ Ry(θ[8]) ├┤ Rz(θ[11]) ├─────────────\n",
" └──────────┘└──────────┘└───┘└──────────┘└───────────┘ "
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Let us initialise Qiskit SU2\n",
"qc = EfficientSU2(3, reps=1).decompose()\n",
"qc.draw()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c302fbd6-3776-4914-96fc-26f9309032f3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-0 ry(θ[0]) rz(θ[3]) cx(1) ry(θ[6]) \n",
"-1 ry(θ[1]) rz(θ[4]) cx(2) ry(θ[7]) \n",
"-2 ry(θ[2]) rz(θ[5]) ry(θ[8]) rz(θ[11])\n"
]
}
],
"source": [
"# We use Abraxas String as a pseudo-IR\n",
"string = toPrime(qc)\n",
"print(string)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "aacf5589-2c29-4512-ab9e-e2c0b92ec0da",
"metadata": {},
"outputs": [],
"source": [
"# We can now use the 'maker' to run all the necessary gates\n",
"# and pass in params into it\n",
"maker, params = toPennylane(string)\n",
"def circ(params):\n",
" maker(qml, params)\n",
"\n",
" return qml.probs()\n",
"\n",
"circuit = qml.QNode(circ, qml.device('default.qubit', wires=3))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a0ae838e-d439-4c05-bcbe-a5a127f8ccfc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([0.70065054, 0.05064247, 0.09717805, 0.03203952, 0.08211337,\n",
" 0.00629748, 0.02449996, 0.00657861], requires_grad=True)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# let's use 0.0, 0.1, 0.2...\n",
"params = [0.1 * i for i in range(len(params))]\n",
"circuit(params)"
]
}
],
"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.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
5 changes: 4 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
FILE=local/token.env
TOK=`cat $(FILE)`

build:
python3 setup.py bdist_wheel sdist
twine check dist/*

deploy:
twine upload dist/*
twine upload dist/* -u plutoniumm -p $(TOK)

0 comments on commit 0510021

Please sign in to comment.