Skip to content

Commit 71dd71b

Browse files
create controlled_modular_multiplcation.ipynb
1 parent 3a458fe commit 71dd71b

File tree

2 files changed

+141
-288
lines changed

2 files changed

+141
-288
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "715a6b6b",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"#controlled modular multiplcation: https://arxiv.org/abs/quant-ph/0205095"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 1,
16+
"id": "5e9b4bfc",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Note: This can be any hub/group/project that has access to the required device and the Qiskit runtime.\n",
21+
"hub = \"ibm-q\"\n",
22+
"group = \"open\"\n",
23+
"project = \"main\"\n",
24+
"backend_name = \"ibm_brisbane\"\n",
25+
"hgp = f\"{hub}/{group}/{project}\""
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 2,
31+
"id": "e5182d47",
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"#fetch api_key\n",
36+
"with open(\"api_key.txt\",\"r\") as f:\n",
37+
" api_key = f.read()"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 3,
43+
"id": "c5a74e91",
44+
"metadata": {},
45+
"outputs": [
46+
{
47+
"name": "stdout",
48+
"output_type": "stream",
49+
"text": [
50+
"Using backend ibm_brisbane\n"
51+
]
52+
}
53+
],
54+
"source": [
55+
"import qiskit\n",
56+
"from qiskit_ibm_provider import IBMProvider\n",
57+
"\n",
58+
"# Save token if necessary\n",
59+
"IBMProvider.save_account(overwrite=True,token=api_key)\n",
60+
"\n",
61+
"# Get our backend\n",
62+
"provider = IBMProvider()\n",
63+
"available_backends = provider.backends()\n",
64+
"backend = provider.get_backend(backend_name, instance=hgp)\n",
65+
"print(f\"Using backend {backend.name}\")"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 5,
71+
"id": "1bb29eee",
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"from qiskit import QuantumCircuit, transpile"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 4,
81+
"id": "d63cbeb5",
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"def c_amod15(a, power):\n",
86+
" \"\"\"Controlled multiplication by a mod 15\"\"\"\n",
87+
" if a not in [2,4,7,8,11,13]:\n",
88+
" raise ValueError(\"'a' must be 2,4,7,8,11 or 13\")\n",
89+
" U = QuantumCircuit(4)\n",
90+
" for _iteration in range(power):\n",
91+
" if a in [2,13]:\n",
92+
" U.swap(2,3)\n",
93+
" U.swap(1,2)\n",
94+
" U.swap(0,1)\n",
95+
" if a in [7,8]:\n",
96+
" U.swap(0,1)\n",
97+
" U.swap(1,2)\n",
98+
" U.swap(2,3)\n",
99+
" if a in [4, 11]:\n",
100+
" U.swap(1,3)\n",
101+
" U.swap(0,2)\n",
102+
" if a in [7,11,13]:\n",
103+
" for q in range(4):\n",
104+
" U.x(q)\n",
105+
" U = U.to_gate()\n",
106+
" U.name = f\"{a}^{power} mod 15\"\n",
107+
" c_U = U.control()\n",
108+
" return c_U"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": null,
114+
"id": "3ff7c8d5",
115+
"metadata": {},
116+
"outputs": [],
117+
"source": []
118+
}
119+
],
120+
"metadata": {
121+
"kernelspec": {
122+
"display_name": "Python 3 (ipykernel)",
123+
"language": "python",
124+
"name": "python3"
125+
},
126+
"language_info": {
127+
"codemirror_mode": {
128+
"name": "ipython",
129+
"version": 3
130+
},
131+
"file_extension": ".py",
132+
"mimetype": "text/x-python",
133+
"name": "python",
134+
"nbconvert_exporter": "python",
135+
"pygments_lexer": "ipython3",
136+
"version": "3.7.9"
137+
}
138+
},
139+
"nbformat": 4,
140+
"nbformat_minor": 5
141+
}

0 commit comments

Comments
 (0)