qml.devices.preprocess.decompose

decompose(tape, stopping_condition, stopping_condition_shots=None, skip_initial_state_prep=True, decomposer=None, max_expansion=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 by stopping_condition, an Exception will be raised (of a type specified by the error keyward argument).

Keyword Arguments
  • stopping_condition_shots (Callable) – a function from an operator to a boolean. If False, the operator should be decomposed. This replaces stopping_condition if and only if the tape has shots.

  • skip_initial_state_prep (bool) – If True, the first operator will not be decomposed if it inherits from StatePrepBase. Defaults to True.

  • decomposer (Callable) – an optional callable that takes an operator and implements the relevant decomposition. If None, defaults to using a callable returning op.decomposition() for any Operator .

  • max_expansion (int) – The maximum depth of the expansion. Defaults to None.

  • 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 to qml.DeviceError.

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.decompose for a more user-friendly interface.

Raises

Exception – Type defaults to qml.DeviceError but 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 a RecursionError.

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")
qml.DeviceError: Operator CNOT(wires=[0, 1]) not supported on device and does not provide a decomposition.

The skip_initial_state_prep specifies 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])]

Contents

Using PennyLane

Release news

Development

API

Internals