qml.workflow.jacobian_products.LightningVJPs¶
-
class
LightningVJPs
(device, gradient_kwargs=None)[source]¶ Bases:
pennylane.workflow.jacobian_products.DeviceDerivatives
Calculates VJPs natively using lightning.qubit.
- Parameters
device (LightningBase) – Lightning ecosystem devices
lightning.gpu
orlightning.kokkos
.gradient_kwargs (Optional[dict]) – Any gradient options.
>>> dev = qml.device('lightning.qubit', wires=5) >>> jpc = LightningVJPs(dev, gradient_kwargs={"use_device_state": True, "method": "adjoint_jacobian"}) >>> tape = qml.tape.QuantumScript([qml.RX(1.2, wires=0)], [qml.expval(qml.Z(0))]) >>> dev.batch_execute((tape,)) [array(0.36235775)] >>> jpc.compute_vjp((tape,), (0.5,) ) ((array(-0.46601954),),) >>> -0.5 * np.sin(1.2) -0.46601954298361314
Methods
compute_jacobian
(tapes)Compute the full Jacobian for a batch of tapes.
compute_vjp
(tapes, dy)Compute the vjp for a given batch of tapes.
execute_and_cache_jacobian
(tapes)Forward pass used to cache the results and jacobians.
execute_and_compute_jacobian
(tapes)Compute the results and the full Jacobian for a batch of tapes.
execute_and_compute_jvp
(tapes, tangents)Calculate both the results for a batch of tapes and the jvp.
-
compute_jacobian
(tapes)¶ Compute the full Jacobian for a batch of tapes.
This method is required to compute Jacobians in the
jax-jit
interface- Parameters
tapes – the batch of tapes to take the Jacobian of
- Returns
the full jacobian
- Return type
TensorLike
- Side Effects:
caches the newly computed jacobian if it wasn’t already present in the cache.
Examples:
For an instance of
DeviceDerivatives
jpc
, we have:>>> tape0 = qml.tape.QuantumScript([qml.RX(0.1, wires=0)], [qml.expval(qml.Z(0))]) >>> tape1 = qml.tape.QuantumScript([qml.RY(0.2, wires=0)], [qml.expval(qml.Z(0)), qml.expval(qml.X(0))]) >>> batch = (tape0, tape1) >>> jpc.compute_jacobian(batch) (array(-0.09983342), (array(-0.19866933), array(0.98006658)))
While this method could support non-scalar parameters in theory, no implementation currently supports jacobians with non-scalar parameters.
-
compute_vjp
(tapes, dy)[source]¶ Compute the vjp for a given batch of tapes.
This method is used by autograd, torch, and tensorflow to compute VJPs.
- Parameters
tapes (tuple[~.QuantumScript]) – the batch of tapes to take the derivatives of
dy (tuple[tuple[TensorLike]]) – the derivatives of the results of an execution. The
i
th entry (cotangent) corresponds to thei
th tape, and thej
th entry of thei
th cotangent corresponds to thej
th return value of thei
th tape.
- Returns
the vector jacobian product.
- Return type
TensorLike
- Side Effects:
caches the newly computed jacobian if it wasn’t already present in the cache.
Examples:
For an instance of
DeviceDerivatives
jpc
, we have:>>> tape0 = qml.tape.QuantumScript([qml.RX(0.1, wires=0)], [qml.expval(qml.Z(0))]) >>> tape1 = qml.tape.QuantumScript([qml.RY(0.2, wires=0)], [qml.expval(qml.Z(0)), qml.expval(qml.X(0))]) >>> batch = (tape0, tape1) >>> dy0 = (0.5, ) >>> dy1 = (2.0, 3.0) >>> dys = (dy0, dy1) >>> vjps = jpc.compute_vjp(batch, dys) >>> vjps (array([-0.04991671]), array([2.54286107])) >>> expected_vjp0 = 0.5 * -np.sin(0.1) >>> qml.math.allclose(vjps[0], expected_vjp0) True >>> expected_jvp1 = 2.0 * -np.sin(0.2) + 3.0 * np.cos(0.2) >>> qml.math.allclose(vjps[1], expected_vjp1) True
While this method could support non-scalar parameters in theory, no implementation currently supports jacobians with non-scalar parameters.
-
execute_and_cache_jacobian
(tapes)¶ Forward pass used to cache the results and jacobians.
- Parameters
tapes (tuple[~.QuantumScript]) – the batch of tapes to execute and take derivatives of
- Returns
the results of the execution.
- Return type
ResultBatch
- Side Effects:
Caches both the results and jacobian into
_results_cache
and_jacs_cache
.
-
execute_and_compute_jacobian
(tapes)¶ Compute the results and the full Jacobian for a batch of tapes.
This method is required to compute Jacobians in the
jax-jit
interface- Parameters
tapes (tuple[QuantumScript]) – the batch of tapes to take the derivatives of
Examples:
For an instance of
JacobianProductCalculator
jpc
, we have:>>> tape0 = qml.tape.QuantumScript([qml.RX(0.1, wires=0)], [qml.expval(qml.Z(0))]) >>> tape1 = qml.tape.QuantumScript([qml.RY(0.2, wires=0)], [qml.expval(qml.Z(0)), qml.expval(qml.X(0))]) >>> batch = (tape0, tape1) >>> results, jacs = jpc.execute_and_compute_jacobian(batch) >>> results (0.9950041652780258, (0.9800665778412417, 0.19866933079506116)) >>> jacs (array(-0.09983342), (array(-0.19866933), array(0.98006658)))
While this method could support non-scalar parameters in theory, no implementation currently supports jacobians with non-scalar parameters.
-
execute_and_compute_jvp
(tapes, tangents)¶ Calculate both the results for a batch of tapes and the jvp.
This method is required to compute JVPs in the JAX interface.
- Parameters
tapes (tuple[~.QuantumScript]) – The batch of tapes to take the derivatives of
tangents (Sequence[Sequence[TensorLike]]) – the tangents for the parameters of the tape. The
i
th tangent corresponds to thei
th tape, and thej
th entry into a tangent entry corresponds to thej
th trainable parameter of the tape.
- Returns
the results of the execution and the jacobian vector product
- Return type
ResultBatch, TensorLike
- Side Effects:
caches newly computed results or jacobians if they were not already cached.
Examples:
For an instance of
DeviceDerivatives
jpc
, we have:>>> tape0 = qml.tape.QuantumScript([qml.RX(0.1, wires=0)], [qml.expval(qml.Z(0))]) >>> tape1 = qml.tape.QuantumScript([qml.RY(0.2, wires=0)], [qml.expval(qml.Z(0))]) >>> batch = (tape0, tape1) >>> tangents0 = (1.5, ) >>> tangents1 = (2.0, ) >>> tangents = (tangents0, tangents1) >>> results, jvps = jpc.execute_and_compute_jvp(batch, tangents) >>> expected_results = (np.cos(0.1), np.cos(0.2)) >>> qml.math.allclose(results, expected_results) True >>> jvps (array(-0.14975012), array(-0.39733866)) >>> expected_jvps = 1.5 * -np.sin(0.1), 2.0 * -np.sin(0.2) >>> qml.math.allclose(jvps, expected_jvps) True
While this method could support non-scalar parameters in theory, no implementation currently supports jacobians with non-scalar parameters.