qml.transforms.split_non_commuting¶
-
split_non_commuting
(tape, grouping_strategy='default')[source]¶ Splits a circuit into tapes measuring groups of commuting observables.
- Parameters
tape (QNode or QuantumScript or Callable) – The quantum circuit to be split.
grouping_strategy (str) – The strategy to use for computing disjoint groups of commuting observables, can be
"default"
,"wires"
,"qwc"
, orNone
to disable grouping.
- Returns
The transformed circuit as described in
qml.transform
.- Return type
qnode (QNode) or tuple[List[QuantumScript], function]
Note
This transform splits expectation values of sums into separate terms, and also distributes the terms into multiple executions if there are terms that do not commute with one another. For state-based simulators that are able to handle non-commuting measurements in a single execution, but don’t natively support sums of observables, consider
split_to_single_terms
instead.Examples:
This transform allows us to transform a QNode measuring multiple observables into multiple circuit executions, each measuring a group of commuting observables.
dev = qml.device("default.qubit", wires=2) @qml.transforms.split_non_commuting @qml.qnode(dev) def circuit(x): qml.RY(x[0], wires=0) qml.RX(x[1], wires=1) return [ qml.expval(qml.X(0)), qml.expval(qml.Y(1)), qml.expval(qml.Z(0) @ qml.Z(1)), qml.expval(qml.X(0) @ qml.Z(1) + 0.5 * qml.Y(1) + qml.Z(0)), ]
Instead of decorating the QNode, we can also create a new function that yields the same result in the following way:
@qml.qnode(dev) def circuit(x): qml.RY(x[0], wires=0) qml.RX(x[1], wires=1) return [ qml.expval(qml.X(0)), qml.expval(qml.Y(1)), qml.expval(qml.Z(0) @ qml.Z(1)), qml.expval(qml.X(0) @ qml.Z(1) + 0.5 * qml.Y(1) + qml.Z(0)), ] circuit = qml.transforms.split_non_commuting(circuit)
Internally, the QNode is split into multiple circuits when executed:
>>> print(qml.draw(circuit)([np.pi/4, np.pi/4])) 0: ──RY(0.79)─┤ ╭<Z@Z> <Z> 1: ──RX(0.79)─┤ ╰<Z@Z> 0: ──RY(0.79)─┤ <X> 1: ──RX(0.79)─┤ <Y> 0: ──RY(0.79)─┤ ╭<X@Z> 1: ──RX(0.79)─┤ ╰<X@Z>
Note that the observable
Y(1)
occurs twice in the original QNode, but only once in the transformed circuits. When there are multiple expecatation value measurements that rely on the same observable, this observable is measured only once, and the result is copied to each original measurement.While internally multiple tapes are created, the end result has the same ordering as the user provides in the return statement. Executing the above QNode returns the original ordering of the expectation values.
>>> circuit([np.pi/4, np.pi/4]) [0.7071067811865475, -0.7071067811865475, 0.5, 0.5]
There are two algorithms used to compute disjoint groups of commuting observables:
"qwc"
grouping usesgroup_observables()
which computes groups of qubit-wise commuting observables, producing the fewest number of circuit executions, but can be expensive to compute for large multi-term Hamiltonians, while"wires"
grouping simply ensures that no circuit contains two measurements with overlapping wires, disregarding commutativity between the observables being measured.The
grouping_strategy
keyword argument can be used to specify the grouping strategy. By default, qwc grouping is used whenever possible, except when the circuit contains multiple measurements that includes an expectation value of aqml.Hamiltonian
, in which case wires grouping is used in case the Hamiltonian is very large, to save on classical runtime. To force qwc grouping in all cases, setgrouping_strategy="qwc"
. Similarly, to force wires grouping, setgrouping_strategy="wires"
:@functools.partial(qml.transforms.split_non_commuting, grouping_strategy="wires") @qml.qnode(dev) def circuit(x): qml.RY(x[0], wires=0) qml.RX(x[1], wires=1) return [ qml.expval(qml.X(0)), qml.expval(qml.Y(1)), qml.expval(qml.Z(0) @ qml.Z(1)), qml.expval(qml.X(0) @ qml.Z(1) + 0.5 * qml.Y(1) + qml.Z(0)), ]
In this case, four circuits are created as follows:
>>> print(qml.draw(circuit)([np.pi/4, np.pi/4])) 0: ──RY(0.79)─┤ <X> 1: ──RX(0.79)─┤ <Y> 0: ──RY(0.79)─┤ ╭<Z@Z> 1: ──RX(0.79)─┤ ╰<Z@Z> 0: ──RY(0.79)─┤ ╭<X@Z> 1: ──RX(0.79)─┤ ╰<X@Z> 0: ──RY(0.79)─┤ <Z> 1: ──RX(0.79)─┤
Alternatively, to disable grouping completely, set
grouping_strategy=None
:@functools.partial(qml.transforms.split_non_commuting, grouping_strategy=None) @qml.qnode(dev) def circuit(x): qml.RY(x[0], wires=0) qml.RX(x[1], wires=1) return [ qml.expval(qml.X(0)), qml.expval(qml.Y(1)), qml.expval(qml.Z(0) @ qml.Z(1)), qml.expval(qml.X(0) @ qml.Z(1) + 0.5 * qml.Y(1) + qml.Z(0)), ]
In this case, each observable is measured in a separate circuit execution.
>>> print(qml.draw(circuit)([np.pi/4, np.pi/4])) 0: ──RY(0.79)─┤ <X> 1: ──RX(0.79)─┤ 0: ──RY(0.79)─┤ 1: ──RX(0.79)─┤ <Y> 0: ──RY(0.79)─┤ ╭<Z@Z> 1: ──RX(0.79)─┤ ╰<Z@Z> 0: ──RY(0.79)─┤ ╭<X@Z> 1: ──RX(0.79)─┤ ╰<X@Z> 0: ──RY(0.79)─┤ <Z> 1: ──RX(0.79)─┤
Note that there is an exception to the above rules: if the circuit only contains a single expectation value measurement of a
Hamiltonian
orSum
with pre-computed grouping indices, the grouping information will be used regardless of the requestedgrouping_strategy
Usage Details
Internally, this function works with tapes. We can create a tape with multiple measurements of non-commuting observables:
measurements = [ qml.expval(qml.Z(0) @ qml.Z(1)), qml.expval(qml.X(0) @ qml.X(1)), qml.expval(qml.Z(0)), qml.expval(qml.X(0)) ] tape = qml.tape.QuantumScript(measurements=measurements) tapes, processing_fn = qml.transforms.split_non_commuting(tape)
Now
tapes
is a list of two tapes, each contains a group of commuting observables:>>> [t.measurements for t in tapes] [[expval(Z(0) @ Z(1)), expval(Z(0))], [expval(X(0) @ X(1)), expval(X(0))]]
The processing function becomes important as the order of the inputs has been modified.
>>> dev = qml.device("default.qubit", wires=2) >>> result_batch = [dev.execute(t) for t in tapes] >>> result_batch [(1.0, 1.0), (0.0, 0.0)]
The processing function can be used to reorganize the results:
>>> processing_fn(result_batch) (1.0, 0.0, 1.0, 0.0)
Measurements that accept both observables and
wires
so that e.g.qml.counts
,qml.probs
andqml.sample
can also be used. When initialized using onlywires
, these measurements are interpreted as measuring with respect to the observableqml.Z(wires[0])@qml.Z(wires[1])@...@qml.Z(wires[len(wires)-1])
measurements = [ qml.expval(qml.X(0)), qml.probs(wires=[1]), qml.probs(wires=[0, 1]) ] tape = qml.tape.QuantumScript(measurements=measurements) tapes, processing_fn = qml.transforms.split_non_commuting(tape)
This results in two tapes, each with commuting measurements:
>>> [t.measurements for t in tapes] [[expval(X(0)), probs(wires=[1])], [probs(wires=[0, 1])]]