pasqal-io / qadence

Digital-analog quantum programming interface
https://pasqal-io.github.io/qadence/latest/
Apache License 2.0
72 stars 21 forks source link

[Bug] Shadows not working #607

Closed chMoussa closed 2 weeks ago

chMoussa commented 2 weeks ago

Short description

Classical shadows are not implemented correctly. When asking for instance an expectation over X(1) that should be non-zero, it returns 0.

What is the expected result?

A result close to the analytical expectation value.

What is the actual result?

0

Steps/Code to reproduce

import torch from torch import tensor from qadence import hamiltonian_factory, BackendName, DiffMode from qadence import Parameter, chain, kron, RX, RY, Z, QuantumCircuit, QuantumModel, X, Y from qadence.measurements import Measurements from qadence import product_state torch.manual_seed(42)

Define parameters for a circuit.

theta1 = Parameter("theta1", trainable=False) theta2 = Parameter("theta2", trainable=False) theta3 = Parameter("theta3", trainable=False) theta4 = Parameter("theta4", trainable=False)

blocks = chain( kron(RX(0, theta1), RY(1, theta2)), kron(RX(0, theta3), RY(1, theta4)), )

values = { "theta1": tensor([0.5]), "theta2": tensor([1.5]), "theta3": tensor([2.0]), "theta4": tensor([2.5]), }

Create a circuit and an observable.

circuit = QuantumCircuit(2, blocks) observable = hamiltonian_factory(2, detuning=Z) observable = X(1)

Create a model.

model = QuantumModel( circuit=circuit, observable=observable, backend=BackendName.PYQTORCH, diff_mode=DiffMode.AD, )

print(model.expectation(values, state= product_state("01"), ))

shadow_options = {"accuracy": 0.1, "confidence": 0.1} shadow_measurement = Measurements(protocol=Measurements.SHADOW, options=shadow_options) model.expectation(values, state= product_state("01"), measurement=shadow_measurement)

Tracebacks (optional)

No response

Environment details (optional)

No response

Would you like to work on this issue?

Yes

chMoussa commented 2 weeks ago

Closes with #608