qp.devices.preprocess.decompose¶
- decompose(tape, stopping_condition, stopping_condition_shots=None, skip_initial_state_prep=True, decomposer=None, device_wires=None, num_work_wires=None, target_gates=None, fixed_decomps=None, alt_decomps=None, name='device', error=None, strict=True)[source]¶
Decompose operations until the stopping condition is met.
- Parameters:
tape (QuantumScript or QNode or Callable) – a quantum circuit.
stopping_condition (Callable) – a function from an operator to a boolean. If
False, the operator should be decomposed. If an operator cannot be decomposed and is not accepted bystopping_condition, anExceptionwill be raised (of a type specified by theerrorkeyword argument).
- Keyword Arguments:
stopping_condition_shots (Callable) – a function from an operator to a boolean. If
False, the operator should be decomposed. This replacesstopping_conditionif and only if the tape has shots.skip_initial_state_prep (bool) – If
True, the first operator will not be decomposed if it inherits fromStatePrepBase. Defaults toTrue.decomposer (Callable) – an optional callable that takes an operator and implements the relevant decomposition. If
None, defaults to using a callable returningop.decomposition()for anyOperator.device_wires (Wires) – The device wires. If provided along with
target_gatesand graph-based decomposition is enabled, will be used to infer available work wires.num_work_wires (int) – Number of work wires to be used if the graph-based decomposition is enabled. If
device_wiresare given, they take precedence overnum_work_wirestarget_gates (set or dict) – Target gate set to be used if the graph-based decomposition is enabled. See
qp.decomposefor more details.fixed_decomps (dict) – Fixed decomposition rules to be used if the graph-based decomposition is enabled. See
qp.decomposefor more details.alt_decomps (dict) – Alternative decomposition rules to be used if the graph-based decomposition is enabled. See
qp.decomposefor more details.name (str) – The name of the transform, process or device using decompose. Used in the error message. Defaults to “device”.
error (type) – An error type to raise if it is not possible to obtain a decomposition that fulfills the
stopping_condition. Defaults toDeviceError.strict (bool) – If
False, operators that do not define a decomposition will be treated as supported. Defaults toTrue
- Returns:
The decomposed circuit. The output type is explained in
qp.transform.- Return type:
qnode (QNode) or quantum function (Callable) or tuple[List[QuantumScript], function]
See also
This transform is intended for device developers. See
qp.decomposefor a more user-friendly interface.- Raises:
Exception – Type defaults to
DeviceErrorbut can be modified via keyword argument. Raised if an operator is not accepted and does not define a decomposition, or if the decomposition enters an infinite loop and raises aRecursionError.
Example:
>>> import pennylane as qp >>> def stopping_condition(obj): ... return obj.name in {"CNOT", "RX", "RZ"} >>> tape = qp.tape.QuantumScript([qp.IsingXX(1.2, wires=(0,1))], [qp.expval(qp.Z(0))]) >>> batch, fn = decompose(tape, stopping_condition) >>> batch[0].circuit [CNOT(wires=[0, 1]), RX(1.2, wires=[0]), CNOT(wires=[0, 1]), expval(Z(0))]
If an operator cannot be decomposed into a supported operation, an error is raised:
>>> decompose(tape, lambda obj: obj.name == "S") Traceback (most recent call last): ... pennylane.exceptions.DeviceError: Operator CNOT(wires=[0, 1]) not supported with device and does not provide a decomposition.
The
skip_initial_state_prepspecifies whether the device supports state prep operations at the beginning of the circuit.>>> tape = qp.tape.QuantumScript([qp.BasisState([1], wires=0), qp.BasisState([1], wires=1)]) >>> batch, fn = decompose(tape, stopping_condition) >>> batch[0].circuit [BasisState(array([1]), wires=[0]), RX(3.141592653589793, wires=[1])] >>> batch, fn = decompose(tape, stopping_condition, skip_initial_state_prep=False) >>> batch[0].circuit [RX(3.141592653589793, wires=[0]), RX(3.141592653589793, wires=[1])]