The Operator and Operators classes

The Operator and Operators classes#

The Operator and Operators classes provide a way to interact with Operators and Hamiltonians.

import numpy as np

import qse

The Operator class#

The Operator class represents a single operator acting on a multiple qubit space.

For example:

# Create an operator acting with a Pauli X on the
# third qubit of a 4-qubit system with coefficient 1.0
qse.Operator("X", indices=2, nqbits=4, coef=1.0)
1.00 X2
# Create an operator acting with a Pauli Z on the
# first qubit and a Pauli Y on the second qubit of a 3-qubit system with coefficient 4.0
qse.Operator(["Z", "Y"], indices=[0, 1], nqbits=3, coef=4.0)
4.00 Z0 Y1

The Operator class supports multiplication, where the coefficient will be multiplied by a scalar.

op = qse.Operator("X", indices=0, nqbits=2)
print(op)

op *= 3.14
print(op)
1.00 X0
3.14 X0

The Operators class#

The Operators class is for representing a sum of Operator classes.

For Example:

op1 = qse.Operator("X", indices=0, nqbits=2)
op2 = qse.Operator("Y", indices=1, nqbits=2, coef=0.5)
ops = qse.Operators([op1, op2])
ops
Number of qubits: 2
Number of terms: 2

1.00 X0
0.50 Y1
ops = qse.Operators([qse.Operator("X", [i, i + 1], nqbits=4) for i in range(3)])
ops
Number of qubits: 4
Number of terms: 3

1.00 X0 X1
1.00 X1 X2
1.00 X2 X3

Creating Operators from Qbits#

Operators can be created from the Qbits class, given an interaction function.

spacing = 1.0
qbits = qse.lattices.ring(spacing, 5)


def distance_func(d):
    # Nearest neighbours interaction
    return -1.0 * np.isclose(d, spacing)


ham_int = qbits.compute_interaction_hamiltonian(
    distance_func=distance_func, interaction="Z"
)
ham_int
Number of qubits: 5
Number of terms: 5

-1.00 Z0 Z1
-1.00 Z0 Z4
-1.00 Z1 Z2
-1.00 Z2 Z3
-1.00 Z3 Z4

We can then combine this with other terms to make complex Hamiltonians. For example, the transverse Ising model:

ham_ext = qse.Operators(
    [qse.Operator("X", i, nqbits=qbits.nqbits) for i in range(qbits.nqbits)]
)

ham = ham_int + ham_ext
ham
Number of qubits: 5
Number of terms: 10

-1.00 Z0 Z1
-1.00 Z0 Z4
-1.00 Z1 Z2
-1.00 Z2 Z3
-1.00 Z3 Z4
1.00 X0
1.00 X1
1.00 X2
1.00 X3
1.00 X4