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:

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

Base classes for quantum programming.

gates

Quantum gate definitions.

operations

Build circuits from gates, and get results from them.

measurement

Quantum measurement result classes.

expv

Expected value calculation utilities.

quantumstate

Quantum state snapshot representation and visualization.

qint

Quantum integer and fixed-point real number data structures.

fermion

Fermionic operators and utilities.

Quantum library

qulib

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

ibm

Module providing functionality to interact with IBM Quantum and IBM Cloud devices.

amazon

AmazonBraket Interface for Ket

mps

Matrix Product State (MPS) Quantum Simulator Backend.

npsim

Live Execution Simulator Based on NumPy

gateset

Native gate set decompositions for Ket.

clib.libket.execution

Base classes for execution targets: simulators, QPUs, and other backends.

Other modules

composer

Drag-and-drop quantum circuit composer for Jupyter notebooks.

Utilities

ket_version

Return the version strings of all Ket platform components.

set_log_level

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 (libket and kbw) for their build information and combines it with the Python package version.

Returns:

The versions of the Python package ("Ket v<version>"), of the libket runtime library, and of the kbw simulator, in this order.

set_log_level(level: int | str | None = None)[source]

Set the log level of Ket, Libket and KBW.

Parameters:

level – A logging level (logging.DEBUG) or its name ("debug", "info", "warning", "error", "off"; "trace" shows every operation of Libket and KBW). None keeps the level of the ket loggers and only passes it on to Libket and KBW, for when you configure logging yourself.

Records go to the handlers of the ket logger or of the root logger (see logging.basicConfig). Without any handler, Python prints warnings and errors to stderr.

Example

import ket
ket.set_log_level("debug")