core.operator#

This module provides the intermediate representation of Hamiltonian for quantum systems.

It defines classes and functions to create and manipulate Pauli operators and Hamiltonians, which are fundamental in quantum mechanics and quantum computing.

Key Components: - Pauli: An enumeration of Pauli operators (X, Y, Z, I). - PauliOperator: A class representing a single Pauli operator acting on a specific qubit. - Hamiltonian: A class representing a quantum Hamiltonian as a sum of Pauli operator products.

Usage:

from qamomile.operator.hamiltonian import X, Y, Z, Hamiltonian

# Create Pauli operators X0 = X(0) # Pauli X operator on qubit 0 Y1 = Y(1) # Pauli Y operator on qubit 1

# Create a Hamiltonian H = Hamiltonian() H.add_term((X0, Y1), 0.5) # Add term 0.5 * X0 * Y1 to the Hamiltonian H.add_term((Z(2),), 1.0) # Add term 1.0 * Z2 to the Hamiltonian

# Access Hamiltonian properties print(H.terms) print(H.num_qubits)

Classes#

Pauli

Enum class for Pauli operators.

PauliOperator

Represents a single Pauli operator acting on a specific qubit.

Hamiltonian

Represents a quantum Hamiltonian as a sum of Pauli operator products.

Functions#

X(→ Hamiltonian)

Creates a Pauli X operator for a specified qubit.

Y(→ Hamiltonian)

Creates a Pauli Y operator for a specified qubit.

Z(→ Hamiltonian)

Creates a Pauli Z operator for a specified qubit.

multiply_pauli_same_qubit(→ tuple[PauliOperator, complex])

Multiplies two Pauli operators acting on the same qubit.

