Skip to content

Commit 659304d

Browse files
authored
Merge pull request #369 from Takishima/release/0.5.1
ProjectQ v0.5.1
2 parents f4a82f2 + 1920531 commit 659304d

31 files changed

+2294
-344
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ instance/
7171

7272
# Sphinx documentation
7373
docs/_build/
74+
docs/_doc_gen/
7475
docs/doxygen
7576

7677
# PyBuilder

README.rst

+26-4
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,35 @@ Instead of simulating a quantum program, one can use our resource counter (as a
102102
103103
**Running a quantum program on IBM's QE chips**
104104

105-
To run a program on the IBM Quantum Experience chips, all one has to do is choose the `IBMBackend` and the corresponding compiler:
105+
To run a program on the IBM Quantum Experience chips, all one has to do is choose the `IBMBackend` and the corresponding setup:
106106

107107
.. code-block:: python
108108
109-
compiler_engines = projectq.setups.ibm16.get_engine_list()
110-
eng = MainEngine(IBMBackend(use_hardware=True, num_runs=1024,
111-
verbose=False, device='ibmqx5'),
109+
import projectq.setups.ibm
110+
from projectq.backends import IBMBackend
111+
112+
token='MY_TOKEN'
113+
device='ibmq_16_melbourne'
114+
compiler_engines = projectq.setups.ibm.get_engine_list(token=token,device=device)
115+
eng = MainEngine(IBMBackend(token=token, use_hardware=True, num_runs=1024,
116+
verbose=False, device=device),
117+
engine_list=compiler_engines)
118+
119+
120+
**Running a quantum program on AQT devices**
121+
122+
To run a program on the AQT trapped ion quantum computer, choose the `AQTBackend` and the corresponding setup:
123+
124+
.. code-block:: python
125+
126+
import projectq.setups.aqt
127+
from projectq.backends import AQTBackend
128+
129+
token='MY_TOKEN'
130+
device='aqt_device'
131+
compiler_engines = projectq.setups.aqt.get_engine_list(token=token,device=device)
132+
eng = MainEngine(AQTBackend(token=token,use_hardware=True, num_runs=1024,
133+
verbose=False, device=device),
112134
engine_list=compiler_engines)
113135
114136

docs/conf.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
#!/usr/bin/env python3
23
# -*- coding: utf-8 -*-
34
#
@@ -28,7 +29,6 @@
2829
import projectq.setups.default
2930
import projectq.setups.grid
3031
import projectq.setups.ibm
31-
import projectq.setups.ibm16
3232
import projectq.setups.linear
3333
import projectq.setups.restrictedgateset
3434
import projectq.setups.decompositions
@@ -527,9 +527,11 @@ def linkcode_resolve(domain, info):
527527
# ------------------------------------------------------------------------------
528528
# Automatically generate ReST files for each package of ProjectQ
529529

530+
docgen_path = os.path.join(os.path.dirname(os.path.abspath('__file__')),
531+
'_doc_gen')
532+
os.mkdir(docgen_path)
530533
for desc in descriptions:
531-
fname = os.path.join(os.path.dirname(os.path.abspath('__file__')),
532-
'projectq.{}.rst'.format(desc.name))
534+
fname = os.path.join(docgen_path, 'projectq.{}.rst'.format(desc.name))
533535
lines = None
534536
if os.path.exists(fname):
535537
with open(fname, 'r') as fd:

docs/package_description.py

+23-28
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
class PackageDescription(object):
77
package_list = []
88

9-
def __init__(self, name, desc='', module_special_members='__init__',
10-
submodule_special_members='', submodules_desc='',
9+
def __init__(self,
10+
pkg_name,
11+
desc='',
12+
module_special_members='__init__',
13+
submodule_special_members='',
14+
submodules_desc='',
1115
helper_submodules=None):
1216
"""
1317
Args:
@@ -25,24 +29,24 @@ def __init__(self, name, desc='', module_special_members='__init__',
2529
automodule_properties)
2630
"""
2731

28-
self.name = name
32+
self.name = pkg_name
2933
self.desc = desc
30-
if name not in PackageDescription.package_list:
31-
PackageDescription.package_list.append(name)
34+
if pkg_name not in PackageDescription.package_list:
35+
PackageDescription.package_list.append(pkg_name)
3236

3337
self.module = sys.modules['projectq.{}'.format(self.name)]
3438
self.module_special_members = module_special_members
3539

36-
self.submodule_special_members = module_special_members
40+
self.submodule_special_members = submodule_special_members
3741
self.submodules_desc = submodules_desc
3842

3943
self.helper_submodules = helper_submodules
4044

4145
module_root = os.path.dirname(self.module.__file__)
4246
sub = [(name, obj) for name, obj in inspect.getmembers(
43-
self.module,
44-
lambda obj: inspect.ismodule(obj) and module_root in obj.__file__)
45-
if name[0] != '_']
47+
self.module, lambda obj: inspect.ismodule(obj) and hasattr(
48+
obj, '__file__') and module_root in obj.__file__)
49+
if pkg_name[0] != '_']
4650

4751
self.subpackages = []
4852
self.submodules = []
@@ -56,19 +60,10 @@ def __init__(self, name, desc='', module_special_members='__init__',
5660
self.subpackages.sort(key=lambda x: x[0].lower())
5761
self.submodules.sort(key=lambda x: x[0].lower())
5862

59-
self.members = [(name, obj)
60-
for name, obj in inspect.getmembers(
61-
self.module,
62-
lambda obj: (inspect.isclass(obj)
63-
or inspect.isfunction(obj)
64-
or isinstance(obj, (int,
65-
float,
66-
tuple,
67-
list,
68-
dict,
69-
set,
70-
frozenset,
71-
str))))
63+
self.members = [(name, obj) for name, obj in inspect.getmembers(
64+
self.module, lambda obj:
65+
(inspect.isclass(obj) or inspect.isfunction(obj) or isinstance(
66+
obj, (int, float, tuple, list, dict, set, frozenset, str))))
7267
if name[0] != '_']
7368
self.members.sort(key=lambda x: x[0].lower())
7469

@@ -100,13 +95,13 @@ def get_ReST(self):
10095
new_lines.append('')
10196
if self.submodules:
10297
for name, _ in self.submodules:
103-
new_lines.append('\tprojectq.{}.{}'.format(self.name,
104-
name))
98+
new_lines.append('\tprojectq.{}.{}'.format(
99+
self.name, name))
105100
new_lines.append('')
106101
if self.members:
107102
for name, _ in self.members:
108-
new_lines.append('\tprojectq.{}.{}'.format(self.name,
109-
name))
103+
new_lines.append('\tprojectq.{}.{}'.format(
104+
self.name, name))
110105
new_lines.append('')
111106

112107
if self.submodules:
@@ -121,8 +116,8 @@ def get_ReST(self):
121116
new_lines.append('.. autosummary::')
122117
new_lines.append('')
123118
for name, _ in self.submodules:
124-
new_lines.append(' projectq.{}.{}'.format(self.name,
125-
name))
119+
new_lines.append(' projectq.{}.{}'.format(
120+
self.name, name))
126121
new_lines.append('')
127122

128123
for name, _ in self.submodules:

docs/projectq.rst

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ For a detailed documentation of a subpackage or module, click on its name below:
1111
:maxdepth: 1
1212
:titlesonly:
1313

14-
projectq.backends
15-
projectq.cengines
16-
projectq.libs
17-
projectq.meta
18-
projectq.ops
19-
projectq.setups
20-
projectq.types
14+
_doc_gen/projectq.backends
15+
_doc_gen/projectq.cengines
16+
_doc_gen/projectq.libs
17+
_doc_gen/projectq.meta
18+
_doc_gen/projectq.ops
19+
_doc_gen/projectq.setups
20+
_doc_gen/projectq.types
2121

2222

examples/aqt.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import matplotlib.pyplot as plt
2+
import getpass
3+
4+
from projectq import MainEngine
5+
from projectq.backends import AQTBackend
6+
from projectq.libs.hist import histogram
7+
from projectq.ops import Measure, Entangle, All
8+
import projectq.setups.aqt
9+
10+
11+
def run_entangle(eng, num_qubits=3):
12+
"""
13+
Runs an entangling operation on the provided compiler engine.
14+
15+
Args:
16+
eng (MainEngine): Main compiler engine to use.
17+
num_qubits (int): Number of qubits to entangle.
18+
19+
Returns:
20+
measurement (list<int>): List of measurement outcomes.
21+
"""
22+
# allocate the quantum register to entangle
23+
qureg = eng.allocate_qureg(num_qubits)
24+
25+
# entangle the qureg
26+
Entangle | qureg
27+
28+
# measure; should be all-0 or all-1
29+
All(Measure) | qureg
30+
31+
# run the circuit
32+
eng.flush()
33+
34+
# access the probabilities via the back-end:
35+
# results = eng.backend.get_probabilities(qureg)
36+
# for state in results:
37+
# print("Measured {} with p = {}.".format(state, results[state]))
38+
# or plot them directly:
39+
histogram(eng.backend, qureg)
40+
plt.show()
41+
42+
# return one (random) measurement outcome.
43+
return [int(q) for q in qureg]
44+
45+
46+
if __name__ == "__main__":
47+
#devices available to subscription:
48+
# aqt_simulator (11 qubits)
49+
# aqt_simulator_noise (11 qubits)
50+
# aqt_device (4 qubits)
51+
#
52+
# To get a subscription, create a profile at :
53+
# https://gateway-portal.aqt.eu/
54+
#
55+
device = None # replace by the AQT device name you want to use
56+
token = None # replace by the token given by AQT
57+
if token is None:
58+
token = getpass.getpass(prompt='AQT token > ')
59+
if device is None:
60+
device = getpass.getpass(prompt='AQT device > ')
61+
# create main compiler engine for the AQT back-end
62+
eng = MainEngine(AQTBackend(use_hardware=True, token=token, num_runs=200,
63+
verbose=False, device=device),
64+
engine_list=projectq.setups.aqt.get_engine_list(
65+
token=token, device=device))
66+
# run the circuit and print the result
67+
print(run_entangle(eng))

examples/bellpair_circuit.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1+
import matplotlib.pyplot as plt
2+
13
from projectq import MainEngine
24
from projectq.backends import CircuitDrawer
5+
from projectq.setups.default import get_engine_list
6+
from projectq.libs.hist import histogram
37

48
from teleport import create_bell_pair
59

610
# create a main compiler engine
711
drawing_engine = CircuitDrawer()
8-
eng = MainEngine(drawing_engine)
12+
eng = MainEngine(engine_list = get_engine_list() + [drawing_engine])
913

10-
create_bell_pair(eng)
14+
qb0, qb1 = create_bell_pair(eng)
1115

1216
eng.flush()
1317
print(drawing_engine.get_latex())
18+
19+
histogram(eng.backend, [qb0, qb1])
20+
plt.show()

examples/ibm.py

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import projectq.setups.ibm
1+
import matplotlib.pyplot as plt
2+
import getpass
3+
4+
from projectq import MainEngine
25
from projectq.backends import IBMBackend
6+
from projectq.libs.hist import histogram
37
from projectq.ops import Measure, Entangle, All
4-
from projectq import MainEngine
5-
import getpass
8+
import projectq.setups.ibm
69

710

811
def run_entangle(eng, num_qubits=3):
@@ -29,25 +32,33 @@ def run_entangle(eng, num_qubits=3):
2932
eng.flush()
3033

3134
# access the probabilities via the back-end:
32-
results = eng.backend.get_probabilities(qureg)
33-
for state in results:
34-
print("Measured {} with p = {}.".format(state, results[state]))
35+
# results = eng.backend.get_probabilities(qureg)
36+
# for state in results:
37+
# print("Measured {} with p = {}.".format(state, results[state]))
38+
# or plot them directly:
39+
histogram(eng.backend, qureg)
40+
plt.show()
3541

3642
# return one (random) measurement outcome.
3743
return [int(q) for q in qureg]
3844

3945

4046
if __name__ == "__main__":
4147
#devices commonly available :
42-
#ibmq_16_melbourne (15 qubit)
43-
#ibmq_essex (5 qubit)
44-
#ibmq_qasm_simulator (32 qubits)
45-
device = None #replace by the IBM device name you want to use
46-
token = None #replace by the token given by IBMQ
48+
# ibmq_16_melbourne (15 qubit)
49+
# ibmq_essex (5 qubit)
50+
# ibmq_qasm_simulator (32 qubits)
51+
# and plenty of other 5 qubits devices!
52+
#
53+
# To get a token, create a profile at:
54+
# https://quantum-computing.ibm.com/
55+
#
56+
device = None # replace by the IBM device name you want to use
57+
token = None # replace by the token given by IBMQ
4758
if token is None:
4859
token = getpass.getpass(prompt='IBM Q token > ')
4960
if device is None:
50-
token = getpass.getpass(prompt='IBM device > ')
61+
device = getpass.getpass(prompt='IBM device > ')
5162
# create main compiler engine for the IBM back-end
5263
eng = MainEngine(IBMBackend(use_hardware=True, token=token, num_runs=1024,
5364
verbose=False, device=device),

examples/ibm16.py

-43
This file was deleted.

0 commit comments

Comments
 (0)