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

Controlled Rotation gates added #251

Merged
merged 14 commits into from Jul 11, 2019

Conversation

Projects
None yet
4 participants
@AroosaIjaz
Copy link
Contributor

commented Jun 26, 2019

Description of the Change:

  1. Added qubit operators CRX, CRY, CRZ, CRot gates in qubit.py

  2. Fixed an error in the documentation of Rot operator. It says number of parameters: 1. it should be number of parameters: 3

Screenshot from 2019-06-26 16-57-00

Benefits:

This adds to our set of operators. I might need to use these operators in our current project.

@co9olguy

This comment has been minimized.

Copy link
Member

commented Jun 27, 2019

Thanks @AroosaIjaz, looks like a useful addition!

@co9olguy

This comment has been minimized.

Copy link
Member

commented Jun 27, 2019

Note that for any new features, new tests should be included. See here: https://github.com/XanaduAI/pennylane/blob/master/.github/PULL_REQUEST_TEMPLATE.md

@co9olguy

This comment has been minimized.

Copy link
Member

commented Jun 27, 2019

Hi @AroosaIjaz, I notice the current test suite is failing for your PR. It looks like the culprit is default.qubit. We have a test to ensure that it supports all native qubit ops in PL. You'll also have to add matrix representations of these new gates here: https://github.com/XanaduAI/pennylane/blob/master/pennylane/plugins/default_qubit.py
and import them into the test here: https://github.com/XanaduAI/pennylane/blob/master/tests/test_default_qubit.py

@josh146

This comment has been minimized.

Copy link
Member

commented Jun 27, 2019

Echoing Nathan, this is a great addition to have in PennyLane, thanks Aroosa!

After adding the matrix representation of the gates of default_qubit, you can add your own test function at the bottom of test_qnode.py if you would like. This would be a pytest style test function, to test the gradients of controlled rotation gates. For example, something like the following:

def test_controlled_RX_gradient(tol):
    """Test gradient of controlled RX gate"""
    dev = qml.device('default.qubit', wires=2)

    @qml.qnode(dev)
    def circuit(x):
        qml.CRX(x, wires=[0, 1])
        return qml.expval(qml.PauliZ(0))

    a = 0.7664

	# get the analytic gradient
    gradA = circuit.jacobian([a], method='A')
	# get the finite difference gradient
    gradF = circuit.jacobian([a], method='F')

	# the expected gradient
    expected = # put explicit numpy expression in terms of 'a' here

    assert np.allclose(gradF, expected, atol=tol, rtol=0)
    assert np.allclose(gradA, expected, atol=tol, rtol=0)

Feel free to ping me if you have any questions!

@josh146 josh146 added the WIP label Jun 27, 2019

@codecov

This comment has been minimized.

Copy link

commented Jun 27, 2019

Codecov Report

❗️ No coverage uploaded for pull request base (master@f557d21). Click here to learn what that means.
The diff coverage is 100%.

@@           Coverage Diff           @@
##             master   #251   +/-   ##
=======================================
  Coverage          ?   100%           
=======================================
  Files             ?     28           
  Lines             ?   1947           
  Branches          ?      0           
=======================================
  Hits              ?   1947           
  Misses            ?      0           
  Partials          ?      0
Impacted Files Coverage Δ
pennylane/ops/qubit.py 100% <100%> (ø)
pennylane/plugins/default_qubit.py 100% <100%> (ø)

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 f557d21...42091a2. Read the comment docs.

@AroosaIjaz

This comment has been minimized.

Copy link
Contributor Author

commented Jun 27, 2019

Thanks!

@co9olguy I have added the matrices in the files you suggested. It passes the current tests. I will try to write tests for these operators too. This is very unfamiliar territory though.

@josh146 , Should I also add something like this for controlled gates in test_default_qubit.py?

def test_x_rotation(self):
        #test x rotation is correct
        self.logTestName()

        # test identity for theta=0
        self.assertAllAlmostEqual(Rotx(0), np.identity(2), delta=self.tol)

        # test identity for theta=pi/2
        expected = np.array([[1, -1j], [-1j, 1]]) / np.sqrt(2)
        self.assertAllAlmostEqual(Rotx(np.pi / 2), expected, delta=self.tol)

        # test identity for theta=pi
        expected = -1j * np.array([[0, 1], [1, 0]])
        self.assertAllAlmostEqual(Rotx(np.pi), expected, delta=self.tol)

Also what do gradient methods gradA and gradF mean?

@josh146

This comment has been minimized.

Copy link
Member

commented Jun 28, 2019

