Comments (4)
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.
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.
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.
Ah this is great! Thank you for sharing it @poojithumeshrao .
from pennylane.
Related Issues (20)
- [BUG] lighthing.qubit torch layer batch size issue HOT 1
- [BUG] Specs does not take into account transforms HOT 7
- How to support Prod observables in custom devices? HOT 3
- [BUG] Can't differentiate `dynamic_one_shot` with `autograd` and `jax` HOT 2
- [BUG] `dynamic_one_shot` does not work with TensorFlow HOT 1
- [BUG] CNOT decomposes to CNOT
- [BUG] Some decompositions/transforms do not preserve derivatives HOT 1
- [BUG] `defer_measurements` does not work with `qml.cond` applied to controlled gates HOT 3
- Define `BasisStateProjector.compute_sparse_matrix` HOT 3
- [BUG] `KerasLayer` has unintended side effects on its QNode HOT 1
- [BUG] Documentation generated does not show the parameters for subclasses of Operation. HOT 1
- Add support for Quantum ESPRESSO HOT 2
- [BUG] `QuantumScript.map_to_standard_wires` sometimes returns original tape HOT 2
- [CHORE] Update `docs/requirements.txt` to reduce package bloat
- [BUG] Differentiation does not work with `dynamic_one_shot` HOT 1
- [BUG] Different ground states when batching with vmap or calling the function directly HOT 5
- Add `PrepSelPrep` block encoding template HOT 4
- Support variable number of trotter steps in the UCCSD ansatz HOT 12
- [BUG] No warnings or errors are being raised when `shots` argument is passed arbitrarily to `@qnode` with no applicable gradient method HOT 5
- [BUG] `qml.simplify` removes information regarding `num_steps` from `Exp`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pennylane.