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

Conversation

lillian542
Copy link
Contributor

@lillian542 lillian542 commented Sep 20, 2024

Context:
Currently, diagonalizing_gates for a tape just iterates over all the observables and adds their diagonalizing gates to the list. This means that if the same observable occurs more than once, it's diagonalizing gates are included more than once.

>>> tape = qml.tape.QuantumScript([], [qml.expval(X(0)), qml.var(X(0))])
>>> tape.diagonalizing_gates
[Hadamard(wires=[0]), Hadamard(wires=[0])]

Description of the Change:
We get diagonalizing gates from a set of the base observables instead of self.observables.

Benefits:

>>> tape = qml.tape.QuantumScript([], [qml.expval(X(0)), qml.var(X(0))])
>>> tape.diagonalizing_gates
[Hadamard(wires=[0])]

Open question:
One slightly unintuitive thing here is how to handle cases with CompositeOp observable where an observable is duplicated, but in an observable that can't be divided because its terms don't commute. For example, currently in this PR for:

tape.observables = [qml.X(0), qml.X(0)+qml.Y(0)]

Here we include X(0).diagonalizing_gates() and the QubitUnitary that diagonalized qml.X(0)+qml.Y(0). Since the sum can't be diagonalized as a single observable we treat is as a completely different observable, the way we would:

tape.observables = [qml.X(0), qml.Y(0)]

In contrast, if the sum doesn't have overlapping wires, and thus would be diagonalized one term at a time instead of as a single observable:

tape.observables = [qml.X(0), qml.X(0)+qml.Y(1)]

we end up with X(0).diagonalizing_gates() + Y(1).diagonalizing_gates()

This feels more consistent now that we have a diagonalizing gate for X(0)+Y(0), but it doesn't reflect how we currently diagonalize (currently we would split the terms on to separate tapes and use X(0).diagonalizing_gates() on one and Y(0).diagonalizing_gates() on the other. So maybe we should remove the special treatment for CompositeOps with overlapping wires and just let the diagonalizing gates for [qml.X(0), qml.X(0)+qml.Y(0)] be X(0).diagonalizing_gates() + Y(0).diagonalizing_gates().

[sc-64695]

@lillian542 lillian542 marked this pull request as ready for review September 20, 2024 17:42
@lillian542 lillian542 changed the title Avoid duplicate diagonalizing_gates [WIP] Avoid duplicate diagonalizing_gates Sep 20, 2024
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@lillian542 lillian542 marked this pull request as draft September 20, 2024 18:17
Copy link

codecov bot commented Sep 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.70%. Comparing base (4ae7113) to head (db62305).
Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6288      +/-   ##
==========================================
+ Coverage   99.69%   99.70%   +0.01%     
==========================================
  Files         444      444              
  Lines       42133    42163      +30     
==========================================
+ Hits        42004    42040      +36     
+ Misses        129      123       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant