Ket API¶
Ket quantum programming.
Every name below is available as ket.<name> (or from ket import *),
except ket.qulib, the library of ready-made algorithms, and
ket.composer, the circuit editor.
Quick tour:
from ket import *
p = Process() # runs on the KBW simulator
q = p.alloc(2) # a `Quant` with 2 qubits: q[0], q[1], q[:1], ...
H(q[0]) # gates take qubits and return them
with control(q[0]): # every gate inside is controlled by q[0]
X(q[1])
print(sample(q).get()) # {0: ~1024, 3: ~1024}
with obs(): # build a Hamiltonian from Pauli operators
h = Z(q[0]) * Z(q[1])
print(exp_value(h).get()) # 1.0
print(measure(q).value) # 0 or 3, and the state collapses
Where to look next:
Process: simulators,livevsbatchexecution, and running on other backends.ket.operations:control,adj,around,measure,sample, andexp_value.ket.qulib: state preparation, oracles, Hamiltonians, and QUBO.ket.composer.Composer: drag-and-drop circuit editor for Jupyter notebooks that returns a gate (pip install ket-lang[composer];from ket.composer import Composer).set_log_level: see what the runtime and simulator are doing.
Note
This reference documents Ket v0.10.5.
Everything in the core modules below is also available directly as
ket.<name> (or with from ket import *).
Core¶
Base classes for quantum programming. |
|
Quantum gate definitions. |
|
Build circuits from gates, and get results from them. |
|
Quantum measurement result classes. |
|
Expected value calculation utilities. |
|
Quantum state snapshot representation and visualization. |
|
Quantum integer and fixed-point real number data structures. |
|
Fermionic operators and utilities. |
Quantum library¶
Quantum library: ready-made building blocks for quantum algorithms. |
Backends and simulators¶
The modules ket.ibm, ket.amazon, and ket.mps need optional
dependencies: install them with pip install ket-lang[ibm],
ket-lang[amazon], or ket-lang[mps].
Module providing functionality to interact with IBM Quantum and IBM Cloud devices. |
|
AmazonBraket Interface for Ket |
|
Matrix Product State (MPS) Quantum Simulator Backend. |
|
Live Execution Simulator Based on NumPy |
|
Native gate set decompositions for Ket. |
Base classes for execution targets: simulators, QPUs, and other backends. |
Other modules¶
Drag-and-drop quantum circuit composer for Jupyter notebooks. |
Utilities¶
Return the version strings of all Ket platform components. |
|
Set the log level of Ket, Libket and KBW. |
- ket_version() list[str][source]¶
Return the version strings of all Ket platform components.
Queries the underlying Rust libraries (
libketandkbw) for their build information and combines it with the Python package version.- Returns:
The versions of the Python package (
"Ket v<version>"), of thelibketruntime library, and of thekbwsimulator, in this order.
- set_log_level(level: int | str | None = None)[source]¶
Set the log level of Ket, Libket and KBW.
- Parameters:
level – A
logginglevel (logging.DEBUG) or its name ("debug","info","warning","error","off";"trace"shows every operation of Libket and KBW).Nonekeeps the level of theketloggers and only passes it on to Libket and KBW, for when you configureloggingyourself.
Records go to the handlers of the
ketlogger or of the root logger (seelogging.basicConfig). Without any handler, Python prints warnings and errors tostderr.Example
import ket ket.set_log_level("debug")