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.36k stars 605 forks source link

[BUG] Device `state` does not change. #1048

Closed glassnotes closed 3 years ago

glassnotes commented 3 years ago

Issue description

It is possible to access the state of the device via dev.state / dev.access_state(). In tape mode, this state does not seem to get updated after gates are applied.

Uses master branch at this commit.

Name: PennyLane
Version: 0.14.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: toml, numpy, scipy, appdirs, autograd, networkx, semantic-version
Required-by: PennyLane-SF, pennylane-qulacs, PennyLane-qsharp, PennyLane-qiskit, PennyLane-Qchem, PennyLane-Forest, PennyLane-Cirq
Platform info:           Linux-5.4.0-64-generic-x86_64-with-glibc2.10
Python version:          3.8.5
Numpy version:           1.19.5
Scipy version:           1.4.1
Installed devices:
- default.gaussian (PennyLane-0.14.0.dev0)
- default.mixed (PennyLane-0.14.0.dev0)
- default.qubit (PennyLane-0.14.0.dev0)
- default.qubit.autograd (PennyLane-0.14.0.dev0)
- default.qubit.jax (PennyLane-0.14.0.dev0)
- default.qubit.tf (PennyLane-0.14.0.dev0)
- default.tensor (PennyLane-0.14.0.dev0)
- default.tensor.tf (PennyLane-0.14.0.dev0)
- strawberryfields.fock (PennyLane-SF-0.13.0.dev0)
- strawberryfields.gaussian (PennyLane-SF-0.13.0.dev0)
- strawberryfields.gbs (PennyLane-SF-0.13.0.dev0)
- strawberryfields.remote (PennyLane-SF-0.13.0.dev0)
- strawberryfields.tf (PennyLane-SF-0.13.0.dev0)
- qulacs.simulator (pennylane-qulacs-0.12.0)
- microsoft.QuantumSimulator (PennyLane-qsharp-0.8.0)
- qiskit.aer (PennyLane-qiskit-0.13.0.dev0)
- qiskit.basicaer (PennyLane-qiskit-0.13.0.dev0)
- qiskit.ibmq (PennyLane-qiskit-0.13.0.dev0)
- forest.numpy_wavefunction (PennyLane-Forest-0.13.0.dev0)
- forest.qpu (PennyLane-Forest-0.13.0.dev0)
- forest.qvm (PennyLane-Forest-0.13.0.dev0)
- forest.wavefunction (PennyLane-Forest-0.13.0.dev0)
- 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

Please include any additional code snippets and error tracebacks related to the issue here.

import pennylane as qml

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

@qml.qnode(dev)
def apply_x():
    qml.PauliX(wires=0)
    return qml.expval(qml.PauliZ(0))

apply_x()
print(dev.state)
[1.+0.j 0.+0.j]

This should be [0, 1] after applying X. It works if tape mode is disabled:

qml.disable_tape()

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

@qml.qnode(dev)
def apply_x():
    qml.PauliX(wires=0)
    return qml.expval(qml.PauliZ(0))

apply_x()
print(dev.state)

Output:

[0.+0.j 1.+0.j]
antalszava commented 3 years ago

@glassnotes thanks for spotting this! #1049 should solve it