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

Add validation for noise channels parameters #1357

Merged
merged 8 commits into from
May 28, 2021

Conversation

nahumsa
Copy link
Contributor

@nahumsa nahumsa commented May 22, 2021

Context: Noise channels do not validate parameter values

Description of the Change: Add validation for noise channel parameters, now it raises an ValueError when
noise parameters are invalid.

Benefits: It no longer raises a RuntimeWarning when using invalid parameters on noise channels.

Possible Drawbacks:

Related GitHub Issues: Closes #1337

@nahumsa nahumsa changed the title add ValueError for invalid parameter values add ValueError for invalid parameter values on Noise Channels May 22, 2021
@nahumsa nahumsa changed the title add ValueError for invalid parameter values on Noise Channels Add validation for noise channels parameters May 22, 2021
@codecov
Copy link

codecov bot commented May 22, 2021

Codecov Report

Merging #1357 (0dae020) into master (be3c88d) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1357   +/-   ##
=======================================
  Coverage   98.15%   98.15%           
=======================================
  Files         154      154           
  Lines       11492    11512   +20     
=======================================
+ Hits        11280    11300   +20     
  Misses        212      212           
Impacted Files Coverage Δ
pennylane/ops/channel.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update be3c88d...0dae020. Read the comment docs.

Copy link
Member

@josh146 josh146 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 fixing this one @nahumsa 💪

I've left a couple of preliminary comments, but will bring in @ixfoduap for any extra comments or suggestions.

.github/CHANGELOG.md Outdated Show resolved Hide resolved
@@ -58,6 +58,10 @@ class AmplitudeDamping(Channel):
@classmethod
def _kraus_matrices(cls, *params):
gamma = params[0]

if gamma < 0.0 or gamma > 1.0:
Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, you could have

Suggested change
if gamma < 0.0 or gamma > 1.0:
if not 0 <= gamma <= 1.0:

I don't think this is any more efficient, but it might be more readable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, it looks more readable to me.

@josh146 josh146 requested a review from ixfoduap May 23, 2021 14:52
@josh146 josh146 added the bug 🐛 Something isn't working label May 23, 2021
Copy link
Contributor

@ixfoduap ixfoduap left a comment

Choose a reason for hiding this comment

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

Hi @nahumsa, thanks for another great contribution!

Everything looks in order, but I have an important concern. I remember removing a warning like this because it was throwing errors when the channel parameters where trainable parameters. If I'm not mistaken, the issue was that a check like

if not 0 <= gamma <= 1.0:

would fail if gamma was not a float. The ability to train noise parameters is a unique feature of PennyLane, as shown in the noisy circuit tutorial, so we should be mindful not to break this functionality.

Please add some tests that gradients can be computed correctly and circuits can be trained, and I'll be happy to approve. If the tests fail, then you should still be able to adapt the checks so that no issues occur

@nahumsa
Copy link
Contributor Author

nahumsa commented May 27, 2021

Hi @ixfoduap!

Thanks for pointing that out! I have a question regarding this test, it would be an end-to-end test just like the example, or just computing the gradient for a given example would be enough?

@ixfoduap
Copy link
Contributor

I would like to test that the gradient of a simple circuit is computed correctly. Actually maybe such tests are already done for some of the channels. If that's the case, then I guess no more code needs to be written. Basically I just want to make sure that we don't ship breaking code.

This doesn't have to appear as an actual integration test, but I would be more comfortable if you could run the examples from the tutorial from a pennylane version from this branch and check that everything goes well.

@nahumsa
Copy link
Contributor Author

nahumsa commented May 27, 2021

I think that this test is covered here:

    @pytest.mark.parametrize("angle", np.linspace(0, 2 * np.pi, 7))
    def test_grad_bitflip(self, angle, tol):
        """Test that analytical gradient is computed correctly for different states. Channel
        grad recipes are independent of channel parameter"""

        dev = qml.device("default.mixed", wires=1)
        prob = 0.5

        @qml.qnode(dev)
        def circuit(p):
            qml.RX(angle, wires=0)
            qml.BitFlip(p, wires=0)
            return qml.expval(qml.PauliZ(0))

        gradient = np.squeeze(qml.grad(circuit)(prob))
        assert gradient == circuit(1) - circuit(0)
        assert np.allclose(gradient, (-2 * np.cos(angle)))

Please, let me know if it needs more tests about this.

Copy link
Contributor

@ixfoduap ixfoduap 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 double-checking my concern regarding gradients!

I'm happy to be reminded that we are testing this. Happy to approve from my side

Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

I tested the gradients locally using both Autograd and TensorFlow, everything continues to work for me 💪 Thanks @nahumsa!

@josh146 josh146 merged commit ccf8d7f into PennyLaneAI:master May 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Noise channels do not validate parameter values, leading to errors
3 participants