Skip to content

Commit

Permalink
Merge 5292b86 into 1411ef8
Browse files Browse the repository at this point in the history
  • Loading branch information
damiansteiger committed Jul 15, 2018
2 parents 1411ef8 + 5292b86 commit 1dce5d0
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 46 deletions.
12 changes: 5 additions & 7 deletions docs/projectq.setups.rst
@@ -1,19 +1,16 @@
setups
======

The setups package contains a collection of setups which can be loaded by the `MainEngine`. Each setup then loads its own set of decomposition rules and default compiler engines.
The setups package contains a collection of setups which can be loaded by the `MainEngine`. Each setup contains a `get_engine_list` function which returns a list of compiler engines:

Example:
.. code-block:: python
import projectq.setups.ibm
import projectq.setups.ibm as ibm_setup
from projectq import MainEngine
eng = MainEngine(setup=projectq.setups.ibm)
eng = MainEngine(engine_list=ibm_setup.get_engine_list())
# eng uses the default Simulator backend
Note:
One can either provide an `engine_list` or a `setup` to the `MainEngine` but not both.

The subpackage decompositions contains all the individual decomposition rules
which can be given to, e.g., an `AutoReplacer`.

Expand All @@ -29,7 +26,8 @@ Subpackages
Submodules
----------

Each of the submodules contains a setup which can be loaded by the `MainEngine` :
Each of the submodules contains a setup which can be used to specify the
`engine_list` used by the `MainEngine` :

.. autosummary::

Expand Down
2 changes: 1 addition & 1 deletion examples/ibm.py
Expand Up @@ -40,6 +40,6 @@ def run_entangle(eng, num_qubits=5):
# create main compiler engine for the IBM back-end
eng = MainEngine(IBMBackend(use_hardware=True, num_runs=1024,
verbose=False, device='ibmqx4'),
setup=projectq.setups.ibm)
engine_list=projectq.setups.ibm.get_engine_list())
# run the circuit and print the result
print(run_entangle(eng))
2 changes: 1 addition & 1 deletion examples/ibm16.py
Expand Up @@ -49,6 +49,6 @@ def run_test(eng):
# create main compiler engine for the 16-qubit IBM back-end
eng = MainEngine(IBMBackend(use_hardware=True, num_runs=1024,
verbose=False, device='ibmqx5'),
setup=projectq.setups.ibm16)
engine_list=projectq.setups.ibm16.get_engine_list())
# run the circuit and print the result
print(run_test(eng))
23 changes: 12 additions & 11 deletions examples/ibm_entangle.ipynb
Expand Up @@ -41,7 +41,7 @@
"outputs": [],
"source": [
"engine = MainEngine(IBMBackend(use_hardware=True, num_runs=1024, verbose=True, device='ibmqx4'),\n",
" setup=projectq.setups.ibm)\n",
" engine_list=projectq.setups.ibm.get_engine_list())\n",
"qureg = engine.allocate_qureg(3)"
]
},
Expand Down Expand Up @@ -100,14 +100,14 @@
"measure q[1] -> c[1];\n",
"- Waiting for results...\n",
"- Done.\n",
"11100 with p = 0.4033203125*\n",
"01100 with p = 0.041015625\n",
"10000 with p = 0.0185546875\n",
"00000 with p = 0.3828125\n",
"01000 with p = 0.015625\n",
"11000 with p = 0.0546875\n",
"00100 with p = 0.0419921875\n",
"10100 with p = 0.0419921875\n"
"11100 with p = 0.388671875*\n",
"01100 with p = 0.0478515625\n",
"10000 with p = 0.0029296875\n",
"00000 with p = 0.4482421875\n",
"01000 with p = 0.0166015625\n",
"11000 with p = 0.025390625\n",
"00100 with p = 0.0263671875\n",
"10100 with p = 0.0439453125\n"
]
}
],
Expand Down Expand Up @@ -163,7 +163,8 @@
"outputs": [],
"source": [
"from projectq.backends import CommandPrinter\n",
"engine2 = MainEngine(CommandPrinter(), setup=projectq.setups.ibm)"
"engine2 = MainEngine(CommandPrinter(),\n",
" engine_list=projectq.setups.ibm.get_engine_list())"
]
},
{
Expand Down Expand Up @@ -324,7 +325,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 1, 1, 1, 1]\n"
"[0, 0, 0, 0, 0]\n"
]
}
],
Expand Down
3 changes: 2 additions & 1 deletion examples/quantum_random_numbers_ibm.py
Expand Up @@ -4,7 +4,8 @@
from projectq.backends import IBMBackend

