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.35k stars 603 forks source link

[unitaryhack] Add the CPhase operation #1147

Closed antalszava closed 3 years ago

antalszava commented 3 years ago

This issue has been tagged for contributions during unitaryHACK

Context We would like to define a new diagonal operation called CPhase, see further details here.

Steps to take

This would involve the following steps to be completed:

A. Adding the operation

  1. Adding the CPhase class and its attributes to pennylane/ops/qubit.py. This class would inherit from DiagonalOperation. Complete its docstring in a similar fashion as for other operations. Make sure the class is accessible through qml.CPhase.
  2. Add its decomposition: a definition for how the operation could be decomposed into elementary operations,
  3. Add its matrix, eigenvalues, adjoint and generator,
  4. Test: include tests for the operation in the tests/ops/test_qubit_ops.py file,

In these steps, having a look at how class PhaseShift is defined can help. Also, see the operation as defined in the PennyLane-Cirq plugin as a basis.

B. Adding default.qubit support

  1. Add support for the operation in the default.qubit device,
  2. Test: test the operation with default.qubit in the tests/devices/test_default_qubit.py file.

Bonus: Add support to the interface-specific default.qubit devices too: default.qubit.autograd and default.qubit.tf. This will require the operation to be defined in a framework-specific way. These devices inherit the supported operations from default.qubit, so if this step is not tackled, the CPHASE can be removed from the operations dictionary for default.qubit.autograd and default.qubit.tf.

nahumsa commented 3 years ago

Hi! I would like to work on this issue.

antalszava commented 3 years ago

Hi @nahumsa, great! :slightly_smiling_face: Thank you for your interest :tada:

Feel free to go for it and open a work in progress pull request whenever you have a draft version. Also, feel free to ask any questions along the way! :slightly_smiling_face:

nahumsa commented 3 years ago

Sure! Thanks for the quick reply!

nahumsa commented 3 years ago

I was looking at the pennylane/ops/qubit.py and verified that there is an implementation of ControlledPhaseShift that looks exactly like the CPhase gate. Does it need to be reimplemented or would just add an alias for the CPhase command?

josh146 commented 3 years ago

Hey @nahumsa, great catch, thanks for pointing this out! Indeed, it seems that this gate already exists. @antalszava was there a difference you had in mind?

nahumsa commented 3 years ago

I added the CPhase gate on a PR because just in case, but I could easily convert the PR to become an alias of the ControlledPhaseShift, or delete it if necessary.

josh146 commented 3 years ago

Thanks @nahumsa! To avoid the code duplication, I think it would make sense to have it be an alias (as you rightly point out in the PR!)