-
Notifications
You must be signed in to change notification settings - Fork 615
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
Allow device to configure conversion to numpy and use of pure_callback
#6788
Open
albi3ro
wants to merge
8
commits into
master
Choose a base branch
from
no-interface-boundary
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cbe1f20
allow device to configure conversion to numpy
albi3ro cef0e31
testing and changelog
albi3ro f6949e1
updating xfailing pulse gradient tests
albi3ro 1b069b8
fixing more tests
albi3ro a733cca
Merge branch 'master' into no-interface-boundary
albi3ro fbce8cc
fixing more tests
albi3ro df4bd13
Merge branch 'no-interface-boundary' of https://github.com/PennyLaneA…
albi3ro 2110c9b
final test
albi3ro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -161,6 +161,14 @@ def _conditional_broastcast_expand(tape): | |||||
return (tape,), null_postprocessing | ||||||
|
||||||
|
||||||
@qml.transform | ||||||
def no_counts(tape): | ||||||
"""Throws an error on counts measurements.""" | ||||||
if any(isinstance(mp, qml.measurements.CountsMP) for mp in tape.measurements): | ||||||
raise NotImplementedError("The JAX-JIT interface doesn't support qml.counts.") | ||||||
astralcai marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return (tape,), null_postprocessing | ||||||
|
||||||
|
||||||
@qml.transform | ||||||
def adjoint_state_measurements( | ||||||
tape: QuantumScript, device_vjp=False | ||||||
|
@@ -535,6 +543,8 @@ def preprocess( | |||||
config = self._setup_execution_config(execution_config) | ||||||
transform_program = TransformProgram() | ||||||
|
||||||
if config.interface == qml.math.Interface.JAX_JIT: | ||||||
transform_program.add_transform(no_counts) | ||||||
transform_program.add_transform(validate_device_wires, self.wires, name=self.name) | ||||||
transform_program.add_transform( | ||||||
mid_circuit_measurements, device=self, mcm_config=config.mcm_config | ||||||
|
@@ -581,6 +591,12 @@ def _setup_execution_config(self, execution_config: ExecutionConfig) -> Executio | |||||
""" | ||||||
updated_values = {} | ||||||
|
||||||
updated_values["convert_to_numpy"] = ( | ||||||
execution_config.interface.value not in {"jax", "jax-jit"} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to continue using the Interface enum instead?
Suggested change
|
||||||
or execution_config.gradient_method == "adjoint" | ||||||
astralcai marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
# need numpy to use caching, and need caching higher order derivatives | ||||||
or execution_config.derivative_order > 1 | ||||||
) | ||||||
for option in execution_config.device_options: | ||||||
if option not in self._device_options: | ||||||
raise qml.DeviceError(f"device option {option} not present on {self}") | ||||||
|
@@ -616,7 +632,6 @@ def execute( | |||||
execution_config: ExecutionConfig = DefaultExecutionConfig, | ||||||
) -> Union[Result, ResultBatch]: | ||||||
self.reset_prng_key() | ||||||
|
||||||
max_workers = execution_config.device_options.get("max_workers", self._max_workers) | ||||||
self._state_cache = {} if execution_config.use_device_jacobian_product else None | ||||||
interface = ( | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -687,12 +687,13 @@ def func(x, y): | |||||||
assert tape.measurements == contents[3:] | ||||||||
|
||||||||
@pytest.mark.jax | ||||||||
def test_jit_counts_raises_error(self): | ||||||||
@pytest.mark.parametrize("dev_name", ("default.qubit", "reference.qubit")) | ||||||||
def test_jit_counts_raises_error(self, dev_name): | ||||||||
"""Test that returning counts in a quantum function with trainable parameters while | ||||||||
jitting raises an error.""" | ||||||||
import jax | ||||||||
|
||||||||
dev = qml.device("default.qubit", wires=2, shots=5) | ||||||||
dev = qml.device(dev_name, wires=2, shots=5) | ||||||||
|
||||||||
def circuit1(param): | ||||||||
qml.Hadamard(0) | ||||||||
|
@@ -706,7 +707,8 @@ def circuit1(param): | |||||||
with pytest.raises( | ||||||||
NotImplementedError, match="The JAX-JIT interface doesn't support qml.counts." | ||||||||
): | ||||||||
jitted_qnode1(0.123) | ||||||||
out = jitted_qnode1(0.123) | ||||||||
print(out) | ||||||||
Comment on lines
+710
to
+711
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
# Test with qnode decorator syntax | ||||||||
@qml.qnode(dev) | ||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.