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.27k stars 586 forks source link

[BUG] Scalar product of observable produces `qml.Hamiltonian` incorrectly for framework tensors #2971

Closed eddddddy closed 2 years ago

eddddddy commented 2 years ago

Expected behavior

A qml.Hamiltonian object is constructed correctly for the product of a Python scalar with an observable:

>>> h = 0.3 * qml.PauliX(0)
>>> h.coeffs, h.ops
(array([0.3]), [PauliX(wires=[0])])

We would expect the product of a framework-specific scalar tensor with an observable to also work:

>>> # should be the same as qml.Hamiltonian([np.array(0.3)], [qml.PauliX(0)])
>>> h = np.array(0.3) * qml.PauliX(0)   

Actual behavior

Instead, the latter code produces the following output:

>>> h = np.array(0.3) * qml.PauliX(0)
>>> h
tensor(<Hamiltonian: terms=1, wires=[0]>, dtype=object, requires_grad=True)

This is a "scalar" tensor, where the scalar is the Hamiltonian object itself.

Additional information

No response

Source code

No response

Tracebacks

No response

System information

Name: PennyLane
Version: 0.25.0.dev0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: None
Author-email: None
License: Apache License 2.0
Location: c:\users\edward.jiang\documents\pennylane
Requires: numpy, scipy, networkx, retworkx, autograd, toml, appdirs, semantic-version, autoray, cachetools, pennylane
-lightning
Required-by: PennyLane-qiskit, PennyLane-Lightning

Platform info:           Windows-10-10.0.19042-SP0
Python version:          3.8.10
Numpy version:           1.20.3
Scipy version:           1.7.3
Installed devices:
- default.gaussian (PennyLane-0.25.0.dev0)
- default.mixed (PennyLane-0.25.0.dev0)
- default.qubit (PennyLane-0.25.0.dev0)
- default.qubit.autograd (PennyLane-0.25.0.dev0)
- default.qubit.jax (PennyLane-0.25.0.dev0)
- default.qubit.tf (PennyLane-0.25.0.dev0)
- default.qubit.torch (PennyLane-0.25.0.dev0)
- qiskit.aer (PennyLane-qiskit-0.24.0)
- qiskit.basicaer (PennyLane-qiskit-0.24.0)
- qiskit.ibmq (PennyLane-qiskit-0.24.0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.24.0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.24.0)
- lightning.qubit (PennyLane-Lightning-0.25.0)

Existing GitHub issues

AlbertMitjans commented 2 years ago

I am not sure if we can change the current behaviour, given that we would lose information about trainability:

>>> np.array(0.3, requires_grad=False) * qml.PauliX(0)
tensor(<Hamiltonian: terms=1, wires=[0]>, dtype=object, requires_grad=False)

FYI, you can obtain the Hamiltonian from the tensor by calling unwrap:

>>> h = np.array(0.3) * qml.PauliX(0)
>>> h.unwrap()
  (0.3) [X0]

@josh146 what do you think?

eddddddy commented 2 years ago

Right, OK. I guess from this and the other issue as well, it's clear that qml.Hamiltonian is not really intended to handle framework-specific tensors.

However, this looks to be handled by the new operator arithmetic. Just wondering, is the new operator arithmetic going to replace qml.Hamiltonian somewhere down the line?

AlbertMitjans commented 2 years ago

However, this looks to be handled by the new operator arithmetic. Just wondering, is the new operator arithmetic going to replace qml.Hamiltonian somewhere down the line?

Yes! We will be deprecating the qml.Hamiltonian soon.

AlbertMitjans commented 2 years ago

@eddddddy can I close this issue?

eddddddy commented 2 years ago

Sure, you can go ahead and close this issue.