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 qulib 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)
# Expected measure 1, result = 1
Submodules¶
AmazonBraket Interface for Ket |
|
Base classes for quantum programming. |
|
Expected value calculation utilities. |
|
Fermionic operators and utilities. |
|
Quantum gate definitions. |
|
Module providing functionality to interact with IBM Quantum and IBM Cloud devices. |
|
Quantum measurement result classes. |
|
Live Execution Simulator Based on NumPy |
|
Functions to manipulate quantum operations. |
|
Quantum integer and fixed-point real number data structures. |
|
Quantum state snapshot representation and visualization. |
|
Quantum library. |
- ket_version() list[str]¶
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:
"Ket v<version>", the Python package version.A build-info string from the
libketRust runtime library.A build-info string from the
kbwquantum simulator library.
- Return type:
A list containing three version strings in the following order