PennyLaneAI / pennylane

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
https://pennylane.ai
Apache License 2.0
2.28k stars 585 forks source link

[BUG] Hamiltonian.compare example doesn't work #3256

Closed lillian542 closed 1 year ago

lillian542 commented 1 year ago

Expected behavior

The first docstring example suggests that it should return True.

Actual behavior

It returns False.

Additional information

No response

Source code

>>> A = np.array([[1, 0], [0, -1]])
        >>> H = qml.Hamiltonian(
        ...     [0.5, 0.5],
        ...     [qml.Hermitian(A, 0) @ qml.PauliY(1), qml.PauliY(1) @ qml.Hermitian(A, 0) @ qml.Identity("a")]
        ... )
        >>> obs = qml.Hermitian(A, 0) @ qml.PauliY(1)
        >>> print(H.compare(obs))

Tracebacks

No response

System information

Name: PennyLane
Version: 0.27.0.dev0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /opt/homebrew/Caskroom/miniforge/base/envs/pennylane/lib/python3.9/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, retworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning

Platform info:           macOS-12.6-arm64-arm-64bit
Python version:          3.9.13
Numpy version:           1.23.2
Scipy version:           1.9.3
Installed devices:
- default.gaussian (PennyLane-0.27.0.dev0)
- default.mixed (PennyLane-0.27.0.dev0)
- default.qubit (PennyLane-0.27.0.dev0)
- default.qubit.autograd (PennyLane-0.27.0.dev0)
- default.qubit.jax (PennyLane-0.27.0.dev0)
- default.qubit.tf (PennyLane-0.27.0.dev0)
- default.qubit.torch (PennyLane-0.27.0.dev0)
- default.qutrit (PennyLane-0.27.0.dev0)
- null.qubit (PennyLane-0.27.0.dev0)
- lightning.qubit (PennyLane-Lightning-0.26.1)

Existing GitHub issues

AlbertMitjans commented 1 year ago

You are right the example doesn't work.

I am not sure if this is a bug, or if it is only an inconsistency in the Hamiltonian.compare docstring. The warning says the following:

The compare method does **not** check if the matrix representation of a :class:`~.Hermitian` observable
is equal to an equivalent observable expressed in terms of Pauli matrices, or as a linear combination
of Hermitians. To do so would require the matrix form of Hamiltonians and Tensors be calculated, 
which would drastically increase runtime.

And in the example they are comparing a Hermitian observable with a linear combination of Hermitian observables, right?

AlbertMitjans commented 1 year ago

If it is an inconsistency we only need to update the docstring.

lillian542 commented 1 year ago

I believe it's only an inconsistency. Can I just replace that example with a random example that does work?

AlbertMitjans commented 1 year ago

Sure!