# 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. The quant constructor allocates qubits in the quantum computer and returns its reference.

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:

a = quant()  # Allocate 1 qubits |0⟩
b = quant(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 API documentation.

Every quantum gate in Ket returns the reference of the operated qubits, allowing to concatenate quantum gates. Also, when one passe a quantum gates as the parameter for another, it creates a new quantum gate from the concatenation. For example:

q = H(X(quant())) # Prepare |+⟩
bell = cnot(H, I) # 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 on_state keyword argument

None operation on the quantum computer other than quantum gate (and plugin) 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):


equivalent to

iqft = adj(qft)


equivalent to

def iqft(q):
with inverse():
while True:
for i in range(len(tail)): ctrl(tail[i], phase(2*pi/2**(i+2)), head)
if not tail: break
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):
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, a, b):
from math import pi
with around(cnot(RX(pi/2), RX(pi/2)), a, b):
RZ(theta, b)


As for controlled operation, no operation on the quantum computer other than quantum gate (and plugin) 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 64-bits signed integer.

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

Ket allows performing several measurements in a set of qubits. But after reading the .value attribute from any future variable, no more quantum operations are authorized in the allocated qubits. For more information, see Ket Runtime Architecture.

Example of measurement in Ket:

a, b = quant(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 dump variable, one can iterate over a quantum state. The attributes states and amplitudes store all the information of a quantum state. For example, one can print the superposition:

q = H(quant(3))
d = dump(q)
for state, amp in sorted(zip(d.states, d.amplitudes)):
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 index of an amplitude probability matches the index of its respective basis states and vice versa. The amplitudes and states list have no particular order. The basis states can repeat if the dump 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.

As for measurement, one can perform several dumps on a quantum system, but after reading the attributes with the quantum information, Ket forbids any quantum operations in the qubits allocated until the reading.