Skip to content

Commit

Permalink
add classiq paper codes
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerGoldfriend committed Dec 9, 2024
1 parent 25fdd7d commit 26e0799
Show file tree
Hide file tree
Showing 8 changed files with 1,278 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "c9c1fa3f-f0e8-4b78-81d7-172d89f1a181",
"metadata": {},
"source": [
"# Classiq code for QSVT example"
]
},
{
"cell_type": "markdown",
"id": "54cf368e-b780-42b3-930e-58caf4a255f2",
"metadata": {},
"source": [
"This notebook shows how to generate for the QSVT example using `classiq`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "4dcb49f6-5436-447a-b5ea-7081b04a1829",
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"\n",
"import numpy as np\n",
"\n",
"from classiq import *\n",
"from classiq.qmod.symbolic import floor"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1b1013df-9cf8-40c4-8362-b89539f9c757",
"metadata": {},
"outputs": [],
"source": [
"MAX_WIDTH = 50\n",
"SIZE = 6\n",
"DEGREE = 3\n",
"QSVT_PHASES = [\n",
" 1.280311896404252,\n",
" 8.127145628464149,\n",
" 1.8439603212845617,\n",
" -5.002873410775335,\n",
"]\n",
"\n",
"constraints = Constraints(optimization_parameter=\"cx\", max_width=MAX_WIDTH)"
]
},
{
"cell_type": "markdown",
"id": "ac29a72b-0238-49b0-b5a6-89b27cd98ef0",
"metadata": {},
"source": [
"## Quantum Functions"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "835628ad-dd86-4128-8c8f-11f64732f733",
"metadata": {},
"outputs": [],
"source": [
"@qfunc\n",
"def be_projection(x: QArray[QBit], aux: QBit):\n",
" within_apply(\n",
" lambda: H(aux),\n",
" lambda: control(\n",
" aux == 0,\n",
" lambda: reflect_about_zero(x),\n",
" ),\n",
" )\n",
"\n",
"\n",
"@qfunc\n",
"def be_amat0(data: QArray[QBit], block: QArray[QBit]):\n",
"\n",
" del_qubit = QBit(\"del_qubit\")\n",
" select = QBit(\"select\")\n",
" packed = QNum(\"packed\", data.size + 1)\n",
"\n",
" within_apply(\n",
" lambda: (\n",
" bind(block, [select, del_qubit]),\n",
" bind([data, del_qubit], packed),\n",
" hadamard_transform(select),\n",
" ),\n",
" lambda: (\n",
" control(select, lambda: IDENTITY(packed), lambda: inplace_add(2, packed)),\n",
" inplace_add(-1, packed),\n",
" ),\n",
" )\n",
"\n",
"\n",
"@qfunc\n",
"def my_be(data: QArray[QBit], block: QArray[QBit]):\n",
"\n",
" be_amat0(data, block[0:2])\n",
" be_projection(data, block[2])\n",
"\n",
"\n",
"@qfunc\n",
"def my_projector_controlled_phase(phase: CReal, block: QNum, aux: QBit):\n",
" control(block == 0, lambda: X(aux))\n",
" RZ(phase, aux)\n",
" control(block == 0, lambda: X(aux))\n",
"\n",
"\n",
"@qfunc\n",
"def my_qsvt_step(\n",
" phase1: CReal,\n",
" phase2: CReal,\n",
" u: QCallable[QArray, QArray],\n",
" data: QArray[QBit],\n",
" block: QArray[QBit],\n",
" qsvt_aux: QBit,\n",
"):\n",
"\n",
" u(data, block)\n",
" my_projector_controlled_phase(phase1, block, qsvt_aux)\n",
" invert(lambda: u(data, block))\n",
" my_projector_controlled_phase(phase2, block, qsvt_aux)\n",
"\n",
"\n",
"@qfunc\n",
"def my_qsvt(\n",
" qsvt_phases: CArray[CReal], data: QArray[QBit], block: QArray[QBit], qsvt_aux: QBit\n",
"):\n",
"\n",
" H(qsvt_aux)\n",
" my_projector_controlled_phase(qsvt_phases[0], block, qsvt_aux)\n",
" repeat(\n",
" floor((qsvt_phases.len - 1) / 2),\n",
" lambda i: my_qsvt_step(\n",
" qsvt_phases[(2 * i) + 1],\n",
" qsvt_phases[(2 * i) + 2],\n",
" lambda d, b: my_be(d, b),\n",
" data,\n",
" block,\n",
" qsvt_aux,\n",
" ),\n",
" )\n",
" my_be(data, block)\n",
" my_projector_controlled_phase(qsvt_phases[qsvt_phases.len - 1], block, qsvt_aux)\n",
" H(qsvt_aux)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d9b9ed1a-62d0-4829-9ea2-4912cf6696a0",
"metadata": {},
"outputs": [],
"source": [
"from classiq import CustomHardwareSettings, Preferences\n",
"\n",
"preferences = Preferences(\n",
" custom_hardware_settings=CustomHardwareSettings(basis_gates=[\"cx\", \"u\"]),\n",
" transpilation_option=\"custom\",\n",
" debug_mode=False,\n",
")"
]
},
{
"cell_type": "markdown",
"id": "632eab9d-8db3-4791-a668-d920243ea3ed",
"metadata": {},
"source": [
"## Example for getting a data point"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "75d56ad1-8793-4c27-a455-68d0ec168e55",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"==== classiq for 6==== time 11.979933977127075\n"
]
}
],
"source": [
"start_time = time.time()\n",
"\n",
"\n",
"@qfunc\n",
"def main(block: Output[QNum], data: Output[QNum], qsvt_aux: Output[QBit]):\n",
" allocate(1, qsvt_aux)\n",
" allocate(3, block)\n",
" allocate(SIZE, data)\n",
" my_qsvt(QSVT_PHASES, data, block, qsvt_aux)\n",
"\n",
"\n",
"qmod = create_model(main, constraints=constraints, preferences=preferences)\n",
"qprog = synthesize(qmod)\n",
"\n",
"compilation_time = time.time() - start_time\n",
"quantum_program = QuantumProgram.from_qprog(qprog)\n",
"width = quantum_program.data.width\n",
"depth = quantum_program.transpiled_circuit.depth\n",
"cx_counts = quantum_program.transpiled_circuit.count_ops[\"cx\"]\n",
"print(f\"==== classiq for {SIZE}==== time {compilation_time}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a740aec0-5189-4869-8193-b3d79ca39379",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "fa77f7d8-295d-4ae8-99a9-60e33a9e0ccb",
"metadata": {},
"source": [
"# PennyLane code for QSVT example"
]
},
{
"cell_type": "markdown",
"id": "84c7f1f0-df91-46df-9853-3bad194c743e",
"metadata": {},
"source": [
"This notebook shows how to generate data for QSVT example using `pennylane` 0.39.0 and `pennylane-catalyst` 0.9.0."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "acaaf57c-cc5e-4d9a-9465-ab7f3e979bb0",
"metadata": {},
"outputs": [],
"source": [
"# import time\n",
"# import pennylane as qml\n",
"# from catalyst import qjit\n",
"# from pennylane import numpy as np\n",
"\n",
"\n",
"# def reflect_around_zero(data):\n",
"# \"\"\"Implements the reflection around zero operator.\"\"\"\n",
"# def circuit(data):\n",
"# qml.PauliX(wires=data[0])\n",
"# qml.Hadamard(wires=data[0])\n",
"# qml.ctrl(qml.PauliX, control=data[1:len(data)], control_values=[0]*(len(data)-1))(wires=data[0])\n",
"# qml.Hadamard(wires=data[0])\n",
"# qml.PauliX(wires=data[0])\n",
"# return circuit\n",
"\n",
"# def get_cir_be(data, block):\n",
"# \"\"\"Constructs the controlled block-encoding circuit.\"\"\"\n",
"# def circuit():\n",
"# qml.Hadamard(wires=block[0])\n",
"# qml.Hadamard(wires=block[2])\n",
"# qml.ctrl(qml.Adder,control=block[0], control_values=1)(2, data+[block[1]], 2**(len(data)+1), work_wires=[])\n",
"# qml.Adder(2**(len(data)+1)-1, data+[block[1]], 2**(len(data)+1), work_wires=[])\n",
"# qml.ctrl(reflect_around_zero(data), control=block[2])(data)\n",
"# qml.Hadamard(wires=block[0])\n",
"# qml.Hadamard(wires=block[2])\n",
"# return circuit\n",
"\n",
"# def apply_projector_controlled_phase(phase, block_reg, aux_reg):\n",
"# def circuit():\n",
"# qml.ctrl(qml.PauliX, control=block_reg, control_values=[0]*len(block_reg))(wires=aux_reg)\n",
"# qml.RZ(phase, wires=aux_reg)\n",
"# qml.ctrl(qml.PauliX, control=block_reg, control_values=[0]*len(block_reg))(wires=aux_reg)\n",
"# return circuit\n",
"\n",
"# def apply_qsvt_step(phase1, phase2, u, data, block, qsvt_aux):\n",
"# def circuit():\n",
"# u()\n",
"# apply_projector_controlled_phase(phase1, block, qsvt_aux)()\n",
"# qml.adjoint(u)()\n",
"# apply_projector_controlled_phase(phase2, block, qsvt_aux)()\n",
"# return circuit\n",
"\n",
"# def get_qsvt_circuit(qsvt_phases, size):\n",
"# dev = qml.device(\"lightning.qubit\", wires=size + 3)\n",
"\n",
"# @qml.qnode(dev)\n",
"# def qsvt_circuit():\n",
"# block = [size, size+1, size+2]\n",
"# qsvt_aux = size + 3\n",
"# data = list(range(size))\n",
"\n",
"# cir_be = get_cir_be(data, block)\n",
"# qml.Hadamard(wires=qsvt_aux)\n",
"# apply_projector_controlled_phase(qsvt_phases[0], block, qsvt_aux)()\n",
"# for i in range((len(qsvt_phases) - 1) // 2):\n",
"# apply_qsvt_step(qsvt_phases[2 * i + 1], qsvt_phases[2 * i + 2], cir_be , data, block, qsvt_aux)()\n",
"\n",
"# cir_be()\n",
"# apply_projector_controlled_phase(qsvt_phases[-1], block, qsvt_aux)()\n",
"# qml.Hadamard(wires=qsvt_aux)\n",
"\n",
"# return qsvt_circuit"
]
},
{
"cell_type": "markdown",
"id": "e7c9ca72-4d20-4c49-ae2f-95c2353f5f35",
"metadata": {},
"source": [
"## run an example"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "2f04231d-aa83-40d0-80d2-c39116528933",
"metadata": {},
"outputs": [],
"source": [
"# SIZE = 6\n",
"# DEGREE = 3\n",
"# QSVT_PHASES = [1.280311896404252, 8.127145628464149, 1.8439603212845617, -5.002873410775335]\n",
"\n",
"\n",
"# start_time = time.time()\n",
"# qsvt_cir = get_qsvt_circuit(QSVT_PHASES, SIZE)\n",
"# cir = qml.transforms.decompose(qsvt_cir, gate_set={qml.CNOT, qml.RZ, qml.RY, qml.RX})\n",
"# jitted_cir = qjit(cir)\n",
"# transpilation_time = time.time()-start_time\n",
"# cx_counts = jitted_cir.mlir.count(\"CNOT\")\n",
"# print(f\"==== pennylane for {SIZE}==== time: {transpilation_time}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b9ce9f97-168d-4b4e-9ad5-e06b26ce4445",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 26e0799

Please sign in to comment.