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))
BatchExecutionreceives the whole circuit at once, when a result is first needed. Use it for QPUs and remote services (seeIBMDeviceandAmazonBraket).LiveExecutionreceives each gate as soon as it is applied, so measurement results are available right away (seeNPSim).NativeGateSettranslates gates into the instruction set of a device (seeket.gateset).
Override only the callbacks that the target supports: the others raise
CallbackNotImplementedError.
Base class for constructing batch target executions. |
|
Base class for constructing live target executions. |
|
Base class for constructing a Python-friendly native gate set. |
|
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,
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.Nonemeans all-to-all.native_gate_set – Optional
NativeGateSetmapping 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.
- class LiveExecution[source]¶
Base class for constructing live target executions.
- 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,
translateandcnot, work at the Python level; all JSON marshalling with the C layer is handled internally.A
NativeGateSetinstance can be passed directly toBatchExecution.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.
- exception CallbackNotImplementedError[source]¶
Bases:
NotImplementedErrorRaised 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.