qml.devices.preprocess.decompose

decompose(tape, stopping_condition, stopping_condition_shots=None, skip_initial_state_prep=True, decomposer=None, max_expansion=None, name='device')[source]

Decompose operations until the stopping condition is met.

Parameters
  • tape (QuantumTape 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, a DecompositionUndefinedError will be raised.

  • stopping_condition_shots (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, a DecompositionUndefinedError will be raised. This replaces stopping_condition if and only if the tape has shots.

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

  • 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.

Returns

The decomposed circuit. The output type is explained in qml.transform.

Return type

qnode (QNode) or quantum function (Callable) or tuple[List[QuantumTape], function]

Raises
  • DecompositionUndefinedError – if an operator is not accepted and does not define a decomposition

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

The skip_initial_state_prep specifies whether or not 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