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

Support wire labels in qinfo transforms #4331

Merged
merged 8 commits into from
Jul 10, 2023
Merged

Support wire labels in qinfo transforms #4331

merged 8 commits into from
Jul 10, 2023

Conversation

eddddddy
Copy link
Contributor

@eddddddy eddddddy commented Jul 6, 2023

Context:
Taken from #4318:

Other functions are affected by this discrepancy between wire indices and wire labels:

  • qinfo.fidelity, qinfo.reduced_dm, qinfo.vn_entropy, qinfo.mutual_info, qinfo.relative_entropy raise an error when passed wire labels
  • qinfo.purity uses wrong wires. It is not possible to compute the purity w.r.t, say, a string-labeled qubit. Importantly, no error is raised in this case.

Users may expect the following code to work:

dev = qml.device("default.qubit", wires=[0, "a", 12])

@qml.qnode(dev)
def circuit0(x):
    qml.RX(x, 0)
    return qml.state()

@qml.qnode(dev)
def circuit1(x):
    qml.RX(x, "a")
    return qml.state()

x = np.array(1.3)
qml.qinfo.fidelity(circuit0, circuit1, [0], ["a"])((x,), (x,)) # raises an error
qml.qinfo.reduced_dm(circuit1, ["a"])((x,), (x,)) # raises an error
qml.qinfo.vn_entropy(circuit, wires=["a"])(x) # raises an error
qml.qinfo.mutual_info(circuit0, [0], [12])(x) # raises an error, already because of the _existence_ of a custom label
qml.qinfo.relative_entropy(circuit0, circuit1, [0], ["a"])((x,), (x,)) # raises an error

Likewise, users may try to compute a purity w.r.t a custom-labeled qubit:

dev = qml.device("default.mixed", wires=[0, "a", 12])

@qml.qnode(dev)
def circuit(x):
    qml.DepolarizingChannel(x, "a")
    return qml.state()

x = np.array(0.2)

qml.qinfo.purity(circuit, wires=["a"])(x) # returns 1, which is incorrect, interpreting `wires` as labels
qml.qinfo.purity(circuit, wires=[1])(x) # returns 1, which is incorrect, interpreting `wires` as indices into the labels

Description of the Change:
Support custom wire labels in all qinfo transforms.

Related GitHub Issues:
Fixes #4318

@github-actions
Copy link
Contributor

github-actions bot commented Jul 6, 2023

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.

@eddddddy eddddddy requested review from dwierichs and a team July 6, 2023 19:54
@codecov
Copy link

codecov bot commented Jul 6, 2023

Codecov Report

Merging #4331 (360ec4d) into master (10cae8d) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #4331   +/-   ##
=======================================
  Coverage   99.79%   99.79%           
=======================================
  Files         351      351           
  Lines       32087    32096    +9     
=======================================
+ Hits        32020    32029    +9     
  Misses         67       67           
Impacted Files Coverage Δ
pennylane/qinfo/transforms.py 100.00% <100.00%> (ø)

Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the super quick fix @eddddddy 💯
Thorough test additions 😎
I only have two very minor suggestions.

doc/releases/changelog-dev.md Show resolved Hide resolved
pennylane/qinfo/transforms.py Show resolved Hide resolved
Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the PR looks good. But two things concern me w.r.t the new device API:

  • The new device doesn't have wires, so qnode.device.wires can't be used for wire mapping.
  • The new device also does not have a C_DTYPE or R_DTYPE attribute, which I'm seeing being used in the qinfo transforms.

Tagging @albi3ro for thoughts.

Once these are addressed, I'm happy to approve. Everything else looked good :)

@timmysilv
Copy link
Contributor

I think the qinfo module (and various other ones) are still incompatible with the new device API, and it'll be quite a bit of work to get things caught up unfortunately. I'd prefer that this PR isn't blocked by that, since it'll need to be handled at a larger scale anyway

@eddddddy
Copy link
Contributor Author

eddddddy commented Jul 7, 2023

I agree with @timmysilv. There are still some things to work out for compatibility with the new device API and I'd rather we do those in a separate PR that includes everything else.

@eddddddy eddddddy requested a review from a team July 10, 2023 14:02
@albi3ro
Copy link
Contributor

albi3ro commented Jul 10, 2023

[sc-41189]

Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As others have pointed out, these transforms will cause issues with the new device API. I've created a new shortcut ticket to resolve these issues, the most tricky of which will be the wire order for a state vector with non-standard labels.

But that shouldn't block this bugfix right now, as long as the other open questions are resolved before the next release.

Thanks for fixing this up :)

@eddddddy eddddddy merged commit e6b403f into master Jul 10, 2023
42 checks passed
@eddddddy eddddddy deleted the qinfo_wire_order branch July 10, 2023 18:00
mudit2812 pushed a commit that referenced this pull request Jul 10, 2023
* Support wire labels in qinfo transforms

* changelog

* pylint and update test

* add bugfix entry
mudit2812 added a commit that referenced this pull request Jul 11, 2023
* Adding changes for shift rules

* Testing changes

* Fixed op indices

* Fixed indexing

* Updated `bind_new_parameters`

* Updated `tape.get_operation`

* Updated tests

* Test updates; multishifting works

* Fixing interface

* Updated shifting; added dispatch for templates

* Updated shifting function

* Fixed index error

* Removed commented code

* [skip ci] Reverted changes to `bind_new_parameters

* Update to remove copying

* Update changelog

* Apply suggestions from code review

Co-authored-by: Matthew Silverman <matthews@xanadu.ai>

* Removed unused import

* Roll back suggested change

* Remove state vector support from `math/quantum.py` (#4322)

* Remove statevector support for qinfo functions

* remove unused funcs

* Fix some tests

* pylint

* more pylint

* Remove unnecessary log

* Changelog and deprecations entry

* trigger ci

* Enable CI and pre-commit hook to lint tests (#4335)

* rename all legacy test files to match pylint pattern

* pylint all tests in CI and pre-commit

* lint legacy/qnn/conftest

* remove the custom pylint test handling

* run black before pylint

* changelog

---------

Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>

* Update broadcasting transforms to use `bind_new_parameters` (#4288)

* use bind_new_parameters

* pylint

* remove batching in measurements and add tests

* More coverage issues

* Add uncopied tests

* Helper function

* pylint

* Make tape._ops public

* use private methods

* pylint

* Add more docs to split_operations

* pylint

* Deprecate X and P (#4330)

* update in docs

* update changelog

* Test warning is raised

* update default gaussian device

* update tests

* update more tests

* fix legacy tests

* Specify v0.33 removal in docstring

* Render X and P in docs

* move deprecation warning in docstring

* fix sphinx linking

* add warning box

* Support wire labels in `qinfo` transforms (#4331)

* Support wire labels in qinfo transforms

* changelog

* pylint and update test

* add bugfix entry

* Deprecations for 0.32 from me! (#4316)

* deprecate the old return system

* deprecate the mode kwarg for QNode

* changelog

* PR feedback

* update notice to avoid wrongly suggesting action needed

* update docstrings, docs and warnings

* add link to qnode returns doc

* change the mode warning depending on return system active

* also add disclaimer to docstring

---------

Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
Co-authored-by: Edward Jiang <34989448+eddddddy@users.noreply.github.com>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: lillian542 <38584660+lillian542@users.noreply.github.com>
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.

[BUG] Clarify wires arguments of qinfo.fidelity
5 participants