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.38k stars 607 forks source link

[BUG] One-shot ZNE returns unexpected results #6440

Closed cosenal closed 1 month ago

cosenal commented 1 month ago

Expected behavior

Given the observable is Z (see non-workking example below) and it's a one-shot simulation, I expect the values returned by the circuits to be either -1.0 or 1.0

Actual behavior

The result returned by the folded circuits is 0.23688169075891283 no matter the scale factor, that is,

[0.2368816907589159, 0.2368816907589159, 0.2368816907589158]

for the example below (before extrapolation.)

Additional information

For debugging the partial results before extrapolation, you want to print the results array in https://github.com/PennyLaneAI/pennylane/blob/ae1f57298f626d9a825f8a0773ff16969e1fbb01/pennylane/transforms/mitigate.py#L590

This issue was noticed while working on https://github.com/PennyLaneAI/qml/pull/1207 @rmoyard and @dime10 are already aware of the issue.

Source code

import numpy as np
from functools import partial
from pennylane import qnode
import pennylane as qml

from pennylane.transforms import fold_global, poly_extrapolate

n_wires = 2
n_layers = 2

shapes = qml.SimplifiedTwoDesign.shape(n_wires, n_layers)
np.random.seed(0)
w1, w2 = [np.random.random(s) for s in shapes]

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

@partial(
    qml.transforms.mitigate_with_zne,
    scale_factors=[1, 3, 5],
    folding=fold_global,
    extrapolate=poly_extrapolate,
    extrapolate_kwargs={'order': 2})
@qnode(dev)
def circuit(w1, w2):
    qml.SimplifiedTwoDesign(w1, w2, wires=range(2))
    return qml.expval(qml.Z(0))

print(circuit(w1, w2))

Tracebacks

No response

System information

Name: PennyLane
Version: 0.39.0.dev30
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: /Users/cosenal/Library/Caches/pypoetry/virtualenvs/qml-L8ZUvnvl-py3.10/lib/python3.10/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing-extensions
Required-by: PennyLane-Catalyst, PennyLane-Cirq, PennyLane-qiskit, pennylane-qrack, pennylane-qulacs, PennyLane_Lightning, PennyLane_Lightning_Kokkos

Platform info:           macOS-15.0.1-arm64-arm-64bit
Python version:          3.10.15
Numpy version:           1.24.4
Scipy version:           1.11.4
Installed devices:
- lightning.qubit (PennyLane_Lightning-0.39.0.dev38)
- nvidia.custatevec (PennyLane-Catalyst-0.9.0.dev41)
- nvidia.cutensornet (PennyLane-Catalyst-0.9.0.dev41)
- oqc.cloud (PennyLane-Catalyst-0.9.0.dev41)
- softwareq.qpp (PennyLane-Catalyst-0.9.0.dev41)
- qiskit.aer (PennyLane-qiskit-0.38.0)
- qiskit.basicaer (PennyLane-qiskit-0.38.0)
- qiskit.basicsim (PennyLane-qiskit-0.38.0)
- qiskit.remote (PennyLane-qiskit-0.38.0)
- lightning.kokkos (PennyLane_Lightning_Kokkos-0.39.0.dev38)
- qrack.simulator (pennylane-qrack-0.10.0)
- default.clifford (PennyLane-0.39.0.dev30)
- default.gaussian (PennyLane-0.39.0.dev30)
- default.mixed (PennyLane-0.39.0.dev30)
- default.qubit (PennyLane-0.39.0.dev30)
- default.qutrit (PennyLane-0.39.0.dev30)
- default.qutrit.mixed (PennyLane-0.39.0.dev30)
- default.tensor (PennyLane-0.39.0.dev30)
- null.qubit (PennyLane-0.39.0.dev30)
- reference.qubit (PennyLane-0.39.0.dev30)
- qulacs.simulator (pennylane-qulacs-0.36.0)
- bluequbit.cpu (bluequbit-0.8.1b1)
- cirq.mixedsimulator (PennyLane-Cirq-0.36.0)
- cirq.pasqal (PennyLane-Cirq-0.36.0)
- cirq.qsim (PennyLane-Cirq-0.36.0)
- cirq.qsimh (PennyLane-Cirq-0.36.0)
- cirq.simulator (PennyLane-Cirq-0.36.0)

Existing GitHub issues

isaacdevlugt commented 1 month ago

Hey @cosenal! We're taking a look and will get back to you with more information as soon as we have it 👍