Ket API

Ket Quantum Programming Platform.

Ket is a comprehensive library designed for quantum programming, providing essential functions and classes to build quantum algorithms and applications. It facilitates the manipulation and storage of quantum states, measurement operations, and the computation of expected values.

Explore the documentation of individual submodules for in-depth information and more practical code examples.

All the functionality from the submodules is conveniently accessible within the ket namespace. Except for the lib module.

Examples

  • Grover algorithm

from math import sqrt, pi
import ket

def grover(size: int, oracle) -> int:
    p = ket.Process(simulator="dense", num_qubits=size)

    s = ket.H(p.alloc(size))

    steps = int((pi / 4) * sqrt(2**size))

    for _ in range(steps):
        oracle(s)
        with ket.around(ket.H, s):
            ket.phase_oracle(0, s)

    return ket.measure(s).value
  • Quantum teleportation protocol

import ket

def teleport(alice_msg, alice_aux, bob_aux):
    ket.ctrl(alice_msg, ket.X)(alice_aux)
    ket.H(alice_msg)

    m0 = ket.measure(alice_msg)
    m1 = ket.measure(alice_aux)

    if m1.value == 1:
        ket.X(bob_aux)
    if m0.value == 1:
        ket.Z(bob_aux)

    return bob_aux

def bell(qubits):
    return ket.ctrl(ket.H(qubits[0]), ket.X)(qubits[1])

def message(qubit):
    ket.H(alice)
    ket.Z(alice)

p = ket.Process()

alice = p.alloc()  # alice = |0⟩
message(alice)     # alice = |–⟩

bob = teleport(alice, *bell(p.alloc(2)))  # bob  <- alice

ket.H(bob)         # bob   = |1⟩
bob_m = ket.measure(bob)
print("Expected measure 1, result =", bob_m.value)

Modules

base

Base quantum programming classes.

clib

expv

Expected value calculation utilities.

gates

Quantum gate definitions.

ibm

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

lib

Quantum library.

operations

Functions to manipulate quantum operations.

quantumstate

Quantum state snapshot.

remote

Remote quantum execution module.

Functions ket

ket_version()

Return the version of the Ket platform components.

ket_version() list[str]

Return the version of the Ket platform components.