Yes, that would be great if you want to add something like that in test_default_qubit 🙂 Just be aware that test_defualt_qubit is currently written using the older unittest style - we are in the process of transitioning to the new PyTest style of tests (like I commented above). So if you want, you could write the test function using PyTest style.

Also what do gradient methods gradA and gradF mean?

gradA is the analytic gradient, computed via the parameter shift rule, and gradF is the numerical gradient computed via finite difference. This test checks that both gradient computations agree with the expected gradient result

@co9olguy

This comment has been minimized.

Copy link
Member

commented Jun 28, 2019

@AroosaIjaz Great, thanks! Some additional tests for these gates would definitely be appreciated. You can follow @josh146 's suggestion above, and replace expected with the correct expval formula that you can work out by hand. You might need to replace the circuits with something more useful though, in case the circuit in @josh146's comment only produces a trivial gradient

@Eman919191

This comment has been minimized.

Copy link

commented Jul 8, 2019

is this function available in PennyLane?

@AroosaIjaz

This comment has been minimized.

Copy link
Contributor Author

commented Jul 10, 2019

@Eman919191 This PR has not been merged into the master yet so you would not find it in the latest version. I am working on it and it might become available by the end of the month :) In case, you want to use it now, you can directly clone from this PR branch.

@AroosaIjaz

This comment has been minimized.

Copy link
Contributor Author

commented Jul 10, 2019

@co9olguy @josh146 I have added the tests for all the controlled rotation gates. I checked the test files by running them with pytest. I also checked that there are no issues in the main directory with make test.

@co9olguy
Copy link
Member

left a comment

Thanks @AroosaIjaz! I left a few comments/suggestions.
My major request is to update your tests to have a nonzero gradient case for each gate (more useful for detecting different edge cases than two circuits each with zero gradient).

Show resolved Hide resolved pennylane/ops/qubit.py Outdated
Show resolved Hide resolved pennylane/ops/qubit.py Outdated
Show resolved Hide resolved pennylane/ops/qubit.py Outdated
Show resolved Hide resolved pennylane/ops/qubit.py Outdated
Show resolved Hide resolved pennylane/plugins/default_qubit.py Outdated
Show resolved Hide resolved pennylane/plugins/default_qubit.py Outdated
Show resolved Hide resolved pennylane/plugins/default_qubit.py Outdated
Show resolved Hide resolved tests/test_default_qubit.py
Show resolved Hide resolved tests/test_qnode.py Outdated
Show resolved Hide resolved tests/test_qnode.py Outdated

AroosaIjaz and others added some commits Jul 10, 2019

Update pennylane/plugins/default_qubit.py
Co-Authored-By: Nathan Killoran <co9olguy@users.noreply.github.com>
@AroosaIjaz

This comment has been minimized.

Copy link
Contributor Author

commented Jul 11, 2019

@co9olguy I have added non-zero gradient tests.

@co9olguy
Copy link
Member

left a comment

Thanks @AroosaIjaz, just two more small changes before it's ready!

  1. can you replace the expected non-zero gradient values with -np.sin(b) (easier for maintainability)
  2. you can remove the lines a=... since the gradient is zero, we never use a
Show resolved Hide resolved tests/test_qnode.py Outdated
Show resolved Hide resolved tests/test_qnode.py Outdated
Show resolved Hide resolved tests/test_qnode.py Outdated

@co9olguy co9olguy added plugins and removed WIP labels Jul 11, 2019

@AroosaIjaz

This comment has been minimized.

Copy link
Contributor Author

commented Jul 11, 2019

This is ready for a final review :)

@co9olguy
Copy link
Member

left a comment

Thanks @AroosaIjaz, and sorry I didn't catch before that you were using a as a parameter to the jacobian 👍

@co9olguy co9olguy merged commit 5d992af into XanaduAI:master Jul 11, 2019

3 of 4 checks passed

CodeFactor 1 issue fixed. 3 issues found.
Details
Travis CI - Pull Request Build Passed
Details
codecov/patch No report found to compare against
Details
codecov/project No report found to compare against
Details
@AroosaIjaz

This comment has been minimized.

Copy link
Contributor Author

commented Jul 11, 2019

Thanks @AroosaIjaz, and sorry I didn't catch before that you were using a as a parameter to the jacobian

No worries! Thank you for your feedback! :)

@AroosaIjaz

This comment has been minimized.

Copy link
Contributor Author

commented Jul 11, 2019

is this function available in PennyLane?

@Eman919191 We sped up the process and added the controlled rotation gates to PennyLane. Thanks a lot for your feedback! Just update your PennyLane software from the github master branch and you should be good to go :)

@AroosaIjaz AroosaIjaz deleted the AroosaIjaz:contRgate branch Jul 11, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.