Control flow¶
Source: euqalyptus/operations/control_flow.py.
return_results(...) / ReturnResults¶
return_results (and the alias ReturnResults, which is the same callable) records a qnet.return op that terminates the current function. Use it to return classical values — typically measurement outcomes — from a Qoala program.
from euqalyptus.operations.control_flow import return_results, ReturnResults
@QoalaProgram
def measure_and_return():
q = LocalQubit()
q.H()
m = q.measure()
return_results(m)
Signatures¶
ReturnResults() # qnet.return
ReturnResults(x) # qnet.return %x
ReturnResults(x, y, z) # qnet.return %x, %y, %z
ReturnResults([x, y, z]) # same — lists/tuples are flattened
The variadic form accepts any number of QoalaExpressions, plus Python int / float literals (auto-promoted), and lists or tuples of the same.
Implicit terminator¶
If you don't call return_results(...) at the end of your program, the SDK infers an empty return when emitting HIR. You only need to call it explicitly when you want to return one or more values, or when you need to terminate early (the latter is less common in HIR, since branching is handled separately).
Branching¶
Branching constructs (if_cond, if_eq, if_lt, …) live in euqalyptus.operations.branching. They exist and are exercised in the test suite (tests/bindings/test_branching.py, tests/syntax/test_branching_syntax.py, tests/semantics/test_branching.py). Please refer to branching documentation for more information about those statements.
API reference¶
control_flow ¶
return_results
module-attribute
¶
return_results = ReturnResults
Lowercase alias of :class:ReturnResults. Records qnet.return.
ReturnResults ¶
ReturnResults(*args, **kwargs)
Record a qnet.return op that terminates the current function.
Use return_results(...) (the lowercase alias) inside a
@QoalaProgram body to return classical values — typically
measurement outcomes — from the program. If the function body
finishes without an explicit return_results(...) call, the SDK
inserts an empty qnet.return automatically; you only need to
call this explicitly when you want to return one or more values or
to terminate early.
Examples:
ReturnResults() → records qnet.return.
ReturnResults(x) → records qnet.return %x.
ReturnResults(x, y, z) → records qnet.return %x, %y, %z.
ReturnResults([x, y, z]) → same as the variadic form;
list / tuple operands are flattened.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Zero or more values to return. Each value must be a
:class: |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
class: |