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

Automatically determine interface from constructed quantum script #3132

Merged
merged 81 commits into from
Oct 28, 2022

Conversation

AlbertMitjans
Copy link
Contributor

@AlbertMitjans AlbertMitjans commented Oct 5, 2022

The QNode class now accepts an auto interface, which automatically detects the interface of the given input.

dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev, interface="auto")
def circuit(weight):
    qml.RX(weight[0], wires=0)
    qml.RY(weight[1], wires=1)
    return qml.expval(qml.PauliZ(0))

interface_tensors = [
    np.array([0, 1]),
    torch.Tensor([0, 1]),
    tf.Variable([0, 1], dtype=float),
    jnp.array([0, 1]),
    [0, 1],
]

for tensor in interface_tensors:
    res = circuit(weight=tensor)
    print(res, type(res))

Prints

1.0 <class 'pennylane.numpy.tensor.tensor'>
tensor(1.0000, dtype=torch.float64) <class 'torch.Tensor'>
tf.Tensor(0.9999999896704281, shape=(), dtype=float64) <class 'tensorflow.python.framework.ops.EagerTensor'>
1.0 <class 'jaxlib.xla_extension.DeviceArray'>
1.0 <class 'pennylane.numpy.tensor.tensor'>

@codecov
Copy link

codecov bot commented Oct 5, 2022

Codecov Report

Merging #3132 (964da3d) into master (afb2070) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #3132   +/-   ##
=======================================
  Coverage   99.69%   99.69%           
=======================================
  Files         276      276           
  Lines       24273    24292   +19     
=======================================
+ Hits        24200    24219   +19     
  Misses         73       73           
Impacted Files Coverage Δ
pennylane/interfaces/execution.py 100.00% <100.00%> (ø)
pennylane/qnode.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@AlbertMitjans AlbertMitjans added the do not merge ⚠️ Do not merge the pull request until this label is removed label Oct 6, 2022
@rmoyard
Copy link
Contributor

rmoyard commented Oct 26, 2022

Hi @AlbertMitjans, great changes! One thing that should be done is adding those changes to _execute_new in the execution.py file. Also do you think we could change the default interface to auto?

@AlbertMitjans
Copy link
Contributor Author

AlbertMitjans commented Oct 26, 2022

One thing that should be done is adding those changes to _execute_new in the execution.py file.

We are calling _execute_new with the updated interface, thus I believe we don't need to add any logic there.

Also do you think we could change the default interface to auto?

@albi3ro mentioned that we should first try this out as an optional feature to make sure it works correctly. If it does we can make it the default.

@antalszava antalszava self-requested a review October 26, 2022 15:23
@rmoyard
Copy link
Contributor

rmoyard commented Oct 26, 2022

@AlbertMitjans Thanks for the second point! Our methodology for the return type project is to duplicate the function (one public and one private). Right at the beginning, the public one calls the private one. The private one should be able to replace the public one without having to double check if something is missing (when we will deprecate it). So in this case I would suggest to move the code bloc after the qml.active_return and add the bloc in the function _execute_new.

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.

As per Romain's find, will just double-check that the new return-type pipeline works as expected.

@AlbertMitjans
Copy link
Contributor Author

@AlbertMitjans Thanks for the second point! Our methodology for the return type project is to duplicate the function (one public and one private). Right at the beginning, the public one calls the private one. The private one should be able to replace the public one without having to double check if something is missing (when we will deprecate it). So in this case I would suggest to move the code bloc after the qml.active_return and add the bloc in the function _execute_new.

Oh OK! I did what you suggested. Thanks for this catch! 🎉

@rmoyard
Copy link
Contributor

rmoyard commented Oct 26, 2022

Thanks @AlbertMitjans ! 💯 You can add ["autograd", "auto"] in the test file test_qnode_autograd_new.py and test_autograd_new.py, it should solve the coverage issues.

@AlbertMitjans
Copy link
Contributor Author

AlbertMitjans commented Oct 27, 2022

You can add ["autograd", "auto"] in the test file test_qnode_autograd_new.py and test_autograd_new.py, it should solve the coverage issues.

Added! @rmoyard

@albi3ro albi3ro added this to the v0.27.0 milestone Oct 27, 2022
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

The docstring for QNode and execute should be updated to include the option "auto".

@AlbertMitjans
Copy link
Contributor Author

The docstring for QNode and execute should be updated to include the option "auto".

Done!

pennylane/qnode.py Outdated Show resolved Hide resolved
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Glad to have this! 👍 Looks good to me.

@Jaybsoni Jaybsoni removed their request for review October 28, 2022 05:48
AlbertMitjans and others added 4 commits October 28, 2022 09:37
Co-authored-by: Christina Lee <christina@xanadu.ai>
Co-authored-by: Christina Lee <christina@xanadu.ai>
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! 🎉 Awesome feature, great work 🙂 😍

@AlbertMitjans AlbertMitjans merged commit 12a9d51 into master Oct 28, 2022
@AlbertMitjans AlbertMitjans deleted the automatic-interface branch October 28, 2022 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants