qml.devices.preprocess.decompose¶
- decompose(tape, stopping_condition, stopping_condition_shots=None, skip_initial_state_prep=True, decomposer=None, device_wires=None, target_gates=None, name='device', error=None)[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_gates, will be used to automatically set up graph decomposition when enabled.target_gates (set) – The target gate set for graph decomposition. If provided along with
device_wires, will automatically enable graph-based decomposition when available.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.
- Returns:
The decomposed circuit. The output type is explained in
qml.transform.- Return type:
qnode (QNode) or quantum function (Callable) or tuple[List[QuantumScript], function]
See also
This transform is intended for device developers. See
qml.transforms.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:
>>> def stopping_condition(obj): ... return obj.name in {"CNOT", "RX", "RZ"} >>> tape = qml.tape.QuantumScript([qml.IsingXX(1.2, wires=(0,1))], [qml.expval(qml.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") DeviceError: Operator CNOT(wires=[0, 1]) not supported on 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 = qml.tape.QuantumScript([qml.BasisState([1], wires=0), qml.BasisState([1], wires=1)]) >>> batch, fn = decompose(tape, stopping_condition) >>> batch[0].circuit [BasisState(array([1]), wires=[0]), RZ(1.5707963267948966, wires=[1]), RX(3.141592653589793, wires=[1]), RZ(1.5707963267948966, wires=[1])] >>> batch, fn = decompose(tape, stopping_condition, skip_initial_state_prep=False) >>> batch[0].circuit [RZ(1.5707963267948966, wires=[0]), RX(3.141592653589793, wires=[0]), RZ(1.5707963267948966, wires=[0]), RZ(1.5707963267948966, wires=[1]), RX(3.141592653589793, wires=[1]), RZ(1.5707963267948966, wires=[1])]