Skip to content

Commit 2e9a69a

Browse files
thomashaenerdamiansteiger
authored andcommitted
Made everything PEP8-conform. (ProjectQ-Framework#29)
* PEP8-linting
1 parent 180eb52 commit 2e9a69a

103 files changed

Lines changed: 10416 additions & 10186 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/conf.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import sphinx_rtd_theme
3434

3535
extensions = [
36-
'sphinx.ext.autodoc','sphinx.ext.napoleon', 'sphinx.ext.mathjax'
36+
'sphinx.ext.autodoc', 'sphinx.ext.napoleon', 'sphinx.ext.mathjax'
3737
]
3838

3939
# Add any paths that contain templates here, relative to this directory.
@@ -154,8 +154,8 @@
154154
# html_logo = None
155155

156156
# The name of an image file (relative to this directory) to use as a favicon of
157-
# the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
158-
# pixels large.
157+
# the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x
158+
# 32 pixels large.
159159
#
160160
# html_favicon = None
161161

@@ -247,21 +247,21 @@
247247
# -- Options for LaTeX output ---------------------------------------------
248248

249249
latex_elements = {
250-
# The paper size ('letterpaper' or 'a4paper').
251-
#
252-
# 'papersize': 'letterpaper',
250+
# The paper size ('letterpaper' or 'a4paper').
251+
#
252+
# 'papersize': 'letterpaper',
253253

254-
# The font size ('10pt', '11pt' or '12pt').
255-
#
256-
# 'pointsize': '10pt',
254+
# The font size ('10pt', '11pt' or '12pt').
255+
#
256+
# 'pointsize': '10pt',
257257

258-
# Additional stuff for the LaTeX preamble.
259-
#
260-
# 'preamble': '',
258+
# Additional stuff for the LaTeX preamble.
259+
#
260+
# 'preamble': '',
261261

262-
# Latex figure (float) alignment
263-
#
264-
# 'figure_align': 'htbp',
262+
# Latex figure (float) alignment
263+
#
264+
# 'figure_align': 'htbp',
265265
}
266266

267267
# Grouping the document tree into LaTeX files. List of tuples
@@ -294,7 +294,7 @@
294294
#
295295
# latex_appendices = []
296296

297-
# It false, will not define \strong, \code, itleref, \crossref ... but only
297+
# It false, will not define \strong, \code, itleref, \crossref ... but only
298298
# \sphinxstrong, ..., \sphinxtitleref, ... To help avoid clash with user added
299299
# packages.
300300
#

examples/grover.py

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,75 +6,75 @@
66

77

88
def run_grover(eng, n, oracle):
9-
"""
10-
Runs Grover's algorithm on n qubit using the provided quantum oracle.
11-
12-
Args:
13-
eng (MainEngine): Main compiler engine to run Grover on.
14-
n (int): Number of bits in the solution.
15-
oracle (function): Function accepting the engine, an n-qubit register, and
16-
an output qubit which is flipped by the oracle for the correct bit-
17-
string.
18-
19-
Returns:
20-
solution (list<int>): Solution bit-string.
21-
"""
22-
x = eng.allocate_qureg(n)
23-
24-
# start in uniform superposition
25-
All(H) | x
26-
27-
# number of iterations we have to run:
28-
num_it = int(math.pi/4.*math.sqrt(1 << n))
29-
30-
# prepare the oracle output qubit (the one that is flipped to indicate the
31-
# solution. start in state 1/sqrt(2) * (|0> - |1>) s.t. a bit-flip turns
32-
# into a (-1)-phase.
33-
oracle_out = eng.allocate_qubit()
34-
X | oracle_out
35-
H | oracle_out
36-
37-
# run num_it iterations
38-
with Loop(eng, num_it):
39-
# oracle adds a (-1)-phase to the solution
40-
oracle(eng, x, oracle_out)
41-
42-
# reflection across uniform superposition
43-
with Compute(eng):
44-
All(H) | x
45-
All(X) | x
46-
47-
with Control(eng, x[0:-1]):
48-
Z | x[-1]
49-
50-
Uncompute(eng)
51-
52-
Measure | x
53-
Measure | oracle_out
54-
55-
eng.flush()
56-
# return result
57-
return [int(qubit) for qubit in x]
9+
"""
10+
Runs Grover's algorithm on n qubit using the provided quantum oracle.
11+
12+
Args:
13+
eng (MainEngine): Main compiler engine to run Grover on.
14+
n (int): Number of bits in the solution.
15+
oracle (function): Function accepting the engine, an n-qubit register,
16+
and an output qubit which is flipped by the oracle for the correct
17+
bit string.
18+
19+
Returns:
20+
solution (list<int>): Solution bit-string.
21+
"""
22+
x = eng.allocate_qureg(n)
23+
24+
# start in uniform superposition
25+
All(H) | x
26+
27+
# number of iterations we have to run:
28+
num_it = int(math.pi/4.*math.sqrt(1 << n))
29+
30+
# prepare the oracle output qubit (the one that is flipped to indicate the
31+
# solution. start in state 1/sqrt(2) * (|0> - |1>) s.t. a bit-flip turns
32+
# into a (-1)-phase.
33+
oracle_out = eng.allocate_qubit()
34+
X | oracle_out
35+
H | oracle_out
36+
37+
# run num_it iterations
38+
with Loop(eng, num_it):
39+
# oracle adds a (-1)-phase to the solution
40+
oracle(eng, x, oracle_out)
41+
42+
# reflection across uniform superposition
43+
with Compute(eng):
44+
All(H) | x
45+
All(X) | x
46+
47+
with Control(eng, x[0:-1]):
48+
Z | x[-1]
49+
50+
Uncompute(eng)
51+
52+
Measure | x
53+
Measure | oracle_out
54+
55+
eng.flush()
56+
# return result
57+
return [int(qubit) for qubit in x]
5858

5959

6060
def alternating_bits_oracle(eng, qubits, output):
61-
"""
62-
Marks the solution string 1,0,1,0,...,0,1 by flipping the output qubit,
63-
conditioned on qubits being equal to the alternating bit-string.
64-
65-
Args:
66-
eng (MainEngine): Main compiler engine the algorithm is being run on.
67-
qubits (Qureg): n-qubit quantum register Grover search is run on.
68-
output (Qubit): Output qubit to flip in order to indicate the solution.
69-
"""
70-
with Compute(eng):
71-
All(X) | qubits[1::2]
72-
with Control(eng, qubits):
73-
X | output
74-
Uncompute(eng)
61+
"""
62+
Marks the solution string 1,0,1,0,...,0,1 by flipping the output qubit,
63+
conditioned on qubits being equal to the alternating bit-string.
64+
65+
Args:
66+
eng (MainEngine): Main compiler engine the algorithm is being run on.
67+
qubits (Qureg): n-qubit quantum register Grover search is run on.
68+
output (Qubit): Output qubit to flip in order to mark the solution.
69+
"""
70+
with Compute(eng):
71+
All(X) | qubits[1::2]
72+
with Control(eng, qubits):
73+
X | output
74+
Uncompute(eng)
7575

7676

7777
if __name__ == "__main__":
78-
eng = MainEngine() # use default compiler engine
79-
# run Grover search to find a 7-bit solution
80-
print(run_grover(eng, 7, alternating_bits_oracle))
78+
eng = MainEngine() # use default compiler engine
79+
# run Grover search to find a 7-bit solution
80+
print(run_grover(eng, 7, alternating_bits_oracle))

examples/ibm.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,40 @@
44

55

66
def run_entangle(eng, num_qubits=5):
7-
"""
8-
Runs an entangling operation on the provided compiler engine.
9-
10-
Args:
11-
eng (MainEngine): Main compiler engine to use.
12-
num_qubits (int): Number of qubits to entangle.
13-
14-
Returns:
15-
measurement (list<int>): List of measurement outcomes.
16-
"""
17-
# allocate the quantum register to entangle
18-
qureg = eng.allocate_qureg(num_qubits)
19-
20-
# entangle the qureg
21-
Entangle | qureg
22-
23-
# measure; should be all-0 or all-1
24-
Measure | qureg
25-
26-
# run the circuit
27-
eng.flush()
28-
29-
# access the probabilities via the back-end:
30-
results = eng.backend.get_probabilities(qureg)
31-
for state in results:
32-
print("Measured {} with p = {}.".format(state, results[state]))
33-
34-
# return one (random) measurement outcome.
35-
return [int(q) for q in qureg]
7+
"""
8+
Runs an entangling operation on the provided compiler engine.
9+
10+
Args:
11+
eng (MainEngine): Main compiler engine to use.
12+
num_qubits (int): Number of qubits to entangle.
13+
14+
Returns:
15+
measurement (list<int>): List of measurement outcomes.
16+
"""
17+
# allocate the quantum register to entangle
18+
qureg = eng.allocate_qureg(num_qubits)
19+
20+
# entangle the qureg
21+
Entangle | qureg
22+
23+
# measure; should be all-0 or all-1
24+
Measure | qureg
25+
26+
# run the circuit
27+
eng.flush()
28+
29+
# access the probabilities via the back-end:
30+
results = eng.backend.get_probabilities(qureg)
31+
for state in results:
32+
print("Measured {} with p = {}.".format(state, results[state]))
33+
34+
# return one (random) measurement outcome.
35+
return [int(q) for q in qureg]
3636

3737

3838
if __name__ == "__main__":
39-
# create main compiler engine for the IBM back-end
40-
eng = MainEngine(IBMBackend(use_hardware=True, num_runs=1024, verbose=False))
41-
# run the circuit and print the result
42-
print(run_entangle(eng))
39+
# create main compiler engine for the IBM back-end
40+
eng = MainEngine(IBMBackend(use_hardware=True, num_runs=1024,
41+
verbose=False))
42+
# run the circuit and print the result
43+
print(run_entangle(eng))

0 commit comments

Comments
 (0)