|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "e768812e", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "Stein's algorithm for Greatest Common Divisor, adapted for implementation on a quantum computer.\n", |
| 9 | + "\n", |
| 10 | + "Source: [this paper](https://arxiv.org/abs/1304.7516)." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 1, |
| 16 | + "id": "b7ab54fe", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [ |
| 19 | + { |
| 20 | + "name": "stdout", |
| 21 | + "output_type": "stream", |
| 22 | + "text": [ |
| 23 | + "Exhaustive tests for N=1 OK\n", |
| 24 | + "Exhaustive tests for N=2 OK\n", |
| 25 | + "Exhaustive tests for N=3 OK\n", |
| 26 | + "Exhaustive tests for N=4 OK\n", |
| 27 | + "Exhaustive tests for N=5 OK\n", |
| 28 | + "Exhaustive tests for N=6 OK\n", |
| 29 | + "Exhaustive tests for N=7 OK\n", |
| 30 | + "Exhaustive tests for N=8 OK\n" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "ename": "NameError", |
| 35 | + "evalue": "name 'gcd_stein2' is not defined", |
| 36 | + "output_type": "error", |
| 37 | + "traceback": [ |
| 38 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 39 | + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", |
| 40 | + "Cell \u001b[0;32mIn[1], line 40\u001b[0m\n\u001b[1;32m 38\u001b[0m b \u001b[38;5;241m=\u001b[39m random\u001b[38;5;241m.\u001b[39mrandint(\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mN\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m 39\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m gcd_stein(N,a,b) \u001b[38;5;241m==\u001b[39m math\u001b[38;5;241m.\u001b[39mgcd(a,b)\n\u001b[0;32m---> 40\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[43mgcd_stein2\u001b[49m(a,b) \u001b[38;5;241m==\u001b[39m math\u001b[38;5;241m.\u001b[39mgcd(a,b)\n\u001b[1;32m 41\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRandom tests for N=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mN\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m OK\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 43\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOK\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", |
| 41 | + "\u001b[0;31mNameError\u001b[0m: name 'gcd_stein2' is not defined" |
| 42 | + ] |
| 43 | + } |
| 44 | + ], |
| 45 | + "source": [ |
| 46 | + "def gcd_stein(N,a,b):\n", |
| 47 | + " assert 0 <= a < 2**N\n", |
| 48 | + " assert 0 < b < 2**N\n", |
| 49 | + " \n", |
| 50 | + " r = 1\n", |
| 51 | + " for i in range(2*N-3):\n", |
| 52 | + " a_is_not_zero = (a!=0)\n", |
| 53 | + " a_is_even = (a%2==0)\n", |
| 54 | + " b_is_even = (b%2==0) \n", |
| 55 | + " if a_is_not_zero and b_is_even:\n", |
| 56 | + " b//=2\n", |
| 57 | + " if a_is_not_zero and a_is_even:\n", |
| 58 | + " a//=2\n", |
| 59 | + " a_less_b = (a<b)\n", |
| 60 | + " if a_is_not_zero and a_less_b:\n", |
| 61 | + " a,b=b,a\n", |
| 62 | + " assert(a==0 or a>=b)\n", |
| 63 | + " if (not a_is_even) and (not b_is_even):\n", |
| 64 | + " a -= b\n", |
| 65 | + " a //= 2\n", |
| 66 | + " if a_is_not_zero and a_is_even and b_is_even:\n", |
| 67 | + " r *= 2\n", |
| 68 | + " assert r<2**N\n", |
| 69 | + " return b * r\n", |
| 70 | + " \n", |
| 71 | + "import math\n", |
| 72 | + "import random\n", |
| 73 | + "\n", |
| 74 | + "for N in [1,2,3,4,5,6,7,8]:\n", |
| 75 | + " for a in range(0, 2**N):\n", |
| 76 | + " for b in range(1, 2**N):\n", |
| 77 | + " assert gcd_stein(N,a,b) == math.gcd(a,b)\n", |
| 78 | + " print(f\"Exhaustive tests for N={N} OK\")\n", |
| 79 | + " \n", |
| 80 | + "for N in [9,10,16,32,64,128]:\n", |
| 81 | + " for _ in range(1000):\n", |
| 82 | + " a = random.randint(0, 2**N-1)\n", |
| 83 | + " b = random.randint(1, 2**N-1)\n", |
| 84 | + " assert gcd_stein(N,a,b) == math.gcd(a,b)\n", |
| 85 | + " assert gcd_stein2(a,b) == math.gcd(a,b)\n", |
| 86 | + " print(f\"Random tests for N={N} OK\")\n", |
| 87 | + " \n", |
| 88 | + "print(\"OK\")" |
| 89 | + ] |
| 90 | + } |
| 91 | + ], |
| 92 | + "metadata": { |
| 93 | + "kernelspec": { |
| 94 | + "display_name": "Python 3 (ipykernel)", |
| 95 | + "language": "python", |
| 96 | + "name": "python3" |
| 97 | + }, |
| 98 | + "language_info": { |
| 99 | + "codemirror_mode": { |
| 100 | + "name": "ipython", |
| 101 | + "version": 3 |
| 102 | + }, |
| 103 | + "file_extension": ".py", |
| 104 | + "mimetype": "text/x-python", |
| 105 | + "name": "python", |
| 106 | + "nbconvert_exporter": "python", |
| 107 | + "pygments_lexer": "ipython3", |
| 108 | + "version": "3.12.3" |
| 109 | + } |
| 110 | + }, |
| 111 | + "nbformat": 4, |
| 112 | + "nbformat_minor": 5 |
| 113 | +} |
0 commit comments