qml.snapshots

snapshots(qnode)[source]

Create a function that retrieves snapshot results from a QNode.

Parameters

qnode (QNode) – the input QNode to be simulated

Returns

A function that has the same argument signature as qnode and returns a dictionary. When called, the function will execute the QNode on the registered device and retrieve the saved snapshots obtained via the qml.Snapshot operation. Additionally, the snapshot dictionary always contains the execution results of the QNode, so the use of the tag “execution_results” should be avoided to prevent conflicting key names.

Example

dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev, interface=None)
def circuit():
    qml.Snapshot()
    qml.Hadamard(wires=0)
    qml.Snapshot("very_important_state")
    qml.CNOT(wires=[0, 1])
    qml.Snapshot()
    return qml.expval(qml.PauliX(0))
>>> qml.snapshots(circuit)()
{0: array([1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]),
'very_important_state': array([0.70710678+0.j, 0.+0.j, 0.70710678+0.j, 0.+0.j]),
2: array([0.70710678+0.j, 0.+0.j, 0.+0.j, 0.70710678+0.j]),
'execution_results': array(0.)}

Contents

Using PennyLane

Development

API