PennyLaneAI / pennylane

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
https://pennylane.ai
Apache License 2.0
2.29k stars 590 forks source link

Add an `is_pure` method in `qml.math.quantum` for quick purity checks on density matrices #5844

Open JerryChen97 opened 3 months ago

JerryChen97 commented 3 months ago

Feature details

The request is for a new utility method, is_pure, to be added to the qml.math.quantum module in PennyLane. This method will facilitate the quick determination of whether a given density matrix represents a pure state.

Implementation

Directly call the purity method in qml.math.quantum. Basically just return Booleans e.g. qml.math.allclose(purity(dm,inds), 1.0). Just need to take care of the subtlety when batched data involved

How important would you say this feature is?

1: Not important. Would be nice to have.

Additional information

While this feature is not critical, it provides a convenient method commonly used in quantum information research and algorithm development, enhancing the usability and functionality of PennyLane. This feature simplifies the process of checking state purity, a common task in quantum simulations and research. It abstracts the complexity of calculating and comparing purity values, making the user's code cleaner and more concise.

CatalinaAlbornoz commented 3 months ago

Hi @JerryChen97, thanks for the suggestion!

I think what you want is the function qml.purity(). You can add it at the return of your qnode. This way if the state is pure you'll get a '1'.

Note that this is different from qml.math.purity(), which is also available for you to use and it also returns a '1' if the state is pure.

I understand that returning a boolean directly can be useful in some cases but it would also be more limiting in others.

Given that the potential function would just do qml.math.allclose(purity_value, 1.0), I'm not sure that it's worth making a whole new function for this, although readability would be better with is_pure 🤔 .

Let's keep the conversation open for a while here and see what others think! Also feel free to add more thoughts on this if you want 😃 .

JerryChen97 commented 3 months ago

Hi @JerryChen97, thanks for the suggestion!

I think what you want is the function qml.purity(). You can add it at the return of your qnode. This way if the state is pure you'll get a '1'.

Note that this is different from qml.math.purity(), which is also available for you to use and it also returns a '1' if the state is pure.

I understand that returning a boolean directly can be useful in some cases but it would also be more limiting in others.

Given that the potential function would just do qml.math.allclose(purity_value, 1.0), I'm not sure that it's worth making a whole new function for this, although readability would be better with is_pure 🤔 .

Let's keep the conversation open for a while here and see what others think! Also feel free to add more thoughts on this if you want 😃 .

Thanks @CatalinaAlbornoz ! Indeed I totally agree this is not a big issue so I tagged it as 'not important'. Readability is the most concerned aspect; is_pure is needed only due to the fact that in many places it happens a lot that the researchers only care about whether or not a state is pure instead of the specific value of purity. Since PennyLane is still in dev stage with many other much more important features to build we should leave this as something in future.

CatalinaAlbornoz commented 3 months ago

Good point @JerryChen97! We can leave it open and decide if/how we want to tackle this in the future. The question can get more complicated if you consider that qml.math.allclose() has a default tolerance of 1e-08 but some people may argue that is_pure should return exact purity instead of "purity up to a certain tolerance". Anyways, just something to consider for a future possible implementation.