Comments (4)
Thanks @cvjjm for catching this! Something else that I think is important to identify: why the existence of the test for this feature did not catch this regression. My suspicion is that the test is not being run 🤔
from pennylane.
Thanks @cvjjm for catching this! Something else that I think is important to identify: why the existence of the test for this feature did not catch this regression. My suspicion is that the test is not being run 🤔
The test is being run. The reason why the current test case is passing is in how the device is defined. In the current version of the test case, the test device overrides the _asarray
method (line 1432 of test_autograd.py
).
@staticmethod
def _asarray(arr, dtype=None):
# pylint: disable=unused-argument
return arr
which bypasses the issue.
from pennylane.
Thanks for the issue.
I see two main differences that have changed. I think PennyLane will actually currently be better at supporting arbitrary results now. We used to always smash all the results into a ragged array, but now we can handle any type of result object.
You can add a custom measurement, with any custom return, by inheriting from qml.measurements.StateMeasurement
and/or qml.measurements.SampleMeasurement
. See documentation on this here
Then with DefaultQubit
, you can simply do something like:
class SpecialObject:
def __init__(self, val):
self.val = val
def __repr__(self):
return f"SpecialObject({self.val})"
class CustomMeas(qml.measurements.StateMeasurement):
def process_state(self, state, wire_order):
return SpecialObject(state) # or some other postprocessing of the state
@qml.qnode(qml.device('default.qubit'), interface=None)
def circuit():
qml.PauliX(0)
return CustomMeas()
circuit()
SpecialObject([0. 1.])
If you want to maintain your own device and not just extend DefaultQubit
, you should simply need to make sure execute
(or old interface batch_execute
) returns an object of the right type.
Update:
I've also managed to get the Legacy Interface code to mostly work by swapping qml.expval
with a CustomExpval
as well.
class CustomExpval(qml.measurements.StateMeasurement):
def process_state(self, state, wire_order):
expval = qml.expval(qml.PauliZ(0)).process_state(state, wire_order)
return SpecialObject(expval)
@qml.qnode(dev, diff_method="parameter-shift")
def qnode(x):
qml.RY(x, wires=0)
return CustomExpval(obs=SpecialObservable(wires=0))
We currently assume that the result of an ExpectationMP
is a float of either size zero or with a batch dimension. By having a different measurement process, we can circumvent that assumption.
from pennylane.
Nice! That was super useful. I will first try to get it to work with v0.30 with overwriting _asarray()
and then work on compatibility of our devices with the new device interface. I will close this for now.
from pennylane.
Related Issues (20)
- [BUG] `ValueError: Unrecognized keyword arguments passed to KerasLayer: {'dynamic': True}` HOT 1
- [BUG] Various `Operator.pow` methods with a batched exponent fail
- [BUG] `param_shift` with `broadcast=True` does not work with zero-length recipes HOT 1
- Add `StateMeasurement.process_density_matrix` method
- [BUG] Issues with `metric_tensor` and trainable params causing errors with jax
- Improve operator support for new `assert_equal` function: Group 1
- Improve measurement support in from_qasm
- Support native measurement-based snapshots in Default Clifford
- Improve operator support for new `assert_equal` function: Group 2
- Add a QutritChannel operation
- [BUG] Error when using `PauliRot` and `torch` interface HOT 3
- PennyLane Torch Implementation Question HOT 5
- Issue when using batched data on StronglyEntanglingLayers template.[BUG] HOT 2
- [BUG] `sum_expand` producing incorrect result dimensions
- [BUG] lighthing.qubit torch layer batch size issue HOT 1
- [BUG] Specs does not take into account transforms HOT 2
- How to support Prod observables in custom devices? HOT 1
- [BUG] Can't differentiate `dynamic_one_shot` with `autograd` and `jax` HOT 1
- [BUG] `dynamic_one_shot` does not work with TensorFlow HOT 1
- [BUG] CNOT decomposes to CNOT
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.