pennylane-qiskit icon indicating copy to clipboard operation
pennylane-qiskit copied to clipboard

Add caching capabilities to `QiskitDevice`.

Open antalszava opened this issue 3 years ago • 4 comments

The PennyLane QubitDevice class allows caching certain circuit executions. Once the same circuit is being executed multiple times, cached values may be used to provide the result of circuit executions.

This capability could also be added to QiskitDevice.

antalszava avatar Dec 10 '21 20:12 antalszava

@antalszava I'm curious if the caching in the device is superceded by the caching in the qml.execute function?

This new caching capability is accessible via the new QNode:

dev = qml.device('qiskit.aer', wires=2)

cache = {}

@qml.qnode(dev, cache=cache)
def expval_circuit(params):
    qml.templates.BasicEntanglerLayers(params, wires=wires, rotation=qml.RX)
    return qml.expval(qml.Hermitian(hamiltonian, wires=wires))

@qml.qnode(dev, cache=cache)
def var_circuit(params):
    qml.templates.BasicEntanglerLayers(params, wires=wires, rotation=qml.RX)
    return qml.var(qml.Hermitian(hamiltonian, wires=wires))
>>> print(expval_circuit(params), var_circuit(params)))
-2.6224875809703287, 0.011605015096940896
>>> print(cache)
{-2104543859499562169: array([-2.62248758]), 3268478774057874589: array([0.01160502])}
>>> print(expval_circuit(params), var_circuit(params)))  # evaluate QNodes again
-2.6224875809703287, 0.011605015096940896
>>> dev.num_executions  # only the first two executions will register
2

josh146 avatar Dec 12 '21 14:12 josh146

@josh146 oh that's a great point! I think so :thinking: Should the QubitDevice caching ability be deprecated then? We could have it on the roadmap.

antalszava avatar Dec 14 '21 14:12 antalszava

Good idea!

josh146 avatar Dec 14 '21 14:12 josh146

Cool, added it :+1:

antalszava avatar Dec 14 '21 14:12 antalszava