Get started#

Installation PyPI_Ket#

Ket is available for Linux (manylinux2014_x86_64) in the Python Package Index (PyPI).
Python 3.7 or newer are required.

Tip

For Windows and macOS, we recommend using Visual Studio Code with a Python container.

Tip

To install Ket on a Google Colab Notebook, use the command:

!pip install ket-lang -q

Installing dependencies:

sudo apt update && sudo apt install python3-pip -y
sudo dnf install python3-pip -y
sudo pacman --noconfirm --needed -Syu python-pip

Installing Ket:

pip3 install ket-lang

Usage#

You can execute a quantum application using the Ket or Python interpreter. For example:

# bell.ket

a, b = quant(2)
cnot(H(a), b)
print(dump(a+b).show())

To run the code above, use the command ket bell.ket or python3 -m ket bell.ket.

$ ket bell.ket
|00⟩    (50.00%)
 0.707107               ≅      1/√2
|11⟩    (50.00%)
 0.707107               ≅      1/√2
# bell.py
from ket import *
a, b = quant(2)
cnot(H(a), b)
print(dump(a+b).show())

To run the code above, use the command python3 bell.py.

$ python3 bell.py
|00⟩    (50.00%)
 0.707107               ≅      1/√2
|11⟩    (50.00%)
 0.707107               ≅      1/√2

Example: Quantum Teleportation#

With the quantum teleportation protocol, one can transfer the quantum information from one qubit to another using a pair of entangled qubits and quantum measurements.

To integrate classical control-flow in quantum programming, quantum measurements in Ket do not return the result right away. Instead, it returns a future which one can use in the if-then and while statements. Reading the value attribute from a future triggers the quantum execution.

Note that there are differences between running a Ket program with the Ket or Python interpreter. For properly handle classical control-flow on the quantum computer with the Python interpreter, use the decorator code_ket. See the example below:

# teleport.ket


def teleport(alice : quant) -> quant:
    alice_b, bob_b = quant(2)
    ctrl(H(alice_b), X, bob_b)

    ctrl(alice, X, alice_b)
    H(alice)

    m0 = measure(alice)   # return a future
    m1 = measure(alice_b) # return a future

    if m1 == 1:  #
        X(bob_b) #  execute on the
    if m0 == 1:  # quantum computer
        Z(bob_b) #

    return bob_b

alice = quant(1)           # alice = |0⟩
H(alice)                   # alice = |+⟩
Z(alice)                   # alice = |–⟩
bob = teleport(alice)      # bob  <- alice
H(bob)                     # bob   = |1⟩
bob_m = measure(bob).value # triggers quantum execution

print('Expected measure 1, result =', bob_m)
$ ket teleport.ket
Expected measure 1, result = 1
# teleport.py
from ket import quant, X, Z, H, measure, ctrl, code_ket
@code_ket # necessary to execute the if-then statement on the quantum computer
def teleport(alice : quant) -> quant:
    alice_b, bob_b = quant(2)
    ctrl(H(alice_b), X, bob_b)

    ctrl(alice, X, alice_b)
    H(alice)

    m0 = measure(alice)   # return a future
    m1 = measure(alice_b) # return a future

    if m1 == 1:  #
        X(bob_b) #  execute on the
    if m0 == 1:  # quantum computer
        Z(bob_b) #

    return bob_b

alice = quant(1)           # alice = |0⟩
H(alice)                   # alice = |+⟩
Z(alice)                   # alice = |–⟩
bob = teleport(alice)      # bob  <- alice
H(bob)                     # bob   = |1⟩
bob_m = measure(bob).value # triggers quantum execution

print('Expected measure 1, result =', bob_m)
$ python teleport.py
Expected measure 1, result = 1