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

jit with sampling multi-wire observable #5422

Merged
merged 5 commits into from
Mar 21, 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
3 changes: 3 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@

<h3>Bug fixes 🐛</h3>

* `jax.jit` now works with `qml.sample` with a multi-wire observable.
albi3ro marked this conversation as resolved.
Show resolved Hide resolved
[(#5422)](https://github.com/PennyLaneAI/pennylane/pull/5422)

* `qml.qinfo.quantum_fisher` now works with non-`default.qubit` devices.
[(#5423)](https://github.com/PennyLaneAI/pennylane/pull/5423)

Expand Down
16 changes: 10 additions & 6 deletions pennylane/measurements/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,24 +198,28 @@ def shape(self, device, shots):
"Shots are required to obtain the shape of the measurement "
f"{self.__class__.__name__}."
)
len_wires = len(self.wires) if len(self.wires) > 0 else len(device.wires)
if self.obs:
num_values_per_shot = 1 # one single eigenvalue
else:
# one value per wire
num_values_per_shot = len(self.wires) if len(self.wires) > 0 else len(device.wires)

def _single_int_shape(shot_val, num_wires):
def _single_int_shape(shot_val, num_values):
# singleton dimensions, whether in shot val or num_wires are squeezed away
inner_shape = []
if shot_val != 1:
inner_shape.append(shot_val)
if num_wires != 1:
inner_shape.append(num_wires)
if num_values != 1:
inner_shape.append(num_values)
return tuple(inner_shape)

if not shots.has_partitioned_shots:
return _single_int_shape(shots.total_shots, len_wires)
return _single_int_shape(shots.total_shots, num_values_per_shot)

shape = []
for s in shots.shot_vector:
for _ in range(s.copies):
shape.append(_single_int_shape(s.shots, len_wires))
shape.append(_single_int_shape(s.shots, num_values_per_shot))

return tuple(shape)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ def test_sampling(self, dev, diff_method, grad_on_execution, device_vjp, interfa
if diff_method == "adjoint":
pytest.skip("Adjoint warns with finite shots")

@qnode(
@qml.qnode(
dev,
diff_method=diff_method,
interface=interface,
Expand All @@ -918,7 +918,7 @@ def test_sampling(self, dev, diff_method, grad_on_execution, device_vjp, interfa
def circuit():
qml.Hadamard(wires=[0])
qml.CNOT(wires=[0, 1])
return qml.sample(qml.PauliZ(0)), qml.sample(qml.PauliX(1))
return qml.sample(qml.Z(0)), qml.sample(qml.s_prod(2, qml.X(0) @ qml.Y(1)))

res = jax.jit(circuit, static_argnames="shots")(shots=10)

Expand Down
Loading