Skip to content

Commit

Permalink
Merge branch 'master' into josh146-patch-3
Browse files Browse the repository at this point in the history
  • Loading branch information
josh146 authored Jul 23, 2024
2 parents f833dd3 + 09b0366 commit fcb2368
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
11 changes: 9 additions & 2 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
* The `qml.PrepSelPrep` template is added. The template implements a block-encoding of a linear
combination of unitaries.
[(#5756)](https://github.com/PennyLaneAI/pennylane/pull/5756)

[(#5987)](https://github.com/PennyLaneAI/pennylane/pull/5987)
[(#5987)](https://github.com/PennyLaneAI/pennylane/pull/5987)

* The `split_to_single_terms` transform is added. This transform splits expectation values of sums
into multiple single-term measurements on a single tape, providing better support for simulators
Expand Down Expand Up @@ -98,6 +97,10 @@
0.11917543, 0.08942104, 0.21545687], dtype=float64)
```

* The `qubit_observable` function is modified to return an ascending wire order for molecular
Hamiltonians.
[(#5950)](https://github.com/PennyLaneAI/pennylane/pull/5950)

<h4>Community contributions 🥳</h4>

* `DefaultQutritMixed` readout error has been added using parameters `readout_relaxation_probs` and
Expand Down Expand Up @@ -171,6 +174,9 @@

<h3>Bug fixes 🐛</h3>

* `dynamic_one_shot` was broken for old-API devices since `override_shots` was deprecated.
[(#6024)](https://github.com/PennyLaneAI/pennylane/pull/6024)

* `CircuitGraph` can now handle circuits with the same operation instance occuring multiple times.
[(#5907)](https://github.com/PennyLaneAI/pennylane/pull/5907)

Expand Down Expand Up @@ -199,6 +205,7 @@ Pietropaolo Frisoni,
Emiliano Godinez,
Renke Huang,
Josh Izaac,
Soran Jahangiri,
Christina Lee,
Austin Huang,
Christina Lee,
Expand Down
5 changes: 5 additions & 0 deletions pennylane/_qubit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,15 @@ def execute(self, circuit, **kwargs):
shots=[1],
trainable_params=circuit.trainable_params,
)
# Some devices like Lightning-Kokkos use `self.shots` to update `_samples`,
# and hence we update `self.shots` temporarily for this loop
shots_copy = self.shots
self.shots = 1
for _ in circuit.shots:
kwargs["mid_measurements"] = {}
self.reset()
results.append(self.execute(aux_circ, **kwargs))
self.shots = shots_copy
return tuple(results)
# apply all circuit operations
self.apply(
Expand Down
13 changes: 8 additions & 5 deletions pennylane/qchem/observable_hf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import pennylane as qml
from pennylane.fermi import FermiSentence, FermiWord
from pennylane.operation import active_new_opmath
from pennylane.pauli import PauliSentence
from pennylane.pauli.utils import simplify


Expand Down Expand Up @@ -81,12 +82,9 @@ def fermionic_observable(constant, one=None, two=None, cutoff=1.0e-12):
coeffs = qml.math.concatenate((coeffs, coeffs_two))
operators = operators + operators_two

indices_sort = [operators.index(i) for i in sorted(operators)]
if indices_sort:
indices_sort = qml.math.array(indices_sort)

sentence = FermiSentence({FermiWord({}): constant[0]})
for c, o in zip(coeffs[indices_sort], sorted(operators)):
for c, o in sorted(zip(coeffs, operators), key=lambda item: item[1]):

if len(o) == 2:
sentence.update({FermiWord({(0, o[0]): "+", (1, o[1]): "-"}): c})
if len(o) == 4:
Expand Down Expand Up @@ -145,6 +143,11 @@ def qubit_observable(o_ferm, cutoff=1.0e-12, mapping="jordan_wigner"):
qubits = len(o_ferm.wires)
h = qml.bravyi_kitaev(o_ferm, qubits, ps=True, tol=cutoff)

if list(h.wires) != sorted(list(h.wires)):
h = PauliSentence(
sorted(h.items(), key=lambda item: max(item[0].wires.tolist(), default=0))
)

h.simplify(tol=cutoff)

if active_new_opmath():
Expand Down
1 change: 0 additions & 1 deletion pennylane/workflow/qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,6 @@ def _execution_component(self, args: tuple, kwargs: dict, override_shots) -> qml
mcm_config=mcm_config,
interface=self.interface,
)
override_shots = 1
elif hasattr(self.device, "capabilities"):
inner_transform_program.add_transform(
qml.defer_measurements,
Expand Down
30 changes: 30 additions & 0 deletions tests/qchem/test_hamiltonians.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,36 @@ def test_diff_hamiltonian_active_space():
assert isinstance(h, qml.ops.Sum if active_new_opmath() else qml.Hamiltonian)


@pytest.mark.parametrize(
("symbols", "geometry", "core", "active", "charge"),
[
(
["H", "H"],
np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 2.0]]),
None,
None,
0,
),
(
["H", "H", "H"],
np.array([[0.0, 0.0, 0.0], [2.0, 0.0, 1.0], [0.0, 2.0, 0.0]]),
[0],
[1, 2],
1,
),
],
)
def test_diff_hamiltonian_wire_order(symbols, geometry, core, active, charge):
r"""Test that diff_hamiltonian has an ascending wire order."""

mol = qchem.Molecule(symbols, geometry, charge)
args = [geometry]

h = qchem.diff_hamiltonian(mol, core=core, active=active)(*args)

assert h.wires.tolist() == sorted(h.wires.tolist())


def test_gradient_expvalH():
r"""Test that the gradient of expval(H) computed with ``qml.grad`` is equal to the value
obtained with the finite difference method."""
Expand Down

0 comments on commit fcb2368

Please sign in to comment.