Source code for kosmos.circuit_runner.pennylane_runner
from collections.abc import Callable
from typing import Any, Literal
import pennylane as qml
from kosmos.circuit_runner.circuit_runner import CircuitRunner
from kosmos.circuit_runner.typing import QuantumCircuitFramework
[docs]
class PennyLaneRunner(CircuitRunner):
"""Runner for PennyLane.
Attributes:
device_name (Literal["default.qubit", "lightning.qubit"]): Name of the PennyLane device.
device (qml.devices.Device | None): The PennyLane device instance.
qnode (qml.QNode | None): The quantum node for circuit execution.
"""
def __init__(
self, device_name: Literal["default.qubit", "lightning.qubit"] = "lightning.qubit"
) -> None:
"""Initialize the PennyLane runner.
Args:
device_name (Literal["default.qubit", "lightning.qubit"]): Name of the PennyLane
device. Defaults to "lightning.qubit".
"""
self.device_name = device_name
self.device: qml.devices.Device | None = None
self.qnode: qml.QNode | None = None
@property
def framework(self) -> QuantumCircuitFramework:
"""The framework used by the circuit runner."""
return "pennylane"
[docs]
def execute(self, *args: Any, **kwargs: Any) -> Any: # noqa: ANN401
"""Execute the configured QNode with the given arguments.
Args:
*args (Any): Positional arguments passed to the underlying QNode.
**kwargs (Any): Keyword arguments passed to the underlying QNode.
Returns:
Any: The result returned by the QNode.
"""
if self.qnode is None:
msg = "QNode has not been configured. Call configure_qnode(...) first."
raise RuntimeError(msg)
return self.qnode(*args, **kwargs)