PennyLaneAI / pennylane-qiskit

The PennyLane-Qiskit plugin integrates the Qiskit quantum computing framework and IBM Q with PennyLane.
https://docs.pennylane.ai/projects/qiskit
Apache License 2.0
189 stars 66 forks source link

Add caching capabilities to `QiskitDevice`. #165

Open antalszava opened 2 years ago

antalszava commented 2 years ago

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.

josh146 commented 2 years ago

@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
antalszava commented 2 years ago

@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.

josh146 commented 2 years ago

Good idea!

antalszava commented 2 years ago

Cool, added it :+1: