mimiqcircuits.lazy¶
Lazy evaluation and composition functions.
Functions
|
Return a controlled operation, or a lazy expression for one. |
|
Return the inverse of an operation; same as |
|
Return an operation repeated in parallel on separate qubits, or a lazy expression for one. |
|
Return a power of an operation, or a lazy expression for one. |
Classes
|
Placeholder for an argument of a |
|
Operation whose construction waits for some of its arguments. |
- class LazyArg[source]¶
Bases:
objectPlaceholder for an argument of a
LazyExprthat is not known yet. It prints as?.
- class LazyExpr(obj, *args)[source]¶
Bases:
objectOperation whose construction waits for some of its arguments.
Calling
control,parallel,powerorrepeaton a gate without the count, or a size-dependent gate such asQFT()without its size, returns aLazyExpr. ALazyExprcan be wrapped further withcontrol(),parallel(),power()andinverse(). Calling it fills theLazyArgplaceholders, in the order they appear in the printed expression (left to right), and builds the operation. Passing more arguments than placeholders raisesTypeError.- Parameters:
obj – Callable that builds the operation.
*args – Its arguments;
LazyArg()marks the ones still missing.
Examples
>>> from mimiqcircuits import * >>> # leave the number of controls open >>> lazy = control(GateX()) >>> lazy lazy control(?, X) >>> lazy(2) C₂X >>> # nest lazy operations: the placeholders are filled left to right >>> lazy = GateX().power().control() >>> lazy lazy control(?, lazy power(X, ?)) >>> lazy(2, 0.5) C₂SX
- parallel(*args)[source]¶
Return a lazy expression for the parallel repetition of this expression.
- Parameters:
*args – Optional number of repetitions. Without it, the number becomes a new placeholder.
- Raises:
TypeError – If more than one argument is given.
- control(*args)[source]¶
Return a controlled operation, or a lazy expression for one.
- Parameters:
*args –
One of the following.
(num_controls, op): returnop.control(num_controls).(op,): return the lazy expressioncontrol(?, op).(num_controls,)with anint: return the lazy expressioncontrol(num_controls, ?).
- Returns:
The controlled operation or a lazy expression.
- Return type:
- Raises:
TypeError – If a single argument is neither an
Operationnor anint, or the number of arguments is not 1 or 2.
Examples
>>> from mimiqcircuits import * >>> control(2, GateX()) C₂X >>> # fix the number of controls, give the operation later >>> lazy_ctrl = control(2) >>> lazy_ctrl(GateX()) C₂X
- parallel(*args)[source]¶
Return an operation repeated in parallel on separate qubits, or a lazy expression for one.
- Parameters:
*args –
One of the following.
(num_repeats, op): returnop.parallel(num_repeats).(op,): return the lazy expressionparallel(?, op).(num_repeats,)with anint: return the lazy expressionparallel(num_repeats, ?).
- Returns:
The parallel operation or a lazy expression.
- Return type:
- Raises:
TypeError – If a single argument is neither an
Operationnor anint, or the number of arguments is not 1 or 2.
Examples
>>> from mimiqcircuits import * >>> parallel(3, GateH()) ⨷ ³ H >>> # fix the number of repetitions, give the operation later >>> parallel(3)(GateH()) ⨷ ³ H
- inverse(op)[source]¶
Return the inverse of an operation; same as
op.inverse().- Parameters:
- Returns:
The inverse.
- Return type:
Examples
>>> from mimiqcircuits import * >>> op = inverse(GateS()) >>> op S†
- power(*args)[source]¶
Return a power of an operation, or a lazy expression for one.
- Parameters:
*args –
One of the following.
(op, exponent): returnop.power(exponent).(op,): return the lazy expressionpower(op, ?).(exponent,)with anint: return the lazy expressionpower(?, exponent). A non-integer exponent cannot be given alone.
- Returns:
The powered operation or a lazy expression.
- Return type:
- Raises:
TypeError – If a single argument is neither an
Operationnor anint, or the number of arguments is not 1 or 2.
Examples
>>> from mimiqcircuits import * >>> power(GateX(), 0.5) SX >>> # fix the exponent, give the operation later >>> power(2)(GateS()) Z