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.

Parameters:
  • 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

Raises:

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

analytic_probability(wires=None)[source]

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

PennyLane uses the convention |q0,q1,,qN1 where q0 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.

Note

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

Parameters:

wires (Iterable[Number, str], Number, str, Wires) – wires to return marginal probabilities for. Wires not provided are traced out of the system.

Returns:

list of the probabilities

Return type:

array[float]

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:

  • 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
"RX"
>>> op.wires # returns a Wires object representing the wires that the operation acts on
Wires([0])
>>> op.parameters # returns a list of parameters
[0.2]
Parameters:

operations (list[Operation]) – operations to apply to the device

Keyword Arguments:
  • 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

Return type:

None

capabilities()[source]

Get the capabilities of this device class.

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

@classmethod
def capabilities(cls):
    capabilities = super().capabilities().copy()
    capabilities.update(
        supports_a_new_capability=True,
    )
    return capabilities
Returns:

results

Return type:

dict[str->*]

generate_samples()[source]

Returns the computational basis samples generated for all wires.

Note that PennyLane uses the convention |q0,q1,,qN1 where q0 is the most significant bit.

Warning

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

Returns:

array of samples in the shape (dev.shots, dev.num_wires)

Return type:

array[complex]

reset()[source]

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:

None

run(compiled_c)[source]

Run the compiled circuit, and query the result.

Return type:

None

property state: ndarray

Returns the state vector of the circuit prior to measurement.

Note

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

Parameters:
  • 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:

Circuit

Returns:

pytket circuit