Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Avoid duplicate diagonalizing_gates #6288

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion pennylane/tape/qscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def diagonalizing_gates(self) -> list[Operation]:
rotation_gates = []

with qml.queuing.QueuingManager.stop_recording():
for observable in self.observables:
for observable in _get_base_obs(self.observables):
# some observables do not have diagonalizing gates,
# in which case we just don't append any
with contextlib.suppress(qml.operation.DiagGatesUndefinedError):
Expand Down Expand Up @@ -1323,3 +1323,28 @@ def wrapper(*args, **kwargs):
QuantumScriptOrBatch = Union[QuantumScript, QuantumScriptBatch]

register_pytree(QuantumScript, QuantumScript._flatten, QuantumScript._unflatten)


def _get_base_obs(observables):

overlapping_ops_observables = []

while any(isinstance(o, (qml.ops.CompositeOp, qml.ops.SymbolicOp)) for o in observables):

new_obs = []

for observable in observables:

if isinstance(observable, qml.ops.CompositeOp):
if any(len(o) > 1 for o in observable.overlapping_ops):
overlapping_ops_observables.append(observable)
else:
new_obs.extend(observable.operands)
elif isinstance(observable, qml.ops.SymbolicOp):
new_obs.append(observable.base)
else:
new_obs.append(observable)

observables = new_obs

return set(observables + overlapping_ops_observables)
Loading