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

Make Molecule the central object for qchem functions #5571

Merged
merged 45 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
4ea854f
[skip ci] Molecule central object
ddhawan11 Apr 24, 2024
7dba28a
Merge branch 'master' into molecule_central_object
ddhawan11 Apr 24, 2024
9e8685f
Make molecule the central object for qchem functions
ddhawan11 Apr 29, 2024
cad2c33
Merge branch 'master' into molecule_central_object
ddhawan11 Apr 29, 2024
cf11858
Fixed CI
ddhawan11 Apr 29, 2024
2ce541e
Removed mapping
ddhawan11 Apr 30, 2024
2618af1
Multiple Dispatch circular import issue
ddhawan11 May 1, 2024
dbf9cab
merge master and update changelog
soranjh May 1, 2024
03b9245
[skip-ci] remove blank line
soranjh May 1, 2024
8197495
use singledispatch
soranjh May 2, 2024
e8ecd63
fix signature
soranjh May 2, 2024
99ee13e
correct signature
soranjh May 2, 2024
ca14841
update docstring
soranjh May 2, 2024
a52a5cb
fix trailing whitespace
soranjh May 2, 2024
11caf0d
Added working tests
ddhawan11 May 3, 2024
2884fd7
Fixed indentation
ddhawan11 May 3, 2024
41322e8
Increased test coverage
ddhawan11 May 6, 2024
b185bbc
Fixed trailing whitespace
ddhawan11 May 6, 2024
67b58ff
Fixed CI
ddhawan11 May 6, 2024
c920bfe
increased test coverage
ddhawan11 May 6, 2024
81ead7d
Fixed trailing whitespace
ddhawan11 May 6, 2024
490c869
Update pennylane/qchem/openfermion_obs.py
ddhawan11 May 9, 2024
0a2f0c9
Fixed tests
ddhawan11 May 10, 2024
c1f651d
Addressed reviews
ddhawan11 May 13, 2024
6027dbe
Fixed trailing whitespace
ddhawan11 May 13, 2024
8f6e3c2
Fix CI for a while
ddhawan11 May 13, 2024
6999f2a
Update pennylane/qchem/openfermion_obs.py
ddhawan11 May 15, 2024
664b023
Update pennylane/qchem/openfermion_obs.py
ddhawan11 May 15, 2024
67eb8d0
Update pennylane/qchem/openfermion_obs.py
ddhawan11 May 15, 2024
943371b
Made formatting changes
ddhawan11 May 16, 2024
46ee6e1
Update pennylane/qchem/openfermion_obs.py
ddhawan11 May 16, 2024
615cba6
Updated documentation
ddhawan11 May 16, 2024
b1ad212
Fixed trailing whitespace
ddhawan11 May 16, 2024
92d4f0d
Fixed trailing whitespace
ddhawan11 May 16, 2024
7588e4b
Update pennylane/qchem/openfermion_obs.py
ddhawan11 May 16, 2024
a2c703a
Update pennylane/qchem/openfermion_obs.py
ddhawan11 May 16, 2024
8e9ad00
Update doc/code/qml_qchem.rst
ddhawan11 May 16, 2024
1c6e59e
Update doc/code/qml_qchem.rst
ddhawan11 May 16, 2024
7efb0b1
Update pennylane/qchem/openfermion_obs.py
ddhawan11 May 16, 2024
8166793
Update pennylane/qchem/openfermion_obs.py
ddhawan11 May 16, 2024
50adcd7
Update pennylane/qchem/openfermion_obs.py
ddhawan11 May 17, 2024
f0ce27d
Fixed whitespace
ddhawan11 May 17, 2024
0a41c26
Formatting change
ddhawan11 May 17, 2024
8ebf71d
Formatting change
ddhawan11 May 17, 2024
83813be
Update requirements-ci.txt
ddhawan11 May 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
174 changes: 174 additions & 0 deletions doc/releases/changelog-dev.md
ddhawan11 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,180 @@

<h3>Improvements 🛠</h3>

<h4>Community contributions 🥳</h4>

