API documentation#

class pytket.extensions.pennylane.PytketDevice(wires: int, shots: int | None = None, pytket_backend: ~pytket.backends.Backend = <pytket.extensions.qiskit.backends.aer.AerStateBackend object>, optimisation_level: int | None = None, compilation_pass: ~pytket.passes.BasePass | None = None)#

PytketDevice allows pytket backends and compilation to be used as Pennylane devices.

__init__(wires: int, shots: int | None = None, pytket_backend: ~pytket.backends.Backend = <pytket.extensions.qiskit.backends.aer.AerStateBackend object>, optimisation_level: int | None = None, compilation_pass: ~pytket.passes.BasePass | None = None)#

Construct a device that use a Pytket Backend and compilation to execute circuits.

  • wires (int) – Number of wires

  • shots (Optional[int], optional) – Number of shots to use (only relevant for sampling backends), defaults to None

  • pytket_backend (Backend, optional) – Pytket Backend class to use, defaults to AerStateBackend() to facilitate automated pennylane testing of this backend

  • optimisation_level (int, optional) – Backend default compilation optimisation level, ignored if compilation_pass is set, defaults to None

  • compilation_pass (Optional[BasePass], optional) – Pytket compiler pass with which to compile circuits, defaults to None


ValueError – If the Backend does not support shots or state results

analytic_probability(wires: int | Iterable[int] | None = None) ndarray#

Return the (marginal) probability of each computational basis state from the last run of the device.

PennyLane uses the convention \(|q_0,q_1,\dots,q_{N-1}\rangle\) where \(q_0\) is the most significant bit.

If no wires are specified, then all the basis states representable by the device are considered and no marginalization takes place.


marginal_prob() may be used as a utility method to calculate the marginal probability distribution.

wires (Iterable[Number, str], Number, str, Wires): wires to return

marginal probabilities for. Wires not provided are traced out of the system.


array[float]: list of the probabilities

apply(operations: List[Operation], rotations: List[Operation] | None = None) None#

Apply quantum operations, rotate the circuit into the measurement basis, and compile and execute the quantum circuit.

This method receives a list of quantum operations queued by the QNode, and should be responsible for:

  • Constructing the quantum program

  • (Optional) Rotating the quantum circuit using the rotation operations provided. This diagonalizes the circuit so that arbitrary observables can be measured in the computational basis.

  • Compile the circuit

  • Execute the quantum circuit

Both arguments are provided as lists of PennyLane Operation instances. Useful properties include name, wires, and parameters:

>>> op = qml.RX(0.2, wires=[0])
>>> op.name # returns the operation name
>>> op.wires # returns a Wires object representing the wires that the operation acts on
<Wires = [0]>
>>> op.parameters # returns a list of parameters

operations (list[~.Operation]): operations to apply to the device

Keyword args:
rotations (list[~.Operation]): operations that rotate the circuit

pre-measurement into the eigenbasis of the observables.

hash (int): the hash value of the circuit constructed by CircuitGraph.hash

capabilities() Dict[str, Any]#

Get the capabilities of this device class.

Inheriting classes that change or add capabilities must override this method, for example via

def capabilities(cls):
    capabilities = super().capabilities().copy()
    return capabilities

dict[str->*]: results

generate_samples() ndarray#

Returns the computational basis samples generated for all wires.

Note that PennyLane uses the convention \(|q_0,q_1,\dots,q_{N-1}\rangle\) where \(q_0\) is the most significant bit.


This method should be overwritten on devices that generate their own computational basis samples, with the resulting computational basis samples stored as self._samples.


array[complex]: array of samples in the shape (dev.shots, dev.num_wires)

reset() None#

Reset the backend state.

After the reset, the backend should be as if it was just constructed. Most importantly the quantum state is reset to its initial value.

run(compiled_c: Circuit) None#

Run the compiled circuit, and query the result.

property state: ndarray#

Returns the state vector of the circuit prior to measurement.


Only state vector simulators support this property. Please see the plugin documentation for more details.

pytket.extensions.pennylane.pennylane_to_tk(operations: List[Operation], wire_map: OrderedDict, qreg: QubitRegister, creg: BitRegister, measure: bool = False) Circuit#

Convert a PennyLane circuit to a pytket circuit

  • operations – list of operations

  • wire_map – mapping of qubits

  • qreg – target qubit register

  • creg – target bit register

  • measure – whether to add measurements


pytket circuit