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

Move to specify shots on the QNode #4375

Closed
wants to merge 6 commits into from
Closed

Move to specify shots on the QNode #4375

wants to merge 6 commits into from

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Jul 20, 2023

We are moving to define shots as part of what we execute, not necessarily how we execute it.

We already:

  • Allow shots to be overridden on each call in the QNode
  • Allow override_shots to be provided to qml.execute
  • Allow shots to be specified in the QuantumTape
  • Purely take shots from the QuantumTape in the new device API.

These changes will allow us to have better shot distribution within batches, since each tape in a batch will be able to have a separate shot number.

This will also help PennyLane become more functional, since we will not be needing to constantly mutate the device in order to support per-call dynamic shots.

Since these warnings are inside the QNode and the qml.device constructor, no changes are currently necessary for plugins other than updated tests and documentation.

Example behaviour for deprecation cycle

import pennylane as qml
import warnings
warnings.simplefilter("always")

Don't specify shots on device? No warning

qml.device('braket.local.qubit', wires=2)
<BraketLocalQubitDevice device (wires=2, shots=None) at 0x14001c400>

Manually specify shots on device? Warning

qml.device('braket.local.qubit', wires=2, shots=None)
/Users/christina/Prog/pennylane/pennylane/__init__.py:309: UserWarning: In v0.33, the shots will always be determined by the QNode. Please specify shots there.
  warnings.warn("In v0.33, the shots will always be determined by the QNode. Please specify shots there.", UserWarning)
<BraketLocalQubitDevice device (wires=2, shots=None) at 0x17fbd8a00>

Don't specify shots on either device or qnode? No warning

dev = qml.device('braket.local.qubit', wires=2)

@qml.qnode(dev)
def circuit():
    return qml.expval(qml.PauliX(0))

Specify shots on device but not qnode?

warning, and make qnode shots equal to device shots

dev = qml.device('braket.local.qubit', wires=2, shots=10)
/Users/christina/Prog/pennylane/pennylane/__init__.py:309: UserWarning: In v0.33, the shots will always be determined by the QNode. Please specify shots there.
  warnings.warn("In v0.33, the shots will always be determined by the QNode. Please specify shots there.", UserWarning)
@qml.qnode(dev)
def circuit():
    return qml.expval(qml.PauliX(0))

circuit(), circuit.default_shots
/Users/christina/Prog/pennylane/pennylane/qnode.py:448: UserWarning: Shots should now be specified on the qnode instead of on the device.Using shots from the device. QNode specified shots will be used in v0.33.
  warnings.warn(

(tensor(0.2, requires_grad=True),
 Shots(total_shots=10, shot_vector=(ShotCopies(10 shots x 1),)))

Shots not none on qnode? no warning when creating the qnode, but there is a warning when putting on them on the device still.

dev = qml.device('lightning.qubit', wires=2, shots=1000)
/Users/christina/Prog/pennylane/pennylane/__init__.py:309: UserWarning: In v0.33, the shots will always be determined by the QNode. Please specify shots there.
  warnings.warn("In v0.33, the shots will always be determined by the QNode. Please specify shots there.", UserWarning)
@qml.qnode(dev, shots=2)
def circuit():
    return qml.sample(wires=0)

circuit()
tensor([0, 0], requires_grad=True)

@github-actions
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@albi3ro
Copy link
Contributor Author

albi3ro commented Jul 20, 2023

[sc-41930]

@albi3ro albi3ro marked this pull request as ready for review July 20, 2023 14:43
@albi3ro albi3ro requested a review from trbromley July 20, 2023 14:44
Copy link
Contributor

@trbromley trbromley left a comment

Choose a reason for hiding this comment

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

Thanks @albi3ro , I left some wording suggestions.

@@ -305,6 +305,12 @@ def run_cnot():
if name in plugin_devices:
options = {}

if "shots" in kwargs:
warnings.warn(
"In v0.33, the shots will always be determined by the QNode. Please specify shots there.",
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we say "Please specify shots either at QNode instantiation or when calling your QNode."

@@ -305,6 +305,12 @@ def run_cnot():
if name in plugin_devices:
options = {}

if "shots" in kwargs:
warnings.warn(
"In v0.33, the shots will always be determined by the QNode. Please specify shots there.",
Copy link
Contributor

Choose a reason for hiding this comment

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

And maybe "You have provided a shots argument to a device. In v0.33, ..."
Just so it's immediately clear to users what they've done wrong.

and shots is None
):
warnings.warn(
"Shots should now be specified on the qnode instead of on the device."
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"Shots should now be specified on the qnode instead of on the device."
"The shots value you provided to the device does not match the value provided to the QNode."

):
warnings.warn(
"Shots should now be specified on the qnode instead of on the device."
"Using shots from the device. QNode specified shots will be used in v0.33."
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"Using shots from the device. QNode specified shots will be used in v0.33."
"Using shots from the device. QNode specified shots will be used in v0.33 "
"and shots will no longer be permitted as a device argument."

@albi3ro
Copy link
Contributor Author

albi3ro commented Jul 31, 2023

Closed in favor of #4388

@albi3ro albi3ro closed this Jul 31, 2023
@albi3ro albi3ro deleted the qnode-shots branch July 31, 2023 20:06
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.

2 participants