* Implemented the method `process_counts` in `ExpectationMP`, `VarianceMP`, `CountsMP`, and `SampleMP`
[(#5256)](https://github.com/PennyLaneAI/pennylane/pull/5256)
[(#5395)](https://github.com/PennyLaneAI/pennylane/pull/5395)

* Add type hints for unimplemented methods of the abstract class `Operator`.
[(#5490)](https://github.com/PennyLaneAI/pennylane/pull/5490)

* Implement `Shots.bins()` method.
[(#5476)](https://github.com/PennyLaneAI/pennylane/pull/5476)

<h4>Updated operators</h4>

* `qml.ops.Sum` now supports storing grouping information. Grouping type and method can be
specified during construction using the `grouping_type` and `method` keyword arguments of
`qml.dot`, `qml.sum`, or `qml.ops.Sum`. The grouping indices are stored in `Sum.grouping_indices`.
[(#5179)](https://github.com/PennyLaneAI/pennylane/pull/5179)

```python
import pennylane as qml

a = qml.X(0)
b = qml.prod(qml.X(0), qml.X(1))
c = qml.Z(0)
obs = [a, b, c]
coeffs = [1.0, 2.0, 3.0]

op = qml.dot(coeffs, obs, grouping_type="qwc")
```

```pycon
>>> op.grouping_indices
((2,), (0, 1))
```

Additionally, grouping type and method can be set or changed after construction using
`Sum.compute_grouping()`:

```python
import pennylane as qml

a = qml.X(0)
b = qml.prod(qml.X(0), qml.X(1))
c = qml.Z(0)
obs = [a, b, c]
coeffs = [1.0, 2.0, 3.0]

op = qml.dot(coeffs, obs)
```

```pycon
>>> op.grouping_indices is None
True
>>> op.compute_grouping(grouping_type="qwc")
>>> op.grouping_indices
((2,), (0, 1))
```

Note that the grouping indices refer to the lists returned by `Sum.terms()`, not `Sum.operands`.

* Added new function `qml.operation.convert_to_legacy_H` to convert `Sum`, `SProd`, and `Prod` to `Hamiltonian` instances.
[(#5309)](https://github.com/PennyLaneAI/pennylane/pull/5309)

* The `qml.is_commuting` function now accepts `Sum`, `SProd`, and `Prod` instances.
[(#5351)](https://github.com/PennyLaneAI/pennylane/pull/5351)

* Operators can now be left multiplied `x * op` by numpy arrays.
[(#5361)](https://github.com/PennyLaneAI/pennylane/pull/5361)

* A new class `qml.ops.LinearCombination` is introduced. In essence, this class is an updated equivalent of `qml.ops.Hamiltonian`
but for usage with new operator arithmetic.
[(#5216)](https://github.com/PennyLaneAI/pennylane/pull/5216)

* The generators in the source code return operators consistent with the global setting for
`qml.operator.active_new_opmath()` wherever possible. `Sum`, `SProd` and `Prod` instances
will be returned even after disabling the new operator arithmetic in cases where they offer
additional functionality not available using legacy operators.
[(#5253)](https://github.com/PennyLaneAI/pennylane/pull/5253)
[(#5410)](https://github.com/PennyLaneAI/pennylane/pull/5410)
[(#5411)](https://github.com/PennyLaneAI/pennylane/pull/5411)
[(#5421)](https://github.com/PennyLaneAI/pennylane/pull/5421)

* `ApproxTimeEvolution` is now compatible with any operator that defines a `pauli_rep`.
[(#5362)](https://github.com/PennyLaneAI/pennylane/pull/5362)

* `Hamiltonian.pauli_rep` is now defined if the hamiltonian is a linear combination of paulis.
[(#5377)](https://github.com/PennyLaneAI/pennylane/pull/5377)

* `Prod.eigvals()` is now compatible with Qudit operators.
[(#5400)](https://github.com/PennyLaneAI/pennylane/pull/5400)

* `qml.transforms.hamiltonian_expand` can now handle multi-term observables with a constant offset.
[(#5414)](https://github.com/PennyLaneAI/pennylane/pull/5414)

* `taper_operation` method is compatible with new operator arithmetic.
[(#5326)](https://github.com/PennyLaneAI/pennylane/pull/5326)

* Removed the warning that an observable might not be hermitian in `qnode` executions. This enables jit-compilation.
[(#5506)](https://github.com/PennyLaneAI/pennylane/pull/5506)

* `qml.transforms.split_non_commuting` will now work with single-term operator arithmetic.
[(#5314)](https://github.com/PennyLaneAI/pennylane/pull/5314)

* `LinearCombination` and `Sum` now accept `_grouping_indices` on initialization.
[(#5524)](https://github.com/PennyLaneAI/pennylane/pull/5524)

<h4>Mid-circuit measurements and dynamic circuits</h4>

* The `QubitDevice` class and children classes support the `dynamic_one_shot` transform provided that they support `MidMeasureMP` operations natively.
[(#5317)](https://github.com/PennyLaneAI/pennylane/pull/5317)

* The `dynamic_one_shot` transform is introduced enabling dynamic circuit execution on circuits with shots and devices that support `MidMeasureMP` operations natively.
[(#5266)](https://github.com/PennyLaneAI/pennylane/pull/5266)

* Added a qml.capture module that will contain PennyLane's own capturing mechanism for hybrid
quantum-classical programs.
[(#5509)](https://github.com/PennyLaneAI/pennylane/pull/5509)

<h4>Performance and broadcasting</h4>

* Gradient transforms may now be applied to batched/broadcasted QNodes, as long as the
broadcasting is in non-trainable parameters.
[(#5452)](https://github.com/PennyLaneAI/pennylane/pull/5452)

* Improve the performance of computing the matrix of `qml.QFT`
[(#5351)](https://github.com/PennyLaneAI/pennylane/pull/5351)

* `qml.transforms.broadcast_expand` now supports shot vectors when returning `qml.sample()`.
[(#5473)](https://github.com/PennyLaneAI/pennylane/pull/5473)

* `LightningVJPs` is now compatible with Lightning devices using the new device API.
[(#5469)](https://github.com/PennyLaneAI/pennylane/pull/5469)

<h4>Other improvements</h4>

* `qml.ops.Conditional` now stores the `data`, `num_params`, and `ndim_param` attributes of
the operator it wraps.
[(#5473)](https://github.com/PennyLaneAI/pennylane/pull/5473)

* The `molecular_hamiltonian` function calls `PySCF` directly when `method='pyscf'` is selected.
[(#5118)](https://github.com/PennyLaneAI/pennylane/pull/5118)

* Upgraded `null.qubit` to the new device API. Also, added support for all measurements and various modes of differentiation.
[(#5211)](https://github.com/PennyLaneAI/pennylane/pull/5211)

* Obtaining classical shadows using the `default.clifford` device is now compatible with
[stim](https://github.com/quantumlib/Stim) `v1.13.0`.
[(#5409)](https://github.com/PennyLaneAI/pennylane/pull/5409)

* `qml.transforms.hamiltonian_expand` and `qml.transforms.sum_expand` can now handle multi-term observables with a constant offset.
[(#5414)](https://github.com/PennyLaneAI/pennylane/pull/5414)
[(#5543)](https://github.com/PennyLaneAI/pennylane/pull/5543)

* `default.mixed` has improved support for sampling-based measurements with non-numpy interfaces.
[(#5514)](https://github.com/PennyLaneAI/pennylane/pull/5514)

* Replaced `cache_execute` with an alternate implementation based on `@transform`.
[(#5318)](https://github.com/PennyLaneAI/pennylane/pull/5318)

* The `QNode` now defers `diff_method` validation to the device under the new device api `qml.devices.Device`.
[(#5176)](https://github.com/PennyLaneAI/pennylane/pull/5176)

* Extend the device test suite to cover gradient methods, templates and arithmetic observables.
[(#5273)](https://github.com/PennyLaneAI/pennylane/pull/5273)
[(#5518)](https://github.com/PennyLaneAI/pennylane/pull/5518)

* A clear error message is added in `KerasLayer` when using the newest version of TensorFlow with Keras 3
(which is not currently compatible with `KerasLayer`), linking to instructions to enable Keras 2.
[(#5488)](https://github.com/PennyLaneAI/pennylane/pull/5488)

* The `molecular_hamiltonian` function now works with Molecule as the central object.
[(#5571)](https://github.com/PennyLaneAI/pennylane/pull/5571)

<h3>Breaking changes 💔</h3>

<h3>Deprecations 👋</h3>
Expand Down
4 changes: 2 additions & 2 deletions pennylane/qchem/hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def _fermionic_hamiltonian(*args):
return _fermionic_hamiltonian


def diff_hamiltonian(mol, cutoff=1.0e-12, core=None, active=None):
def diff_hamiltonian(mol, cutoff=1.0e-12, core=None, active=None, mapping="jordan_wigner"):
r"""Return a function that computes the qubit Hamiltonian.

Args:
Expand Down Expand Up @@ -222,6 +222,6 @@ def _molecular_hamiltonian(*args):

h_ferm = fermionic_hamiltonian(mol, cutoff, core, active)(*args)

return qubit_observable(h_ferm)
return qubit_observable(h_ferm, mapping=mapping)

return _molecular_hamiltonian
9 changes: 8 additions & 1 deletion pennylane/qchem/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(
charge=0,
mult=1,
basis_name="sto-3g",
name="molecule",
load_data=False,
l=None,
alpha=None,
Expand Down Expand Up @@ -98,7 +99,7 @@ def __init__(
self.charge = charge
self.mult = mult
self.basis_name = basis_name.lower()

self.name = name
self.n_basis, self.basis_data = mol_basis_data(self.basis_name, self.symbols, load_data)

self.nuclear_charges = [atomic_numbers[s] for s in self.symbols]
Expand All @@ -125,6 +126,8 @@ def __init__(
for i, c in enumerate(coeff)
]

print("coordinates: ", self.coordinates)
print("n_basis: ", self.n_basis)
ddhawan11 marked this conversation as resolved.
Show resolved Hide resolved
r = list(
itertools.chain(
*[[self.coordinates[i]] * self.n_basis[i] for i in range(len(self.n_basis))]
Expand All @@ -135,6 +138,10 @@ def __init__(
self.alpha = alpha
self.coeff = coeff
self.r = r
print("l: ", self.l)
print("alpha: ", self.alpha)
print("coeff: ", self.coeff)
print("r: ", self.r)
ddhawan11 marked this conversation as resolved.
Show resolved Hide resolved

self.basis_set = [
BasisFunction(self.l[i], self.alpha[i], self.coeff[i], self.r[i]) for i in range(len(l))
Expand Down
13 changes: 11 additions & 2 deletions pennylane/qchem/observable_hf.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def fermionic_observable(constant, one=None, two=None, cutoff=1.0e-12):
return sentence


def qubit_observable(o_ferm, cutoff=1.0e-12):
def qubit_observable(o_ferm, cutoff=1.0e-12, mapping="jordan_wigner"):
r"""Convert a fermionic observable to a PennyLane qubit observable.

Args:
Expand Down Expand Up @@ -131,7 +131,16 @@ def qubit_observable(o_ferm, cutoff=1.0e-12):
+ ((0.775+0j)) [Y1 Y2]
+ ((0.775+0j)) [X1 X2]
"""
h = qml.jordan_wigner(o_ferm, ps=True, tol=cutoff)
if mapping == "jordan_wigner":
print("cutoff: ", cutoff)
ddhawan11 marked this conversation as resolved.
Show resolved Hide resolved
h = qml.jordan_wigner(o_ferm, ps=True, tol=cutoff)
elif mapping == "parity_transform":
qubits = len(o_ferm.wires)
h = qml.parity_transform(o_ferm, qubits, ps=True, tol=cutoff)
elif mapping == "bravyi_kitaev":
qubits = len(o_ferm.wires)
h = qml.bravyi_kitaev(o_ferm, qubits, ps=True, tol=cutoff)

h.simplify(tol=cutoff)

if active_new_opmath():
Expand Down