Ket Programming Guide

Qubits

Qubits are the basic quantum computation units. As the the state of a qubit 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 references to qubits, which can be used to manipulate their state in the quantum computer. To allocate qubits, instantiate a Process and then call its method alloc, passing the number of qubits to be allocated as the argument.

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 control qubits to any quantum gate. Moreover, 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 for the operated qubits, allowing the concatenation of quantum gates. Also, you can use the cat and kron to create a new quantum gate from the concatenation and tensor product, respectively. 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 function 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.

No 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 returns the inverse of a quantum gate or function. Similarly, the inverse statement opens a scope that is executed backwards in the quantum computer. For example, given the implementation of a Quantum Fourier Transformation, its inverse can be defined as follows:

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 wraps 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)

The same way as a controlled operation, no quantum operation other than gate application is allowed inside an 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 the measurement of several qubits, storing the result in an unsigned integer, each bit representing the measurement of a qubit.

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 using the dump function, a quantum state can be iterated over. The attribute states stores all the information of a quantum state, with which, for example, you can print its 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 its partial trace.