QubitOperator#

class QubitOperator(terms_dict={})[source]#

This class provides an efficient implementation of QubitOperators, i.e. Operators, that act on a qubit space \((\mathbb{C}^2)^{\otimes n}\). Supported are operators of the following form:

\[O=\sum\limits_{j}\alpha_j O_j \]

where \(O_j=\bigotimes_i m_i^j\) is a product of the following operators:

Operator

Ket-Bra Realization

Description

\(X\)

\(\ket{0}\bra{1} + \ket{1}\bra{0}\)

Pauli-X operator (bit flip)

\(Y\)

\(-i\ket{0}\bra{1} + i\ket{1}\bra{0}\)

Pauli-Y operator (bit flip with phase)

\(Z\)

\(\ket{0}\bra{0} - \ket{1}\bra{1}\)

Pauli-Z operator (phase flip)

\(A\)

\(\ket{0}\bra{1}\)

Annihilation operator

\(C\)

\(\ket{1}\bra{0}\)

Creation operator

\(P_0\)

\(\ket{0}\bra{0}\)

Projector onto the \(\ket{0}\) state

\(P_1\)

\(\ket{1}\bra{1}\)

Projector onto the \(\ket{1}\) state

\(I\)

\(\ket{1}\bra{1} + \ket{0}\bra{0}\)

Identity operator

If you already have some experience you might wonder why to include the non-Pauli operators - after all they can be represented as a linear combination of X, Y and Z.

\[\begin{split}\begin{align} A_0 C_1 &= (X_0 - i Y_0)(X_1 + Y_1)/4 \\ & = (X_0X_1 + X_0Y_1 - Y_0X_1 + Y_0Y_1)/4 \end{align}\end{split}\]

Recently, a much more efficient method of simulating A and C has been proposed by Kornell and Selinger, which avoids decomposing these Operators into Paulis strings but instead simulates

\[H = A_0C_1 + h.c. \]

within a single step. This idea is deeply integrated into the Operators module of Qrisp. For an example circuit see below.

Examples

A QubitOperator can be specified conveniently in terms of arithmetic combinations of the mentioned operators:

from qrisp.operators.qubit import X,Y,Z,A,C,P0,P1

H = 1+2*X(0)+3*X(0)*Y(1)*A(2)+C(4)*P1(0)
H

Yields \(1 + P^1_0C_4 + 2X_0 + 3X_0Y_1A_2\).

We create a QubitOperator and perform Hamiltonian simulation via trotterization:

from sympy import Symbol
from qrisp.operators import A,C,Z,Y
from qrisp import QuantumVariable
O = A(0)*C(1)*Z(2)*A(3) + Y(3)

t = Symbol("t")
def state_prep(t):
    qv = QuantumVariable(4)
    U = O.trotterization()
    U(qv, t = t)
    return qv

qv = state_prep(t)
>>> print(qv.qs)
QuantumCircuit:
---------------
          โ”Œโ”€โ”€โ”€โ”                                                                ยป
    qv.0: โ”ค X โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€oโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€oโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ยป
          โ””โ”€โ”ฌโ”€โ”˜โ”Œโ”€โ”€โ”€โ”       โ”‚                                      โ”‚       โ”Œโ”€โ”€โ”€โ”ยป
    qv.1: โ”€โ”€โ”ผโ”€โ”€โ”ค X โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ– โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ– โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค X โ”œยป
            โ”‚  โ””โ”€โ”ฌโ”€โ”˜       โ”‚                                      โ”‚       โ””โ”€โ”ฌโ”€โ”˜ยป
    qv.2: โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ– โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ– โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ยป
            โ”‚    โ”‚  โ”Œโ”€โ”€โ”€โ”  โ”‚  โ”Œโ”€โ”ดโ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”ดโ”€โ”  โ”‚  โ”Œโ”€โ”€โ”€โ”  โ”‚  ยป
    qv.3: โ”€โ”€โ– โ”€โ”€โ”€โ”€โ– โ”€โ”€โ”ค H โ”œโ”€โ”€โ”ผโ”€โ”€โ”ค X โ”œโ”€โ”€โ– โ”€โ”€โ”ค Rz(-0.5*t) โ”œโ”€โ”€โ– โ”€โ”€โ”ค X โ”œโ”€โ”€โ”ผโ”€โ”€โ”ค H โ”œโ”€โ”€โ– โ”€โ”€ยป
                    โ””โ”€โ”€โ”€โ”˜โ”Œโ”€โ”ดโ”€โ”โ””โ”€โ”€โ”€โ”˜โ”Œโ”€โ”ดโ”€โ”โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”˜โ”Œโ”€โ”ดโ”€โ”โ””โ”€โ”€โ”€โ”˜โ”Œโ”€โ”ดโ”€โ”โ””โ”€โ”€โ”€โ”˜     ยป
hs_anc.0: โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค X โ”œโ”€โ”€โ”€โ”€โ”€โ”ค X โ”œโ”ค Rz(0.5*t) โ”œโ”€โ”ค X โ”œโ”€โ”€โ”€โ”€โ”€โ”ค X โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ยป
                         โ””โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”˜โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”˜          ยป
ยซ          โ”Œโ”€โ”€โ”€โ”                            
ยซ    qv.0: โ”ค X โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
ยซ          โ””โ”€โ”ฌโ”€โ”˜                            
ยซ    qv.1: โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
ยซ            โ”‚                              
ยซ    qv.2: โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
ยซ            โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”
ยซ    qv.3: โ”€โ”€โ– โ”€โ”€โ”ค โˆšX โ”œโ”ค Rz(-2.0*t) โ”œโ”ค โˆšXdg โ”œ
ยซ               โ””โ”€โ”€โ”€โ”€โ”˜โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
ยซhs_anc.0: โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
ยซ                                           
Live QuantumVariables:
----------------------
QuantumVariable qv

Call the simulator:

>>> O.expectation_value(state_prep)(0.5)  # Calculate the expectation value
0.007990479428765712

Methods#

QubitOperator.adjoint()

Returns the adjoint operator.

QubitOperator.coeffs()

Returns the coefficients of the operator.

QubitOperator.commutator(other)

Computes the commutator.

QubitOperator.expectation_value(state_prep)

The expectation value function allows to estimate the expectation value of a Hamiltonian for a state that is specified by a preparation procedure.

QubitOperator.from_matrix(matrix[,ย ...])

Represents a matrix as an operator

QubitOperator.ground_state_energy()

Calculates the ground state energy (i.e., the minimum eigenvalue) of the operator classically.

QubitOperator.hermitize()

Returns the hermitian part of self.

QubitOperator.qdrift([forward_evolution])

Simulates the time-evolution of a quantum state under a Hamiltonian using the QDrift (Quantum Stochastic Drift Protocol) algorithm.

QubitOperator.to_array([factor_amount])

Returns a numpy array representing the operator

QubitOperator.to_sparse_matrix([factor_amount])

Returns a scipy matrix representing the operator

QubitOperator.to_pauli()

Returns an equivalent operator, which however only contains Pauli factors.

QubitOperator.trotterization([order,ย ...])

Returns a function for performing Hamiltonian simulation, i.e., approximately implementing the unitary operator \(U(t) = e^{-itH}\) via Trotterization.

QubitOperator.unitaries()

Returns unitiaries and coefficients for the Pauli representation of the operator.