qml.devices.qubit.apply_operation¶
- apply_operation(op, state, is_state_batched=False, debugger=None, **_)[source]¶
Apply and operator to a given state.
- Parameters
op (Operator) – The operation to apply to
state
state (TensorLike) – The starting state.
is_state_batched (bool) – Boolean representing whether the state is batched or not
debugger (_Debugger) – The debugger to use
**execution_kwargs (Optional[dict]) – Optional keyword arguments needed for applying some operations described below.
- Keyword Arguments
mid_measurements (dict, None) – Mid-circuit measurement dictionary mutated to record the sampled value
interface (str) – The machine learning interface of the state
postselect_mode (str) – Configuration for handling shots with mid-circuit measurement postselection. Use
"hw-like"
to discard invalid shots and"fill-shots"
to keep the same number of shots.None
by default.rng (Optional[numpy.random._generator.Generator]) – A NumPy random number generator.
prng_key (Optional[jax.random.PRNGKey]) – An optional
jax.random.PRNGKey
. This is the key to the JAX pseudo random number generator. Only for simulation using JAX. If None, anumpy.random.default_rng
will be used for sampling.tape_shots (Shots) – the shots object of the tape
- Returns
output state
- Return type
ndarray
Warning
apply_operation
is an internal function, and thus subject to change without a deprecation cycle.Warning
apply_operation
applies no validation to its inputs.This function assumes that the wires of the operator correspond to indices of the state. See
map_wires()
to convert operations to integer wire labels.The shape of state should be
[2]*num_wires
.This is a
functools.singledispatch
function, so additional specialized kernels for specific operations can be registered like:@apply_operation.register def _(op: type_op, state): # custom op application method here
Example:
>>> state = np.zeros((2,2)) >>> state[0][0] = 1 >>> state tensor([[1., 0.], [0., 0.]], requires_grad=True) >>> apply_operation(qml.X(0), state) tensor([[0., 0.], [1., 0.]], requires_grad=True)