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

[unitaryHACK] Add the CPhase operation #1319

Merged
merged 25 commits into from
May 18, 2021

Conversation

nahumsa
Copy link
Contributor

@nahumsa nahumsa commented May 15, 2021

Context: Add a new diagonal operator, CPhase.

Description of the Change: Adds the CPhase operator as an alias for the ControlledPhaseShift operator.

Benefits: Adds the CPhase diagonal operator.

Possible Drawbacks: It has the same structure as ControlledPhaseShift, maybe adds duplicated code that could be solved by using CPhase as an alias for the ControlledPhaseShift.

Related GitHub Issues: closes #1147

@josh146 josh146 added the unitaryhack Dedicated issue for Unitary Fund open-source hackathon label May 17, 2021
@josh146
Copy link
Member

josh146 commented May 17, 2021

Thanks @nahumsa! Let us know as soon as the PR is ready for review, and someone from the team will be on hand to answer any questions/provide a review 🙂

@nahumsa nahumsa changed the title [WIP] [unitaryHACK] Add the CPhase operation [unitaryHACK] Add the CPhase operation May 17, 2021
@nahumsa
Copy link
Contributor Author

nahumsa commented May 17, 2021

Hi @josh146. I guess this is ready for a review.

@nahumsa
Copy link
Contributor Author

nahumsa commented May 17, 2021

Could someone clarify why the tests/tape/test_reversible.py::TestGradients::test_gradients[CPhase-PauliX] would fail?
It was ok locally 😅

@nahumsa
Copy link
Contributor Author

nahumsa commented May 17, 2021

Is it ok just to remove qml.CPhase from analytic_qubit_ops, just like the qml.ControlledPhaseShift?

@codecov
Copy link

codecov bot commented May 17, 2021

Codecov Report

Merging #1319 (97afc35) into master (b8ea0de) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1319   +/-   ##
=======================================
  Coverage   98.16%   98.16%           
=======================================
  Files         148      148           
  Lines       11456    11457    +1     
=======================================
+ Hits        11246    11247    +1     
  Misses        210      210           
Impacted Files Coverage Δ
pennylane/devices/default_qubit.py 100.00% <ø> (ø)
pennylane/devices/default_qubit_autograd.py 100.00% <ø> (ø)
pennylane/devices/default_qubit_jax.py 94.36% <ø> (ø)
pennylane/devices/default_qubit_tf.py 90.66% <ø> (ø)
pennylane/ops/qubit.py 98.51% <100.00%> (+<0.01%) ⬆️

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 b8ea0de...97afc35. Read the comment docs.

@antalszava
Copy link
Contributor

Hi @nahumsa, thank you for exploring this addition and identifying that we already have an equivalent gate!

Could we create the alias by having

CPhase = ControlledPhaseShift

in the pennylane/pennylane/ops/qubit.py file? That would essentially mean that CPhase is another name to the same class object as ControlledPhaseShift and could help with keeping the aliasing short.

@antalszava antalszava self-requested a review May 17, 2021 17:20
@nahumsa
Copy link
Contributor Author

nahumsa commented May 17, 2021

Sure! I thought about doing just that, but at the end I think I overcomplicated the solution. 😅
Sorry about the overcomplication

@antalszava
Copy link
Contributor

No worries at all! :) Let us know if you'd like us to go for another review round or if any questions come up.

@nahumsa
Copy link
Contributor Author

nahumsa commented May 17, 2021

Would it be good to remove the tests as well, or should I keep it? Since it is exactly the same tests as ControlledPhaseShift

@antalszava
Copy link
Contributor

The test cases could be removed and potentially the existing ones that test the eigvals and decomposition of qml.ControlledPhaseShift could be parametrized such that:

    @pytest.mark.parametrize("phi", [-0.1, 0.2, 0.5])
    @pytest.mark.parametrize("cphase_op", [qml.ControlledPhaseShift, qml.CPhase])
    def test_controlled_phase_shift_decomp(self, phi):
        op = cphase_op(phi, wires=[0, 1])
        (...)

This will make sure that aliasing works fine.

@nahumsa
Copy link
Contributor Author

nahumsa commented May 17, 2021

The test cases could be removed and potentially the existing ones that test the eigvals and decomposition of qml.ControlledPhaseShift could be parametrized such that:

    @pytest.mark.parametrize("phi", [-0.1, 0.2, 0.5])
    @pytest.mark.parametrize("cphase_op", [qml.ControlledPhaseShift, qml.CPhase])
    def test_controlled_phase_shift_decomp(self, phi):
        op = cphase_op(phi, wires=[0, 1])
        (...)

This will make sure that aliasing works fine.

Awesome, just added this parameterized test, thanks!

@antalszava
Copy link
Contributor

Great! 🙏 The aliasing could be used in framework-specific files too, so we could assign the CPhase name the underlying ControlledPhaseShift function in each file to have the same effect. :)

@nahumsa
Copy link
Contributor Author

nahumsa commented May 17, 2021

Great! The aliasing could be used in framework-specific files too, so we could assign the CPhase name the underlying ControlledPhaseShift function in each file to have the same effect. :)

Sure. I'll do that.

Copy link
Contributor

@antalszava antalszava left a comment

Choose a reason for hiding this comment

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

Awesome! :) This is looking great, left two comments, but very near being ready. 🙂

tests/gate_data.py Outdated Show resolved Hide resolved
.github/CHANGELOG.md Show resolved Hide resolved
.github/CHANGELOG.md Outdated Show resolved Hide resolved
pennylane/ops/qubit.py Outdated Show resolved Hide resolved
Copy link
Contributor

@antalszava antalszava 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 to me! 🎉 🙂 Thank you @nahumsa for your contribution and for tracking down how exactly it's best to have this addition! 💪

.github/CHANGELOG.md Outdated Show resolved Hide resolved
@antalszava antalszava merged commit 8fd6785 into PennyLaneAI:master May 18, 2021
@nahumsa
Copy link
Contributor Author

nahumsa commented May 18, 2021

Looks good to me! Thank you @nahumsa for your contribution and for tracking down how exactly it's best to have this addition!

Thank you, @antalszava, for such an amazing guidance through this PR! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
unitaryhack Dedicated issue for Unitary Fund open-source hackathon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[unitaryhack] Add the CPhase operation
3 participants