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#
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#
Cite Ket#
When using Ket for research projects, please cite:
Evandro Chagas Ribeiro da Rosa and Rafael de Santiago. 2021. Ket Quantum Programming. J. Emerg. Technol. Comput. Syst. 18, 1, Article 12 (January 2022), 25 pages. https://doi.org/10.1145/3474224
@article{ket2021,
author = {Evandro Chagas Ribeiro da Rosa and Rafael de Santiago},
title = {Ket Quantum Programming},
year = {2021},
issue_date = {January 2022},
publisher = {Association for Computing Machinery},
address = {New York, NY, USA},
volume = {18},
number = {1},
issn = {1550-4832},
url = {https://doi.org/10.1145/3474224},
doi = {10.1145/3474224},
journal = {J. Emerg. Technol. Comput. Syst.},
month = {oct},
articleno = {12},
numpages = {25},
keywords = {Quantum programming, cloud quantum computation, qubit simulation}
}
Download the original article that proposes the Ket language: pdf
Contact