Ket Programming Guide

Qubits

Qubits are the basic quantum computation units. As the qubits state is only available to the quantum computer, the classical computer can only operate with its reference. In Ket, Quant is a list-like type that stores qubits reference, which one can use to manipulate its state in the quantum computer. To allocate qubits, one need to instantiate a Process and than call for alloc.

The only operations available for the classical computer that can change the quantum state are the quantum gates and the measure function, making it impossible to violate any principle of quantum mechanics, like the no-cloning theorem.

Example of qubit allocation and indexing:

from ket import *

p = Process()

a = q.alloc()  # Allocate 1 qubits |0⟩
b = q.alloc(4) # Allocate 4 qubits |0000⟩
ab = a+b # Concatenate |a⟩|b⟩
q0, q1, q2, q3 = ab # Unpack qubits
# Indexing Qubits
first, tail = ab[0], ab[1:]
init, last  = ab[:-1], ab[-1]

Quantum Operations

Ket provides two kinds of operations to manipulate the quantum state. Quantum gates operate only on the qubits state and have no classical side-effect. And the measure function, which collapses the quantum superposition and generates classical information.

To entangle two or more qubits, Ket allows adding qubits of control to any quantum gate. And as quantum gates are unitary operations, Ket provides ways to apply the inverse of a quantum gate.

Note

You can see a list of the available quantum gates in ket.gates.

Every quantum gate in Ket returns the reference of the operated qubits, allowing to concatenate quantum gates. Also, you can use the cat and kron to create a new quantum gate from the concatenation and tensor product. For example:

qubit = H(X(qubit))          # Prepare |+⟩
bell = cat(kron(H, I), CNOT) # Create bell gate

Controlled Gates

Ket provides ways to add control qubits to any quantum gate or function that encapsulates quantum gates. For example, we can create a Toffoli gate by adding a control qubits gate to a Pauli X gate:

def toffoli(c0 : Quant, c1 : Quant, t : Quant):
    ctrl(c0+c1, X)(t)

equivalent to

def toffoli(c0 : Quant, c1 : Quant, t : Quant):
    with control(c0, c1):
        X(t)

The function ctrl adds control qubits to a call. The control statement opens a controlled scope, adding control qubits to every quantum gate and functions call. By default, the controlled operations are applied in the superposition when the control qubits are in state \(\left|1\right>\). To change this behavior, use the flip_to_control function.

None operation on the quantum computer other than quantum gate application is allowed inside a controlled scope or call. For example, calling a function that allocates or measures qubits will raise an error.

Note

See the Ket API documentation for more examples and information on ctrl and control.

Inverse Gates

Ket provides three ways to call the inverse of quantum gate or function encapsulating quantum gates. The adj function calls the inverse of a quantum gate or function. Similarly, the inverse statement opens a scope that runs backward in the quantum computer. For example, given the implementation of a Quantum Fourier Transformation, one can call its inverse with:

def iqft(q: quant):
    adj(qft)(q)

equivalent to

iqft = adj(qft)

equivalent to

def iqft(process: Process, q : Quant):
    with inverse():
        head, *tail = q
        while True:
            H(head)
            for i in range(len(tail)): ctrl(tail[i], PHASE(2*pi/2**(i+2)))(head)
            if not tail: break
            head, *tail = tail
        for i in range(len(q)//2): SWAP(q[i], q[len(q)-i-1])

Quantum Fourier Transformation in Ket

def qft(q : Quant, invert = True):
    head, *tail = q
    H(head)
    for i in range(len(tail)): ctrl(tail[i], PHASE(2*pi/2**(i+2)))(head)
    if tail: qft(tail, invert=False)
    if invert:
        for i in range(len(q)//2): SWAP(q[i], q[len(q)-i-1])

The around statement whapper a quantum operation \(U\) between a quantum gate \(V\) and its inverse \(V^\dagger\), a structure used in several quantum algorithms. For example, the decomposition of the \(R_{yy}\) gate:

\[R_{yy}(\theta) = \overbrace{\left[R_x(\frac{\pi}{2})^{\otimes2}\right] \text{CNOT}}^V \underbrace{\left[I\otimes R_x(\theta) \right]}_U \overbrace{\text{CNOT} \left[R_x(\frac{-\pi}{2})^{\otimes2}\right]}^{V^\dagger}\]
def ryy(theta : float, a : Quant, b : Quant):
    from math import pi
    with around(cat(kron(RX(pi/2), RX(pi/2)), CNOT), a, b):
        RZ(theta, b)

As for controlled operation, no operation on the quantum computer other than quantum gate application is allowed inside a inverse scope or call.

Note

See the Ket API documentation for more examples and information on adj, inverse, and around.

Measurement

The only operation that affects both classical and quantum states is quantum measurement. Ket allows measuring together several qubits, storing the result in a unsigned integer.

The measure function accepts a Quant as a parameter and returns a Measurement variable. Reading the value attribute of a Measurement variable returns the measurement result from the quantum computer.

Example of measurement in Ket:

from ket import *

p = Process
a, b = p.alloc(2)
CNOT(H(a), b)
# Measure qubits
measurement = measure(a+b)
# Get value from the quantum computer
result = measurement.value

Quantum Dump

In simulated quantum execution, Ket allows dumping the quantum state to the classical computer. This operation has no side effect in the quantum simulation.

With a QuantumState instance, created use the dump functions, one can iterate over a quantum state. The attributes states store all the information of a quantum state. For example, one can print the superposition:

from ket import *

p = Process()
q = H(p.alloc(3))
d = dump(q)
for state, amp in d.states.items():
    print(f'{amp}|{state:0{len(q)}b}⟩')
# (0.35355339059327384+0j)|000⟩
# (0.35355339059327384+0j)|001⟩
# (0.35355339059327384+0j)|010⟩
# (0.35355339059327384+0j)|011⟩
# (0.35355339059327384+0j)|100⟩
# (0.35355339059327384+0j)|101⟩
# (0.35355339059327384+0j)|110⟩
# (0.35355339059327384+0j)|111⟩

The basis states can repeat if the QuantumState variable does not cover all qubits in the quantum system. The result of dumping parts of a quantum system does not necessarily correspond with the partial trace of the same.