# create a main compiler engine
eng = MainEngine(IBMBackend(), setup=projectq.setups.ibm)
eng = MainEngine(IBMBackend(),
engine_list=projectq.setups.ibm.get_engine_list())

# allocate one qubit
q1 = eng.allocate_qubit()
Expand Down
34 changes: 12 additions & 22 deletions projectq/cengines/_main.py
Expand Up @@ -54,42 +54,38 @@ class MainEngine(BasicEngine):
mapper (BasicMapperEngine): Access to the mapper if there is one.
"""
def __init__(self, backend=None, engine_list=None, setup=None,
verbose=False):
def __init__(self, backend=None, engine_list=None, verbose=False):
"""
Initialize the main compiler engine and all compiler engines.
Sets 'next_engine'- and 'main_engine'-attributes of all compiler
engines and adds the back-end as the last engine.
Args:
backend (BasicEngine): Backend to send the circuit to.
backend (BasicEngine): Backend to send the compiled circuit to.
engine_list (list<BasicEngine>): List of engines / backends to use
as compiler engines. Note: The engine list must not contain
multiple mappers (instances of BasicMapperEngine).
setup (module): Setup module which defines a function called
`get_engine_list()`. `get_engine_list()` returns the list
of engines to be used as compiler engines.
The default setup is `projectq.setups.default` (if no engine
list and no setup is provided).
Default: projectq.setups.default.get_engine_list()
verbose (bool): Either print full or compact error messages.
Default: False (i.e. compact error messages).
Example:
.. code-block:: python
from projectq import MainEngine
eng = MainEngine() # uses default setup and the Simulator
eng = MainEngine() # uses default engine_list and the Simulator
Instead of the default setup one can use, e.g., one of the IBM setups
which defines a custom `engine_list` useful for one of the IBM chips
Instead of the default `engine_list` one can use, e.g., one of the IBM
setups which defines a custom `engine_list` useful for one of the IBM
chips
Example:
.. code-block:: python
import projectq.setups.ibm
import projectq.setups.ibm as ibm_setup
from projectq import MainEngine
eng = MainEngine(setup=projectq.setups.ibm)
eng = MainEngine(engine_list=ibm_setup.get_engine_list())
# eng uses the default Simulator backend
Alternatively, one can specify all compiler engines explicitly, e.g.,
Expand Down Expand Up @@ -119,17 +115,11 @@ def __init__(self, backend=None, engine_list=None, setup=None,
"Did you forget the brackets to create an instance?\n"
"E.g. MainEngine(backend=Simulator) instead of \n"
" MainEngine(backend=Simulator())")
# default setup is projectq.setups.default
if engine_list is None and setup is None:
# default engine_list is projectq.setups.default.get_engine_list()
if engine_list is None:
import projectq.setups.default
setup = projectq.setups.default

if not (engine_list is None or setup is None): # can't provide both
raise ValueError("\nPlease provide either a setup or an engine "
"list, but not both.")
engine_list = projectq.setups.default.get_engine_list()

if engine_list is None:
engine_list = setup.get_engine_list()
self.mapper = None
if isinstance(engine_list, list):
# Test that engine list elements are all BasicEngine objects
Expand Down
3 changes: 0 additions & 3 deletions projectq/cengines/_main_test.py
Expand Up @@ -49,9 +49,6 @@ def test_main_engine_init():


def test_main_engine_init_failure():
with pytest.raises(ValueError):
eng = _main.MainEngine(engine_list=[DummyEngine()],
setup=projectq.setups.default)
with pytest.raises(_main.UnsupportedEngineError):
eng = _main.MainEngine(backend=DummyEngine)
with pytest.raises(_main.UnsupportedEngineError):
Expand Down

0 comments on commit 1dce5d0

Please sign in to comment.