Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

project #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
70 changes: 70 additions & 0 deletions Classical_karatsuba.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def classical_karatsuba_square(v: int) -> int:\n",
" n = v.bit_length()\n",
" if n <= 32:\n",
" # Base case: Use standard multiplication\n",
" return v ** 2\n",
"\n",
" pivot = n >> 1\n",
" low = v & ~((-1) << pivot)\n",
" high = v >> pivot\n",
"\n",
" low_sq = classical_karatsuba_square(low)\n",
" high_sq = classical_karatsuba_square(high)\n",
" sum_sq = classical_karatsuba_square(low + high)\n",
"\n",
" return (\n",
" low_sq\n",
" + (sum_sq - low_sq - high_sq) << pivot\n",
" + high_sq << (pivot << 1)\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"152399025\n"
]
}
],
"source": [
"result = classical_karatsuba_square(12345)\n",
"print(result)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added Final_ppt_with_Voice_recording.pptx
Binary file not shown.
35 changes: 35 additions & 0 deletions Gate_Optimised_circuit.qmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
qfunc doubly_controlled_increment(q: qbit[], ctrl1: qbit, ctrl2: qbit, aux: qbit) {
// Implementing the increment under double control manually
control (ctrl1) {
control (ctrl2) {
X(aux); // This simulates the doubly-controlled increment operation
}
}
}




qfunc qft_and_operations(q: qbit[], ctrl1: qbit, ctrl2: qbit, aux: qbit) {
// Apply QFT, followed by controlled operations, and inverse QFT
within {
qft(q);
} apply {
doubly_controlled_increment(q, ctrl1, ctrl2, aux);
}
}

qfunc main() {
q: qbit[4]; // Number of qubits for the main register
ctrl1: qbit; // First control qubit
ctrl2: qbit; // Second control qubit
aux: qbit; // Auxiliary qubit

allocate(4, q);
allocate(1, ctrl1);
allocate(1, ctrl2);
allocate(1, aux);

// Initialize and apply QFT-based operations
qft_and_operations(q, ctrl1, ctrl2, aux);
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Susana Rivera

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions PHASE CIRCUIT_X_2_MOD_N.qmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
qfunc phase_rot(x: qbit[], N: int) {
repeat (i: x.len) {
repeat (j: x.len) {
repeat (k: x.len) {
PHASE(((2 * pi) * (2 ** ((i + j) + k))) / N, x[i]);
}
}
}
}

qfunc ccmod_add(N: int, y: qbit[], c1: qbit, c2: qbit, aux: qbit) {
ctrl: qbit[];
{c1, c2} -> ctrl;
control (ctrl) {
phase_rot(y, N);
}
ctrl -> {c1, c2};
}

qfunc main(output b: qnum, output c: qbit[8], output ctrl: qbit[2], output aux: qbit) {
allocate(8, c);
allocate(1, b);
allocate(2, ctrl);
allocate(1, aux);
inplace_prepare_int(8, b);
X(ctrl[0]);
X(ctrl[1]);
within {
qft(b);
} apply {
ccmod_add(8, c, ctrl[0], ctrl[1], aux);
}
}
176 changes: 176 additions & 0 deletions Phase_circuit_code.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "a1acd08d-d3aa-41de-b75d-393a267abf13",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bfa7ea6f-3170-44af-abe1-847a3b7c1ec5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Opening: https://platform.classiq.io/circuit/6bb24848-33f9-42e9-bd0b-aea20954473f?version=0.44.0\n"
]
}
],
"source": [
"from classiq import (\n",
" PHASE,\n",
" QArray,\n",
" QBit,\n",
" CInt,\n",
" Output,\n",
" X,\n",
" allocate,\n",
" control,\n",
" create_model,\n",
" invert,\n",
" qft,\n",
" repeat,\n",
" show,\n",
" synthesize,\n",
" write_qmod,\n",
")\n",
"from classiq.qmod import *\n",
"from classiq.qmod.symbolic import pi\n",
"\n",
"@qfunc\n",
"def phase_rot(x: QArray[QBit], N: CInt) -> None:\n",
" repeat(\n",
" count=x.len,\n",
" iteration=lambda i: repeat(\n",
" count=x.len,\n",
" iteration=lambda j: repeat(\n",
" count=x.len,\n",
" iteration=lambda k: PHASE(\n",
" (2 * pi * (2 ** (i + j + k))) / N, x[i]\n",
" ),\n",
" ),\n",
" ),\n",
" )\n",
"\n",
"@qfunc\n",
"def ccmod_add(\n",
" N: CInt,\n",
" y: QArray[QBit],\n",
" c1: QBit,\n",
" c2: QBit,\n",
" aux: QBit\n",
") -> None:\n",
" ctrl = QArray(\"ctrl\")\n",
" bind([c1, c2], ctrl)\n",
" control(ctrl, lambda: phase_rot(y, N))\n",
" bind(ctrl, [c1, c2])\n",
"\n",
"@qfunc\n",
"def main(\n",
" b: Output[QNum],\n",
" c: Output[QArray[8]],\n",
" ctrl: Output[QArray[2]],\n",
" aux: Output[QBit]\n",
") -> None:\n",
" allocate(8, c)\n",
" allocate(1, b)\n",
" allocate(2, ctrl)\n",
" allocate(1, aux)\n",
"\n",
" inplace_prepare_int(8, b)\n",
" X(ctrl[0])\n",
" X(ctrl[1])\n",
"\n",
" within_apply(\n",
" lambda: qft(b),\n",
" lambda: ccmod_add(8, c, ctrl[0], ctrl[1], aux),\n",
" )\n",
"\n",
"qmod = create_model(main)\n",
"\n",
"\n",
"\n",
"qprog = synthesize(qmod)\n",
"write_qmod(qmod, \"ccmod_add_model\")\n",
"show(qprog)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c7a3348-8f77-4ca7-a89b-76db3d8febe6",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
"id": "678aa304-c42b-4dd6-8bda-0739e32bb3c2",
"metadata": {},
"outputs": [],
"source": [
"from classiq import execute\n",
"\n",
"results = execute(qprog).result()\n",
"res = results[0].value"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "cbdcb58b-75fa-4b0f-a791-425aa0f6e3be",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ExecutionDetails(vendor_format_result={}, counts={'011000000000': 2048}, counts_lsb_right=True, parsed_states={'011000000000': {'b': 0.0, 'c': [0, 0, 0, 0, 0, 0, 0, 0], 'ctrl': [1, 1], 'aux': 0.0}}, histogram=None, output_qubits_map={'b': (0,), 'c': (1, 2, 3, 4, 5, 6, 7, 8), 'ctrl': (9, 10), 'aux': (11,)}, state_vector=None, parsed_state_vector_states=None, physical_qubits_map={'b': (8,), 'c': (0, 1, 2, 3, 4, 5, 6, 7), 'ctrl': (9, 10), 'aux': (11,)}, num_shots=2048)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb56a25f-c519-4fd6-9b8c-7acf35d5054c",
"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
}
26 changes: 26 additions & 0 deletions Qubit_Optimised_circuit.qmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
qfunc controlled_rotation(q: qbit[], j: int, k: int, N: int) {
// Apply phase rotation using the formula from the diagram
PHASE((2 * pi * (2 ** (j + k))) / N, q[j]);
}

qfunc modular_exponentiation(q: qbit[], aux: qbit, N: int) {
// Implement the double loop structure for the controlled rotation
repeat (j: q.len) {
repeat (k: q.len) {
controlled_rotation(q, j, k, N);
}
}
// Apply Hadamard and Rk gates around the exponentiation logic
H(aux);
// Assuming Rk is similar to controlled rotation or a custom gate based on the diagram
controlled_rotation(q, 0, 0, N); // Simplified call
H(aux);
}

qfunc main() {
q: qbit[3]; // Number of qubits optimized
aux: qbit; // Auxiliary qubit
allocate(3, q);
allocate(1, aux);
modular_exponentiation(q, aux, 15); // Example with N=15
}
Loading