pennylane¶
Dependencies¶
Install with pip install qc-executor[pennylane]. Requires:
pennylane>=0.42.3packaging>=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:
ExecutorBaseQuantum circuit executor backed by PennyLane.
The backend parameter accepts either a string (device name) or a ready-made
Deviceinstance.- 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 astr). 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;
Nonemakes them unbounded.**kwargs – Keyword arguments forwarded to
pennylane.device()(only when backend is astr). Typical keys areconfigandcustom_decomps.
Note
If shots is set and a PennyLane
configobject containing ashotsvalue is passed via**kwargs, theconfigvalue takes precedence and aUserWarningis 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.
Native abstraction¶
Circuit¶
- class qc_executor.pennylane.PennyLaneCircuit(num_qubits=0, num_clbits=0, *, measurement=None, _ir=None)[source]¶
Bases:
QuantumCircuitBaseA quantum circuit that compiles to PennyLane.
Built like any other circuit –
PennyLaneCircuit(2)thencircuit.h(0)– or converted from an existing one withfrom_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"returnsqml.probsover every qubit and"state"returnsqml.state(), which makes the callable a complete QNode body, e.g. forqml.metric_tensor. Circuits derived throughcopy,invertorassign_parametersstart again fromNone._ir (
CircuitIR|None) – Adopt this instruction store instead of starting empty.
- Raises:
ValueError – If
measurementis notNone,"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:
- Returns:
circuitunchanged 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.
Operator¶
- class qc_executor.pennylane.PennyLaneOperator(paulis=None, coeffs=None, num_qubits=None, *, _ir=None)[source]¶
Bases:
QuantumOperatorBaseAn observable that compiles to a PennyLane measurement.
Built like any other operator –
PennyLaneOperator(["ZI"], [1.0])– or converted from an existing one withfrom_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.sumhere 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.0for an empty operator.