API documentation#

class pytket.extensions.pennylane.PytketDevice(wires, shots=None, pytket_backend=<pytket.extensions.qiskit.backends.aer.AerStateBackend object>, optimisation_level=None, compilation_pass=None)[source]#

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

__init__(wires, shots=None, pytket_backend=<pytket.extensions.qiskit.backends.aer.AerStateBackend object>, optimisation_level=None, compilation_pass=None)[source]#

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


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.

Return type:



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, rotations=None)[source]#

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: :rtype: None

  • 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


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
Return type:

Dict[str, Any]


dict[str->*]: results


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. :rtype: ndarray


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 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.

Return type:



Run the compiled circuit, and query the result.

Return type:


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, wire_map, qreg, creg, measure=False)[source]#

Convert a PennyLane circuit to a pytket circuit

  • operations (List[Operation]) – list of operations

  • wire_map (OrderedDict) – mapping of qubits

  • qreg (QubitRegister) – target qubit register

  • creg (BitRegister) – target bit register

  • measure (bool) – whether to add measurements

Return type:



pytket circuit