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
Remove norm check for jax.jit functions in QubitStateVector
#1683
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c3d59e4
remove newlines in docstring (#1647)
thisac ed6082a
Merge branch 'v0.18.0-rc0' of https://github.com/PennyLaneAI/pennylane
rmoyard e80b914
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane
rmoyard 101fb1d
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane
rmoyard 1cc5e1e
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane
rmoyard 6e940ce
Differentiate jit jax and jax
rmoyard cd71d11
Update
rmoyard 484b85d
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
rmoyard 12c4fbc
Add tests.
rmoyard 883473d
Black
rmoyard 2e7c09b
avoid import jax
rmoyard 6bbf192
Update comments and tests.
rmoyard 4f1a892
Add changelog.
rmoyard b29d5f4
jnp to np
rmoyard e26dbd7
jnp.array for jnp.allclose
rmoyard 3ba3c3e
jnp.array
rmoyard 3754765
Differentiate the case more explicitly.
rmoyard 55ad9ef
Merge branch 'master' into jaxitnorm
rmoyard File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
|
@@ -172,6 +172,96 @@ def inner_circuit(): | |
np.testing.assert_array_equal(a, b) | ||
assert not np.all(a == c) | ||
|
||
@pytest.mark.parametrize( | ||
"state_vector", | ||
[np.array([0.5 + 0.5j, 0.5 + 0.5j, 0, 0]), jnp.array([0.5 + 0.5j, 0.5 + 0.5j, 0, 0])], | ||
) | ||
def test_qubit_state_vector_arg_jax_jit(self, state_vector, tol): | ||
"""Test that Qubit state vector as argument works with a jax.jit""" | ||
dev = qml.device("default.qubit.jax", wires=list(range(2))) | ||
|
||
@jax.jit | ||
@qml.qnode(dev, interface="jax") | ||
def circuit(x): | ||
wires = list(range(2)) | ||
qml.QubitStateVector(x, wires=wires) | ||
return [qml.expval(qml.PauliX(wires=i)) for i in wires] | ||
|
||
res = circuit(state_vector) | ||
assert jnp.allclose(jnp.array(res), jnp.array([0, 1]), atol=tol, rtol=0) | ||
|
||
@pytest.mark.parametrize( | ||
"state_vector", | ||
[np.array([0.5 + 0.5j, 0.5 + 0.5j, 0, 0]), jnp.array([0.5 + 0.5j, 0.5 + 0.5j, 0, 0])], | ||
) | ||
def test_qubit_state_vector_arg_jax(self, state_vector, tol): | ||
"""Test that Qubit state vector as argument works with jax""" | ||
dev = qml.device("default.qubit.jax", wires=list(range(2))) | ||
|
||
@qml.qnode(dev, interface="jax") | ||
def circuit(x): | ||
wires = list(range(2)) | ||
qml.QubitStateVector(x, wires=wires) | ||
return [qml.expval(qml.PauliX(wires=i)) for i in wires] | ||
|
||
res = circuit(state_vector) | ||
assert jnp.allclose(jnp.array(res), jnp.array([0, 1]), atol=tol, rtol=0) | ||
|
||
@pytest.mark.parametrize( | ||
"state_vector", | ||
[np.array([0.5 + 0.5j, 0.5 + 0.5j, 0, 0]), jnp.array([0.5 + 0.5j, 0.5 + 0.5j, 0, 0])], | ||
) | ||
def test_qubit_state_vector_jax_jit(self, state_vector, tol): | ||
"""Test that Qubit state vector works with a jax.jit""" | ||
dev = qml.device("default.qubit.jax", wires=list(range(2))) | ||
|
||
@jax.jit | ||
@qml.qnode(dev, interface="jax") | ||
def circuit(x): | ||
qml.QubitStateVector(state_vector, wires=dev.wires) | ||
for w in dev.wires: | ||
qml.RZ(x, wires=w, id="x") | ||
return qml.expval(qml.PauliZ(wires=0)) | ||
|
||
res = circuit(0.1) | ||
assert jnp.allclose(jnp.array(res), 1, atol=tol, rtol=0) | ||
|
||
@pytest.mark.parametrize( | ||
"state_vector", | ||
[np.array([0.5 + 0.5j, 0.5 + 0.5j, 0, 0]), jnp.array([0.5 + 0.5j, 0.5 + 0.5j, 0, 0])], | ||
) | ||
def test_qubit_state_vector_jax(self, state_vector, tol): | ||
"""Test that Qubit state vector works with a jax""" | ||
dev = qml.device("default.qubit.jax", wires=list(range(2))) | ||
|
||
@qml.qnode(dev, interface="jax") | ||
def circuit(x): | ||
qml.QubitStateVector(state_vector, wires=dev.wires) | ||
for w in dev.wires: | ||
qml.RZ(x, wires=w, id="x") | ||
return qml.expval(qml.PauliZ(wires=0)) | ||
|
||
res = circuit(0.1) | ||
assert jnp.allclose(jnp.array(res), 1, atol=tol, rtol=0) | ||
|
||
@pytest.mark.parametrize( | ||
"state_vector", | ||
[np.array([0.1 + 0.1j, 0.2 + 0.2j, 0, 0]), jnp.array([0.1 + 0.1j, 0.2 + 0.2j, 0, 0])], | ||
) | ||
def test_qubit_state_vector_jax_not_normed(self, state_vector, tol): | ||
"""Test that an error is raised when Qubit state vector is not normed works with a jax""" | ||
dev = qml.device("default.qubit.jax", wires=list(range(2))) | ||
|
||
@qml.qnode(dev, interface="jax") | ||
def circuit(x): | ||
qml.QubitStateVector(state_vector, wires=dev.wires) | ||
for w in dev.wires: | ||
qml.RZ(x, wires=w, id="x") | ||
return qml.expval(qml.PauliZ(wires=0)) | ||
|
||
with pytest.raises(ValueError, match="Sum of amplitudes-squared does not equal one."): | ||
circuit(0.1) | ||
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. really nice tests @rmoyard! |
||
|
||
def test_sampling_op_by_op(self): | ||
"""Test that op-by-op sampling works as a new user would expect""" | ||
dev = qml.device("default.qubit.jax", wires=1, shots=1000) | ||
|
@@ -372,7 +462,7 @@ def cost(a, b): | |
|
||
grad = jax.jit(jax.grad(cost, argnums=(0, 1)))(a, b) | ||
expected = [jnp.sin(a) * jnp.cos(b), jnp.cos(a) * jnp.sin(b)] | ||
assert jnp.allclose(grad, expected, atol=tol, rtol=0) | ||
assert jnp.allclose(jnp.array(grad), jnp.array(expected), atol=tol, rtol=0) | ||
|
||
def test_backprop_gradient(self, tol): | ||
"""Tests that the gradient of the qnode is correct""" | ||
|
@@ -394,7 +484,7 @@ def circuit(a, b): | |
expected_grad = jnp.array( | ||
[-0.5 * jnp.sin(a) * (jnp.cos(b) + 1), 0.5 * jnp.sin(b) * (1 - jnp.cos(a))] | ||
) | ||
assert jnp.allclose(res, expected_grad, atol=tol, rtol=0) | ||
assert jnp.allclose(jnp.array(res), expected_grad, atol=tol, rtol=0) | ||
|
||
@pytest.mark.parametrize("operation", [qml.U3, qml.U3.decomposition]) | ||
@pytest.mark.parametrize("diff_method", ["backprop"]) | ||
|
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.
This is much clearer now 👍