pennylane

Dependencies

Install with pip install qc-executor[pennylane]. Requires:

  • pennylane>=0.42.3

  • packaging>=21.0

Executor

class qc_executor.pennylane.PennyLaneExecutor(backend='default.qubit', *args, shots=None, seed=None, log_file=None, log_level='WARNING', caching=None, cache_dir='cache', max_cache_size=4096, **kwargs)[source]

Bases: ExecutorBase

Quantum circuit executor backed by PennyLane.

The backend parameter accepts either a string (device name) or a ready-made Device instance.

Parameters:
  • backend (str or qml.devices.Device) – PennyLane device name or an already-instantiated device. Defaults to "default.qubit".

  • *args – Positional arguments forwarded to pennylane.device() (only when backend is a str). The most common positional argument is wires.

  • shots (int, optional) – Number of shots for sampling. Defaults to None.

  • seed (int, optional) – Random seed for reproducibility. Defaults to None.

  • log_file (str, optional) – Path to the log file. Defaults to None.

  • log_level (str) – Logging level ("DEBUG" / "INFO" / "WARNING" / "ERROR"). Defaults to "WARNING".

  • caching (bool, optional) – Whether to use caching. Defaults to None.

  • cache_dir (str) – Directory for caching. Defaults to "cache".

  • max_cache_size (int | None, optional) – Maximum number of entries kept in each in-memory cache; None makes them unbounded.

  • **kwargs – Keyword arguments forwarded to pennylane.device() (only when backend is a str). Typical keys are config and custom_decomps.

Note

If shots is set and a PennyLane config object containing a shots value is passed via **kwargs, the config value takes precedence and a UserWarning is emitted.

Devices are created once during initialization and are never recreated dynamically. The configured device must therefore provide enough wires for every executed circuit.

property shots: int | None

Return the number of shots.

property remote: bool

Return True if the execution access a remote backend.

property device_name: str

Return the name of the PennyLane device.

classmethod get_accepted_backend_types()[source]

Return all types accepted as the backend argument.

Covers: * PennyLane device instances (pennylane.devices.Device)

Returns:

List of accepted backend types.

Return type:

List[type]

classmethod get_accepted_backend_aliases()[source]

Return all currently available PennyLane device strings.

The list is discovered from PennyLane’s plugin registry so external PennyLane device plugins become available automatically.

Return type:

List[str]

Native abstraction

Circuit

class qc_executor.pennylane.PennyLaneCircuit(num_qubits=0, num_clbits=0, *, measurement=None, _ir=None)[source]

Bases: QuantumCircuitBase

A quantum circuit that compiles to PennyLane.

Built like any other circuit – PennyLaneCircuit(2) then circuit.h(0) – or converted from an existing one with from_quantum_circuit(). Compilation into PennyLane operations is lazy and re-runs whenever the instruction store changes.

Parameters:
  • num_qubits (int) – Number of qubits in the circuit.

  • num_clbits (int) – Number of classical bits, for mid-circuit measurement.

  • measurement (Optional[str]) – What the built callable returns besides applying the gates. None (default) returns nothing, for a caller that attaches its own measurement – as the executor does when it pairs the circuit with an observable. "probs" returns qml.probs over every qubit and "state" returns qml.state(), which makes the callable a complete QNode body, e.g. for qml.metric_tensor. Circuits derived through copy, invert or assign_parameters start again from None.

  • _ir (CircuitIR | None) – Adopt this instruction store instead of starting empty.

Raises:

ValueError – If measurement is not None, "probs" or "state".

classmethod supported_opcodes()[source]

Return the opcodes PennyLane executes directly.

Without this the base default (the whole gate set) would apply and the lowering pass would silently stop rewriting anything.

Return type:

frozenset

classmethod from_quantum_circuit(circuit, measurement=None)[source]

Convert any circuit into a PennyLane circuit.

Parameters:
  • circuit (QuantumCircuitBase) – The circuit to convert.

  • measurement (Optional[str]) – What the built callable returns; see the class docstring.

Return type:

PennyLaneCircuit

Returns:

circuit unchanged if it already is a PennyLane circuit with this measurement, else a new one holding a copy of its instructions.

property measurement: str | None

None, "probs" or "state".

Type:

What the built callable returns

property pennylane_circuit: Callable | None

PennyLane circuit that can be called with parameters

property parameter_names: list

List of circuit parameter names

property parameter_dimensions: dict

Dictionary with the dimension of each circuit parameter.

Derived from the instruction store rather than from the compiled operations, so it answers without forcing compilation. Lowering does not change which parameters appear, only which gates carry them.

build_pennylane_circuit()[source]

Return a callable PennyLane circuit built from the instruction store.

Compiles the store first if it has changed since the last call.

Returns:

Callable PennyLane circuit

Operator

class qc_executor.pennylane.PennyLaneOperator(paulis=None, coeffs=None, num_qubits=None, *, _ir=None)[source]

Bases: QuantumOperatorBase

An observable that compiles to a PennyLane measurement.

Built like any other operator – PennyLaneOperator(["ZI"], [1.0]) – or converted from an existing one with from_quantum_operator(). Compilation is lazy, so the inherited algebra (compose(), adjoint(), apply_layout(), …) works without paying for it.

Parameters:
  • paulis (Optional[Sequence[str]]) – Pauli labels, qubit 0 leftmost.

  • coeffs (Optional[Sequence[Any]]) – One coefficient per label.

  • num_qubits (int | None) – Width, required only when no labels are given.

  • _ir (PauliIR | None) – Adopt this representation instead of building one.

property parameter_dimensions: Dict[str, int]

Number of parameter occurrences, keyed by vector name.

property parameter_names: list

List of operator parameter names

property free_parameters: set

Return the set of free (non-bound) parameters in the operator.

property pennylane_words: List[Any]

The compiled PennyLane Pauli words.

measurement(values)[source]

Build the PennyLane measurement for one set of parameter values.

Coefficients are weights, not decoration: an unweighted qml.sum here silently returned the value for all-ones coefficients.

Parameters:

values (Sequence) – Values for this operator’s parameters, in order.

Returns:

The measurement process, or 0.0 for an empty operator.

build_pennylane_observable()[source]

Return a callable that measures this observable inside a QNode.

Return type:

Callable

Returns:

A callable taking one sequence of values per operator parameter.