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

fix QubitDensityMatrix for subsystems with jit on default.mixed #5236

Merged
merged 5 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/releases/changelog-0.35.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@

* `QubitDensityMatrix` now works with jax-jit on the `default.mixed` device.
[(#5203)](https://github.com/PennyLaneAI/pennylane/pull/5203)
[(#5236)](https://github.com/PennyLaneAI/pennylane/pull/5236)

* When a QNode specifies `diff_method="adjoint"`, `default.qubit` no longer tries to decompose non-trainable operations with non-scalar parameters such as `QubitUnitary`.
[(#5233)](https://github.com/PennyLaneAI/pennylane/pull/5233)
Expand Down
2 changes: 1 addition & 1 deletion pennylane/devices/default_mixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def _apply_density_matrix(self, state, device_wires):
right_axes.append(index + self.num_wires)
transpose_axes = left_axes + right_axes
rho = qnp.transpose(rho, axes=transpose_axes)
assert qnp.allclose(
assert qml.math.is_abstract(rho) or qnp.allclose(
qnp.trace(qnp.reshape(rho, (2**self.num_wires, 2**self.num_wires))),
1.0,
atol=tolerance,
Expand Down
11 changes: 8 additions & 3 deletions tests/devices/test_default_mixed_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ def circuit(a):

assert np.allclose(state, expected, atol=tol, rtol=0)

def test_qubit_density_matrix_jit_compatible(self, mocker):
@pytest.mark.parametrize("n_qubits", [1, 2])
def test_qubit_density_matrix_jit_compatible(self, n_qubits, mocker):
"""Test that _apply_density_matrix works with jax-jit"""

dev = qml.device("default.mixed", wires=1)
dev = qml.device("default.mixed", wires=n_qubits)
spy = mocker.spy(dev, "_apply_density_matrix")

@jax.jit
Expand All @@ -106,7 +107,11 @@ def circuit(state_ini):
rho_out = circuit(rho_ini)
spy.assert_called_once()
assert qml.math.get_interface(rho_out) == "jax"
assert np.array_equal(rho_out, [[1, 0], [0, 0]])

dim = 2**n_qubits
expected = np.zeros((dim, dim))
expected[0, 0] = 1.0
assert np.array_equal(rho_out, expected)


class TestDtypePreserved:
Expand Down
Loading