Comments (3)
I have a fairly hacky patch, but it might be sufficient for your purposes. A better fix might involve with copying and updating Device.execute
to change the call signature for expval
.
class ProdPatch(qml.operation.Observable):
def __init__(self, prod_op):
self.prod_op = prod_op
@property
def name(self):
return [op.name for op in self.prod_op]
@property
def parameters(self):
return self.prod_op.parameters
@property
def wires(self):
return self.prod_op.wires
class MyDevice(Device):
author = "Christian Gogolin"
pennylane_requires = ">=0.29.1"
name = "MyDevice"
short_name = "my.device"
version = "0.1.0"
_capabilities = {
"tensor_observables": True,
}
@property
def observables(self):
return ["PauliZ", "Prod"]
def supports_observable(self, observable):
if isinstance(observable, list):
return all(self.supports_observable(ob) for ob in observable)
return super().supports_observable(observable)
def execute(self, queue, observables, parameters=None, **kwargs):
new_measurements = []
for mp in observables:
if mp.obs and isinstance(mp.obs, qml.ops.Prod):
new_measurements.append( type(mp)(obs=ProdPatch(mp.obs)) )
else:
new_measurements.append(mp)
return super().execute(queue, new_measurements, parameters=parameters, **kwargs)
def __init__(self, wires, *args, **kwargs):
if wires != 2:
raise NotImplementedError()
self._state = None
super().__init__(wires, *args, **kwargs)
def apply(self, operation, wires, par):
raise NotImplementedError(f"operation={operation}")
def expval(self, observable, wires, par):
if isinstance(observable, list):
observables = observable
for observable in observables:
if observable != "PauliZ":
raise NotImplementedError()
return np.prod([self.expval(observable, wire, par) for observable, wire in zip(observables, wires)])
if observable != "PauliZ":
raise NotImplementedError()
return float(np.real(np.dot(self._state.conj().T, qml.matrix(qml.PauliZ(wires), wire_order=qml.wires.Wires([0, 1])).dot(self._state))).item())
@property
def operations(self):
return []
def reset(self):
self._state = np.array([1., 0., 0., 0.])
I think this case actually helps demonstrate why we went though all the effort to both change operator arithmetic and to move to a new device interface.
-
Tensor.name
broke the interface set out inOperator
. Every other operator had it's name be a string, butTensor
had its name be a list of strings. This exception can be difficult to anticipate and account for. -
A name or type by itself can be insufficient to determine whether or not something is supported. For example, a hamiltonian containing a
Hermitian
. The set of strings might say the device supportsHamiltonian
, but then it won't be able to support aHamiltonian
that contains aHermitian
. -
Legacy devices (
qml.Device
) rely on things hardcoded into an "abstract base class" that make it rather difficult to extend and customize.
We can add in some patches to qml.Device
if that would help, but those wouldn't be released till our next release in July.
from pennylane.
Thanks for going through the trouble of proposing a workaround!!!
I agree that it is rather ugly, but it may help me not waste too much time on code that I anyway plan to port over to the new device API...
I reported this here just because it was a bit annoying to experience breaking changes to a part of the codebase that is deprecated and which I had therefore hoped would not change, but I understand that this is due to the coupling between the legacy devices and the operator arithmetic.
from pennylane.
Thank you for reporting this here @cvjjm! It does help us a lot to receive your feedback, both the good and bad experiences.
from pennylane.
Related Issues (20)
- [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 2
- [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`
- [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`
- [BUG] QAOA layers incorrectly forbid `Sum` observables
- Add function to convert PennyLane objects to OpenFermion objects HOT 2
- [BUG] Adjoint method does not respect global wires return types
- [BUG] TorchLayer does not work correctly with broadcasting and `tuple` returns HOT 1
- Propagate the gradient to part of an array only HOT 2
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.