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

Hamiltonians created within QNodes get added to operation queue #1224

Closed glassnotes closed 3 years ago

glassnotes commented 3 years ago

Issue description

Name: PennyLane
Version: 0.15.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: /home/olivia/Software/anaconda3/envs/xanadu/lib/python3.8/site-packages
Requires: appdirs, networkx, toml, autograd, numpy, semantic-version, scipy
Required-by: PennyLane-SF, pennylane-qulacs, PennyLane-qsharp, PennyLane-qiskit, PennyLane-Qchem, PennyLane-Forest, PennyLane-Cirq
Platform info:           Linux-5.4.0-71-generic-x86_64-with-glibc2.10
Python version:          3.8.5
Numpy version:           1.18.5
Scipy version:           1.4.1
Installed devices:
- default.gaussian (PennyLane-0.15.0.dev0)
- default.mixed (PennyLane-0.15.0.dev0)
- default.qubit (PennyLane-0.15.0.dev0)
- default.qubit.autograd (PennyLane-0.15.0.dev0)
- default.qubit.jax (PennyLane-0.15.0.dev0)
- default.qubit.tf (PennyLane-0.15.0.dev0)
- default.tensor (PennyLane-0.15.0.dev0)
- default.tensor.tf (PennyLane-0.15.0.dev0)
- strawberryfields.fock (PennyLane-SF-0.15.0)
- strawberryfields.gaussian (PennyLane-SF-0.15.0)
- strawberryfields.gbs (PennyLane-SF-0.15.0)
- strawberryfields.remote (PennyLane-SF-0.15.0)
- strawberryfields.tf (PennyLane-SF-0.15.0)
- qulacs.simulator (pennylane-qulacs-0.14.0)
- microsoft.QuantumSimulator (PennyLane-qsharp-0.8.0)
- qiskit.aer (PennyLane-qiskit-0.15.0)
- qiskit.basicaer (PennyLane-qiskit-0.15.0)
- qiskit.ibmq (PennyLane-qiskit-0.15.0)
- forest.numpy_wavefunction (PennyLane-Forest-0.15.0)
- forest.qvm (PennyLane-Forest-0.15.0)
- forest.wavefunction (PennyLane-Forest-0.15.0)
- cirq.mixedsimulator (PennyLane-Cirq-0.13.0)
- cirq.pasqal (PennyLane-Cirq-0.13.0)
- cirq.qsim (PennyLane-Cirq-0.13.0)
- cirq.qsimh (PennyLane-Cirq-0.13.0)
- cirq.simulator (PennyLane-Cirq-0.13.0)

Source code and tracebacks

Code to reproduce:

import pennylane as qml

dev = qml.device('default.qubit', wires=1)

@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=0)
    return qml.expval(qml.PauliX(0))

@qml.qnode(dev)
def circuit_internal_ham():
    internal_ham = qml.Hamiltonian([0.001, 0.001], [qml.PauliX(0), qml.PauliZ(0)])
    qml.Hadamard(wires=0)
    return qml.expval(qml.PauliX(0))

out = circuit()
out_with_ham = circuit_internal_ham()

print(f"Out no ham = {out}")
print(f"Out with ham = {out_with_ham}\n")

for op in circuit.qtape.operations:
    print(op)
print()

for op in circuit_internal_ham.qtape.operations:
    print(op)
print()

Running this gives:

Out no ham = 0.9999999999999996
Out with ham = -0.9999999999999996

Hadamard(wires=[0])

PauliX(wires=[0])
PauliZ(wires=[0])
Hadamard(wires=[0])

You can see the expectation value is incorrect, and the Pauli X and Pauli Z from the Hamiltonian are tacked on to the beginning of the second circuit.

josh146 commented 3 years ago

Thanks @glassnotes! This should be fixed in #1142, could you try running this code in that branch and see if it works?

glassnotes commented 3 years ago

It does! (My bad, searched issues but not PRs). Shall we close this then, or leave it open as a reminder / searchability?

josh146 commented 3 years ago

Since this isn't fixed in master, best to keep it open :)

It might also be worth porting just the bugfix out of #1142 to a separate PR so that we can fix this now, without waiting for that PR to be complete.