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 585 forks source link

[BUG] Incorrect output dtypes for multiple types of QNode evaluations #2374

Open antalszava opened 2 years ago

antalszava commented 2 years ago

Expected behavior

User-specified dtypes on the device are not considered in a wide variety of cases. The ones detailed below are just specific ones found by chance, a more systematic think-through of cases is required.

A couple of examples for specific use cases to consider: various diff_method types, every interface, potentially external plugins.

Actual behavior

The dtypes that users define on the device are preserved throughout the whole circuit execution and the result of a QNode adheres to that dtype.

Additional information

Use tapes to automatically detect implicitly defined user dtypes

QuantumTape objects may contain parameters that already have a specific dtype set. If the user didn't define a custom dtype and the dtype of the parameters differ from the internal dtype used by the device, it would be desirable to use the dtype implicitly defined on the tape by inspecting the gate parameter objects.

To achieve this, having a tape.shape attribute defined would help (WIP in #2044).

Sampling

Sampling certain observables may result in integer outputs. However, devices don't yet allow specifying an integer typed dtype. Introducing that and ensuring that computations work as expected would be key.

Using backprop devices

The dtype doesn't seem to be copied when swapping the devices in the QNode.

Changing https://github.com/PennyLaneAI/pennylane/blob/0814da9b27211dc010ae49942b947993c14f1601/pennylane/qnode.py#L403 to look like:

                r_dtype = device.R_DTYPE
                c_dtype = device.C_DTYPE

                device = qml.device(
                    backprop_devices[interface],
                    wires=device.wires,
                    shots=device.shots,
                )
                device.expand_fn = expand_fn
                device.batch_transform = batch_transform

                device.R_DTYPE = r_dtype
                device.C_DTYPE = c_dtype

could solve this issue. Note that an additional integer dtype could also be used here.

Source code

No response

Tracebacks

No response

System information

Name: PennyLane
Version: 0.23.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: /pennylane
Requires: numpy, scipy, networkx, retworkx, autograd, toml, appdirs, semantic_version, autoray, cachetools, pennylane-lightning
Required-by: PennyLane-Orquestra, pennylane-qulacs, PennyLane-Honeywell, PennyLane-AQT, PennyLane-PQ, PennyLane-Forest, PennyLane-qsharp, PennyLane-Qchem, PennyLane-IonQ, PennyLane-Cirq, PennyLane-qiskit, amazon-braket-pennylane-plugin, PennyLane-SF, PennyLane-Lightning
Platform info:           Linux-5.13.0-35-generic-x86_64-with-glibc2.10
Python version:          3.8.5
Numpy version:           1.22.3
Scipy version:           1.7.3
Installed devices:
- orquestra.forest (PennyLane-Orquestra-0.15.0)
- orquestra.ibmq (PennyLane-Orquestra-0.15.0)
- orquestra.qiskit (PennyLane-Orquestra-0.15.0)
- orquestra.qulacs (PennyLane-Orquestra-0.15.0)
- qulacs.simulator (pennylane-qulacs-0.17.0.dev0)
- honeywell.hqs (PennyLane-Honeywell-0.16.0.dev0)
- aqt.noisy_sim (PennyLane-AQT-0.18.0)
- aqt.sim (PennyLane-AQT-0.18.0)
- projectq.classical (PennyLane-PQ-0.18.0.dev0)
- projectq.ibm (PennyLane-PQ-0.18.0.dev0)
- projectq.simulator (PennyLane-PQ-0.18.0.dev0)
- forest.numpy_wavefunction (PennyLane-Forest-0.18.0.dev0)
- forest.qvm (PennyLane-Forest-0.18.0.dev0)
- forest.wavefunction (PennyLane-Forest-0.18.0.dev0)
- microsoft.QuantumSimulator (PennyLane-qsharp-0.19.0)
- ionq.qpu (PennyLane-IonQ-0.17.0.dev0)
- ionq.simulator (PennyLane-IonQ-0.17.0.dev0)
- cirq.mixedsimulator (PennyLane-Cirq-0.22.0)
- cirq.pasqal (PennyLane-Cirq-0.22.0)
- cirq.qsim (PennyLane-Cirq-0.22.0)
- cirq.qsimh (PennyLane-Cirq-0.22.0)
- cirq.simulator (PennyLane-Cirq-0.22.0)
- qiskit.aer (PennyLane-qiskit-0.22.0.dev0)
- qiskit.basicaer (PennyLane-qiskit-0.22.0.dev0)
- qiskit.ibmq (PennyLane-qiskit-0.22.0.dev0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.22.0.dev0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.22.0.dev0)
- braket.aws.qubit (amazon-braket-pennylane-plugin-1.5.7.dev0)
- braket.local.qubit (amazon-braket-pennylane-plugin-1.5.7.dev0)
- strawberryfields.fock (PennyLane-SF-0.21.0.dev0)
- strawberryfields.gaussian (PennyLane-SF-0.21.0.dev0)
- strawberryfields.gbs (PennyLane-SF-0.21.0.dev0)
- strawberryfields.remote (PennyLane-SF-0.21.0.dev0)
- strawberryfields.tf (PennyLane-SF-0.21.0.dev0)
- lightning.qubit (PennyLane-Lightning-0.23.0.dev2)
- default.gaussian (PennyLane-0.23.0.dev0)
- default.mixed (PennyLane-0.23.0.dev0)
- default.qubit (PennyLane-0.23.0.dev0)
- default.qubit.autograd (PennyLane-0.23.0.dev0)
- default.qubit.jax (PennyLane-0.23.0.dev0)
- default.qubit.tf (PennyLane-0.23.0.dev0)
- default.qubit.torch (PennyLane-0.23.0.dev0)

Existing GitHub issues

josh146 commented 2 years ago

Use tapes to automatically detect implicitly defined user dtypes To achieve this, having a tape.shape attribute defined would help (WIP in https://github.com/PennyLaneAI/pennylane/pull/2044).

Thinking about this more, I'm starting to really like the idea that tapes always have an associated dtype. Whether it is implicit, or explicitly set by the user.

We can think of the dtype as metadata associated with the tape