Commit b4b839e
Context Manager: with flushing(MainEngine()) as eng: (#449)
* Context Manager: with flushing(MainEngine()) as eng:
Simplify the creation, use, and flushing of any Engine using a Python context manager.
When exiting the `with` block, the engine is _automatically_ flushed.
Modeled after https://docs.python.org/3/library/contextlib.html#contextlib.closing
```python
from projectq import MainEngine, flushing # import the main compiler engine
from projectq.ops import (
H,
Measure,
) # import the operations we want to perform (Hadamard and measurement)
with flushing(MainEngine()) as eng: # create a default compiler (the back-end is a simulator)
qubit = eng.allocate_qubit() # allocate a quantum register with 1 qubit
H | qubit # apply a Hadamard gate
Measure | qubit # measure the qubit
# This line is no longer required... eng.flush()
print(f"Measured {int(qubit)}") # converting a qubit to int or bool gives access to the measurement result
* Add tests
* Run linters/formatters on code
* Fix isort comment pragmas
* Really fix isort issues in cengines/__init__.py
Co-authored-by: Damien Nguyen <ngn.damien@gmail.com>1 parent b768038 commit b4b839e
File tree
3 files changed
+67
-0
lines changed- projectq/cengines
3 files changed
+67
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
8 | 14 | | |
9 | 15 | | |
10 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| 23 | + | |
| 24 | + | |
21 | 25 | | |
22 | 26 | | |
23 | 27 | | |
| |||
33 | 37 | | |
34 | 38 | | |
35 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
0 commit comments