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.17k stars 566 forks source link

[BUG] `qml.AmplitudeEmbedding` not supported in `lightning.qubit` in the middle of the circuit #5526

Open KetpuntoG opened 2 months ago

KetpuntoG commented 2 months ago

Expected behavior

import pennylane as qml

dev = qml.device("lightning.qubit", wires = 1)

@qml.qnode(dev)
def circuit():
    qml.Hadamard(0)
    qml.AmplitudeEmbedding([1.,0.], wires = 0)

    return qml.probs(wires=0)

circuit()

This code should give the same output than default.qubit.

Actual behavior

Operation StatePrep cannot we use after other Operations have been already applied [...]

Additional information

No response

Source code

No response

Tracebacks

No response

System information

Name: PennyLane
Version: 0.36.0.dev0
Summary: 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.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /usr/local/lib/python3.10/dist-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info:           Linux-6.1.58+-x86_64-with-glibc2.35
Python version:          3.10.12
Numpy version:           1.25.2
Scipy version:           1.11.4
Installed devices:
- lightning.qubit (PennyLane_Lightning-0.35.1)
- default.clifford (PennyLane-0.36.0.dev0)
- default.gaussian (PennyLane-0.36.0.dev0)
- default.mixed (PennyLane-0.36.0.dev0)
- default.qubit (PennyLane-0.36.0.dev0)
- default.qubit.autograd (PennyLane-0.36.0.dev0)
- default.qubit.jax (PennyLane-0.36.0.dev0)
- default.qubit.legacy (PennyLane-0.36.0.dev0)
- default.qubit.tf (PennyLane-0.36.0.dev0)
- default.qubit.torch (PennyLane-0.36.0.dev0)
- default.qutrit (PennyLane-0.36.0.dev0)
- null.qubit (PennyLane-0.36.0.dev0)

Existing GitHub issues

albi3ro commented 2 months ago

@KetpuntoG have you tried with master? Might. have different behavior with the new device interface.

mudit2812 commented 2 months ago

@albi3ro This happens because lightning.qubit and default.qubit use different stopping conditions. DQ decomposes until all ops have a matrix. In contrast, LQ decomposes until all decomposed ops are in the list of supported ops, so AmplitudeEmbedding decomposes to StatePrep, and does not get decomposed again. We need to update the decomposition logic to accommodate state prep ops in the middle of a circuit, but the stopping conditions do not currently account for operation index. I can add a new decomposition transform to lightning.qubit to use during preprocessing which does this, but it's going to be almost identical to pennylane.qubit.preprocess.decompose (which is what we were originally using with LQ) so there will be some code smell.