-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix interface detection with closure variables (#6892)
**Context:** We were not properly detecting the interface of a qnode when all parameters were being passed as closure variables: ``` import jax import numpy as np V = jax.numpy.array([[ 0.53672126+0.j , -0.1126064 -2.41479668j], [-0.1126064 +2.41479668j, 1.48694623+0.j ]]) eigen_vals, eigen_vecs = jax.numpy.linalg.eigh(V) umat = eigen_vecs.T wires = range(len(umat)) @jax.jit @qml.qnode(qml.device("lightning.qubit", wires = wires)) def circuit(): qml.BasisRotation(wires=wires, unitary_matrix=umat) return qml.state() print(circuit()) ``` Since with the qnode, we detect the interface from what it gets called with, we did not detect that the `umat` was a jax variable, and that it's decomposition would produce tracers. **Description of the Change:** We stop detecting the interface from what the qnode is called with. We only detect the interface from what is on the tape. We also detecting jitting by creating a new array and seeing if it a tracer. Closure variables are not tracers until they are transformed, so detecting jitting with unmodified closure variables will not work. **Benefits:** Better use of closure variables. **Possible Drawbacks:** **Related GitHub Issues:** --------- Co-authored-by: Andrija Paurevic <46359773+andrijapau@users.noreply.github.com>
- Loading branch information
1 parent
04bfe4d
commit 58d4f4f
Showing
18 changed files
with
78 additions
and
115 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2018-2025 Xanadu Quantum Technologies Inc. | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
Unit tests for reference qubit. | ||
""" | ||
|
||
import pytest | ||
|
||
import pennylane as qml | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"interface", | ||
( | ||
pytest.param("autograd", marks=pytest.mark.autograd), | ||
pytest.param("jax", marks=pytest.mark.jax), | ||
pytest.param("torch", marks=pytest.mark.torch), | ||
pytest.param("tensorflow", marks=pytest.mark.tf), | ||
), | ||
) | ||
def test_error_on_non_numpy_data(interface): | ||
"""Test that an error is thrown in the interface data is not numpy.""" | ||
|
||
x = qml.math.asarray(0.5, like=interface) | ||
tape = qml.tape.QuantumScript([qml.RX(x, 0)], [qml.expval(qml.Z(0))]) | ||
dev = qml.device("reference.qubit", wires=1) | ||
|
||
with pytest.raises(ValueError, match="Reference qubit can only work with numpy data."): | ||
dev.execute(tape) |
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
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