Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 47 additions & 74 deletions examples/evolutions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "21685242",
"metadata": {},
"outputs": [],
Expand All @@ -53,7 +53,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"id": "d8527d79",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -97,7 +97,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"id": "5714c87f",
"metadata": {},
"outputs": [
Expand All @@ -115,7 +115,7 @@
"include \"stdgates.inc\";\n",
"qubit[4] qb;\n",
"bit[4] cb;\n",
"gate TFIM_3q_J100_h70(time) aa,ab,ac{\n",
"gate TFIM_3q_j100_h70(time) aa,ab,ac{\n",
"\tcnot aa, ab;\n",
"\trz(2.0 * time) ab;\n",
"\tcnot aa, ab;\n",
Expand All @@ -132,7 +132,7 @@
"\n",
"gate GQSP_1_TFIM(θa,θb,θc) aa,ab,ac,ad{\n",
"\try(θa) aa;\n",
"\tctrl(1) @ TFIM_3q_J100_h70(0.1) aa, ab, ac, ad;\n",
"\tctrl(1) @ TFIM_3q_j100_h70(0.1) aa, ab, ac, ad;\n",
"\tp(θb) aa;\n",
"\try(θc) aa;\n",
"}\n",
Expand All @@ -149,7 +149,7 @@
"include \"stdgates.inc\";\n",
"qubit[4] qb;\n",
"bit[4] cb;\n",
"gate TFIM_3q_J100_h70(time) aa,ab,ac{\n",
"gate TFIM_3q_j100_h70(time) aa,ab,ac{\n",
"\tcnot aa, ab;\n",
"\trz(2.0 * time) ab;\n",
"\tcnot aa, ab;\n",
Expand All @@ -166,13 +166,13 @@
"\n",
"gate GQSP_3_TFIM(θa,θb,θc,θd,θe,θf,θg) aa,ab,ac,ad{\n",
"\try(θa) aa;\n",
"\tctrl(1) @ TFIM_3q_J100_h70(0.1) aa, ab, ac, ad;\n",
"\tctrl(1) @ TFIM_3q_j100_h70(0.1) aa, ab, ac, ad;\n",
"\tp(θb) aa;\n",
"\try(θe) aa;\n",
"\tctrl(1) @ TFIM_3q_J100_h70(0.1) aa, ab, ac, ad;\n",
"\tctrl(1) @ TFIM_3q_j100_h70(0.1) aa, ab, ac, ad;\n",
"\tp(θc) aa;\n",
"\try(θf) aa;\n",
"\tctrl(1) @ TFIM_3q_J100_h70(0.1) aa, ab, ac, ad;\n",
"\tctrl(1) @ TFIM_3q_j100_h70(0.1) aa, ab, ac, ad;\n",
"\tp(θd) aa;\n",
"\try(θg) aa;\n",
"}\n",
Expand Down Expand Up @@ -288,7 +288,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"id": "7c651bcd",
"metadata": {},
"outputs": [
Expand All @@ -300,13 +300,13 @@
"Trotter Decomposition - Two vs Multi-Hamiltonian\n",
"----------------------------------------------------\n",
"Configuration 1: Two-Hamiltonian Trotter (Suzuki)\n",
"Two-Hamiltonian: 1858 characters\n",
"Two-Hamiltonian: 1864 characters\n",
"\tMethod: Suzuki-Trotter, Depth: 2\n",
"OPENQASM 3;\n",
"include \"stdgates.inc\";\n",
"qubit[3] qb;\n",
"bit[3] cb;\n",
"gate TFIM_3q_J100_h70(time) aa,ab,ac{\n",
"gate TFIM_3q_j100_h70(time) aa,ab,ac{\n",
"\tcnot aa, ab;\n",
"\trz(2.0 * time) ab;\n",
"\tcnot aa, ab;\n",
Expand All @@ -321,7 +321,7 @@
"\trx(1.4 * time) ac;\n",
"}\n",
"\n",
"gate HeisenbergXYZ_3q_Jx100_Jy120_Jz80(time) aa,ab,ac{\n",
"gate HeisenbergXYZ_3q_j_x100_j_y120_j_z80(time) aa,ab,ac{\n",
"\try(pi/2) aa;\n",
"\try(pi/2) ab;\n",
"\tcnot aa, ab;\n",
Expand Down Expand Up @@ -360,9 +360,9 @@
"\n",
"def trot_suz_3_TFIM_HeisenbergXYZ_2(qubit[3] qubits,float time,int recursion_depth) {\n",
"\tif (recursion_depth < 2){\n",
"\t\tTFIM_3q_J100_h70(time/2) qb[qubits[0]],qb[qubits[1]],qb[qubits[2]];\n",
"\t\tHeisenbergXYZ_3q_Jx100_Jy120_Jz80(time) qb[qubits[0]],qb[qubits[1]],qb[qubits[2]];\n",
"\t\tTFIM_3q_J100_h70(time/2) qb[qubits[0]],qb[qubits[1]],qb[qubits[2]];\n",
"\t\tTFIM_3q_j100_h70(time/2) qb[qubits[0]],qb[qubits[1]],qb[qubits[2]];\n",
"\t\tHeisenbergXYZ_3q_j_x100_j_y120_j_z80(time) qb[qubits[0]],qb[qubits[1]],qb[qubits[2]];\n",
"\t\tTFIM_3q_j100_h70(time/2) qb[qubits[0]],qb[qubits[1]],qb[qubits[2]];\n",
"\t\treturn;\n",
"\t}\n",
"\tfloat suzuki_coeff = 1.0/(4.0 - pow(4.0, 1.0/(2.0*recursion_depth - 1.0)));\n",
Expand All @@ -377,16 +377,16 @@
"\n",
"\n",
"Configuration 2: Multi-Hamiltonian Trotter\n",
"Multi-Hamiltonian: 3342 characters\n",
"Multi-Hamiltonian: 3351 characters\n",
"\tHamiltonians: 3, Depth: 2\n",
"\n",
"Configuration 3: Linear Trotter Decomposition\n",
"Linear Trotter: 1769 characters\n",
"Linear Trotter: 1784 characters\n",
"Method: First-order, Steps: 4\n",
"Trotter Configuration Comparison:\n",
"\tTwo-Hamiltonian (Suzuki): 1858 chars\n",
"\tMulti-Hamiltonian: 3342 chars\n",
"\tLinear decomposition: 1769 chars\n"
"\tTwo-Hamiltonian (Suzuki): 1864 chars\n",
"\tMulti-Hamiltonian: 3351 chars\n",
"\tLinear decomposition: 1784 chars\n"
]
}
],
Expand Down Expand Up @@ -492,7 +492,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"id": "4b4b3bff",
"metadata": {},
"outputs": [
Expand All @@ -508,19 +508,13 @@
"[('Z', np.complex128(1+0j))]\n",
"Pauli-Z: 508 characters\n",
"Testing Random 4x4 matrix (4, 4)...\n",
"[('II', np.complex128(0.4604637367120007+0.838946781387169j)), ('XI', np.complex128(0.5655598377382944+0.588833075307832j)), ('XX', np.complex128(0.4238844968885312+0.3326317061016123j)), ('IX', np.complex128(0.33106499350145013+0.33007330113196753j)), ('XY', np.complex128(-0.06002262495215413+0.2992097507618375j)), ('IY', np.complex128(-0.1797796889495182+0.20356340119461624j)), ('YI', np.complex128(-0.13521719508002197+0.19794667244217146j)), ('ZX', np.complex128(0.02442078075316162-0.23360780554666702j)), ('XZ', np.complex128(-0.18057751029366081-0.148700172760367j)), ('ZY', np.complex128(0.2184605953247674+0.03821255057383305j)), ('IZ', np.complex128(-0.095147349933698+0.07351264181838879j)), ('ZI', np.complex128(-0.11616413227155017+0.02567510247519522j)), ('YY', np.complex128(-0.05126587163767765+0.10703189572053626j)), ('YZ', np.complex128(-0.09614107149005136-0.02889548280897075j)), ('YX', np.complex128(-0.0870637778551501+0.04327330864487838j)), ('ZZ', np.complex128(0.0896637111235748-0.035398963079813106j))]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Random 4x4: 2019 characters\n",
"[('IX', np.complex128(0.6312692451983448+0.7062795340748755j)), ('II', np.complex128(0.3312704466731056+0.6543110856138611j)), ('XX', np.complex128(0.48885549810002155+0.46708079988828466j)), ('XI', np.complex128(0.5064276643525388+0.42560849024074227j)), ('YI', np.complex128(-0.2209432140524214-0.32680593560317794j)), ('XY', np.complex128(0.3051770998894734-0.08729195918180288j)), ('YY', np.complex128(0.22903059831001216-0.17048096768847246j)), ('ZZ', np.complex128(-0.12674613849154037+0.09991843038070033j)), ('ZI', np.complex128(-0.14733787825849548+0.023772954678058178j)), ('IY', np.complex128(-0.13339759254293293-0.04272733136592735j)), ('IZ', np.complex128(-0.018907216413733607-0.13091360941091704j)), ('ZX', np.complex128(0.007991339043142087-0.10180140392762566j)), ('ZY', np.complex128(-0.05995761592784832-0.0542893312281765j)), ('XZ', np.complex128(-0.016121493337997672+0.07795486151092063j)), ('YZ', np.complex128(0.06407397205381465+0.006693388785256826j)), ('YX', np.complex128(0.03553879879197233-0.04432680229575253j))]\n",
"Random 4x4: 2023 characters\n",
"\n",
"Configuration 2: Operator Chain Input\n",
"Testing Single Pauli chain (3 operators)...\n",
"[('X', 0.5), ('Z', 0.3), ('Y', 0.2)]\n",
"\tSingle Pauli: 719 characters\n",
"\tSingle Pauli: 717 characters\n",
"\tOperators: ['X', 'Z', 'Y']\n",
"OPENQASM 3;\n",
"include \"stdgates.inc\";\n",
Expand All @@ -545,7 +539,7 @@
"\ty aa;\n",
"}\n",
"\n",
"gate SEL_8998145479025848162 aa,ab,ac,ad{\n",
"gate SEL_247308947824619046 aa,ab,ac,ad{\n",
"\tctrl(2) @ X aa,ab,ac,ad;\n",
"\tx aa;\n",
"\tctrl(2) @ Z aa,ab,ac,ad;\n",
Expand All @@ -555,13 +549,13 @@
"\tx ab;\n",
"}\n",
"\n",
"gate PS_2_8654309695166846104 aa,ab,ac,ad{\n",
"gate PS_2_8139876655021836051 aa,ab,ac,ad{\n",
"\tPREP_3905172865891942725 aa,ab;\n",
"\tSEL_8998145479025848162 aa,ab,ac,ad;\n",
"\tSEL_247308947824619046 aa,ab,ac,ad;\n",
"\tinv @ PREP_3905172865891942725 aa,ab;\n",
"}\n",
"\n",
"PS_2_8654309695166846104 qb[3],qb[4],qb[0],qb[1];\n",
"PS_2_8139876655021836051 qb[3],qb[4],qb[0],qb[1];\n",
"cb[{3, 4}] = measure qb[{3, 4}];\n",
"cb[{0, 1, 2, 3}] = measure qb[{0, 1, 2, 3}];\n",
"\n",
Expand All @@ -573,9 +567,9 @@
"Prep-Select Configuration Comparison:\n",
"Matrix inputs:\n",
"\tPauli-Z: 508 chars\n",
"\tRandom 4x4: 2019 chars\n",
"\tRandom 4x4: 2023 chars\n",
"Operator chains:\n",
"\tSingle Pauli: 719 chars\n",
"\tSingle Pauli: 717 chars\n",
"\tTwo-qubit Pauli: 756 chars\n"
]
}
Expand Down Expand Up @@ -681,7 +675,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "a44b4a64",
"metadata": {},
"outputs": [
Expand All @@ -698,10 +692,10 @@
" Step 2: Applying GQSP...\n",
" Step 3: Applying prep-select...\n",
"[('Z', np.complex128(1+0j))]\n",
"Integrated pipeline: 2465 characters\n",
" Contains Trotter: True\n",
" Contains GQSP: True\n",
" Contains PrepSelect: True\n"
"Integrated pipeline: 2471 characters\n",
"\tContains Trotter: True\n",
"\tContains GQSP: True\n",
"\tContains PrepSelect: True\n"
]
}
],
Expand Down Expand Up @@ -789,40 +783,19 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 7,
"id": "22dfb86d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OPENQASM 3;\n",
"include \"stdgates.inc\";\n",
"qubit[3] qb;\n",
"gate Z_on_two3 aa,ab,ac{\n",
"\tx ac;\n",
"\tctrl(2) @ z aa, ab, ac;\n",
"\tx ac;\n",
"}\n",
"\n",
"def Grover3Z_on_two3(qubit[3] reg) {\n",
"\th reg;\n",
"\tfor int i in [0:2] {\n",
"\t\t//Za\n",
"\t\tZ_on_two3 reg[0], reg[1], reg[2];\n",
"\t\th reg;\n",
"\t\t//Z0\n",
"\t\tx reg;\n",
"\t\tctrl(2) @ z reg[0], reg[1], reg[0];\n",
"\t\tx reg;\n",
"\t\th reg;\n",
"\t}\n",
"}\n",
"\n",
"input angle[32] theta ;\n",
"Grover3Z_on_two3(qb[{0 ,1 ,2}]);\n",
"\n"
"ename": "TypeError",
"evalue": "GateLibrary.add_var() got an unexpected keyword argument 'type'. Did you mean 'qtype'?",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mTypeError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[7]\u001b[39m\u001b[32m, line 51\u001b[39m\n\u001b[32m 47\u001b[39m \u001b[38;5;28mself\u001b[39m.controlled_op(\u001b[38;5;28mself\u001b[39m.name, (qubits[-\u001b[32m1\u001b[39m], [control] + qubits[:-\u001b[32m1\u001b[39m]))\n\u001b[32m 50\u001b[39m \u001b[38;5;66;03m# Define input parameter\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m51\u001b[39m theta = \u001b[43mprogram\u001b[49m\u001b[43m.\u001b[49m\u001b[43madd_var\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtheta\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mtype\u001b[39;49m\u001b[43m=\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43minput angle[32]\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m 53\u001b[39m \u001b[38;5;66;03m# Apply Grover with custom gate\u001b[39;00m\n\u001b[32m 54\u001b[39m ampl.grover(Za, reg, \u001b[32m3\u001b[39m)\n",
"\u001b[31mTypeError\u001b[39m: GateLibrary.add_var() got an unexpected keyword argument 'type'. Did you mean 'qtype'?"
]
}
],
Expand Down Expand Up @@ -901,7 +874,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -915,7 +888,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down
522 changes: 522 additions & 0 deletions examples/qaoa.ipynb

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions qbraid_algorithms/qaoa/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2025 qBraid
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Quantum Approximate Optimization Algorithm (QAOA) used to define cost and mixer Hamiltonians
and generate QASM programs accordingly.
"""
from .qaoa import QAOA

__all__ = ["QAOA"]
Loading