Here is a minimal example of what I am trying to do in terms of expectation value calculations:
import pennylane as qml
dev = qml.device("default.qubit", wires=8)
obs1 = qml.prod(qml.PauliX(3), qml.PauliX(4))
obs2 = qml.prod(qml.PauliY(3), qml.PauliY(4))
obs3 = qml.prod(qml.PauliZ(3), qml.PauliZ(4))
@qml.qnode(dev)
def circuit(Obs):
expect = [qml.expval(o) for o in Obs]
return expect
print(circuit([obs1, obs2, obs3]))
print(dev.num_executions)
When I run it, I get
Traceback (most recent call last):
File "/usr/local/lib/python3.11/dist-packages/pennylane/tape/tape.py", line 87, in rotations_and_diagonal_measurements
rotations, diag_obs = qml.pauli.diagonalize_qwc_pauli_words(tape._obs_sharing_wires)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/pennylane/pauli/utils.py", line 1231, in diagonalize_qwc_pauli_words
raise ValueError("This function only supports Tensor products of pauli ops.")
ValueError: This function only supports Tensor products of pauli ops.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.11/idlelib/run.py", line 578, in runcode
exec(code, self.locals)
File "/home/justin/qml_expect_test.py", line 14, in <module>
print(circuit([obs1, obs2, obs3]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/pennylane/qnode.py", line 867, in __call__
res = qml.execute(
^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/pennylane/interfaces/execution.py", line 407, in execute
qml.interfaces.cache_execute(
File "/usr/local/lib/python3.11/dist-packages/pennylane/interfaces/execution.py", line 204, in wrapper
res = fn(execution_tapes.values(), **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/pennylane/interfaces/execution.py", line 129, in fn
tapes = [expand_fn(tape) for tape in tapes]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/pennylane/interfaces/execution.py", line 129, in <listcomp>
tapes = [expand_fn(tape) for tape in tapes]
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/pennylane/interfaces/execution.py", line 388, in <lambda>
expand_fn = lambda tape: device.expand_fn(tape, max_expansion=max_expansion)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/pennylane/_device.py", line 696, in expand_fn
return self.default_expand_fn(circuit, max_expansion=max_expansion)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/pennylane/_device.py", line 670, in default_expand_fn
circuit = circuit.expand(depth=max_expansion, stop_at=self.stopping_condition)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/pennylane/tape/qscript.py", line 1079, in expand
new_script = qml.tape.tape.expand_tape(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/pennylane/tape/tape.py", line 181, in expand_tape
diagonalizing_gates, diagonal_measurements = rotations_and_diagonal_measurements(tape)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/pennylane/tape/tape.py", line 98, in rotations_and_diagonal_measurements
raise qml.QuantumFunctionError(_err_msg_for_some_meas_not_qwc(tape.measurements)) from e
pennylane.QuantumFunctionError: Only observables that are qubit-wise commuting Pauli words can be returned on the same wire, some of the following measurements do not commute:
[PauliX(wires=[3]) @ PauliX(wires=[4]), PauliY(wires=[3]) @ PauliY(wires=[4]), PauliZ(wires=[3]) @ PauliZ(wires=[4])]
Is there a workaround that I might be able to use so that I am not violating commutativity rules? The main thing that I am trying to do is to get the expectation values for all of the observables without having to carry out the main circuit operations more than once.