simplify_pauliop_terms(→ tuple[tuple[PauliOperator, ...)

Simplifies a tuple of Pauli operators by combining operators acting on the same qubit.

Module Contents#

class Pauli#

Bases: enum.Enum

Enum class for Pauli operators.

X#

Pauli X operator, represented by 0.

Type:

int

Y#

Pauli Y operator, represented by 1.

Type:

int

Z#

Pauli Z operator, represented by 2.

Type:

int

I#

Identity operator, represented by 3.

Type:

int

X = 0#
Y = 1#
Z = 2#
I = 3#
class PauliOperator#

Represents a single Pauli operator acting on a specific qubit.

pauli#

The type of Pauli operator (X, Y, or Z).

Type:

Pauli

index#

The index of the qubit on which this operator acts.

Type:

int

Example

>>> X0 = PauliOperator(Pauli.X, 0)
>>> print(X0)
X0
pauli: Pauli#
index: int#
class Hamiltonian(num_qubits: int | None = None)#

Represents a quantum Hamiltonian as a sum of Pauli operator products.

The Hamiltonian is stored as a dictionary where keys are tuples of PauliOperators and values are their corresponding coefficients.

_terms#

The terms of the Hamiltonian.

Type:

Dict[Tuple[PauliOperator, …], complex]

constant#

A constant term added to the Hamiltonian.

Type:

float

Example

>>> H = Hamiltonian()
>>> H.add_term((PauliOperator(Pauli.X, 0), PauliOperator(Pauli.Y, 1)), 0.5)
>>> H.add_term((PauliOperator(Pauli.Z, 2),), 1.0)
>>> print(H.terms)
{(X0, Y1): 0.5, (Z2,): 1.0}
constant: float = 0.0#
property terms: Dict[Tuple[PauliOperator, Ellipsis], complex]#

Getter for the terms of the Hamiltonian.

Returns:

A dictionary representing the Hamiltonian terms.

Return type:

Dict[Tuple[PauliOperator, …], complex]

Example

>>> H = Hamiltonian()
>>> H.add_term((PauliOperator(Pauli.X, 0), PauliOperator(Pauli.Y, 1)), 0.5)
>>> print(H.terms)
{(X0, Y1): 0.5}
add_term(operators: Tuple[PauliOperator, Ellipsis], coeff: float | complex)#

Adds a term to the Hamiltonian.

This method adds a product of Pauli operators with a given coefficient to the Hamiltonian. If the term already exists, the coefficients are summed.

Parameters:
  • operators (Tuple[PauliOperator, ...]) – A tuple of PauliOperators representing the term.

  • coeff (Union[float, complex]) – The coefficient of the term.

Example

>>> H = Hamiltonian()
>>> H.add_term((PauliOperator(Pauli.X, 0), PauliOperator(Pauli.Y, 1)), 0.5)
>>> H.add_term((PauliOperator(Pauli.X, 0), PauliOperator(Pauli.Y, 1)), 0.5j)
>>> print(H.terms)
{(X0, Y1): (0.5+0.5j)}
property num_qubits: int#

Calculates the number of qubits in the Hamiltonian.

Returns:

The number of qubits, which is the highest qubit index plus one.

Return type:

int

Example

>>> H = Hamiltonian()
>>> H.add_term((PauliOperator(Pauli.X, 0), PauliOperator(Pauli.Y, 3)), 1.0)
>>> print(H.num_qubits)
4
to_latex() str#

Converts the Hamiltonian to a LaTeX representation.

This function does not add constant term when we show the Hamiltonian. This function does not add $ symbols.

Returns:

A LaTeX representation of the Hamiltonian.

Return type:

str

import qamomile.core.operator as qm_o
import IPython.display as ipd

h = qm_o.Hamiltonian()
h += -qm_o.X(0) * qm_o.Y(1) - 2.0 * qm_o.Z(0) * qm_o.Z(1)

# Show the Hamiltonian in LaTeX at Jupyter Notebook
ipd.display(ipd.Latex("$" + h.to_latex() + "$"))
X(index: int) Hamiltonian#

Creates a Pauli X operator for a specified qubit.

Parameters:

index (int) – The index of the qubit.

Returns:

A Pauli X Hamiltonian operator acting on the specified qubit.

Return type:

Hamiltonian

Example

>>> X0 = X(0)
>>> print(X0)
Hamiltonian((X0,): 1.0)
Y(index: int) Hamiltonian#

Creates a Pauli Y operator for a specified qubit.

Parameters:

index (int) – The index of the qubit.

Returns:

A Pauli Y Hamiltonian operator acting on the specified qubit.

Return type:

Hamiltonian

Example

>>> Y1 = Y(1)
>>> print(Y1)
Hamiltonian((Y1,): 1.0)
Z(index: int) Hamiltonian#

Creates a Pauli Z operator for a specified qubit.

Parameters:

index (int) – The index of the qubit.

Returns:

A Pauli Z Hamiltonian operator acting on the specified qubit.

Return type:

Hamiltonian

Example

>>> Z2 = Z(2)
>>> print(Z2)
Hamiltonian((Z2,): 1.0)
multiply_pauli_same_qubit(pauli1: PauliOperator, pauli2: PauliOperator) tuple[PauliOperator, complex]#

Multiplies two Pauli operators acting on the same qubit.

Parameters:
Returns:

A tuple containing the resulting Pauli operator and the complex coefficient.

Return type:

tuple[PauliOperator, complex]

Raises:

ValueError – If the Pauli operators act on different qubits.

Example

>>> X0 = PauliOperator(Pauli.X, 0)
>>> Y0 = PauliOperator(Pauli.Y, 0)
>>> Z0 = PauliOperator(Pauli.Z, 0)
>>> multiply_pauli_same_qubit(X0, Y0)
(Z0, 1j)
simplify_pauliop_terms(term: tuple[PauliOperator]) tuple[tuple[PauliOperator, Ellipsis], complex]#

Simplifies a tuple of Pauli operators by combining operators acting on the same qubit.

Parameters:

term (tuple[PauliOperator]) – A tuple of Pauli operators.

Returns:

A tuple containing the simplified Pauli operators and the phase factor.

Return type:

tuple[tuple[PauliOperator,…],complex]

Example

>>> X0 = PauliOperator(Pauli.X, 0)
>>> Y0 = PauliOperator(Pauli.Y, 0)
>>> Z1 = PauliOperator(Pauli.Z, 1)
>>> simplify_pauliop_terms((X0, Y0, Z1))
((Z0, Z1), 1j)