Ket Quantum Programming#

Ket Quantum Programming is an open-source platform that provides dynamic interaction between classical and quantum data at the programming level, streamlining classical-quantum development. The platform has three main projects:

  • Ket is an embedded programming language that introduces the ease of Python to quantum programming, letting anyone quickly prototype and test a quantum application. (Ket repo)

  • Libket is the runtime library for the Ket language, but one can use it for quantum acceleration on embedded systems using C/C++ or Rust. (Libket repo)

  • Ket Bitwise Simulator (KBW) is a noise-free quantum computer simulator that allows anyone to test quantum applications on classical computers. KBW features two simulation methods: Dense simulation based on state vector simulation and Sparse simulation based on the Bitwise representation. (KBW repo)

Getting started with Ket#

Installation#

Ket requires Python 3.7 or newer and is available for Linux, Windows, and macOS.
If you are not using an x86_64 (Intel/AMD) CPU, you must install Rust before installing Ket.
Ket is available in the Python Package Index (PyPI), and you can install it using pip.
To do so, copy and paste the following command into your terminal:
python3 -m pip 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 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 immediately. Instead, it returns a future that one can use in if-then-else and while statements. When one reads the .value attribute from a future variable, it triggers the quantum execution.

Note that there are differences between running a Ket program with the Ket or Python interpreter. To 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

Used by#

Otto M. Pires, Rafael de Santiago and Jerusa Marchi, “Two Stage Quantum Optimization for the School Timetabling Problem”, 2021 IEEE Congress on Evolutionary Computation (CEC), 2021, pp. 2347-2353, doi: 10.1109/CEC45853.2021.9504701.

Portable GPU-Accelerated Quantum Computer Simulator QuBOX at Universidade Federal de Santa Catarina (Brazil).


Sponsored by QuantuloopSponsored by Quantuloop