Giter VIP home page Giter VIP logo

Comments (4)

trbromley avatar trbromley commented on September 21, 2024 1

Hi @poojithumeshrao! Thanks for posting this. I tweaked the title of this issue to reflect that we still have a valid feature request to eventually take care of, i.e., support for qml.Hermitian in classical shadows workflows.

from pennylane.

CatalinaAlbornoz avatar CatalinaAlbornoz commented on September 21, 2024

Hi @poojithumeshrao, thank you for opening this bug report!
Our team took a look at your bug and found that this seems to be expected behaviour but with an error message that doesn't say much.

A ClassicalShadow is a classical description of a quantum state that is capable of reproducing expectation values of local Pauli observables. In this case your error arises because the expected observable(s) aren't valid Pauli words. The Hermitian observable has no (obvious and a priori available) Pauli decomposition/representation.

Unfortunately it doesn't look like an easy fix from PennyLane's side but we will indeed improve the error message to make it easier to understand what's going on.

On the other hand we noticed that you're using PennyLane v0.32. If possible my recommendation would be to upgrade to the latest version. We will make a new release in two weeks so you can update your version now and again in two weeks to smooth out any possible issues.

If you want to learn more about classical shadows feel free to take a look at this demo.

from pennylane.

poojithumeshrao avatar poojithumeshrao commented on September 21, 2024

Hi, @CatalinaAlbornoz, thank you for the timely response.

After going through the traceback, I was able to make it work (at least in my case) by manually decomposing the Hermitian matrix using qml.pauli_decompose(). The modified code is as follows:

import pennylane as qml
import pennylane.numpy as np
from matplotlib import pyplot as plt
from pennylane import classical_shadow, shadow_expval, ClassicalShadow, expval

np.random.seed(666)

def pqc(f1,wires=range(2)):
    qml.ArbitraryStatePreparation(f1,wires=wires)

q = np.random.random((6))

mat = np.matrix(qml.matrix(pqc)(q))
mat = mat + mat.getH()
mat /= 2

H = qml.pauli_decompose(mat)

dev = qml.device("default.qubit", wires=range(2), shots=10000)
@qml.qnode(dev, interface="autograd")
def qnode(x, H):
    qml.Hadamard(0)
    qml.CNOT((0,1))
    qml.RX(x, wires=0)
    return shadow_expval([H])

x = np.array(0.5, requires_grad=True)

print(qnode(x, H), qml.grad(qnode)(x, H))

I am not sure if it works generally, but I thought it would also be useful for you.

from pennylane.

CatalinaAlbornoz avatar CatalinaAlbornoz commented on September 21, 2024

Ah this is great! Thank you for sharing it @poojithumeshrao .

from pennylane.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.