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 317 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

Related GitHub Issues:
[sc-73324]

@JerryChen97 JerryChen97 requested a review from mudit2812 January 3, 2025 15:44
@JerryChen97 JerryChen97 added the do not merge ⚠️ Do not merge the pull request until this label is removed label Jan 3, 2025
@JerryChen97
Copy link
Contributor Author

Let's merge this after the v0.40 release is live

@JerryChen97
Copy link
Contributor Author

Note that the coverage issue is due to the fact that the legacy mixed deletion. We might need to override it some time.

@JerryChen97 JerryChen97 removed the do not merge ⚠️ Do not merge the pull request until this label is removed label Jan 16, 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.

Looks good. Some final comments left below.

While it was a good change to remove the QubitDensityMatrix validation from apply_operation, I think it's meaningful to have it somewhere. It's probably worth adding it to QubitDensityMatrix.__init__. However, that is not a change needed for this PR. Could you open a story for this in the Top of the Backlog?

Comment on lines +9 to +11
* Converted current tests that used `default.mixed` to use other equivalent devices in-place.
[(#6684)](https://github.com/PennyLaneAI/pennylane/pull/6684)

Copy link
Contributor

Choose a reason for hiding this comment

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

There should also be a changelog entry under either the "New features" or "Improvements" section about porting default.mixed to the new device API. I will let @isaacdevlugt pick which section makes more sense.

Comment on lines +135 to +138
import numpy as np
import pennylane as qml

from pennylane.devices.qubit_mixed import measure_final_state
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to import pennylane and numpy. We assume users to import those in our docstrings.

Suggested change
import numpy as np
import pennylane as qml
from pennylane.devices.qubit_mixed import measure_final_state
from pennylane.devices.qubit_mixed import measure_final_state

Comment on lines +246 to +249
from pennylane import expval, probs
from pennylane.devices.qubit_mixed import simulate
from pennylane.ops import RX, PauliX
from pennylane.tape import QuantumScript
Copy link
Contributor

Choose a reason for hiding this comment

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

For functions/classes available top-level, it's probably best to use them with qml. instead of importing them.

Suggested change
from pennylane import expval, probs
from pennylane.devices.qubit_mixed import simulate
from pennylane.ops import RX, PauliX
from pennylane.tape import QuantumScript
from pennylane.devices.qubit_mixed import simulate
from pennylane.tape import QuantumScript

Copy link
Contributor

Choose a reason for hiding this comment

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

The rest of the docstring needs to be updated accordingly.

@@ -365,22 +364,6 @@ def func(x):
):
d_func(pnp.array(0.1, requires_grad=True))

def test_no_state_capability(self, monkeypatch):
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't be removing tests. Instead of removing this test. You can test it with DefaultQubitLegacy

Copy link
Contributor

Choose a reason for hiding this comment

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

Same for any other unit tests have been removed for similar reasons.

Comment on lines +436 to +437
# !TODO: bring back angle 0 when the bug fixed https://github.com/PennyLaneAI/pennylane/pull/6684#issuecomment-2552123064
@pytest.mark.parametrize("angle", np.linspace(0, 2 * np.pi, 7)[1:])
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 add a shortcut story to the top of the backlog for this TODO and include the story ID in this TODO comment?

Suggested change
# !TODO: bring back angle 0 when the bug fixed https://github.com/PennyLaneAI/pennylane/pull/6684#issuecomment-2552123064
@pytest.mark.parametrize("angle", np.linspace(0, 2 * np.pi, 7)[1:])
# TODO: bring back angle 0 when the bug fixed https://github.com/PennyLaneAI/pennylane/pull/6684#issuecomment-2552123064
@pytest.mark.parametrize("angle", np.linspace(0, 2 * np.pi, 7)[1:])

@@ -334,32 +348,25 @@ def _(op: type_op, state, is_state_batched=False, **kwargs):
>>> state[0][0] = 1
Copy link
Contributor

Choose a reason for hiding this comment

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

The state has 4 dims

Suggested change
>>> state[0][0] = 1
>>> state[0][0][0][0] = 1

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.

Another thing I found. The Qubit mixed-state simulation tools heading in the qml.devices docs is not showing all functions. Additionally, sampling should not be in the docs at all since that is a submodule, not a function/class.
image

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.

Comments for apply_operation docs:

Due to the singledispatch, the docs for apply_operation look really weird:
image

We can fix this by using a similar strategy to devices.qubit.apply_operation, which calls an internal apply_operation_default method. This apply_operation_default method is what gets decorated with singledispatch, so we don't get the weird doc rendering.

Comment on lines +257 to +260
# Simulate the circuit
results = simulate(circuit)
print(results)
# Output: (0.0, array([0.68117888, 0.0, 0.31882112, 0.0]))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# Simulate the circuit
results = simulate(circuit)
print(results)
# Output: (0.0, array([0.68117888, 0.0, 0.31882112, 0.0]))
>>> print(simulate(circuit))
(0.0, array([0.68117888, 0.0, 0.31882112, 0.0]))

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.

4 participants