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

Integration into the current PL device ecosys #6684

Open
wants to merge 380 commits into
base: master
Choose a base branch
from

Conversation

JerryChen97
Copy link
Contributor

@JerryChen97 JerryChen97 commented Dec 6, 2024

Context:
We finished all the necessary components of the new API interface of default_mixed. However, since this is an internal change, which involves many different files and paths, we would like to have one more round of check, success and merge of which should concludes the whole epic.

Description of the Change:

  • DefaultMixedLegacy removed! keep DefaultMixed, but rename it to DefaultMixedLegacy (note: still the legacy one will be purged before this PR merged into master; it's kept right now due to great help in debug)
  • rename DefaultMixedNewAPI to DefaultMixed
  • a previous TODO: renaming operations_mixed back to operations, and copy it to DefaultMixedLegacy
  • add a intermediate step to apply readout_error just as BitFlip with arbitrary input error channels
  • remove redundant measure process functions
  • add an IntegrationTest for our new DefaultMixed
  • modify all the device tests in tests/devices accordingly. Basically the tests/devices/test_default_mixed*.py Deleted legacy specified device tests. These should be covered in the integration test suite mentioned above
  • if no errors come up, then separate the legacy code into another file Lots of errors, see drawback section for a record along with updates.
  • some meaningless tests were deleted:
    • all the legacy-specific tests
    • some tests that appear to be all-device, but actually skip all the other dev but legacy default mixed (e.g. tests in test_return_types_qnode
  • with completed PL package, test against all qml demos. REQUIREMENT: literally nothing should be modified for any downstreaming applications, except for few necessary breaking
    • After search we found the following 9 demos explicitly using default.mixed
    • tutorial_noisy_circuits.py the usage of dev._state needs to be removed and may be replaced by equivalent or not. Sol: had a PR to improve this line
    • tutorial_error_mitigation.py other irrelavant breaks involved (removal of top-level access to QubitStateVec) (!Note: to reproduce this one has to install the latest version of pennylane qiskit via pip install git+https://github.com/PennyLaneAI/pennylane-qiskit.git@master)
    • !The following two demos are super heavy and resource consuming.

some by-the-way improvement:

  • applied the new get_canonical_interface_name and removed the hardcoded dictionary mimic of enum.
  • aligned the implementation of qubit_mixed much more with qubit module

Benefits:

  • Legacy code was preserved for reference purpose as well as catering for potential users
  • Better separation between new interface and legacy
  • Maintained the functionalities from demos
  • Device default.mixed is ported directly with our new API.

Possible Drawbacks:

  • Some advanced users that involved the src code deep within legacy default_mixed might find it annoying for a while. However, we would include a direct indication in changelog as well as the new class.
  • Fixed unexpected bug 1: finite-diff support for PurityMP on torch, tf, and jax down
  • Fixed unexpected bug 2: gradient calculation at PhaseFlip(0) breaks with nan returned at initial state |0> (all good for other other channels or other state with PhaseFlip`
  • Fixed. expected bug 3: classical shadow is down for most of tests. This is due to the fact that the implementation is hidden inside QubitDevice which was missed out before. Need to be fixed asap

The drop of legacy device might introduce some missing in related code coverage. For visibility, we track them as todos here as well.

  • ClassicalShadow related missing: the entire ClassicalShadowMP.process() method; shadow_expval branch in _qubit_device.py
  • (coverage affected files: single_dispatch and mutliple_dispatch) scatter related missing: this is suspicious since it might imply that some functinoalities were lost. nothing has been lost. The legacy code that use scatter was for initializing states in a bruteforce way, which is not needed anymore.

Related GitHub Issues:
[sc-73324]

@JerryChen97 JerryChen97 self-assigned this Dec 6, 2024
…ultMixed

In this commit, we would like to replace all the usecase across the whole codebase that require the legacy default mixed from DefaultMixed to DefaultMixedLegacy
@JerryChen97 JerryChen97 marked this pull request as ready for review December 9, 2024 15:25
@JerryChen97 JerryChen97 marked this pull request as draft December 9, 2024 19:19
@JerryChen97
Copy link
Contributor Author

Finally we passed the CodeCov 🥳

Copy link
Contributor

@PietropaoloFrisoni PietropaoloFrisoni left a comment

Choose a reason for hiding this comment

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

This was a huge effort. Congrats @JerryChen97 : )

@JerryChen97
Copy link
Contributor Author

3450f5b patched the missing coverage of device constructor. Since its design is to process device name, we are not able to make use of LegacyQubitDevice. Instead, we do a dumb construction with custom decomposition (i.e. no actual call of expand, which might lead to inf recursion) for default.qutrit.

@JerryChen97 JerryChen97 added the ci:run-full-test-suite Run the full test-suite on the pull request label Feb 28, 2025
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'm pretty much ready to approve once the _apply_state_vector stuff is resolved. Amazing effort so far.

Comment on lines +603 to +604
for op_class in SYMMETRIC_REAL_OPS:
apply_operation.register(op_class)(apply_symmetric_real_op)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you leave a comment about why this syntax was used rather than using @apply_operation.register` as a decorator? It was so that the docs render nicely right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines 104 to 137
class TestLegacyDefaultMixed:
"""
Tests that covered parts of legacy method of the DefaultMixed device.
"""

@pytest.mark.all_interfaces
@pytest.mark.parametrize("interface", ML_INTERFACES)
@pytest.mark.parametrize(
"total_wires", [qml.wires.Wires([0]), qml.wires.Wires([0, 1]), qml.wires.Wires([0, 2, 1])]
)
def test_apply_state_vector(self, total_wires, interface):
"""Initialize the internal state in a specified pure state."""
if interface == "autograd":
pytest.skip(
"Autograd interface not supported for this test. No autograd scatter support."
)
num_total_wires = len(total_wires)

device_wires = qml.wires.Wires([0])
num_wires = len(device_wires)
tolerance = 1e-10
state_np = np.zeros(shape=(2**num_wires,), dtype=np.complex128)
state_np[0] = 1.0
_state = _apply_state_vector(
total_wires=total_wires,
state=state_np,
device_wires=device_wires,
interface=interface,
)

rho_expected_numpy = np.zeros([2] * 2 * num_total_wires, dtype=np.complex128)
rho_expected_numpy[tuple([0] * 2 * num_total_wires)] = 1.0
rho_expected = qml.math.asarray(rho_expected_numpy, like=interface)
assert qml.math.allclose(_state, rho_expected, atol=tolerance)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better to remove _apply_state_vector and just add a unit test for scatter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright! I'll try cleaning it up here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mudit2812 Redesigned the unitttests here ce0e8ad

@JerryChen97 JerryChen97 removed the ci:run-full-test-suite Run the full test-suite on the pull request label Mar 10, 2025
@@ -600,6 +600,7 @@ def apply_symmetric_real_op(
return state


# NOTE: this loop is for a nice doc rendering for `apply_operation`. With a direct multiple single registers over different op class there will be severe rendering issue as discussed in https://github.com/PennyLaneAI/pennylane/pull/6684#pullrequestreview-2565634328
Copy link
Contributor

Choose a reason for hiding this comment

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

Just splitting it into multiple lines for readability

Suggested change
# NOTE: this loop is for a nice doc rendering for `apply_operation`. With a direct multiple single registers over different op class there will be severe rendering issue as discussed in https://github.com/PennyLaneAI/pennylane/pull/6684#pullrequestreview-2565634328
# NOTE: this loop is for a nice doc rendering for `apply_operation`. With a direct multiple single
# registers over different op class there will be severe rendering issue as discussed in
# https://github.com/PennyLaneAI/pennylane/pull/6684#pullrequestreview-2565634328

@PietropaoloFrisoni
Copy link
Contributor

We are finally merging this PR! 🍾

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.

6 participants