Custom backends

Ket runs programs through an execution target. Besides the built-in KBW simulators, a target can be any object that subclasses one of the base classes below (from ket.clib.libket.execution) and is passed to Process:

from ket import Process
from ket.npsim import NPSim

p = Process(NPSim(num_qubits=4))

Override only the callbacks that the target supports: the others raise CallbackNotImplementedError.

BatchExecution

Base class for constructing batch target executions.

LiveExecution

Base class for constructing live target executions.

NativeGateSet

Base class for constructing a Python-friendly native gate set.

CallbackNotImplementedError

Raised by an optional execution callback that a target does not support.

class BatchExecution[source]

Base class for constructing batch target executions.

configure(
num_qubits: int,
gradient=False,
coupling_graph: list[tuple[int, int]] | None = None,
native_gate_set: NativeGateSet | None = None,
decompose: bool = False,
)[source]

Configure the batch execution and return a QuantumExecution pointer.

Parameters:
  • num_qubits – Total number of qubits available in the execution target.

  • gradient – If True, enables parameter-shift gradient computation.

  • coupling_graph – Optional list of (control, target) pairs defining device connectivity. None means all-to-all.

  • native_gate_set – Optional NativeGateSet mapping abstract gates to hardware primitives.

  • decompose – If True, Libket decomposes gates into U/CNOT primitives before passing them to the callbacks.

sample(gates, qubits_to_sample, shots)[source]

Execute the circuit and sample the given qubits.

Warning

This method is called by Libket and should not be called directly.

exp_value(gates, hamiltonian_list)[source]

Execute the circuit and compute expectation values.

Warning

This method is called by Libket and should not be called directly.

sample_native(gates, qubits, shots)[source]

Execute the circuit with native gates and compute expectation values.

Warning

This method is called by Libket and should not be called directly.

exp_value_native(gates, hamiltonians)[source]

Execute the circuit with native and compute expectation values.

Warning

This method is called by Libket and should not be called directly.

gradient(gates, hamiltonian)[source]

Execute the circuit, compute expectation values and the parameters gradient.

Warning

This method is called by Libket and should not be called directly.

class LiveExecution[source]

Base class for constructing live target executions.

pauli_x(target, control)[source]

Apply a Pauli-X gate to the target qubit.

pauli_y(target, control)[source]

Apply a Pauli-Y gate to the target qubit.

pauli_z(target, control)[source]

Apply a Pauli-Z gate to the target qubit.

hadamard(target, control)[source]

Apply a Hadamard gate to the target qubit.

rotation_x(target, control, angle)[source]

Apply a X-Rotation gate to the target qubit.

rotation_y(target, control, angle)[source]

Apply a Y-Rotation gate to the target qubit.

rotation_z(target, control, angle)[source]

Apply a Z-Rotation gate to the target qubit.

phase(target, control, angle)[source]

Apply a Phase gate to the target qubit.

measure(qubits: list[int]) → int[source]

Measure the qubits and return the result as an integer.

exp_value(hamiltonian: dict)[source]

Compute the expectation value of the given Hamiltonian.

sample(qubits: list[int], shots: int)[source]

Sample the state of the qubits.

dump(qubits: list[int]) → dict[source]

Dump the state of the qubits.

compute_native_gates(gates) → dict[source]

Compute native gates.

abstractmethod connect()[source]

Call configure with the appropriated arguments to generate the object.

configure(num_qubits: int, decompose: bool = False)[source]

Configure the live execution and return a QuantumExecution pointer.

Parameters:
  • num_qubits – Total number of qubits available in the execution target.

  • decompose – If True, Libket decomposes multi-qubit gates into U/CNOT primitives before invoking the callbacks.

class NativeGateSet[source]

Base class for constructing a Python-friendly native gate set.

Subclass this to define how abstract gates are translated into the instruction set of a specific backend (hardware or simulator). The two methods, translate and cnot, work at the Python level; all JSON marshalling with the C layer is handled internally.

A NativeGateSet instance can be passed directly to BatchExecution.configure.

Example:

class MyGateSet(NativeGateSet):
    def translate(self, matrix, target):
        # matrix is [[u00, u01], [u10, u11]] (complex values)
        # Return a list of native gate dicts
        return [{"U": {"matrix": str(matrix), "target": target}}]

    def cnot(self, control, target):
        return [{"CNOT": {"control": control, "target": target}}]

gate_set = MyGateSet()
ptr = batch_exec.configure(native_gate_set=gate_set)
abstractmethod translate(matrix: list[list[complex]], target: int) → list[source]

Translate a single-qubit gate into native gate instructions.

The gate is described by its 2x2 unitary matrix as a list of two rows of complex amplitudes:

[[u00, u01],
 [u10, u11]]
Parameters:
  • matrix – The 2x2 unitary matrix as complex values.

  • target – Index of the target qubit.

Returns:

A list of native gate objects (serialisable to JSON) that implement the given unitary on target.

abstractmethod cnot(control: int, target: int) → list[source]

Produce native instructions implementing a CNOT gate.

Parameters:
  • control – Index of the control qubit.

  • target – Index of the target qubit.

Returns:

A list of native gate objects that implement CNOT.

exception CallbackNotImplementedError[source]

Bases: NotImplementedError

Raised by an optional execution callback that a target does not support.

Subclasses override only the callbacks their target supports, so these methods are not abstract.

__eq__(value, /)

Return self==value.

__ge__(value, /)

Return self>=value.

__gt__(value, /)

Return self>value.

__le__(value, /)

Return self<=value.

__lt__(value, /)

Return self<value.

__ne__(value, /)

Return self!=value.