Instruction¶
- class Instruction(*, params: dict | None = None)¶
Base class for all instructions.
- Parameters:
params – Mapping of parameters specified by the users.
- on_modes(*modes: int) Instruction¶
Specifies the modes on which the state should be executed.
- Returns:
The same instruction with the modes specified.
- Return type:
- classmethod from_dict(dict_: dict) Instruction¶
Creates an
Instructioninstance from a dict specified.- Parameters:
dict (dict) – The desired
Instructioninstance in the format of a dict.- Returns:
An
Instructioninitialized using the specified dict.- Return type:
- classmethod set_subclass(instruction: Type[Instruction]) None¶
Registers a class in the instruction subclass map.
This is meaningful in contexts when one has multiple instructions with the same name.
Example
When one creates a custom beamsplitter with name Beamsplitter and subclasses
Beamsplitter, then for e.g. executing a Blackbird code will be performed with the custom one, not the original one. When one wants to use the original one in this case, one can reset it with this method.- Parameters:
instruction (Type[Instruction]) – The instruction class to be registered.
- Raises:
PiquassoException – When the class is not actually an instance of
Insruction.
- classmethod get_subclass(name: str) Type[Instruction]¶
Returns the instruction subclass specified by its name.
- Returns:
The instruction class.
- Return type:
Type[Instruction]
- when(condition: Callable | str) Instruction¶
Makes the execution of the instruction conditional on a callable object.
The callable object takes a single argument - the tuple of measurement outcomes - and returns a boolean indicating whether the condition evaluates to
True. The condition must be met for the instruction to be executed.The measurement outcomes are provided as a tuple, in the order which the measurements are defined. For example, x[0] refers to the first outcome, and x[-1] to the last outcome.
- Example usage::
- with pq.Program() as program:
pq.Q(0, 1) | pq.StateVector([0, 2]) * np.sqrt(1 / 2) pq.Q(0, 1) | pq.StateVector([2, 0]) * np.sqrt(1 / 2)
pq.Q(1) | pq.ParticleNumberMeasurement().on_modes(1) pq.Q(0) | pq.Squeezing(r=r).on_modes(0).when(lambda x: x[-1] == 2)
pq.Q(0, 1) | pq.Beamsplitter5050().when(lambda x: x[0] > 0)
- The following example uses an expression string as a condition::
- with pq.Program() as program:
pq.Q(0, 1) | pq.StateVector([0, 2]) * np.sqrt(1 / 2) pq.Q(0, 1) | pq.StateVector([2, 0]) * np.sqrt(1 / 2)
pq.Q(1) | pq.ParticleNumberMeasurement().on_modes(1) pq.Q(0) | pq.Squeezing(r=r).on_modes(0).when(“x[-1] == 2”)
pq.Q(0, 1) | pq.Beamsplitter5050().when(“x[0] > 0”)
- For specifying expressions strings, the following operators are supported:
Arithmetic operators: +, -, *, /, **, %, ^
Comparison operators: ==, !=, <, <=, >, >=
Boolean operators: and, or, not
Parentheses for grouping: ()
The expression can use the variable x, which represents the tuple of measurement outcomes.
- Raises:
PiquassoException – If the instruction is already conditioned on another callable, or if the provided condition is not callable.
- Parameters:
condition (Callable or str) – A callable that takes the measurement outcomes and returns a boolean.