Skip to content
This repository was archived by the owner on Apr 22, 2022. It is now read-only.

Commit 44ee3f0

Browse files
committed
add code for shor algorithm on rigetti
1 parent e7533a0 commit 44ee3f0

File tree

8 files changed

+1059
-333
lines changed

8 files changed

+1059
-333
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.idea
12
.vscode
2-
.ipynb_checkpoints
3+
.ipynb_checkpoints

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ verify_ssl = true
99
cirq = "*"
1010
qiskit = "*"
1111
jupyterlab = "*"
12+
pyquil = "*"
1213

1314
[requires]
1415
python_version = "3.7"

Pipfile.lock

Lines changed: 466 additions & 332 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shor.ipynb

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"C:\\Users\\DSB4KOR\\Desktop\\projects\\quantum_computing\\shor\n"
13+
]
14+
}
15+
],
16+
"source": [
17+
"%cd shor"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 4,
23+
"metadata": {},
24+
"outputs": [
25+
{
26+
"name": "stdout",
27+
"output_type": "stream",
28+
"text": [
29+
"^C\n",
30+
"Factoring 25\n",
31+
"y: 13\n",
32+
"c: 307; r: 10\n",
33+
"factor: 18\n",
34+
"f1: 1; f2: 1\n",
35+
"trivial factor, continuing\n",
36+
"y: 7\n",
37+
"c: 0; r: 1\n",
38+
"r odd; continuing\n",
39+
"y: 19\n",
40+
"c: 307; r: 10\n",
41+
"factor: 24\n",
42+
"trivial factor; continuing\n",
43+
"y: 9\n",
44+
"c: 0; r: 1\n",
45+
"r odd; continuing\n",
46+
"y: 6\n",
47+
"c: 819; r: 5\n",
48+
"r odd; continuing\n",
49+
"y: 19\n",
50+
"c: 820; r: 5\n",
51+
"r odd; continuing\n",
52+
"y: 19\n",
53+
"c: 717; r: 10\n",
54+
"factor: 24\n",
55+
"trivial factor; continuing\n",
56+
"y: 6\n",
57+
"c: 819; r: 5\n",
58+
"r odd; continuing\n",
59+
"y: 3\n",
60+
"c: 102; r: 10\n",
61+
"factor: 18\n",
62+
"f1: 1; f2: 1\n",
63+
"trivial factor, continuing\n",
64+
"y: 11\n",
65+
"c: 205; r: 5\n",
66+
"r odd; continuing\n",
67+
"y: 22\n",
68+
"c: 102; r: 10\n",
69+
"factor: 7\n",
70+
"f1: 1; f2: 1\n",
71+
"trivial factor, continuing\n",
72+
"y: 3\n",
73+
"c: 921; r: 10\n",
74+
"factor: 18\n",
75+
"f1: 1; f2: 1\n",
76+
"trivial factor, continuing\n",
77+
"y: 3\n",
78+
"c: 410; r: 5\n",
79+
"r odd; continuing\n",
80+
"y: 24\n",
81+
"c: 512; r: 2\n",
82+
"factor: 24\n",
83+
"trivial factor; continuing\n",
84+
"y: 9\n",
85+
"c: 410; r: 5\n",
86+
"r odd; continuing\n",
87+
"y: 13\n",
88+
"c: 973; r: 20\n",
89+
"factor: 24\n",
90+
"trivial factor; continuing\n",
91+
"y: 9\n",
92+
"c: 490; r: 23\n",
93+
"r odd; continuing\n",
94+
"y: 12\n",
95+
"c: 973; r: 20\n",
96+
"factor: 24\n",
97+
"trivial factor; continuing\n",
98+
"y: 19\n",
99+
"c: 607; r: 22\n",
100+
"factor: 19\n",
101+
"f1: 1; f2: 5\n",
102+
"Factorization: (5, 5)\n"
103+
]
104+
}
105+
],
106+
"source": [
107+
"!python shor.py 25"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": null,
113+
"metadata": {},
114+
"outputs": [],
115+
"source": []
116+
}
117+
],
118+
"metadata": {
119+
"kernelspec": {
120+
"display_name": "Python 3",
121+
"language": "python",
122+
"name": "python3"
123+
},
124+
"language_info": {
125+
"codemirror_mode": {
126+
"name": "ipython",
127+
"version": 3
128+
},
129+
"file_extension": ".py",
130+
"mimetype": "text/x-python",
131+
"name": "python",
132+
"nbconvert_exporter": "python",
133+
"pygments_lexer": "ipython3",
134+
"version": "3.7.6"
135+
}
136+
},
137+
"nbformat": 4,
138+
"nbformat_minor": 4
139+
}

shor/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# shor
2+
An implementation of Shor's factoring algorithm for the CS269Q final project at Stanford.
3+
4+
To run the algorithm, you'll need to have the latest versions of Pyquil and Numpy. Additionally, you need to use Python >= 3.6, due to the constraints of Pyquil.
5+
```
6+
pip install numpy
7+
pip install pyquil
8+
```
9+
Next, install the Rigetti QVM from the [Rigetti website](https://www.rigetti.com/forest).
10+
11+
Once you've gotten everything set up, run the QVM server with the following command:
12+
```
13+
qvm -S
14+
```
15+
16+
Now, you're ready to run Shor's algorithm. To do so, use the following command, where N is the number you want to factor.
17+
```
18+
python shor.py N
19+
```

shor/fourier.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import cmath
2+
import math
3+
4+
def f_c(c, q, r):
5+
x = 0
6+
for i in range(round(q / r)):
7+
x += cmath.exp(2 * cmath.pi * 1j * i * ((r * c) % q) / q)
8+
return x
9+
10+
def prob(c, q, r):
11+
fc = f_c(c, q, r)
12+
total = fc.real**2 + fc.imag**2
13+
return total * r / q**2

0 commit comments

Comments
 (0)